Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: shell/android/intent_manager_impl.cc

Issue 1061313003: Introduce intent manager. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Follow review Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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()) {
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::RegisterReceiver(
57 intent::IntentReceiverPtr receiver,
58 const RegisterReceiverCallback& callback) {
59 JNIEnv* env = base::android::AttachCurrentThread();
60 base::android::ScopedJavaLocalRef<jobject> buffer =
61 Java_IntentReceiverRegistry_registerReceiver(
62 env,
63 reinterpret_cast<uintptr_t>(new IntentDispatcher(receiver.Pass())));
64 callback.Run(BufferToArray(env, buffer.obj()));
65 }
66
67 bool RegisterIntentReceiverRegistry(JNIEnv* env) {
68 return RegisterNativesImpl(env);
69 }
70
71 void OnIntentReceived(JNIEnv* env,
72 jclass jcaller,
73 jlong intent_dispatcher_ptr,
74 jobject intent) {
75 IntentDispatcher* intent_dispatcher =
76 reinterpret_cast<IntentDispatcher*>(intent_dispatcher_ptr);
77 intent_dispatcher->OnIntentReceived(env, intent);
78 }
79
80 } // namespace shell
81 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698