Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / mojo / service_registry_android.cc
blob269c8be61f43f4a515f47c970fb390ca78941bd6
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;
17 namespace content {
19 namespace {
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());
36 } // namespace
38 // static
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();
48 obj_.Reset(
49 env,
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,
60 jobject j_manager,
61 jobject j_factory,
62 jstring j_name) {
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,
77 j_scoped_manager,
78 j_scoped_factory));
81 void ServiceRegistryAndroid::RemoveService(JNIEnv* env,
82 jobject j_service_registry,
83 jstring j_name) {
84 std::string name(ConvertJavaStringToUTF8(env, j_name));
85 service_registry_->RemoveService(name);
88 void ServiceRegistryAndroid::ConnectToRemoteService(JNIEnv* env,
89 jobject j_service_registry,
90 jstring j_name,
91 jint j_handle) {
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