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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: shell/android/intent_receiver_manager_impl.cc
diff --git a/shell/android/intent_receiver_manager_impl.cc b/shell/android/intent_receiver_manager_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2a8919181e2a32686497ca47a82db98a50aff380
--- /dev/null
+++ b/shell/android/intent_receiver_manager_impl.cc
@@ -0,0 +1,82 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "shell/android/intent_receiver_manager_impl.h"
+
+#include "base/android/jni_android.h"
+#include "jni/IntentReceiverRegistry_jni.h"
+#include "mojo/public/cpp/bindings/error_handler.h"
+
+namespace mojo {
+namespace shell {
+
+namespace {
+
+mojo::Array<uint8> BufferToArray(JNIEnv* env, jobject buffer) {
+ const size_t size = env->GetDirectBufferCapacity(buffer);
+ Array<uint8> result(size);
+ memcpy(&result.front(), env->GetDirectBufferAddress(buffer), size);
+ return result.Pass();
+}
+
+class IntentDispatcher : public ErrorHandler {
+ public:
+ IntentDispatcher(intent_receiver::IntentReceiverPtr intent_receiver)
+ : intent_receiver_(intent_receiver.Pass()) {
+ intent_receiver_.set_error_handler(this);
+ }
+
+ ~IntentDispatcher() {
+ Java_IntentReceiverRegistry_unregisterReceiver(
+ base::android::AttachCurrentThread(),
+ reinterpret_cast<uintptr_t>(this));
+ }
+
+ void OnIntentReceived(JNIEnv* env, jobject intent) {
+ intent_receiver_->OnIntent(BufferToArray(env, intent));
+ }
+
+ private:
+ // Overriden from ErrorHandler
+ void OnConnectionError() {
+ intent_receiver_.set_error_handler(nullptr);
+ delete this;
+ }
+
+ intent_receiver::IntentReceiverPtr intent_receiver_;
+};
+
+} // namespace
+
+void IntentReceiverManagerImpl::Bind(
+ InterfaceRequest<intent_receiver::IntentReceiverManager> request) {
+ bindings_.AddBinding(this, request.Pass());
+}
+
+void IntentReceiverManagerImpl::RegisterReceiver(
+ intent_receiver::IntentReceiverPtr receiver,
+ const RegisterReceiverCallback& callback) {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ base::android::ScopedJavaLocalRef<jobject> buffer =
+ Java_IntentReceiverRegistry_registerReceiver(
+ env,
+ reinterpret_cast<uintptr_t>(new IntentDispatcher(receiver.Pass())));
+ callback.Run(BufferToArray(env, buffer.obj()));
+}
+
+bool RegisterIntentReceiverRegistry(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+void OnIntentReceived(JNIEnv* env,
+ jclass jcaller,
+ jlong intent_dispatcher_ptr,
+ jobject intent) {
+ IntentDispatcher* intent_dispatcher =
+ reinterpret_cast<IntentDispatcher*>(intent_dispatcher_ptr);
+ intent_dispatcher->OnIntentReceived(env, intent);
+}
+
+} // namespace shell
+} // namespace mojo
« 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