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.ServiceRegistry.ImplementationFactory; | 12 import org.chromium.content.browser.ServiceRegistry.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; |
| 18 import org.chromium.mojom.device.Nfc; |
15 import org.chromium.mojom.device.VibrationManager; | 19 import org.chromium.mojom.device.VibrationManager; |
16 | 20 |
17 /** | 21 /** |
18 * Registers mojo services exposed by the browser in the given registry. | 22 * Registers mojo services exposed by the browser in the given registry. |
19 */ | 23 */ |
20 @JNINamespace("content") | 24 @JNINamespace("content") |
21 class ServiceRegistrar { | 25 class ServiceRegistrar { |
22 // BatteryMonitorFactory can't implement ImplementationFactory itself, as we
don't depend on | 26 // BatteryMonitorFactory can't implement ImplementationFactory itself, as we
don't depend on |
23 // /content in /device. Hence we use BatteryMonitorImplementationFactory as
a wrapper. | 27 // /content in /device. Hence we use BatteryMonitorImplementationFactory as
a wrapper. |
24 private static class BatteryMonitorImplementationFactory | 28 private static class BatteryMonitorImplementationFactory |
25 implements ImplementationFactory<BatteryMonitor> { | 29 implements ImplementationFactory<BatteryMonitor> { |
26 private final BatteryMonitorFactory mFactory; | 30 private final BatteryMonitorFactory mFactory; |
27 | 31 |
28 BatteryMonitorImplementationFactory(Context applicationContext) { | 32 BatteryMonitorImplementationFactory(Context applicationContext) { |
29 mFactory = new BatteryMonitorFactory(applicationContext); | 33 mFactory = new BatteryMonitorFactory(applicationContext); |
30 } | 34 } |
31 | 35 |
32 @Override | 36 @Override |
33 public BatteryMonitor createImpl() { | 37 public BatteryMonitor createImpl() { |
34 return mFactory.createMonitor(); | 38 return mFactory.createMonitor(); |
35 } | 39 } |
36 } | 40 } |
37 | 41 |
| 42 private static class NfcImplementationFactory implements ImplementationFacto
ry<Nfc> { |
| 43 private final WebContents mContents; |
| 44 |
| 45 NfcImplementationFactory(WebContents contents) { |
| 46 mContents = contents; |
| 47 } |
| 48 |
| 49 @Override |
| 50 public Nfc createImpl() { |
| 51 Activity activity = null; |
| 52 ContentViewCore viewCore = null; |
| 53 if (mContents != null) { |
| 54 viewCore = ContentViewCore.fromWebContents(mContents); |
| 55 } |
| 56 |
| 57 if (viewCore != null && viewCore.getWindowAndroid() != null) { |
| 58 activity = viewCore.getWindowAndroid().getActivity().get(); |
| 59 } |
| 60 |
| 61 return new NfcImpl(activity); |
| 62 } |
| 63 } |
| 64 |
38 private static class VibrationManagerImplementationFactory | 65 private static class VibrationManagerImplementationFactory |
39 implements ImplementationFactory<VibrationManager> { | 66 implements ImplementationFactory<VibrationManager> { |
40 private final Context mApplicationContext; | 67 private final Context mApplicationContext; |
41 | 68 |
42 VibrationManagerImplementationFactory(Context applicationContext) { | 69 VibrationManagerImplementationFactory(Context applicationContext) { |
43 mApplicationContext = applicationContext; | 70 mApplicationContext = applicationContext; |
44 } | 71 } |
45 | 72 |
46 @Override | 73 @Override |
47 public VibrationManager createImpl() { | 74 public VibrationManager createImpl() { |
48 return new VibrationManagerImpl(mApplicationContext); | 75 return new VibrationManagerImpl(mApplicationContext); |
49 } | 76 } |
50 } | 77 } |
51 | 78 |
52 @CalledByNative | 79 @CalledByNative |
53 static void registerProcessHostServices(ServiceRegistry registry, Context ap
plicationContext) { | 80 static void registerProcessHostServices(ServiceRegistry registry, Context ap
plicationContext) { |
54 assert applicationContext != null; | 81 assert applicationContext != null; |
55 registry.addService(BatteryMonitor.MANAGER, | 82 registry.addService(BatteryMonitor.MANAGER, |
56 new BatteryMonitorImplementationFactory(applicationContext)); | 83 new BatteryMonitorImplementationFactory(applicationContext)); |
57 registry.addService(VibrationManager.MANAGER, | 84 registry.addService(VibrationManager.MANAGER, |
58 new VibrationManagerImplementationFactory(applicationContext)); | 85 new VibrationManagerImplementationFactory(applicationContext)); |
59 } | 86 } |
60 | 87 |
61 @CalledByNative | 88 @CalledByNative |
62 static void registerFrameHostServices(ServiceRegistry registry, Context appl
icationContext) { | 89 static void registerFrameHostServices(ServiceRegistry registry, |
| 90 Context applicationContext, WebContents contents) { |
63 assert applicationContext != null; | 91 assert applicationContext != null; |
64 // TODO(avayvod): Register the PresentationService implementation here. | 92 // TODO(avayvod): Register the PresentationService implementation here. |
| 93 registry.addService(Nfc.MANAGER, new NfcImplementationFactory(contents))
; |
65 } | 94 } |
66 } | 95 } |
OLD | NEW |