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

Side by Side Diff: shell/android/intent_receiver_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
« no previous file with comments | « shell/android/intent_receiver_manager_impl.h ('k') | shell/android/library_loader.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_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
OLDNEW
« no previous file with comments | « shell/android/intent_receiver_manager_impl.h ('k') | shell/android/library_loader.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698