Bjorn Bringert | a761181 | 2010-03-24 11:12:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.SharedPreferences; |
| 21 | import android.database.ContentObserver; |
| 22 | import android.net.Uri; |
| 23 | import android.os.AsyncTask; |
| 24 | import android.os.Handler; |
| 25 | import android.preference.PreferenceManager; |
| 26 | import android.provider.Settings; |
| 27 | import android.text.TextUtils; |
| 28 | import android.webkit.GeolocationPermissions; |
| 29 | import android.webkit.ValueCallback; |
| 30 | |
| 31 | import java.util.HashSet; |
| 32 | import java.util.Set; |
| 33 | |
| 34 | /** |
| 35 | * Manages the interaction between the secure system setting for default geolocation |
| 36 | * permissions and the browser. |
| 37 | */ |
| 38 | class SystemAllowGeolocationOrigins { |
| 39 | |
| 40 | // Preference key for the value of the system setting last read by the browser |
| 41 | private final static String LAST_READ_ALLOW_GEOLOCATION_ORIGINS = |
| 42 | "last_read_allow_geolocation_origins"; |
| 43 | |
| 44 | // The application context |
| 45 | private final Context mContext; |
| 46 | |
| 47 | // The observer used to listen to the system setting. |
| 48 | private final SettingObserver mSettingObserver; |
| 49 | |
| 50 | public SystemAllowGeolocationOrigins(Context context) { |
| 51 | mContext = context; |
| 52 | mSettingObserver = new SettingObserver(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Checks whether the setting has changed and installs an observer to listen for |
| 57 | * future changes. Must be called on the application main thread. |
| 58 | */ |
| 59 | public void start() { |
| 60 | // Register to receive notifications when the system settings change. |
| 61 | Uri uri = Settings.Secure.getUriFor(Settings.Secure.ALLOWED_GEOLOCATION_ORIGINS); |
| 62 | mContext.getContentResolver().registerContentObserver(uri, false, mSettingObserver); |
| 63 | |
| 64 | // Read and apply the setting if needed. |
| 65 | maybeApplySettingAsync(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Stops the manager. |
| 70 | */ |
| 71 | public void stop() { |
| 72 | mContext.getContentResolver().unregisterContentObserver(mSettingObserver); |
| 73 | } |
| 74 | |
| 75 | void maybeApplySettingAsync() { |
| 76 | new AsyncTask<Void,Void,Void>() { |
| 77 | @Override |
| 78 | protected Void doInBackground(Void... params) { |
| 79 | maybeApplySetting(); |
| 80 | return null; |
| 81 | } |
| 82 | }.execute(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Checks to see if the system setting has changed and if so, |
| 87 | * updates the Geolocation permissions accordingly. |
| 88 | */ |
| 89 | private void maybeApplySetting() { |
| 90 | // Get the new value |
| 91 | String newSetting = getSystemSetting(); |
| 92 | |
| 93 | // Get the last read value |
| 94 | SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext); |
| 95 | String lastReadSetting = |
| 96 | preferences.getString(LAST_READ_ALLOW_GEOLOCATION_ORIGINS, ""); |
| 97 | |
| 98 | // If the new value is the same as the last one we read, we're done. |
| 99 | if (TextUtils.equals(lastReadSetting, newSetting)) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | // Save the new value as the last read value |
| 104 | preferences.edit() |
| 105 | .putString(LAST_READ_ALLOW_GEOLOCATION_ORIGINS, newSetting) |
Brad Fitzpatrick | 1a6e0e8 | 2010-09-01 16:29:03 -0700 | [diff] [blame^] | 106 | .apply(); |
Bjorn Bringert | a761181 | 2010-03-24 11:12:02 +0000 | [diff] [blame] | 107 | |
| 108 | Set<String> oldOrigins = parseAllowGeolocationOrigins(lastReadSetting); |
| 109 | Set<String> newOrigins = parseAllowGeolocationOrigins(newSetting); |
| 110 | Set<String> addedOrigins = setMinus(newOrigins, oldOrigins); |
| 111 | Set<String> removedOrigins = setMinus(oldOrigins, newOrigins); |
| 112 | |
| 113 | // Remove the origins in the last read value |
| 114 | removeOrigins(removedOrigins); |
| 115 | |
| 116 | // Add the origins in the new value |
| 117 | addOrigins(addedOrigins); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Parses the value of the default geolocation permissions setting. |
| 122 | * |
| 123 | * @param setting A space-separated list of origins. |
| 124 | * @return A mutable set of origins. |
| 125 | */ |
| 126 | private static HashSet<String> parseAllowGeolocationOrigins(String setting) { |
| 127 | HashSet<String> origins = new HashSet<String>(); |
| 128 | if (!TextUtils.isEmpty(setting)) { |
| 129 | for (String origin : setting.split("\\s+")) { |
| 130 | if (!TextUtils.isEmpty(origin)) { |
| 131 | origins.add(origin); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | return origins; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Gets the difference between two sets. Does not modify any of the arguments. |
| 140 | * |
| 141 | * @return A set containing all elements in {@code x} that are not in {@code y}. |
| 142 | */ |
| 143 | private <A> Set<A> setMinus(Set<A> x, Set<A> y) { |
| 144 | HashSet<A> z = new HashSet<A>(x.size()); |
| 145 | for (A a : x) { |
| 146 | if (!y.contains(a)) { |
| 147 | z.add(a); |
| 148 | } |
| 149 | } |
| 150 | return z; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Gets the current system setting for default allowed geolocation origins. |
| 155 | * |
| 156 | * @return The default allowed origins. Returns {@code ""} if not set. |
| 157 | */ |
| 158 | private String getSystemSetting() { |
| 159 | String value = Settings.Secure.getString(mContext.getContentResolver(), |
| 160 | Settings.Secure.ALLOWED_GEOLOCATION_ORIGINS); |
| 161 | return value == null ? "" : value; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Adds geolocation permissions for the given origins. |
| 166 | */ |
| 167 | private void addOrigins(Set<String> origins) { |
| 168 | for (String origin : origins) { |
| 169 | GeolocationPermissions.getInstance().allow(origin); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Removes geolocation permissions for the given origins, if they are allowed. |
| 175 | * If they are denied or not set, nothing is done. |
| 176 | */ |
| 177 | private void removeOrigins(Set<String> origins) { |
| 178 | for (final String origin : origins) { |
| 179 | GeolocationPermissions.getInstance().getAllowed(origin, new ValueCallback<Boolean>() { |
| 180 | public void onReceiveValue(Boolean value) { |
| 181 | if (value != null && value.booleanValue()) { |
| 182 | GeolocationPermissions.getInstance().clear(origin); |
| 183 | } |
| 184 | } |
| 185 | }); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Listens for changes to the system setting. |
| 191 | */ |
| 192 | private class SettingObserver extends ContentObserver { |
| 193 | |
| 194 | SettingObserver() { |
| 195 | super(new Handler()); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void onChange(boolean selfChange) { |
| 200 | maybeApplySettingAsync(); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | } |