| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "shell/android/intent_receiver_manager_impl.h" | 5 #include "shell/android/intent_receiver_manager_impl.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "jni/IntentReceiverRegistry_jni.h" | 8 #include "jni/IntentReceiverRegistry_jni.h" |
| 9 #include "mojo/public/cpp/bindings/error_handler.h" | 9 #include "mojo/public/cpp/bindings/error_handler.h" |
| 10 | 10 |
| 11 namespace mojo { | |
| 12 namespace shell { | 11 namespace shell { |
| 13 | 12 |
| 14 namespace { | 13 namespace { |
| 15 | 14 |
| 16 mojo::Array<uint8> BufferToArray(JNIEnv* env, jobject buffer) { | 15 mojo::Array<uint8_t> BufferToArray(JNIEnv* env, jobject buffer) { |
| 17 const size_t size = env->GetDirectBufferCapacity(buffer); | 16 const size_t size = env->GetDirectBufferCapacity(buffer); |
| 18 Array<uint8> result(size); | 17 mojo::Array<uint8_t> result(size); |
| 19 memcpy(&result.front(), env->GetDirectBufferAddress(buffer), size); | 18 memcpy(&result.front(), env->GetDirectBufferAddress(buffer), size); |
| 20 return result.Pass(); | 19 return result.Pass(); |
| 21 } | 20 } |
| 22 | 21 |
| 23 class IntentDispatcher : public ErrorHandler { | 22 class IntentDispatcher : public mojo::ErrorHandler { |
| 24 public: | 23 public: |
| 25 IntentDispatcher(intent_receiver::IntentReceiverPtr intent_receiver) | 24 IntentDispatcher(intent_receiver::IntentReceiverPtr intent_receiver) |
| 26 : intent_receiver_(intent_receiver.Pass()) { | 25 : intent_receiver_(intent_receiver.Pass()) { |
| 27 intent_receiver_.set_error_handler(this); | 26 intent_receiver_.set_error_handler(this); |
| 28 } | 27 } |
| 29 | 28 |
| 30 ~IntentDispatcher() { | 29 ~IntentDispatcher() { |
| 31 Java_IntentReceiverRegistry_unregisterReceiver( | 30 Java_IntentReceiverRegistry_unregisterReceiver( |
| 32 base::android::AttachCurrentThread(), | 31 base::android::AttachCurrentThread(), |
| 33 reinterpret_cast<uintptr_t>(this)); | 32 reinterpret_cast<uintptr_t>(this)); |
| 34 } | 33 } |
| 35 | 34 |
| 36 void OnIntentReceived(JNIEnv* env, jobject intent) { | 35 void OnIntentReceived(JNIEnv* env, jobject intent) { |
| 37 intent_receiver_->OnIntent(BufferToArray(env, intent)); | 36 intent_receiver_->OnIntent(BufferToArray(env, intent)); |
| 38 } | 37 } |
| 39 | 38 |
| 40 private: | 39 private: |
| 41 // Overriden from ErrorHandler | 40 // Overriden from mojo::ErrorHandler |
| 42 void OnConnectionError() { | 41 void OnConnectionError() { |
| 43 intent_receiver_.set_error_handler(nullptr); | 42 intent_receiver_.set_error_handler(nullptr); |
| 44 delete this; | 43 delete this; |
| 45 } | 44 } |
| 46 | 45 |
| 47 intent_receiver::IntentReceiverPtr intent_receiver_; | 46 intent_receiver::IntentReceiverPtr intent_receiver_; |
| 48 }; | 47 }; |
| 49 | 48 |
| 50 } // namespace | 49 } // namespace |
| 51 | 50 |
| 52 void IntentReceiverManagerImpl::Bind( | 51 void IntentReceiverManagerImpl::Bind( |
| 53 InterfaceRequest<intent_receiver::IntentReceiverManager> request) { | 52 mojo::InterfaceRequest<intent_receiver::IntentReceiverManager> request) { |
| 54 bindings_.AddBinding(this, request.Pass()); | 53 bindings_.AddBinding(this, request.Pass()); |
| 55 } | 54 } |
| 56 | 55 |
| 57 void IntentReceiverManagerImpl::RegisterReceiver( | 56 void IntentReceiverManagerImpl::RegisterReceiver( |
| 58 intent_receiver::IntentReceiverPtr receiver, | 57 intent_receiver::IntentReceiverPtr receiver, |
| 59 const RegisterReceiverCallback& callback) { | 58 const RegisterReceiverCallback& callback) { |
| 60 JNIEnv* env = base::android::AttachCurrentThread(); | 59 JNIEnv* env = base::android::AttachCurrentThread(); |
| 61 base::android::ScopedJavaLocalRef<jobject> buffer = | 60 base::android::ScopedJavaLocalRef<jobject> buffer = |
| 62 Java_IntentReceiverRegistry_registerReceiver( | 61 Java_IntentReceiverRegistry_registerReceiver( |
| 63 env, | 62 env, |
| 64 reinterpret_cast<uintptr_t>(new IntentDispatcher(receiver.Pass()))); | 63 reinterpret_cast<uintptr_t>(new IntentDispatcher(receiver.Pass()))); |
| 65 callback.Run(BufferToArray(env, buffer.obj())); | 64 callback.Run(BufferToArray(env, buffer.obj())); |
| 66 } | 65 } |
| 67 | 66 |
| 68 bool RegisterIntentReceiverRegistry(JNIEnv* env) { | 67 bool RegisterIntentReceiverRegistry(JNIEnv* env) { |
| 69 return RegisterNativesImpl(env); | 68 return RegisterNativesImpl(env); |
| 70 } | 69 } |
| 71 | 70 |
| 72 void OnIntentReceived(JNIEnv* env, | 71 void OnIntentReceived(JNIEnv* env, |
| 73 jclass jcaller, | 72 jclass jcaller, |
| 74 jlong intent_dispatcher_ptr, | 73 jlong intent_dispatcher_ptr, |
| 75 jobject intent) { | 74 jobject intent) { |
| 76 IntentDispatcher* intent_dispatcher = | 75 IntentDispatcher* intent_dispatcher = |
| 77 reinterpret_cast<IntentDispatcher*>(intent_dispatcher_ptr); | 76 reinterpret_cast<IntentDispatcher*>(intent_dispatcher_ptr); |
| 78 intent_dispatcher->OnIntentReceived(env, intent); | 77 intent_dispatcher->OnIntentReceived(env, intent); |
| 79 } | 78 } |
| 80 | 79 |
| 81 } // namespace shell | 80 } // namespace shell |
| 82 } // namespace mojo | |
| OLD | NEW |