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