Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(615)

Side by Side Diff: content/public/android/java/src/org/chromium/content/browser/ServiceRegistrar.java

Issue 1486043002: [webnfc] Implement push method for Android nfc mojo service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@step_6_add_mojo_service_CL
Patch Set: Rebased to master. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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;
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;
21 import org.chromium.ui.base.WindowAndroidChangedObserver;
16 22
17 /** 23 /**
18 * Registers mojo services exposed by the browser in the given registry. 24 * Registers mojo services exposed by the browser in the given registry.
19 */ 25 */
20 @JNINamespace("content") 26 @JNINamespace("content")
21 class ServiceRegistrar { 27 class ServiceRegistrar {
22 // BatteryMonitorFactory can't implement ImplementationFactory itself, as we don't depend on 28 // BatteryMonitorFactory can't implement ImplementationFactory itself, as we don't depend on
23 // /content in /device. Hence we use BatteryMonitorImplementationFactory as a wrapper. 29 // /content in /device. Hence we use BatteryMonitorImplementationFactory as a wrapper.
24 private static class BatteryMonitorImplementationFactory 30 private static class BatteryMonitorImplementationFactory
25 implements ImplementationFactory<BatteryMonitor> { 31 implements ImplementationFactory<BatteryMonitor> {
26 private final BatteryMonitorFactory mFactory; 32 private final BatteryMonitorFactory mFactory;
27 33
28 BatteryMonitorImplementationFactory(Context applicationContext) { 34 BatteryMonitorImplementationFactory(Context applicationContext) {
29 mFactory = new BatteryMonitorFactory(applicationContext); 35 mFactory = new BatteryMonitorFactory(applicationContext);
30 } 36 }
31 37
32 @Override 38 @Override
33 public BatteryMonitor createImpl() { 39 public BatteryMonitor createImpl() {
34 return mFactory.createMonitor(); 40 return mFactory.createMonitor();
35 } 41 }
36 } 42 }
37 43
44 private static class NfcImplementationFactory implements ImplementationFacto ry<Nfc> {
45 private final Context mContext;
46 private final WebContents mContents;
47
48 NfcImplementationFactory(Context context, WebContents contents) {
49 mContext = context;
50 mContents = contents;
51 }
52
53 private static class ContextAwareNfcImpl extends NfcImpl implements
54 WindowAndroidChangedObserver {
55 private final ContentViewCore mContextViewCore;
56
57 ContextAwareNfcImpl(Context context, ContentViewCore contextViewCore ) {
58 super(context);
59 mContextViewCore = contextViewCore;
60 if (mContextViewCore != null) {
61 mContextViewCore.addWindowAndroidChangedObserver(this);
62 if (mContextViewCore.getWindowAndroid() != null) {
63 setActivity(mContextViewCore.getWindowAndroid().getActiv ity().get());
64 }
65 }
66 }
67
68 @Override
69 public void close() {
70 super.close();
71 if (mContextViewCore != null) {
72 mContextViewCore.removeWindowAndroidChangedObserver(this);
73 }
74 }
75
76 @Override
77 public void onWindowAndroidChanged(WindowAndroid newWindowAndroid) {
78 Activity activity = null;
79 if (newWindowAndroid != null) {
80 activity = newWindowAndroid.getActivity().get();
81 }
82 setActivity(activity);
83 }
84 }
85
86 @Override
87 public Nfc createImpl() {
88 return new ContextAwareNfcImpl(mContext, ContentViewCore.fromWebCont ents(mContents));
89 }
90 }
91
38 private static class VibrationManagerImplementationFactory 92 private static class VibrationManagerImplementationFactory
39 implements ImplementationFactory<VibrationManager> { 93 implements ImplementationFactory<VibrationManager> {
40 private final Context mApplicationContext; 94 private final Context mApplicationContext;
41 95
42 VibrationManagerImplementationFactory(Context applicationContext) { 96 VibrationManagerImplementationFactory(Context applicationContext) {
43 mApplicationContext = applicationContext; 97 mApplicationContext = applicationContext;
44 } 98 }
45 99
46 @Override 100 @Override
47 public VibrationManager createImpl() { 101 public VibrationManager createImpl() {
48 return new VibrationManagerImpl(mApplicationContext); 102 return new VibrationManagerImpl(mApplicationContext);
49 } 103 }
50 } 104 }
51 105
52 @CalledByNative 106 @CalledByNative
53 static void registerProcessHostServices(ServiceRegistry registry, Context ap plicationContext) { 107 static void registerProcessHostServices(ServiceRegistry registry, Context ap plicationContext) {
54 assert applicationContext != null; 108 assert applicationContext != null;
55 registry.addService(BatteryMonitor.MANAGER, 109 registry.addService(BatteryMonitor.MANAGER,
56 new BatteryMonitorImplementationFactory(applicationContext)); 110 new BatteryMonitorImplementationFactory(applicationContext));
57 } 111 }
58 112
59 @CalledByNative 113 @CalledByNative
60 static void registerFrameHostServices(ServiceRegistry registry, Context appl icationContext) { 114 static void registerFrameHostServices(ServiceRegistry registry,
115 Context applicationContext, WebContents contents) {
61 assert applicationContext != null; 116 assert applicationContext != null;
62 registry.addService(VibrationManager.MANAGER, 117 registry.addService(VibrationManager.MANAGER,
63 new VibrationManagerImplementationFactory(applicationContext)); 118 new VibrationManagerImplementationFactory(applicationContext));
64 // TODO(avayvod): Register the PresentationService implementation here. 119 // TODO(avayvod): Register the PresentationService implementation here.
120 registry.addService(Nfc.MANAGER,
121 new NfcImplementationFactory(applicationContext, contents));
65 } 122 }
66 } 123 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698