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

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: Avoid caching Activity. Created 4 years, 7 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
« no previous file with comments | « content/public/android/BUILD.gn ('k') | device/nfc/android/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
9 import android.support.annotation.Nullable;
8 10
9 import org.chromium.base.annotations.CalledByNative; 11 import org.chromium.base.annotations.CalledByNative;
10 import org.chromium.base.annotations.JNINamespace; 12 import org.chromium.base.annotations.JNINamespace;
11 import org.chromium.content.browser.ServiceRegistry.ImplementationFactory; 13 import org.chromium.content.browser.ServiceRegistry.ImplementationFactory;
14 import org.chromium.content_public.browser.WebContents;
12 import org.chromium.device.battery.BatteryMonitorFactory; 15 import org.chromium.device.battery.BatteryMonitorFactory;
16 import org.chromium.device.nfc.NfcImpl;
13 import org.chromium.device.vibration.VibrationManagerImpl; 17 import org.chromium.device.vibration.VibrationManagerImpl;
14 import org.chromium.mojom.device.BatteryMonitor; 18 import org.chromium.mojom.device.BatteryMonitor;
15 import org.chromium.mojom.device.VibrationManager; 19 import org.chromium.mojom.device.VibrationManager;
20 import org.chromium.mojom.device.nfc.Nfc;
21
22 import java.util.concurrent.Callable;
16 23
17 /** 24 /**
18 * Registers mojo services exposed by the browser in the given registry. 25 * Registers mojo services exposed by the browser in the given registry.
19 */ 26 */
20 @JNINamespace("content") 27 @JNINamespace("content")
21 class ServiceRegistrar { 28 class ServiceRegistrar {
22 // BatteryMonitorFactory can't implement ImplementationFactory itself, as we don't depend on 29 // BatteryMonitorFactory can't implement ImplementationFactory itself, as we don't depend on
23 // /content in /device. Hence we use BatteryMonitorImplementationFactory as a wrapper. 30 // /content in /device. Hence we use BatteryMonitorImplementationFactory as a wrapper.
24 private static class BatteryMonitorImplementationFactory 31 private static class BatteryMonitorImplementationFactory
25 implements ImplementationFactory<BatteryMonitor> { 32 implements ImplementationFactory<BatteryMonitor> {
26 private final BatteryMonitorFactory mFactory; 33 private final BatteryMonitorFactory mFactory;
27 34
28 BatteryMonitorImplementationFactory(Context applicationContext) { 35 BatteryMonitorImplementationFactory(Context applicationContext) {
29 mFactory = new BatteryMonitorFactory(applicationContext); 36 mFactory = new BatteryMonitorFactory(applicationContext);
30 } 37 }
31 38
32 @Override 39 @Override
33 public BatteryMonitor createImpl() { 40 public BatteryMonitor createImpl() {
34 return mFactory.createMonitor(); 41 return mFactory.createMonitor();
35 } 42 }
36 } 43 }
37 44
45 private static class NfcImplementationFactory implements ImplementationFacto ry<Nfc> {
46 private final Context mContext;
47 private final WebContents mContents;
48
49 NfcImplementationFactory(Context context, WebContents contents) {
50 mContext = context;
51 mContents = contents;
52 }
53
54 @Nullable
55 private Activity getActivity() {
56 Activity activity = null;
57 ContentViewCore viewCore = null;
58 if (mContents != null) {
59 viewCore = ContentViewCore.fromWebContents(mContents);
60 }
61
62 if (viewCore != null && viewCore.getWindowAndroid() != null) {
63 activity = viewCore.getWindowAndroid().getActivity().get();
64 }
65
66 return activity;
67 }
68
69 @Override
70 public Nfc createImpl() {
71 return new NfcImpl(mContext, new Callable<Activity>() {
Ted C 2016/05/13 22:04:08 to avoid the awkward layering, maybe we can put th
shalamov 2016/05/17 12:30:32 Done.
72 public Activity call() {
73 return getActivity();
74 }
75 });
76 }
77 }
78
38 private static class VibrationManagerImplementationFactory 79 private static class VibrationManagerImplementationFactory
39 implements ImplementationFactory<VibrationManager> { 80 implements ImplementationFactory<VibrationManager> {
40 private final Context mApplicationContext; 81 private final Context mApplicationContext;
41 82
42 VibrationManagerImplementationFactory(Context applicationContext) { 83 VibrationManagerImplementationFactory(Context applicationContext) {
43 mApplicationContext = applicationContext; 84 mApplicationContext = applicationContext;
44 } 85 }
45 86
46 @Override 87 @Override
47 public VibrationManager createImpl() { 88 public VibrationManager createImpl() {
48 return new VibrationManagerImpl(mApplicationContext); 89 return new VibrationManagerImpl(mApplicationContext);
49 } 90 }
50 } 91 }
51 92
52 @CalledByNative 93 @CalledByNative
53 static void registerProcessHostServices(ServiceRegistry registry, Context ap plicationContext) { 94 static void registerProcessHostServices(ServiceRegistry registry, Context ap plicationContext) {
54 assert applicationContext != null; 95 assert applicationContext != null;
55 registry.addService(BatteryMonitor.MANAGER, 96 registry.addService(BatteryMonitor.MANAGER,
56 new BatteryMonitorImplementationFactory(applicationContext)); 97 new BatteryMonitorImplementationFactory(applicationContext));
57 registry.addService(VibrationManager.MANAGER, 98 registry.addService(VibrationManager.MANAGER,
58 new VibrationManagerImplementationFactory(applicationContext)); 99 new VibrationManagerImplementationFactory(applicationContext));
59 } 100 }
60 101
61 @CalledByNative 102 @CalledByNative
62 static void registerFrameHostServices(ServiceRegistry registry, Context appl icationContext) { 103 static void registerFrameHostServices(ServiceRegistry registry,
104 Context applicationContext, WebContents contents) {
63 assert applicationContext != null; 105 assert applicationContext != null;
64 // TODO(avayvod): Register the PresentationService implementation here. 106 // TODO(avayvod): Register the PresentationService implementation here.
107 registry.addService(Nfc.MANAGER,
108 new NfcImplementationFactory(applicationContext, contents));
65 } 109 }
66 } 110 }
OLDNEW
« no previous file with comments | « content/public/android/BUILD.gn ('k') | device/nfc/android/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698