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

Unified Diff: shell/android/intent_manager_impl.cc

Issue 1061313003: Introduce intent manager. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: 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
Index: shell/android/intent_manager_impl.cc
diff --git a/shell/android/intent_manager_impl.cc b/shell/android/intent_manager_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..017c23938b1eff023b1e7537dbea868e2b36d843
--- /dev/null
+++ b/shell/android/intent_manager_impl.cc
@@ -0,0 +1,80 @@
+// 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_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::IntentReceiverPtr intent_receiver)
+ : 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
+ 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::IntentReceiverPtr intent_receiver_;
+};
+
+} // namespace
+
+void IntentManagerImpl::Bind(InterfaceRequest<intent::IntentManager> request) {
+ bindings_.AddBinding(this, request.Pass());
+}
+
+void IntentManagerImpl::GetIntent(intent::IntentReceiverPtr receiver,
+ const GetIntentCallback& 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

Powered by Google App Engine
This is Rietveld 408576698