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..819e8e695b85a19f99891a0831253342b2afcf25 |
--- /dev/null |
+++ b/shell/android/apk/src/org/chromium/mojo/shell/IntentReceiverRegistry.java |
@@ -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. |
+ |
+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 class for services/android/intent.mojom. Create intent for privileged mojo applications and |
ppi
2015/04/07 15:42:14
The first sentence feels ambiguous. Please indicat
qsr
2015/04/08 10:04:14
Done.
|
+ * route received intent back to the mojo application. |
+ */ |
+@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<>(); |
ppi
2015/04/07 15:42:14
Should this be mReceiversByUuid to match the style
qsr
2015/04/08 10:04:13
Done.
|
+ 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); |
+} |