Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1606)

Side by Side Diff: webrtc/modules/utility/include/jvm_android.h

Issue 2894593002: Revert of Removes usage of native base::android::GetApplicationContext() (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 }; 111 };
112 112
113 // Main class for working with Java from C++ using JNI in WebRTC. 113 // Main class for working with Java from C++ using JNI in WebRTC.
114 // 114 //
115 // Example usage: 115 // Example usage:
116 // 116 //
117 // // At initialization (e.g. in JNI_OnLoad), call JVM::Initialize. 117 // // At initialization (e.g. in JNI_OnLoad), call JVM::Initialize.
118 // JNIEnv* jni = ::base::android::AttachCurrentThread(); 118 // JNIEnv* jni = ::base::android::AttachCurrentThread();
119 // JavaVM* jvm = NULL; 119 // JavaVM* jvm = NULL;
120 // jni->GetJavaVM(&jvm); 120 // jni->GetJavaVM(&jvm);
121 // webrtc::JVM::Initialize(jvm); 121 // jobject context = ::base::android::GetApplicationContext();
122 // webrtc::JVM::Initialize(jvm, context);
122 // 123 //
123 // // Header (.h) file of example class called User. 124 // // Header (.h) file of example class called User.
124 // std::unique_ptr<JNIEnvironment> env; 125 // std::unique_ptr<JNIEnvironment> env;
125 // std::unique_ptr<NativeRegistration> reg; 126 // std::unique_ptr<NativeRegistration> reg;
126 // std::unique_ptr<GlobalRef> obj; 127 // std::unique_ptr<GlobalRef> obj;
127 // 128 //
128 // // Construction (in .cc file) of User class. 129 // // Construction (in .cc file) of User class.
129 // User::User() { 130 // User::User() {
130 // // Calling thread must be attached to the JVM. 131 // // Calling thread must be attached to the JVM.
131 // env = JVM::GetInstance()->environment(); 132 // env = JVM::GetInstance()->environment();
132 // reg = env->RegisterNatives("org/webrtc/WebRtcTest", ,); 133 // reg = env->RegisterNatives("org/webrtc/WebRtcTest", ,);
133 // obj = reg->NewObject("<init>", ,); 134 // obj = reg->NewObject("<init>", ,);
134 // } 135 // }
135 // 136 //
136 // // Each User method can now use |reg| and |obj| and call Java functions 137 // // Each User method can now use |reg| and |obj| and call Java functions
137 // // in WebRtcTest.java, e.g. boolean init() {}. 138 // // in WebRtcTest.java, e.g. boolean init() {}.
138 // bool User::Foo() { 139 // bool User::Foo() {
139 // jmethodID id = reg->GetMethodId("init", "()Z"); 140 // jmethodID id = reg->GetMethodId("init", "()Z");
140 // return obj->CallBooleanMethod(id); 141 // return obj->CallBooleanMethod(id);
141 // } 142 // }
142 // 143 //
143 // // And finally, e.g. in JNI_OnUnLoad, call JVM::Uninitialize. 144 // // And finally, e.g. in JNI_OnUnLoad, call JVM::Uninitialize.
144 // JVM::Uninitialize(); 145 // JVM::Uninitialize();
145 class JVM { 146 class JVM {
146 public: 147 public:
147 // Stores global handles to the Java VM interface. 148 // Stores global handles to the Java VM interface and the application context.
148 // Should be called once on a thread that is attached to the JVM. 149 // Should be called once on a thread that is attached to the JVM.
149 static void Initialize(JavaVM* jvm); 150 static void Initialize(JavaVM* jvm, jobject context);
150 // Clears handles stored in Initialize(). Must be called on same thread as 151 // Clears handles stored in Initialize(). Must be called on same thread as
151 // Initialize(). 152 // Initialize().
152 static void Uninitialize(); 153 static void Uninitialize();
153 // Gives access to the global Java VM interface pointer, which then can be 154 // Gives access to the global Java VM interface pointer, which then can be
154 // used to create a valid JNIEnvironment object or to get a JavaClass object. 155 // used to create a valid JNIEnvironment object or to get a JavaClass object.
155 static JVM* GetInstance(); 156 static JVM* GetInstance();
156 157
157 // Creates a JNIEnvironment object. 158 // Creates a JNIEnvironment object.
158 // This method returns a NULL pointer if AttachCurrentThread() has not been 159 // This method returns a NULL pointer if AttachCurrentThread() has not been
159 // called successfully. Use the AttachCurrentThreadIfNeeded class if needed. 160 // called successfully. Use the AttachCurrentThreadIfNeeded class if needed.
160 std::unique_ptr<JNIEnvironment> environment(); 161 std::unique_ptr<JNIEnvironment> environment();
161 162
162 // Returns a JavaClass object given class |name|. 163 // Returns a JavaClass object given class |name|.
163 // Note that the class name must be one of the names in the static 164 // Note that the class name must be one of the names in the static
164 // |loaded_classes| array defined in jvm_android.cc. 165 // |loaded_classes| array defined in jvm_android.cc.
165 // This method must be called on the construction thread. 166 // This method must be called on the construction thread.
166 JavaClass GetClass(const char* name); 167 JavaClass GetClass(const char* name);
167 168
168 // TODO(henrika): can we make these private? 169 // TODO(henrika): can we make these private?
169 JavaVM* jvm() const { return jvm_; } 170 JavaVM* jvm() const { return jvm_; }
171 jobject context() const { return context_; }
170 172
171 protected: 173 protected:
172 JVM(JavaVM* jvm); 174 JVM(JavaVM* jvm, jobject context);
173 ~JVM(); 175 ~JVM();
174 176
175 private: 177 private:
176 JNIEnv* jni() const { return GetEnv(jvm_); } 178 JNIEnv* jni() const { return GetEnv(jvm_); }
177 179
178 rtc::ThreadChecker thread_checker_; 180 rtc::ThreadChecker thread_checker_;
179 JavaVM* const jvm_; 181 JavaVM* const jvm_;
182 jobject context_;
180 }; 183 };
181 184
182 } // namespace webrtc 185 } // namespace webrtc
183 186
184 #endif // WEBRTC_MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_ 187 #endif // WEBRTC_MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698