| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/mojo/service_registry_android.h" | 5 #include "content/public/browser/android/service_registry_android.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "content/common/mojo/service_registry_impl.h" | |
| 13 #include "jni/ServiceRegistry_jni.h" | 12 #include "jni/ServiceRegistry_jni.h" |
| 14 | 13 |
| 15 using base::android::AttachCurrentThread; | 14 using base::android::AttachCurrentThread; |
| 16 using base::android::ConvertJavaStringToUTF8; | 15 using base::android::ConvertJavaStringToUTF8; |
| 17 using base::android::ScopedJavaGlobalRef; | 16 using base::android::ScopedJavaGlobalRef; |
| 18 | 17 |
| 19 namespace content { | 18 namespace content { |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 22 const char kServiceRegistryAndroidKey[] = "service_registry_android"; |
| 23 |
| 23 // Callback passed to the wrapped ServiceRegistry upon AddService(). The | 24 // Callback passed to the wrapped ServiceRegistry upon AddService(). The |
| 24 // ServiceRegistry will call it to create a registered Java service | 25 // ServiceRegistry will call it to create a registered Java service |
| 25 void CreateImplAndAttach( | 26 void CreateImplAndAttach( |
| 26 const ScopedJavaGlobalRef<jobject>& j_scoped_service_registry, | 27 const ScopedJavaGlobalRef<jobject>& j_scoped_service_registry, |
| 27 const ScopedJavaGlobalRef<jobject>& j_scoped_manager, | 28 const ScopedJavaGlobalRef<jobject>& j_scoped_manager, |
| 28 const ScopedJavaGlobalRef<jobject>& j_scoped_factory, | 29 const ScopedJavaGlobalRef<jobject>& j_scoped_factory, |
| 29 mojo::ScopedMessagePipeHandle handle) { | 30 mojo::ScopedMessagePipeHandle handle) { |
| 30 JNIEnv* env = AttachCurrentThread(); | 31 JNIEnv* env = AttachCurrentThread(); |
| 31 Java_ServiceRegistry_createImplAndAttach(env, | 32 Java_ServiceRegistry_createImplAndAttach(env, |
| 32 j_scoped_service_registry.obj(), | 33 j_scoped_service_registry.obj(), |
| 33 handle.release().value(), | 34 handle.release().value(), |
| 34 j_scoped_manager.obj(), | 35 j_scoped_manager.obj(), |
| 35 j_scoped_factory.obj()); | 36 j_scoped_factory.obj()); |
| 36 } | 37 } |
| 37 | 38 |
| 38 } // namespace | 39 } // namespace |
| 39 | 40 |
| 40 // static | 41 // static |
| 41 bool ServiceRegistryAndroid::Register(JNIEnv* env) { | 42 bool ServiceRegistryAndroid::Register(JNIEnv* env) { |
| 42 return RegisterNativesImpl(env); | 43 return RegisterNativesImpl(env); |
| 43 } | 44 } |
| 44 | 45 |
| 45 // Constructor and destructor call into Java. | 46 // static |
| 46 ServiceRegistryAndroid::ServiceRegistryAndroid( | 47 ServiceRegistryAndroid* ServiceRegistryAndroid::From( |
| 47 ServiceRegistryImpl* service_registry) | 48 ServiceRegistry* registry) { |
| 48 : service_registry_(service_registry) { | 49 ServiceRegistryAndroid* registry_android = |
| 49 JNIEnv* env = AttachCurrentThread(); | 50 static_cast<ServiceRegistryAndroid*>( |
| 50 obj_.Reset( | 51 registry->GetUserData(kServiceRegistryAndroidKey)); |
| 51 env, | 52 |
| 52 Java_ServiceRegistry_create(env, reinterpret_cast<intptr_t>(this)).obj()); | 53 if (!registry_android) { |
| 54 registry_android = new ServiceRegistryAndroid(registry); |
| 55 registry->SetUserData(kServiceRegistryAndroidKey, registry_android); |
| 56 } |
| 57 |
| 58 return registry_android; |
| 53 } | 59 } |
| 54 | 60 |
| 55 ServiceRegistryAndroid::~ServiceRegistryAndroid() { | 61 ServiceRegistryAndroid::~ServiceRegistryAndroid() { |
| 56 Java_ServiceRegistry_destroy(AttachCurrentThread(), obj_.obj()); | 62 Java_ServiceRegistry_destroy(AttachCurrentThread(), obj_.obj()); |
| 57 } | 63 } |
| 58 | 64 |
| 59 // Methods called from Java. | 65 // Methods called from Java. |
| 60 void ServiceRegistryAndroid::AddService( | 66 void ServiceRegistryAndroid::AddService( |
| 61 JNIEnv* env, | 67 JNIEnv* env, |
| 62 const JavaParamRef<jobject>& j_service_registry, | 68 const JavaParamRef<jobject>& j_service_registry, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 92 void ServiceRegistryAndroid::ConnectToRemoteService( | 98 void ServiceRegistryAndroid::ConnectToRemoteService( |
| 93 JNIEnv* env, | 99 JNIEnv* env, |
| 94 const JavaParamRef<jobject>& j_service_registry, | 100 const JavaParamRef<jobject>& j_service_registry, |
| 95 const JavaParamRef<jstring>& j_name, | 101 const JavaParamRef<jstring>& j_name, |
| 96 jint j_handle) { | 102 jint j_handle) { |
| 97 std::string name(ConvertJavaStringToUTF8(env, j_name)); | 103 std::string name(ConvertJavaStringToUTF8(env, j_name)); |
| 98 mojo::ScopedMessagePipeHandle handle((mojo::MessagePipeHandle(j_handle))); | 104 mojo::ScopedMessagePipeHandle handle((mojo::MessagePipeHandle(j_handle))); |
| 99 service_registry_->ConnectToRemoteService(name, std::move(handle)); | 105 service_registry_->ConnectToRemoteService(name, std::move(handle)); |
| 100 } | 106 } |
| 101 | 107 |
| 108 // Constructor and destructor call into Java. |
| 109 ServiceRegistryAndroid::ServiceRegistryAndroid( |
| 110 ServiceRegistry* service_registry) |
| 111 : service_registry_(service_registry) { |
| 112 JNIEnv* env = AttachCurrentThread(); |
| 113 obj_.Reset( |
| 114 env, |
| 115 Java_ServiceRegistry_create(env, reinterpret_cast<intptr_t>(this)).obj()); |
| 116 } |
| 117 |
| 102 } // namespace content | 118 } // namespace content |
| OLD | NEW |