OLD | NEW |
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 Loading... |
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 // jobject context = ::base::android::GetApplicationContext(); | 121 // webrtc::JVM::Initialize(jvm); |
122 // webrtc::JVM::Initialize(jvm, context); | |
123 // | 122 // |
124 // // Header (.h) file of example class called User. | 123 // // Header (.h) file of example class called User. |
125 // std::unique_ptr<JNIEnvironment> env; | 124 // std::unique_ptr<JNIEnvironment> env; |
126 // std::unique_ptr<NativeRegistration> reg; | 125 // std::unique_ptr<NativeRegistration> reg; |
127 // std::unique_ptr<GlobalRef> obj; | 126 // std::unique_ptr<GlobalRef> obj; |
128 // | 127 // |
129 // // Construction (in .cc file) of User class. | 128 // // Construction (in .cc file) of User class. |
130 // User::User() { | 129 // User::User() { |
131 // // Calling thread must be attached to the JVM. | 130 // // Calling thread must be attached to the JVM. |
132 // env = JVM::GetInstance()->environment(); | 131 // env = JVM::GetInstance()->environment(); |
133 // reg = env->RegisterNatives("org/webrtc/WebRtcTest", ,); | 132 // reg = env->RegisterNatives("org/webrtc/WebRtcTest", ,); |
134 // obj = reg->NewObject("<init>", ,); | 133 // obj = reg->NewObject("<init>", ,); |
135 // } | 134 // } |
136 // | 135 // |
137 // // Each User method can now use |reg| and |obj| and call Java functions | 136 // // Each User method can now use |reg| and |obj| and call Java functions |
138 // // in WebRtcTest.java, e.g. boolean init() {}. | 137 // // in WebRtcTest.java, e.g. boolean init() {}. |
139 // bool User::Foo() { | 138 // bool User::Foo() { |
140 // jmethodID id = reg->GetMethodId("init", "()Z"); | 139 // jmethodID id = reg->GetMethodId("init", "()Z"); |
141 // return obj->CallBooleanMethod(id); | 140 // return obj->CallBooleanMethod(id); |
142 // } | 141 // } |
143 // | 142 // |
144 // // And finally, e.g. in JNI_OnUnLoad, call JVM::Uninitialize. | 143 // // And finally, e.g. in JNI_OnUnLoad, call JVM::Uninitialize. |
145 // JVM::Uninitialize(); | 144 // JVM::Uninitialize(); |
146 class JVM { | 145 class JVM { |
147 public: | 146 public: |
148 // Stores global handles to the Java VM interface and the application context. | 147 // Stores global handles to the Java VM interface. |
149 // Should be called once on a thread that is attached to the JVM. | 148 // Should be called once on a thread that is attached to the JVM. |
| 149 static void Initialize(JavaVM* jvm); |
| 150 // TODO(sakal): Remove once downstream dependencies have been updated. |
| 151 // DEPRECATED (crbug.com/webrtc/7710): Old signature taking the Android |
| 152 // context as a parameter. Currently passes in the context to the ContextUtils |
| 153 // class. |
150 static void Initialize(JavaVM* jvm, jobject context); | 154 static void Initialize(JavaVM* jvm, jobject context); |
151 // Clears handles stored in Initialize(). Must be called on same thread as | 155 // Clears handles stored in Initialize(). Must be called on same thread as |
152 // Initialize(). | 156 // Initialize(). |
153 static void Uninitialize(); | 157 static void Uninitialize(); |
154 // Gives access to the global Java VM interface pointer, which then can be | 158 // Gives access to the global Java VM interface pointer, which then can be |
155 // used to create a valid JNIEnvironment object or to get a JavaClass object. | 159 // used to create a valid JNIEnvironment object or to get a JavaClass object. |
156 static JVM* GetInstance(); | 160 static JVM* GetInstance(); |
157 | 161 |
158 // Creates a JNIEnvironment object. | 162 // Creates a JNIEnvironment object. |
159 // This method returns a NULL pointer if AttachCurrentThread() has not been | 163 // This method returns a NULL pointer if AttachCurrentThread() has not been |
160 // called successfully. Use the AttachCurrentThreadIfNeeded class if needed. | 164 // called successfully. Use the AttachCurrentThreadIfNeeded class if needed. |
161 std::unique_ptr<JNIEnvironment> environment(); | 165 std::unique_ptr<JNIEnvironment> environment(); |
162 | 166 |
163 // Returns a JavaClass object given class |name|. | 167 // Returns a JavaClass object given class |name|. |
164 // Note that the class name must be one of the names in the static | 168 // Note that the class name must be one of the names in the static |
165 // |loaded_classes| array defined in jvm_android.cc. | 169 // |loaded_classes| array defined in jvm_android.cc. |
166 // This method must be called on the construction thread. | 170 // This method must be called on the construction thread. |
167 JavaClass GetClass(const char* name); | 171 JavaClass GetClass(const char* name); |
168 | 172 |
169 // TODO(henrika): can we make these private? | 173 // TODO(henrika): can we make these private? |
170 JavaVM* jvm() const { return jvm_; } | 174 JavaVM* jvm() const { return jvm_; } |
171 jobject context() const { return context_; } | |
172 | 175 |
173 protected: | 176 protected: |
174 JVM(JavaVM* jvm, jobject context); | 177 JVM(JavaVM* jvm); |
175 ~JVM(); | 178 ~JVM(); |
176 | 179 |
177 private: | 180 private: |
178 JNIEnv* jni() const { return GetEnv(jvm_); } | 181 JNIEnv* jni() const { return GetEnv(jvm_); } |
179 | 182 |
180 rtc::ThreadChecker thread_checker_; | 183 rtc::ThreadChecker thread_checker_; |
181 JavaVM* const jvm_; | 184 JavaVM* const jvm_; |
182 jobject context_; | |
183 }; | 185 }; |
184 | 186 |
185 } // namespace webrtc | 187 } // namespace webrtc |
186 | 188 |
187 #endif // WEBRTC_MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_ | 189 #endif // WEBRTC_MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_ |
OLD | NEW |