OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.content.browser; |
| 6 |
| 7 import android.app.Activity; |
| 8 import android.content.Context; |
| 9 |
| 10 import org.chromium.base.annotations.CalledByNative; |
| 11 import org.chromium.base.annotations.JNINamespace; |
| 12 import org.chromium.content.browser.InterfaceRegistry.ImplementationFactory; |
| 13 import org.chromium.content_public.browser.WebContents; |
| 14 import org.chromium.content.browser.installedapp.InstalledAppProviderImpl; |
| 15 import org.chromium.device.battery.BatteryMonitorFactory; |
| 16 import org.chromium.device.nfc.NfcImpl; |
| 17 import org.chromium.device.vibration.VibrationManagerImpl; |
| 18 import org.chromium.mojom.content.InstalledAppProvider; |
| 19 import org.chromium.mojom.device.BatteryMonitor; |
| 20 import org.chromium.mojom.device.VibrationManager; |
| 21 import org.chromium.mojom.device.nfc.Nfc; |
| 22 import org.chromium.ui.base.WindowAndroid; |
| 23 |
| 24 /** |
| 25 * Registers interfaces exposed by the browser in the given registry. |
| 26 */ |
| 27 @JNINamespace("content") |
| 28 class InterfaceRegistrar { |
| 29 // BatteryMonitorFactory can't implement ImplementationFactory itself, as we
don't depend on |
| 30 // /content in /device. Hence we use BatteryMonitorImplementationFactory as
a wrapper. |
| 31 private static class BatteryMonitorImplementationFactory |
| 32 implements ImplementationFactory<BatteryMonitor> { |
| 33 private final BatteryMonitorFactory mFactory; |
| 34 |
| 35 BatteryMonitorImplementationFactory(Context applicationContext) { |
| 36 mFactory = new BatteryMonitorFactory(applicationContext); |
| 37 } |
| 38 |
| 39 @Override |
| 40 public BatteryMonitor createImpl() { |
| 41 return mFactory.createMonitor(); |
| 42 } |
| 43 } |
| 44 |
| 45 private static class VibrationManagerImplementationFactory |
| 46 implements ImplementationFactory<VibrationManager> { |
| 47 private final Context mApplicationContext; |
| 48 |
| 49 VibrationManagerImplementationFactory(Context applicationContext) { |
| 50 mApplicationContext = applicationContext; |
| 51 } |
| 52 |
| 53 @Override |
| 54 public VibrationManager createImpl() { |
| 55 return new VibrationManagerImpl(mApplicationContext); |
| 56 } |
| 57 } |
| 58 |
| 59 private static class NfcImplementationFactory implements ImplementationFacto
ry<Nfc> { |
| 60 private final Context mContext; |
| 61 private final WebContents mContents; |
| 62 |
| 63 NfcImplementationFactory(Context context, WebContents contents) { |
| 64 mContext = context; |
| 65 mContents = contents; |
| 66 } |
| 67 |
| 68 private static class ContextAwareNfcImpl extends NfcImpl implements |
| 69 WindowAndroidChangedObserver { |
| 70 private final ContentViewCore mContextViewCore; |
| 71 |
| 72 ContextAwareNfcImpl(Context context, ContentViewCore contextViewCore
) { |
| 73 super(context); |
| 74 mContextViewCore = contextViewCore; |
| 75 if (mContextViewCore != null) { |
| 76 mContextViewCore.addWindowAndroidChangedObserver(this); |
| 77 if (mContextViewCore.getWindowAndroid() != null) { |
| 78 setActivity(mContextViewCore.getWindowAndroid().getActiv
ity().get()); |
| 79 } |
| 80 } |
| 81 } |
| 82 |
| 83 @Override |
| 84 public void close() { |
| 85 super.close(); |
| 86 if (mContextViewCore != null) { |
| 87 mContextViewCore.removeWindowAndroidChangedObserver(this); |
| 88 } |
| 89 } |
| 90 |
| 91 @Override |
| 92 public void onWindowAndroidChanged(WindowAndroid newWindowAndroid) { |
| 93 Activity activity = null; |
| 94 if (newWindowAndroid != null) { |
| 95 activity = newWindowAndroid.getActivity().get(); |
| 96 } |
| 97 setActivity(activity); |
| 98 } |
| 99 } |
| 100 |
| 101 @Override |
| 102 public Nfc createImpl() { |
| 103 return new ContextAwareNfcImpl(mContext, ContentViewCore.fromWebCont
ents(mContents)); |
| 104 } |
| 105 } |
| 106 |
| 107 private static class InstalledAppProviderFactory |
| 108 implements ImplementationFactory<InstalledAppProvider> { |
| 109 private final Context mApplicationContext; |
| 110 |
| 111 InstalledAppProviderFactory(Context applicationContext) { |
| 112 mApplicationContext = applicationContext; |
| 113 } |
| 114 |
| 115 @Override |
| 116 public InstalledAppProvider createImpl() { |
| 117 return new InstalledAppProviderImpl(mApplicationContext); |
| 118 } |
| 119 } |
| 120 |
| 121 @CalledByNative |
| 122 static void exposeInterfacesToRenderer(InterfaceRegistry registry, Context a
pplicationContext) { |
| 123 assert applicationContext != null; |
| 124 registry.addInterface(BatteryMonitor.MANAGER, |
| 125 new BatteryMonitorImplementationFactory(applicationContext)); |
| 126 registry.addService( |
| 127 InstalledAppProvider.MANAGER, new InstalledAppProviderFactory(ap
plicationContext)); |
| 128 } |
| 129 |
| 130 @CalledByNative |
| 131 static void exposeInterfacesToFrame(InterfaceRegistry registry, |
| 132 Context applicationContext, WebContents contents) { |
| 133 assert applicationContext != null; |
| 134 registry.addInterface(VibrationManager.MANAGER, |
| 135 new VibrationManagerImplementationFactory(applicationContext)); |
| 136 registry.addInterface(Nfc.MANAGER, |
| 137 new NfcImplementationFactory(applicationContext, contents)); |
| 138 // TODO(avayvod): Register the PresentationService implementation here. |
| 139 } |
| 140 } |
OLD | NEW |