Index: content/public/android/java/src/org/chromium/content/browser/ServiceRegistrar.java |
diff --git a/content/public/android/java/src/org/chromium/content/browser/ServiceRegistrar.java b/content/public/android/java/src/org/chromium/content/browser/ServiceRegistrar.java |
index a9d7f8f9d8cd64e7457dd7b6371028a611217c61..b5a372dc3a9c1294d39a2e7bc859eb78f61e3136 100644 |
--- a/content/public/android/java/src/org/chromium/content/browser/ServiceRegistrar.java |
+++ b/content/public/android/java/src/org/chromium/content/browser/ServiceRegistrar.java |
@@ -4,15 +4,21 @@ |
package org.chromium.content.browser; |
+import android.app.Activity; |
import android.content.Context; |
import org.chromium.base.annotations.CalledByNative; |
import org.chromium.base.annotations.JNINamespace; |
import org.chromium.content.browser.ServiceRegistry.ImplementationFactory; |
+import org.chromium.content_public.browser.WebContents; |
import org.chromium.device.battery.BatteryMonitorFactory; |
+import org.chromium.device.nfc.NfcImpl; |
import org.chromium.device.vibration.VibrationManagerImpl; |
import org.chromium.mojom.device.BatteryMonitor; |
import org.chromium.mojom.device.VibrationManager; |
+import org.chromium.mojom.device.nfc.Nfc; |
+import org.chromium.ui.base.WindowAndroid; |
+import org.chromium.ui.base.WindowAndroidChangedObserver; |
/** |
* Registers mojo services exposed by the browser in the given registry. |
@@ -35,6 +41,54 @@ class ServiceRegistrar { |
} |
} |
+ private static class NfcImplementationFactory implements ImplementationFactory<Nfc> { |
+ private final Context mContext; |
+ private final WebContents mContents; |
+ |
+ NfcImplementationFactory(Context context, WebContents contents) { |
+ mContext = context; |
+ mContents = contents; |
+ } |
+ |
+ private static class ContextAwareNfcImpl extends NfcImpl implements |
+ WindowAndroidChangedObserver { |
+ private final ContentViewCore mContextViewCore; |
+ |
+ ContextAwareNfcImpl(Context context, ContentViewCore contextViewCore) { |
+ super(context); |
+ mContextViewCore = contextViewCore; |
+ if (mContextViewCore != null) { |
+ mContextViewCore.addWindowAndroidChangedObserver(this); |
+ if (mContextViewCore.getWindowAndroid() != null) { |
+ setActivity(mContextViewCore.getWindowAndroid().getActivity().get()); |
+ } |
+ } |
+ } |
+ |
+ @Override |
+ public void close() { |
+ super.close(); |
+ if (mContextViewCore != null) { |
+ mContextViewCore.removeWindowAndroidChangedObserver(this); |
+ } |
+ } |
+ |
+ @Override |
+ public void onWindowAndroidChanged(WindowAndroid newWindowAndroid) { |
+ Activity activity = null; |
+ if (newWindowAndroid != null) { |
+ activity = newWindowAndroid.getActivity().get(); |
+ } |
+ setActivity(activity); |
+ } |
+ } |
+ |
+ @Override |
+ public Nfc createImpl() { |
+ return new ContextAwareNfcImpl(mContext, ContentViewCore.fromWebContents(mContents)); |
+ } |
+ } |
+ |
private static class VibrationManagerImplementationFactory |
implements ImplementationFactory<VibrationManager> { |
private final Context mApplicationContext; |
@@ -57,10 +111,13 @@ class ServiceRegistrar { |
} |
@CalledByNative |
- static void registerFrameHostServices(ServiceRegistry registry, Context applicationContext) { |
+ static void registerFrameHostServices(ServiceRegistry registry, |
+ Context applicationContext, WebContents contents) { |
assert applicationContext != null; |
registry.addService(VibrationManager.MANAGER, |
new VibrationManagerImplementationFactory(applicationContext)); |
// TODO(avayvod): Register the PresentationService implementation here. |
+ registry.addService(Nfc.MANAGER, |
+ new NfcImplementationFactory(applicationContext, contents)); |
} |
} |