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 shell { | 11 namespace shell { |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 mojo::Array<uint8_t> BufferToArray(JNIEnv* env, jobject buffer) { | 15 mojo::Array<uint8_t> BufferToArray(JNIEnv* env, jobject buffer) { |
16 const size_t size = env->GetDirectBufferCapacity(buffer); | 16 const size_t size = env->GetDirectBufferCapacity(buffer); |
17 mojo::Array<uint8_t> result(size); | 17 mojo::Array<uint8_t> result(size); |
18 memcpy(&result.front(), env->GetDirectBufferAddress(buffer), size); | 18 memcpy(&result.front(), env->GetDirectBufferAddress(buffer), size); |
19 return result.Pass(); | 19 return result.Pass(); |
20 } | 20 } |
21 | 21 |
22 class IntentDispatcher : public mojo::ErrorHandler { | 22 class IntentDispatcher : public mojo::ErrorHandler { |
23 public: | 23 public: |
24 IntentDispatcher(intent_receiver::IntentReceiverPtr intent_receiver) | 24 enum LifeCycle { Persistent, OneShot }; |
25 : intent_receiver_(intent_receiver.Pass()) { | 25 IntentDispatcher(intent_receiver::IntentReceiverPtr intent_receiver, |
| 26 LifeCycle life_cycle) |
| 27 : intent_receiver_(intent_receiver.Pass()), life_cycle_(life_cycle) { |
26 intent_receiver_.set_error_handler(this); | 28 intent_receiver_.set_error_handler(this); |
27 } | 29 } |
28 | 30 |
29 ~IntentDispatcher() { | 31 ~IntentDispatcher() { |
30 Java_IntentReceiverRegistry_unregisterReceiver( | 32 Java_IntentReceiverRegistry_unregisterReceiver( |
31 base::android::AttachCurrentThread(), | 33 base::android::AttachCurrentThread(), |
32 reinterpret_cast<uintptr_t>(this)); | 34 reinterpret_cast<uintptr_t>(this)); |
33 } | 35 } |
34 | 36 |
35 void OnIntentReceived(JNIEnv* env, jobject intent) { | 37 void OnIntentReceived(JNIEnv* env, jobject intent) { |
36 intent_receiver_->OnIntent(BufferToArray(env, intent)); | 38 intent_receiver_->OnIntent(BufferToArray(env, intent)); |
| 39 if (life_cycle_ == OneShot) |
| 40 delete this; |
37 } | 41 } |
38 | 42 |
39 private: | 43 private: |
40 // Overriden from mojo::ErrorHandler | 44 // Overriden from mojo::ErrorHandler |
41 void OnConnectionError() { | 45 void OnConnectionError() { |
42 intent_receiver_.set_error_handler(nullptr); | 46 intent_receiver_.set_error_handler(nullptr); |
43 delete this; | 47 delete this; |
44 } | 48 } |
45 | 49 |
46 intent_receiver::IntentReceiverPtr intent_receiver_; | 50 intent_receiver::IntentReceiverPtr intent_receiver_; |
| 51 LifeCycle life_cycle_; |
47 }; | 52 }; |
48 | 53 |
49 } // namespace | 54 } // namespace |
50 | 55 |
51 void IntentReceiverManagerImpl::Bind( | 56 void IntentReceiverManagerImpl::Bind( |
52 mojo::InterfaceRequest<intent_receiver::IntentReceiverManager> request) { | 57 mojo::InterfaceRequest<intent_receiver::IntentReceiverManager> request) { |
53 bindings_.AddBinding(this, request.Pass()); | 58 bindings_.AddBinding(this, request.Pass()); |
54 } | 59 } |
55 | 60 |
56 void IntentReceiverManagerImpl::RegisterReceiver( | 61 void IntentReceiverManagerImpl::RegisterIntentReceiver( |
57 intent_receiver::IntentReceiverPtr receiver, | 62 intent_receiver::IntentReceiverPtr receiver, |
58 const RegisterReceiverCallback& callback) { | 63 const RegisterIntentReceiverCallback& callback) { |
59 JNIEnv* env = base::android::AttachCurrentThread(); | 64 JNIEnv* env = base::android::AttachCurrentThread(); |
60 base::android::ScopedJavaLocalRef<jobject> buffer = | 65 base::android::ScopedJavaLocalRef<jobject> buffer = |
61 Java_IntentReceiverRegistry_registerReceiver( | 66 Java_IntentReceiverRegistry_registerIntentReceiver( |
62 env, | 67 env, reinterpret_cast<uintptr_t>(new IntentDispatcher( |
63 reinterpret_cast<uintptr_t>(new IntentDispatcher(receiver.Pass()))); | 68 receiver.Pass(), IntentDispatcher::Persistent))); |
64 callback.Run(BufferToArray(env, buffer.obj())); | 69 callback.Run(BufferToArray(env, buffer.obj())); |
65 } | 70 } |
66 | 71 |
| 72 void IntentReceiverManagerImpl::RegisterActivityResultReceiver( |
| 73 intent_receiver::IntentReceiverPtr receiver, |
| 74 const RegisterActivityResultReceiverCallback& callback) { |
| 75 JNIEnv* env = base::android::AttachCurrentThread(); |
| 76 base::android::ScopedJavaLocalRef<jobject> buffer = |
| 77 Java_IntentReceiverRegistry_registerActivityResultReceiver( |
| 78 env, reinterpret_cast<uintptr_t>(new IntentDispatcher( |
| 79 receiver.Pass(), IntentDispatcher::Persistent))); |
| 80 callback.Run(BufferToArray(env, buffer.obj())); |
| 81 } |
| 82 |
67 bool RegisterIntentReceiverRegistry(JNIEnv* env) { | 83 bool RegisterIntentReceiverRegistry(JNIEnv* env) { |
68 return RegisterNativesImpl(env); | 84 return RegisterNativesImpl(env); |
69 } | 85 } |
70 | 86 |
71 void OnIntentReceived(JNIEnv* env, | 87 void OnIntentReceived(JNIEnv* env, |
72 jclass jcaller, | 88 jclass jcaller, |
73 jlong intent_dispatcher_ptr, | 89 jlong intent_dispatcher_ptr, |
| 90 jboolean success, |
74 jobject intent) { | 91 jobject intent) { |
75 IntentDispatcher* intent_dispatcher = | 92 IntentDispatcher* intent_dispatcher = |
76 reinterpret_cast<IntentDispatcher*>(intent_dispatcher_ptr); | 93 reinterpret_cast<IntentDispatcher*>(intent_dispatcher_ptr); |
| 94 if (success) { |
77 intent_dispatcher->OnIntentReceived(env, intent); | 95 intent_dispatcher->OnIntentReceived(env, intent); |
| 96 } else { |
| 97 delete intent_dispatcher; |
| 98 } |
78 } | 99 } |
79 | 100 |
80 } // namespace shell | 101 } // namespace shell |
OLD | NEW |