OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
| 7 import android.app.Activity; |
7 import android.content.Context; | 8 import android.content.Context; |
8 | 9 |
9 import org.chromium.base.annotations.CalledByNative; | 10 import org.chromium.base.annotations.CalledByNative; |
10 import org.chromium.base.annotations.JNINamespace; | 11 import org.chromium.base.annotations.JNINamespace; |
11 import org.chromium.content.browser.InterfaceRegistry.ImplementationFactory; | 12 import org.chromium.content.browser.InterfaceRegistry.ImplementationFactory; |
| 13 import org.chromium.content_public.browser.WebContents; |
12 import org.chromium.device.battery.BatteryMonitorFactory; | 14 import org.chromium.device.battery.BatteryMonitorFactory; |
| 15 import org.chromium.device.nfc.NfcImpl; |
13 import org.chromium.device.vibration.VibrationManagerImpl; | 16 import org.chromium.device.vibration.VibrationManagerImpl; |
14 import org.chromium.mojom.device.BatteryMonitor; | 17 import org.chromium.mojom.device.BatteryMonitor; |
15 import org.chromium.mojom.device.VibrationManager; | 18 import org.chromium.mojom.device.VibrationManager; |
| 19 import org.chromium.mojom.device.nfc.Nfc; |
| 20 import org.chromium.ui.base.WindowAndroid; |
16 | 21 |
17 /** | 22 /** |
18 * Registers interfaces exposed by the browser in the given registry. | 23 * Registers interfaces exposed by the browser in the given registry. |
19 */ | 24 */ |
20 @JNINamespace("content") | 25 @JNINamespace("content") |
21 class InterfaceRegistrar { | 26 class InterfaceRegistrar { |
22 // BatteryMonitorFactory can't implement ImplementationFactory itself, as we
don't depend on | 27 // BatteryMonitorFactory can't implement ImplementationFactory itself, as we
don't depend on |
23 // /content in /device. Hence we use BatteryMonitorImplementationFactory as
a wrapper. | 28 // /content in /device. Hence we use BatteryMonitorImplementationFactory as
a wrapper. |
24 private static class BatteryMonitorImplementationFactory | 29 private static class BatteryMonitorImplementationFactory |
25 implements ImplementationFactory<BatteryMonitor> { | 30 implements ImplementationFactory<BatteryMonitor> { |
(...skipping 16 matching lines...) Expand all Loading... |
42 VibrationManagerImplementationFactory(Context applicationContext) { | 47 VibrationManagerImplementationFactory(Context applicationContext) { |
43 mApplicationContext = applicationContext; | 48 mApplicationContext = applicationContext; |
44 } | 49 } |
45 | 50 |
46 @Override | 51 @Override |
47 public VibrationManager createImpl() { | 52 public VibrationManager createImpl() { |
48 return new VibrationManagerImpl(mApplicationContext); | 53 return new VibrationManagerImpl(mApplicationContext); |
49 } | 54 } |
50 } | 55 } |
51 | 56 |
| 57 private static class NfcImplementationFactory implements ImplementationFacto
ry<Nfc> { |
| 58 private final Context mContext; |
| 59 private final WebContents mContents; |
| 60 |
| 61 NfcImplementationFactory(Context context, WebContents contents) { |
| 62 mContext = context; |
| 63 mContents = contents; |
| 64 } |
| 65 |
| 66 private static class ContextAwareNfcImpl extends NfcImpl implements |
| 67 WindowAndroidChangedObserver { |
| 68 private final ContentViewCore mContextViewCore; |
| 69 |
| 70 ContextAwareNfcImpl(Context context, ContentViewCore contextViewCore
) { |
| 71 super(context); |
| 72 mContextViewCore = contextViewCore; |
| 73 if (mContextViewCore != null) { |
| 74 mContextViewCore.addWindowAndroidChangedObserver(this); |
| 75 if (mContextViewCore.getWindowAndroid() != null) { |
| 76 setActivity(mContextViewCore.getWindowAndroid().getActiv
ity().get()); |
| 77 } |
| 78 } |
| 79 } |
| 80 |
| 81 @Override |
| 82 public void close() { |
| 83 super.close(); |
| 84 if (mContextViewCore != null) { |
| 85 mContextViewCore.removeWindowAndroidChangedObserver(this); |
| 86 } |
| 87 } |
| 88 |
| 89 @Override |
| 90 public void onWindowAndroidChanged(WindowAndroid newWindowAndroid) { |
| 91 Activity activity = null; |
| 92 if (newWindowAndroid != null) { |
| 93 activity = newWindowAndroid.getActivity().get(); |
| 94 } |
| 95 setActivity(activity); |
| 96 } |
| 97 } |
| 98 |
| 99 @Override |
| 100 public Nfc createImpl() { |
| 101 return new ContextAwareNfcImpl(mContext, ContentViewCore.fromWebCont
ents(mContents)); |
| 102 } |
| 103 } |
| 104 |
52 @CalledByNative | 105 @CalledByNative |
53 static void exposeInterfacesToRenderer(InterfaceRegistry registry, Context a
pplicationContext) { | 106 static void exposeInterfacesToRenderer(InterfaceRegistry registry, Context a
pplicationContext) { |
54 assert applicationContext != null; | 107 assert applicationContext != null; |
55 registry.addInterface(BatteryMonitor.MANAGER, | 108 registry.addInterface(BatteryMonitor.MANAGER, |
56 new BatteryMonitorImplementationFactory(applicationContext)); | 109 new BatteryMonitorImplementationFactory(applicationContext)); |
57 } | 110 } |
58 | 111 |
59 @CalledByNative | 112 @CalledByNative |
60 static void exposeInterfacesToFrame(InterfaceRegistry registry, Context appl
icationContext) { | 113 static void exposeInterfacesToFrame(InterfaceRegistry registry, |
| 114 Context applicationContext, WebContents contents) { |
61 assert applicationContext != null; | 115 assert applicationContext != null; |
62 registry.addInterface(VibrationManager.MANAGER, | 116 registry.addInterface(VibrationManager.MANAGER, |
63 new VibrationManagerImplementationFactory(applicationContext)); | 117 new VibrationManagerImplementationFactory(applicationContext)); |
| 118 registry.addInterface(Nfc.MANAGER, |
| 119 new NfcImplementationFactory(applicationContext, contents)); |
64 // TODO(avayvod): Register the PresentationService implementation here. | 120 // TODO(avayvod): Register the PresentationService implementation here. |
65 } | 121 } |
66 } | 122 } |
OLD | NEW |