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
3 // found in the LICENSE file.
5 #include "content/browser/mojo/service_registry_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/callback.h"
10 #include "content/common/mojo/service_registry_impl.h"
11 #include "jni/ServiceRegistry_jni.h"
13 using base::android::AttachCurrentThread
;
14 using base::android::ConvertJavaStringToUTF8
;
15 using base::android::ScopedJavaGlobalRef
;
21 // Callback passed to the wrapped ServiceRegistry upon AddService(). The
22 // ServiceRegistry will call it to create a registered Java service
23 void CreateImplAndAttach(
24 const ScopedJavaGlobalRef
<jobject
>& j_scoped_service_registry
,
25 const ScopedJavaGlobalRef
<jobject
>& j_scoped_manager
,
26 const ScopedJavaGlobalRef
<jobject
>& j_scoped_factory
,
27 mojo::ScopedMessagePipeHandle handle
) {
28 JNIEnv
* env
= AttachCurrentThread();
29 Java_ServiceRegistry_createImplAndAttach(env
,
30 j_scoped_service_registry
.obj(),
31 handle
.release().value(),
32 j_scoped_manager
.obj(),
33 j_scoped_factory
.obj());
39 bool ServiceRegistryAndroid::Register(JNIEnv
* env
) {
40 return RegisterNativesImpl(env
);
43 // Constructor and destructor call into Java.
44 ServiceRegistryAndroid::ServiceRegistryAndroid(
45 ServiceRegistryImpl
* service_registry
)
46 : service_registry_(service_registry
) {
47 JNIEnv
* env
= AttachCurrentThread();
50 Java_ServiceRegistry_create(env
, reinterpret_cast<intptr_t>(this)).obj());
53 ServiceRegistryAndroid::~ServiceRegistryAndroid() {
54 Java_ServiceRegistry_destroy(AttachCurrentThread(), obj_
.obj());
57 // Methods called from Java.
58 void ServiceRegistryAndroid::AddService(JNIEnv
* env
,
59 jobject j_service_registry
,
63 std::string
name(ConvertJavaStringToUTF8(env
, j_name
));
65 ScopedJavaGlobalRef
<jobject
> j_scoped_service_registry
;
66 j_scoped_service_registry
.Reset(env
, j_service_registry
);
68 ScopedJavaGlobalRef
<jobject
> j_scoped_manager
;
69 j_scoped_manager
.Reset(env
, j_manager
);
71 ScopedJavaGlobalRef
<jobject
> j_scoped_factory
;
72 j_scoped_factory
.Reset(env
, j_factory
);
74 service_registry_
->AddService(name
,
75 base::Bind(&CreateImplAndAttach
,
76 j_scoped_service_registry
,
81 void ServiceRegistryAndroid::RemoveService(JNIEnv
* env
,
82 jobject j_service_registry
,
84 std::string
name(ConvertJavaStringToUTF8(env
, j_name
));
85 service_registry_
->RemoveService(name
);
88 void ServiceRegistryAndroid::ConnectToRemoteService(JNIEnv
* env
,
89 jobject j_service_registry
,
92 std::string
name(ConvertJavaStringToUTF8(env
, j_name
));
93 mojo::ScopedMessagePipeHandle
handle((mojo::MessagePipeHandle(j_handle
)));
94 service_registry_
->ConnectToRemoteService(name
, handle
.Pass());
97 } // namespace content