| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.chrome.browser.preferences; | 5 package org.chromium.chrome.browser.preferences; |
| 6 | 6 |
| 7 import android.Manifest; | 7 import android.Manifest; |
| 8 | 8 |
| 9 import org.chromium.base.Callback; |
| 9 import org.chromium.base.ThreadUtils; | 10 import org.chromium.base.ThreadUtils; |
| 10 import org.chromium.base.VisibleForTesting; | 11 import org.chromium.base.VisibleForTesting; |
| 11 import org.chromium.base.annotations.CalledByNative; | 12 import org.chromium.base.annotations.CalledByNative; |
| 12 import org.chromium.base.annotations.SuppressFBWarnings; | 13 import org.chromium.base.annotations.SuppressFBWarnings; |
| 13 import org.chromium.chrome.browser.AppHooks; | 14 import org.chromium.chrome.browser.AppHooks; |
| 15 import org.chromium.components.location.LocationSettingsDialogContext.LocationSe
ttingsDialogContextEnum; |
| 16 import org.chromium.components.location.LocationSettingsDialogOutcome; |
| 14 import org.chromium.components.location.LocationUtils; | 17 import org.chromium.components.location.LocationUtils; |
| 15 import org.chromium.content.browser.ContentViewCore; | 18 import org.chromium.content.browser.ContentViewCore; |
| 16 import org.chromium.content_public.browser.WebContents; | 19 import org.chromium.content_public.browser.WebContents; |
| 17 import org.chromium.ui.base.WindowAndroid; | 20 import org.chromium.ui.base.WindowAndroid; |
| 18 | 21 |
| 19 /** | 22 /** |
| 20 * Provides methods for querying Chrome's internal location setting and | 23 * Provides methods for querying Chrome's internal location setting and |
| 21 * combining that with the system-wide setting and permissions. | 24 * combining that with the system-wide setting and permissions. |
| 22 * | 25 * |
| 23 * This class should be used only on the UI thread. | 26 * This class should be used only on the UI thread. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 40 public static LocationSettings getInstance() { | 43 public static LocationSettings getInstance() { |
| 41 ThreadUtils.assertOnUiThread(); | 44 ThreadUtils.assertOnUiThread(); |
| 42 if (sInstance == null) { | 45 if (sInstance == null) { |
| 43 sInstance = AppHooks.get().createLocationSettings(); | 46 sInstance = AppHooks.get().createLocationSettings(); |
| 44 } | 47 } |
| 45 return sInstance; | 48 return sInstance; |
| 46 } | 49 } |
| 47 | 50 |
| 48 @CalledByNative | 51 @CalledByNative |
| 49 private static boolean canSitesRequestLocationPermission(WebContents webCont
ents) { | 52 private static boolean canSitesRequestLocationPermission(WebContents webCont
ents) { |
| 50 ContentViewCore cvc = ContentViewCore.fromWebContents(webContents); | 53 WindowAndroid windowAndroid = windowFromWebContents(webContents); |
| 51 if (cvc == null) return false; | |
| 52 WindowAndroid windowAndroid = cvc.getWindowAndroid(); | |
| 53 if (windowAndroid == null) return false; | 54 if (windowAndroid == null) return false; |
| 54 | 55 |
| 55 LocationUtils locationUtils = LocationUtils.getInstance(); | 56 LocationUtils locationUtils = LocationUtils.getInstance(); |
| 56 if (!locationUtils.isSystemLocationSettingEnabled()) return false; | 57 if (!locationUtils.isSystemLocationSettingEnabled()) return false; |
| 57 | 58 |
| 58 return locationUtils.hasAndroidLocationPermission() | 59 return locationUtils.hasAndroidLocationPermission() |
| 59 || windowAndroid.canRequestPermission(Manifest.permission.ACCESS
_FINE_LOCATION); | 60 || windowAndroid.canRequestPermission(Manifest.permission.ACCESS
_FINE_LOCATION); |
| 60 } | 61 } |
| 61 | 62 |
| 63 @CalledByNative |
| 64 private static boolean canPromptToEnableSystemLocationSetting() { |
| 65 return LocationUtils.getInstance().canPromptToEnableSystemLocationSettin
g(); |
| 66 } |
| 67 |
| 68 @CalledByNative |
| 69 private static void promptToEnableSystemLocationSetting( |
| 70 @LocationSettingsDialogContextEnum int promptContext, WebContents we
bContents, |
| 71 final long nativeCallback) { |
| 72 WindowAndroid window = windowFromWebContents(webContents); |
| 73 if (window == null) { |
| 74 nativeOnLocationSettingsDialogOutcome( |
| 75 nativeCallback, LocationSettingsDialogOutcome.NO_PROMPT); |
| 76 return; |
| 77 } |
| 78 LocationUtils.getInstance().promptToEnableSystemLocationSetting( |
| 79 promptContext, window, new Callback<Integer>() { |
| 80 @Override |
| 81 public void onResult(Integer result) { |
| 82 nativeOnLocationSettingsDialogOutcome(nativeCallback, re
sult); |
| 83 } |
| 84 }); |
| 85 } |
| 86 |
| 62 /** | 87 /** |
| 63 * Returns true if location is enabled system-wide and the Chrome location s
etting is enabled. | 88 * Returns true if location is enabled system-wide and the Chrome location s
etting is enabled. |
| 64 */ | 89 */ |
| 65 public boolean areAllLocationSettingsEnabled() { | 90 public boolean areAllLocationSettingsEnabled() { |
| 66 return isChromeLocationSettingEnabled() | 91 return isChromeLocationSettingEnabled() |
| 67 && LocationUtils.getInstance().isSystemLocationSettingEnabled(); | 92 && LocationUtils.getInstance().isSystemLocationSettingEnabled(); |
| 68 } | 93 } |
| 69 | 94 |
| 70 /** | 95 /** |
| 71 * Returns whether Chrome's user-configurable location setting is enabled. | 96 * Returns whether Chrome's user-configurable location setting is enabled. |
| 72 */ | 97 */ |
| 73 public boolean isChromeLocationSettingEnabled() { | 98 public boolean isChromeLocationSettingEnabled() { |
| 74 return PrefServiceBridge.getInstance().isAllowLocationEnabled(); | 99 return PrefServiceBridge.getInstance().isAllowLocationEnabled(); |
| 75 } | 100 } |
| 76 | 101 |
| 77 @VisibleForTesting | 102 @VisibleForTesting |
| 78 public static void setInstanceForTesting(LocationSettings instance) { | 103 public static void setInstanceForTesting(LocationSettings instance) { |
| 79 sInstance = instance; | 104 sInstance = instance; |
| 80 } | 105 } |
| 106 |
| 107 private static WindowAndroid windowFromWebContents(WebContents webContents)
{ |
| 108 ContentViewCore contentViewCore = ContentViewCore.fromWebContents(webCon
tents); |
| 109 if (contentViewCore == null) return null; |
| 110 return contentViewCore.getWindowAndroid(); |
| 111 } |
| 112 |
| 113 private static native void nativeOnLocationSettingsDialogOutcome(long callba
ck, int result); |
| 81 } | 114 } |
| OLD | NEW |