Index: shell/android/apk/src/org/chromium/mojo/shell/IntentReceiverRegistry.java |
diff --git a/shell/android/apk/src/org/chromium/mojo/shell/IntentReceiverRegistry.java b/shell/android/apk/src/org/chromium/mojo/shell/IntentReceiverRegistry.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b3f8482471a975660f05fc9436f9993135d36639 |
--- /dev/null |
+++ b/shell/android/apk/src/org/chromium/mojo/shell/IntentReceiverRegistry.java |
@@ -0,0 +1,84 @@ |
+// 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. |
+ |
+package org.chromium.mojo.shell; |
+ |
+import android.content.Intent; |
+import android.os.Parcel; |
+ |
+import org.chromium.base.ApplicationStatus; |
+import org.chromium.base.CalledByNative; |
+import org.chromium.base.JNINamespace; |
+ |
+import java.nio.ByteBuffer; |
+import java.util.HashMap; |
+import java.util.Map; |
+import java.util.UUID; |
+ |
+/** |
+ * Java helper class for services/android/intent.mojom. This class creates intents for privileged |
+ * mojo applications and route received intent back to the mojo application.The implementation of |
+ * the IntentManager service is in C++, but calls back to this class to access the android |
+ * framework. |
+ */ |
+@JNINamespace("mojo::shell") |
+public class IntentReceiverRegistry { |
+ private static class LazyHolder { |
+ private static final IntentReceiverRegistry INSTANCE = new IntentReceiverRegistry(); |
+ } |
+ |
+ /** |
+ * Returns the instance. |
+ */ |
+ public static IntentReceiverRegistry getInstance() { |
+ return LazyHolder.INSTANCE; |
+ } |
+ |
+ private final Map<String, Long> mReceiversByUuid = new HashMap<>(); |
+ private final Map<Long, String> mUuidsByReceiver = new HashMap<>(); |
+ |
+ private IntentReceiverRegistry() {} |
+ |
+ public void onIntentReceived(Intent intent) { |
+ String uuid = intent.getAction(); |
+ if (uuid == null) return; |
+ Long ptr = mReceiversByUuid.get(uuid); |
+ if (ptr == null) return; |
+ nativeOnIntentReceived(ptr, intentToBuffer(intent)); |
+ } |
+ |
+ private static ByteBuffer intentToBuffer(Intent intent) { |
+ Parcel p = Parcel.obtain(); |
+ intent.writeToParcel(p, 0); |
+ p.setDataPosition(0); |
+ byte[] b = p.marshall(); |
+ ByteBuffer result = ByteBuffer.allocateDirect(b.length); |
+ result.put(b); |
+ result.flip(); |
+ return result; |
+ } |
+ |
+ @CalledByNative |
+ private static ByteBuffer registerReceiver(long intentDispatcher) { |
+ IntentReceiverRegistry registry = getInstance(); |
+ String uuid = UUID.randomUUID().toString(); |
+ Intent intent = new Intent(uuid, null, ApplicationStatus.getApplicationContext(), |
+ DelegatingIntentService.class); |
+ registry.mReceiversByUuid.put(uuid, intentDispatcher); |
+ registry.mUuidsByReceiver.put(intentDispatcher, uuid); |
+ return intentToBuffer(intent); |
+ } |
+ |
+ @CalledByNative |
+ private static void unregisterReceiver(long intentDispatcher) { |
+ IntentReceiverRegistry registry = getInstance(); |
+ String uuid = registry.mUuidsByReceiver.get(intentDispatcher); |
+ if (uuid != null) { |
+ registry.mUuidsByReceiver.remove(intentDispatcher); |
+ registry.mReceiversByUuid.remove(uuid); |
+ } |
+ } |
+ |
+ private static native void nativeOnIntentReceived(long intentDispatcher, ByteBuffer intent); |
+} |