Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: jni/JniLogSystem.cpp

Issue 4761138508070912: Issue 1848 - Clean up local reference handling (Closed)
Patch Set: Stupid auto-formatter^^ Created Feb. 4, 2015, 12:04 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « jni/JniJsValue.cpp ('k') | jni/JniUpdateAvailableCallback.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 18 matching lines...) Expand all
29 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) 29 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
30 { 30 {
31 delete JniLongToTypePtr<JniLogSystemCallback>(ptr); 31 delete JniLongToTypePtr<JniLogSystemCallback>(ptr);
32 } 32 }
33 33
34 JniLogSystemCallback::JniLogSystemCallback(JNIEnv* env, jobject callbackObject) 34 JniLogSystemCallback::JniLogSystemCallback(JNIEnv* env, jobject callbackObject)
35 : JniCallbackBase(env, callbackObject), AdblockPlus::LogSystem(), logLevelClas s(new JniGlobalReference<jclass>(env, env->FindClass(PKG("LogSystem$LogLevel"))) ) 35 : JniCallbackBase(env, callbackObject), AdblockPlus::LogSystem(), logLevelClas s(new JniGlobalReference<jclass>(env, env->FindClass(PKG("LogSystem$LogLevel"))) )
36 { 36 {
37 } 37 }
38 38
39 void JniLogSystemCallback::operator()(AdblockPlus::LogSystem::LogLevel logLevel, const std::string& message, const std::string& source) 39 void JniLogSystemCallback::operator()(AdblockPlus::LogSystem::LogLevel logLevel,
40 const std::string& message, const std::string& source)
40 { 41 {
41 JNIEnvAcquire env(GetJavaVM()); 42 JNIEnvAcquire env(GetJavaVM());
42 43
43 jclass clazz = env->GetObjectClass(GetCallbackObject()); 44 jmethodID method = env->GetMethodID(
44 jmethodID method = env->GetMethodID(clazz, "logCallback", "(" TYP("LogSystem$L ogLevel") "Ljava/lang/String;Ljava/lang/String;)V"); 45 *JniLocalReference<jclass>(*env,
46 env->GetObjectClass(GetCallbackObject())),
47 "logCallback",
48 "(" TYP("LogSystem$LogLevel") "Ljava/lang/String;Ljava/lang/String;)V");
45 49
46 // TODO: Set log level from Java and handle it here (to reduce C++->Java calls ) 50 // TODO: Set log level from Java and handle it here (to reduce C++->Java calls )
47 51
48 if (method) 52 if (method)
49 { 53 {
50 const char* enumName = 0; 54 const char* enumName = 0;
51 55
52 switch (logLevel) 56 switch (logLevel)
53 { 57 {
54 default: 58 default:
(...skipping 10 matching lines...) Expand all
65 enumName = "WARN"; 69 enumName = "WARN";
66 break; 70 break;
67 case AdblockPlus::LogSystem::LOG_LEVEL_ERROR: 71 case AdblockPlus::LogSystem::LOG_LEVEL_ERROR:
68 enumName = "ERROR"; 72 enumName = "ERROR";
69 break; 73 break;
70 } 74 }
71 75
72 jclass enumClass = logLevelClass->Get(); 76 jclass enumClass = logLevelClass->Get();
73 if (enumClass) 77 if (enumClass)
74 { 78 {
75 jfieldID enumField = env->GetStaticFieldID(enumClass, enumName, TYP("LogSy stem$LogLevel")); 79 jfieldID enumField = env->GetStaticFieldID(enumClass, enumName,
76 jobject jLogLevel = env->GetStaticObjectField(enumClass, enumField); 80 TYP("LogSystem$LogLevel"));
81 JniLocalReference<jobject> jLogLevel(*env,
82 env->GetStaticObjectField(enumClass, enumField));
77 83
78 jstring jMessage = env->NewStringUTF(message.c_str()); 84 JniLocalReference<jstring> jMessage(*env,
79 jstring jSource = env->NewStringUTF(source.c_str()); 85 env->NewStringUTF(message.c_str()));
86 JniLocalReference<jstring> jSource(*env,
87 env->NewStringUTF(source.c_str()));
80 88
81 env->CallVoidMethod(GetCallbackObject(), method, jLogLevel, jMessage, jSou rce); 89 env->CallVoidMethod(GetCallbackObject(), method, *jLogLevel, *jMessage,
90 *jSource);
82 } 91 }
83 92
84 CheckAndLogJavaException(*env); 93 CheckAndLogJavaException(*env);
85 } 94 }
86 } 95 }
87 96
88 static JNINativeMethod methods[] = 97 static JNINativeMethod methods[] =
89 { 98 {
90 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor }, 99 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor },
91 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } 100 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
92 }; 101 };
93 102
94 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_LogSystem_ registerNatives(JNIEnv *env, jclass clazz) 103 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_LogSystem_ registerNatives(JNIEnv *env, jclass clazz)
95 { 104 {
96 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 105 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
97 } 106 }
OLDNEW
« no previous file with comments | « jni/JniJsValue.cpp ('k') | jni/JniUpdateAvailableCallback.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld