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_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::IntentReceiverPtr intent_receiver) | |
26 : intent_receiver_(intent_receiver.Pass()) { | |
ppi
2015/04/07 15:42:14
should we call Java_IntentReceiverRegistry_registe
qsr
2015/04/08 10:04:14
It is kind of hard, as registerReceiver is returni
| |
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::IntentReceiverPtr intent_receiver_; | |
48 }; | |
49 | |
50 } // namespace | |
51 | |
52 void IntentManagerImpl::Bind(InterfaceRequest<intent::IntentManager> request) { | |
53 bindings_.AddBinding(this, request.Pass()); | |
54 } | |
55 | |
56 void IntentManagerImpl::GetIntent(intent::IntentReceiverPtr receiver, | |
57 const GetIntentCallback& callback) { | |
58 JNIEnv* env = base::android::AttachCurrentThread(); | |
59 base::android::ScopedJavaLocalRef<jobject> buffer = | |
60 Java_IntentReceiverRegistry_registerReceiver( | |
61 env, | |
62 reinterpret_cast<uintptr_t>(new IntentDispatcher(receiver.Pass()))); | |
63 callback.Run(BufferToArray(env, buffer.obj())); | |
64 } | |
65 | |
66 bool RegisterIntentReceiverRegistry(JNIEnv* env) { | |
67 return RegisterNativesImpl(env); | |
68 } | |
69 | |
70 void OnIntentReceived(JNIEnv* env, | |
71 jclass jcaller, | |
72 jlong intent_dispatcher_ptr, | |
73 jobject intent) { | |
74 IntentDispatcher* intent_dispatcher = | |
75 reinterpret_cast<IntentDispatcher*>(intent_dispatcher_ptr); | |
76 intent_dispatcher->OnIntentReceived(env, intent); | |
77 } | |
78 | |
79 } // namespace shell | |
80 } // namespace mojo | |
OLD | NEW |