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

Side by Side Diff: content/browser/android/app_web_message_port.cc

Issue 2422793002: HTML MessagePort as mojo::MessagePipeHandle (Closed)
Patch Set: Eliminate unnecessary PostTask Created 3 years, 10 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
(Empty)
1 // Copyright 2017 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.
4
5 #include "content/browser/android/app_web_message_port.h"
6
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/bind.h"
10 #include "content/browser/android/string_message_codec.h"
11 #include "jni/AppWebMessagePort_jni.h"
12
13 namespace content {
14
15 // static
16 void AppWebMessagePort::CreateAndBindToJavaObject(
17 JNIEnv* env,
18 mojo::ScopedMessagePipeHandle handle,
19 const base::android::JavaRef<jobject>& jobject) {
20 AppWebMessagePort* instance =
21 new AppWebMessagePort(env, std::move(handle), jobject);
22 Java_AppWebMessagePort_setNativeAppWebMessagePort(
23 env, jobject, reinterpret_cast<jlong>(instance));
24 }
25
26 // static
27 std::vector<MessagePort> AppWebMessagePort::UnwrapJavaArray(
28 JNIEnv* env,
29 const base::android::JavaRef<jobjectArray>& jports) {
30 std::vector<MessagePort> ports;
31 if (!jports.is_null()) {
32 jsize num_ports = env->GetArrayLength(jports.obj());
33 for (jsize i = 0; i < num_ports; ++i) {
34 base::android::ScopedJavaLocalRef<jobject> jport(
35 env, env->GetObjectArrayElement(jports.obj(), i));
36 jlong native_port =
37 Java_AppWebMessagePort_releaseNativePortForTransfer(env, jport);
38 // Uninitialized ports should be trapper earlier at the Java layer.
39 DCHECK(native_port != -1);
40 AppWebMessagePort* instance =
41 reinterpret_cast<AppWebMessagePort*>(native_port);
42 ports.push_back(instance->port_);
43 }
44 }
45 return ports;
46 }
47
48 void AppWebMessagePort::CloseMessagePort(
49 JNIEnv* env,
50 const base::android::JavaParamRef<jobject>& jcaller) {
51 // Explicitly reset the port here to ensure that OnMessagesAvailable has
52 // finished before we destroy this.
53 port_ = MessagePort();
54
55 delete this;
56 }
57
58 void AppWebMessagePort::PostMessage(
59 JNIEnv* env,
60 const base::android::JavaParamRef<jobject>& jcaller,
61 const base::android::JavaParamRef<jstring>& jmessage,
62 const base::android::JavaParamRef<jobjectArray>& jports) {
63 port_.PostMessage(
64 EncodeStringMessage(base::android::ConvertJavaStringToUTF16(jmessage)),
65 UnwrapJavaArray(env, jports));
66 }
67
68 void AppWebMessagePort::DispatchReceivedMessages(
69 JNIEnv* env,
70 const base::android::JavaParamRef<jobject>& jcaller) {
71 jmethodID app_web_message_port_constructor =
72 base::android::MethodID::Get<base::android::MethodID::TYPE_INSTANCE>(
73 env, AppWebMessagePort_clazz(env), "<init>", "()V");
74
75 // Consume all of the available messages.
76 // TODO(darin): Consider breaking this up across multiple PostTask calls.
77 for (;;) {
78 base::string16 encoded_message;
79 std::vector<MessagePort> ports;
80 if (!port_.GetMessage(&encoded_message, &ports))
81 return;
82
83 base::string16 message;
84 if (!DecodeStringMessage(encoded_message, &message))
85 return;
86
87 base::android::ScopedJavaLocalRef<jstring> jmessage =
88 base::android::ConvertUTF16ToJavaString(env, message);
89
90 base::android::ScopedJavaLocalRef<jobjectArray> jports;
91 if (ports.size() > 0) {
92 jports = base::android::ScopedJavaLocalRef<jobjectArray>(
93 env,
94 env->NewObjectArray(ports.size(),
95 AppWebMessagePort_clazz(env),
96 nullptr));
97
98 // Instantiate the Java and C++ wrappers for the transferred ports.
99 for (size_t i = 0; i < ports.size(); ++i) {
100 base::android::ScopedJavaLocalRef<jobject> jport(
101 env,
102 env->NewObject(AppWebMessagePort_clazz(env),
103 app_web_message_port_constructor));
104 CreateAndBindToJavaObject(env, ports[i].ReleaseHandle(), jport);
105
106 env->SetObjectArrayElement(jports.obj(), i, jport.obj());
107 }
108 }
109
110 Java_AppWebMessagePort_onReceivedMessage(
111 env, java_ref_.get(env), jmessage, jports);
sgurun-gerrit only 2017/02/08 21:47:50 see comment below about using weak ref.
112 }
113 }
114
115 void AppWebMessagePort::StartReceivingMessages(
116 JNIEnv* env,
117 const base::android::JavaParamRef<jobject>& jcaller) {
118 port_.SetCallback(
119 base::Bind(&AppWebMessagePort::OnMessagesAvailable,
120 base::Unretained(this)));
121 }
122
123 AppWebMessagePort::AppWebMessagePort(
124 JNIEnv* env,
125 mojo::ScopedMessagePipeHandle handle,
126 const base::android::JavaRef<jobject>& jobject)
127 : port_(std::move(handle)),
128 java_ref_(env, jobject) {
129 }
130
131 AppWebMessagePort::~AppWebMessagePort() {
132 }
133
134 void AppWebMessagePort::OnMessagesAvailable() {
135 // Called on the IO thread.
sgurun-gerrit only 2017/02/08 21:47:50 after moving to Mojo, is this still called on IO t
darin (slow to review) 2017/02/09 00:13:17 Yeah, good catch. This code should not assume that
136 JNIEnv* env = base::android::AttachCurrentThread();
137 Java_AppWebMessagePort_onMessagesAvailable(env, java_ref_.get(env));
sgurun-gerrit only 2017/02/08 21:47:50 java_ref_ is weak. I think you need to make sure i
darin (slow to review) 2017/02/09 00:13:17 Done.
138 }
139
140 bool RegisterAppWebMessagePort(JNIEnv* env) {
141 return RegisterNativesImpl(env);
142 }
143
144 void InitializeAppWebMessagePortPair(
145 JNIEnv* env,
146 const base::android::JavaParamRef<jclass>& jcaller,
147 const base::android::JavaParamRef<jobjectArray>& ports) {
148 DCHECK_EQ(2, env->GetArrayLength(ports.obj()));
149
150 mojo::MessagePipe pipe;
151
152 base::android::ScopedJavaLocalRef<jobject> jport0(
153 env, env->GetObjectArrayElement(ports.obj(), 0));
154 AppWebMessagePort::CreateAndBindToJavaObject(
155 env, std::move(pipe.handle0), jport0);
156
157 base::android::ScopedJavaLocalRef<jobject> jport1(
158 env, env->GetObjectArrayElement(ports.obj(), 1));
159 AppWebMessagePort::CreateAndBindToJavaObject(
160 env, std::move(pipe.handle1), jport1);
161 }
162
163 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698