OLD | NEW |
(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_); // Transfers ownership. |
| 43 delete instance; |
| 44 } |
| 45 } |
| 46 return ports; |
| 47 } |
| 48 |
| 49 void AppWebMessagePort::CloseMessagePort( |
| 50 JNIEnv* env, |
| 51 const base::android::JavaParamRef<jobject>& jcaller) { |
| 52 // Explicitly reset the port here to ensure that OnMessagesAvailable has |
| 53 // finished before we destroy this. |
| 54 port_ = MessagePort(); |
| 55 |
| 56 delete this; |
| 57 } |
| 58 |
| 59 void AppWebMessagePort::PostMessage( |
| 60 JNIEnv* env, |
| 61 const base::android::JavaParamRef<jobject>& jcaller, |
| 62 const base::android::JavaParamRef<jstring>& jmessage, |
| 63 const base::android::JavaParamRef<jobjectArray>& jports) { |
| 64 port_.PostMessage( |
| 65 EncodeStringMessage(base::android::ConvertJavaStringToUTF16(jmessage)), |
| 66 UnwrapJavaArray(env, jports)); |
| 67 } |
| 68 |
| 69 jboolean AppWebMessagePort::DispatchNextMessage( |
| 70 JNIEnv* env, |
| 71 const base::android::JavaParamRef<jobject>& jcaller) { |
| 72 base::android::ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 73 if (obj.is_null()) |
| 74 return false; |
| 75 |
| 76 jmethodID app_web_message_port_constructor = |
| 77 base::android::MethodID::Get<base::android::MethodID::TYPE_INSTANCE>( |
| 78 env, AppWebMessagePort_clazz(env), "<init>", "()V"); |
| 79 |
| 80 base::string16 encoded_message; |
| 81 std::vector<MessagePort> ports; |
| 82 if (!port_.GetMessage(&encoded_message, &ports)) |
| 83 return false; |
| 84 |
| 85 base::string16 message; |
| 86 if (!DecodeStringMessage(encoded_message, &message)) |
| 87 return false; |
| 88 |
| 89 base::android::ScopedJavaLocalRef<jstring> jmessage = |
| 90 base::android::ConvertUTF16ToJavaString(env, message); |
| 91 |
| 92 base::android::ScopedJavaLocalRef<jobjectArray> jports; |
| 93 if (ports.size() > 0) { |
| 94 jports = base::android::ScopedJavaLocalRef<jobjectArray>( |
| 95 env, |
| 96 env->NewObjectArray(ports.size(), |
| 97 AppWebMessagePort_clazz(env), |
| 98 nullptr)); |
| 99 |
| 100 // Instantiate the Java and C++ wrappers for the transferred ports. |
| 101 for (size_t i = 0; i < ports.size(); ++i) { |
| 102 base::android::ScopedJavaLocalRef<jobject> jport( |
| 103 env, |
| 104 env->NewObject(AppWebMessagePort_clazz(env), |
| 105 app_web_message_port_constructor)); |
| 106 CreateAndBindToJavaObject(env, ports[i].ReleaseHandle(), jport); |
| 107 |
| 108 env->SetObjectArrayElement(jports.obj(), i, jport.obj()); |
| 109 } |
| 110 } |
| 111 |
| 112 Java_AppWebMessagePort_onReceivedMessage(env, obj, jmessage, jports); |
| 113 return true; |
| 114 } |
| 115 |
| 116 void AppWebMessagePort::StartReceivingMessages( |
| 117 JNIEnv* env, |
| 118 const base::android::JavaParamRef<jobject>& jcaller) { |
| 119 port_.SetCallback( |
| 120 base::Bind(&AppWebMessagePort::OnMessagesAvailable, |
| 121 base::Unretained(this))); |
| 122 } |
| 123 |
| 124 AppWebMessagePort::AppWebMessagePort( |
| 125 JNIEnv* env, |
| 126 mojo::ScopedMessagePipeHandle handle, |
| 127 const base::android::JavaRef<jobject>& jobject) |
| 128 : port_(std::move(handle)), |
| 129 java_ref_(env, jobject) { |
| 130 } |
| 131 |
| 132 AppWebMessagePort::~AppWebMessagePort() { |
| 133 } |
| 134 |
| 135 void AppWebMessagePort::OnMessagesAvailable() { |
| 136 // Called on a background thread. |
| 137 JNIEnv* env = base::android::AttachCurrentThread(); |
| 138 base::android::ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 139 if (obj.is_null()) |
| 140 return; |
| 141 Java_AppWebMessagePort_onMessagesAvailable(env, obj); |
| 142 } |
| 143 |
| 144 bool RegisterAppWebMessagePort(JNIEnv* env) { |
| 145 return RegisterNativesImpl(env); |
| 146 } |
| 147 |
| 148 void InitializeAppWebMessagePortPair( |
| 149 JNIEnv* env, |
| 150 const base::android::JavaParamRef<jclass>& jcaller, |
| 151 const base::android::JavaParamRef<jobjectArray>& ports) { |
| 152 DCHECK_EQ(2, env->GetArrayLength(ports.obj())); |
| 153 |
| 154 mojo::MessagePipe pipe; |
| 155 |
| 156 base::android::ScopedJavaLocalRef<jobject> jport0( |
| 157 env, env->GetObjectArrayElement(ports.obj(), 0)); |
| 158 AppWebMessagePort::CreateAndBindToJavaObject( |
| 159 env, std::move(pipe.handle0), jport0); |
| 160 |
| 161 base::android::ScopedJavaLocalRef<jobject> jport1( |
| 162 env, env->GetObjectArrayElement(ports.obj(), 1)); |
| 163 AppWebMessagePort::CreateAndBindToJavaObject( |
| 164 env, std::move(pipe.handle1), jport1); |
| 165 } |
| 166 |
| 167 } // namespace content |
OLD | NEW |