donut snapshot
This commit is contained in:
240
core/java/android/webkit/GearsPermissionsManager.java
Normal file
240
core/java/android/webkit/GearsPermissionsManager.java
Normal file
@@ -0,0 +1,240 @@
|
||||
/*
|
||||
* Copyright (C) 2009 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.webkit;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.Editor;
|
||||
import android.database.ContentObserver;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
import android.database.sqlite.SQLiteStatement;
|
||||
import android.os.Handler;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* Donut-specific hack to keep Gears permissions in sync with the
|
||||
* system location setting.
|
||||
*/
|
||||
class GearsPermissionsManager {
|
||||
// The application context.
|
||||
Context mContext;
|
||||
// The path to gears.so.
|
||||
private String mGearsPath;
|
||||
|
||||
// The Gears permissions database directory.
|
||||
private final static String GEARS_DATABASE_DIR = "gears";
|
||||
// The Gears permissions database file name.
|
||||
private final static String GEARS_DATABASE_FILE = "permissions.db";
|
||||
// The Gears location permissions table.
|
||||
private final static String GEARS_LOCATION_ACCESS_TABLE_NAME =
|
||||
"LocationAccess";
|
||||
// The Gears storage access permissions table.
|
||||
private final static String GEARS_STORAGE_ACCESS_TABLE_NAME = "Access";
|
||||
// The Gears permissions db schema version table.
|
||||
private final static String GEARS_SCHEMA_VERSION_TABLE_NAME =
|
||||
"VersionInfo";
|
||||
// The Gears permission value that denotes "allow access to location".
|
||||
private static final int GEARS_ALLOW_LOCATION_ACCESS = 1;
|
||||
// The shared pref name.
|
||||
private static final String LAST_KNOWN_LOCATION_SETTING =
|
||||
"lastKnownLocationSystemSetting";
|
||||
// The Browser package name.
|
||||
private static final String BROWSER_PACKAGE_NAME = "com.android.browser";
|
||||
// The Secure Settings observer that will be notified when the system
|
||||
// location setting changes.
|
||||
private SecureSettingsObserver mSettingsObserver;
|
||||
// The Google URLs whitelisted for Gears location access.
|
||||
private static HashSet<String> sGearsWhiteList;
|
||||
|
||||
static {
|
||||
sGearsWhiteList = new HashSet<String>();
|
||||
// NOTE: DO NOT ADD A "/" AT THE END!
|
||||
sGearsWhiteList.add("http://www.google.com");
|
||||
sGearsWhiteList.add("http://www.google.co.uk");
|
||||
}
|
||||
|
||||
private static final String LOGTAG = "webcore";
|
||||
static final boolean DEBUG = false;
|
||||
static final boolean LOGV_ENABLED = DEBUG;
|
||||
|
||||
GearsPermissionsManager(Context context, String gearsPath) {
|
||||
mContext = context;
|
||||
mGearsPath = gearsPath;
|
||||
}
|
||||
|
||||
public void doCheckAndStartObserver() {
|
||||
// Are we running in the browser?
|
||||
if (!BROWSER_PACKAGE_NAME.equals(mContext.getPackageName())) {
|
||||
return;
|
||||
}
|
||||
// Do the check.
|
||||
checkGearsPermissions();
|
||||
// Install the observer.
|
||||
mSettingsObserver = new SecureSettingsObserver();
|
||||
mSettingsObserver.observe();
|
||||
}
|
||||
|
||||
private void checkGearsPermissions() {
|
||||
// Get the current system settings.
|
||||
int setting = Settings.Secure.getInt(mContext.getContentResolver(),
|
||||
Settings.Secure.USE_LOCATION_FOR_SERVICES, -1);
|
||||
// Check if we need to set the Gears permissions.
|
||||
if (setting != -1 && locationSystemSettingChanged(setting)) {
|
||||
setGearsPermissionForGoogleDomains(setting);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean locationSystemSettingChanged(int newSetting) {
|
||||
SharedPreferences prefs =
|
||||
PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||
int oldSetting = 0;
|
||||
oldSetting = prefs.getInt(LAST_KNOWN_LOCATION_SETTING, oldSetting);
|
||||
if (oldSetting == newSetting) {
|
||||
return false;
|
||||
}
|
||||
Editor ed = prefs.edit();
|
||||
ed.putInt(LAST_KNOWN_LOCATION_SETTING, newSetting);
|
||||
ed.commit();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void setGearsPermissionForGoogleDomains(int systemPermission) {
|
||||
// Transform the system permission into a boolean flag. When this
|
||||
// flag is true, it means the origins in gGearsWhiteList are added
|
||||
// to the Gears location permission table with permission 1 (allowed).
|
||||
// When the flag is false, the origins in gGearsWhiteList are removed
|
||||
// from the Gears location permission table. Next time the user
|
||||
// navigates to one of these origins, she will see the normal Gears
|
||||
// permission prompt.
|
||||
boolean addToGearsLocationTable = (systemPermission == 1 ? true : false);
|
||||
// Build the path to the Gears library.
|
||||
|
||||
File file = new File(mGearsPath).getParentFile();
|
||||
if (file == null) {
|
||||
return;
|
||||
}
|
||||
// Build the Gears database file name.
|
||||
file = new File(file.getAbsolutePath() + File.separator
|
||||
+ GEARS_DATABASE_DIR + File.separator + GEARS_DATABASE_FILE);
|
||||
// Remember whether or not we need to create the LocationAccess table.
|
||||
boolean needToCreateTables = !file.exists();
|
||||
// If the database file does not yet exist and the system location
|
||||
// setting says that the Gears origins need to be removed from the
|
||||
// location permission table, it means that we don't actually need
|
||||
// to do anything at all.
|
||||
if (needToCreateTables && !addToGearsLocationTable) {
|
||||
return;
|
||||
}
|
||||
// Try opening the Gears database.
|
||||
SQLiteDatabase permissions;
|
||||
try {
|
||||
permissions = SQLiteDatabase.openOrCreateDatabase(file, null);
|
||||
} catch (SQLiteException e) {
|
||||
if (LOGV_ENABLED) {
|
||||
Log.v(LOGTAG, "Could not open Gears permission DB: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
// Just bail out.
|
||||
return;
|
||||
}
|
||||
// We now have a database open. Begin a transaction.
|
||||
permissions.beginTransaction();
|
||||
try {
|
||||
if (needToCreateTables) {
|
||||
// Create the tables. Note that this creates the
|
||||
// Gears tables for the permissions DB schema version 2.
|
||||
// The Gears schema upgrade process will take care of the rest.
|
||||
// First, the storage access table.
|
||||
SQLiteStatement statement = permissions.compileStatement(
|
||||
"CREATE TABLE IF NOT EXISTS "
|
||||
+ GEARS_STORAGE_ACCESS_TABLE_NAME
|
||||
+ " (Name TEXT UNIQUE, Value)");
|
||||
statement.execute();
|
||||
// Next the location access table.
|
||||
statement = permissions.compileStatement(
|
||||
"CREATE TABLE IF NOT EXISTS "
|
||||
+ GEARS_LOCATION_ACCESS_TABLE_NAME
|
||||
+ " (Name TEXT UNIQUE, Value)");
|
||||
statement.execute();
|
||||
// Finally, the schema version table.
|
||||
statement = permissions.compileStatement(
|
||||
"CREATE TABLE IF NOT EXISTS "
|
||||
+ GEARS_SCHEMA_VERSION_TABLE_NAME
|
||||
+ " (Name TEXT UNIQUE, Value)");
|
||||
statement.execute();
|
||||
// Set the schema version to 2.
|
||||
ContentValues schema = new ContentValues();
|
||||
schema.put("Name", "Version");
|
||||
schema.put("Value", 2);
|
||||
permissions.insert(GEARS_SCHEMA_VERSION_TABLE_NAME, null,
|
||||
schema);
|
||||
}
|
||||
|
||||
if (addToGearsLocationTable) {
|
||||
ContentValues permissionValues = new ContentValues();
|
||||
|
||||
for (String url : sGearsWhiteList) {
|
||||
permissionValues.put("Name", url);
|
||||
permissionValues.put("Value", GEARS_ALLOW_LOCATION_ACCESS);
|
||||
permissions.replace(GEARS_LOCATION_ACCESS_TABLE_NAME, null,
|
||||
permissionValues);
|
||||
permissionValues.clear();
|
||||
}
|
||||
} else {
|
||||
for (String url : sGearsWhiteList) {
|
||||
permissions.delete(GEARS_LOCATION_ACCESS_TABLE_NAME, "Name=?",
|
||||
new String[] { url });
|
||||
}
|
||||
}
|
||||
// Commit the transaction.
|
||||
permissions.setTransactionSuccessful();
|
||||
} catch (SQLiteException e) {
|
||||
if (LOGV_ENABLED) {
|
||||
Log.v(LOGTAG, "Could not set the Gears permissions: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
} finally {
|
||||
permissions.endTransaction();
|
||||
permissions.close();
|
||||
}
|
||||
}
|
||||
|
||||
class SecureSettingsObserver extends ContentObserver {
|
||||
SecureSettingsObserver() {
|
||||
super(new Handler());
|
||||
}
|
||||
|
||||
void observe() {
|
||||
ContentResolver resolver = mContext.getContentResolver();
|
||||
resolver.registerContentObserver(Settings.Secure.getUriFor(
|
||||
Settings.Secure.USE_LOCATION_FOR_SERVICES), false, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange) {
|
||||
checkGearsPermissions();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@ import com.android.internal.R;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -72,7 +73,12 @@ class LoadListener extends Handler implements EventHandler {
|
||||
private static final int HTTP_NOT_FOUND = 404;
|
||||
private static final int HTTP_PROXY_AUTH = 407;
|
||||
|
||||
private static final String CERT_MIMETYPE = "application/x-x509-ca-cert";
|
||||
private static HashSet<String> sCertificateMimeTypeMap;
|
||||
static {
|
||||
sCertificateMimeTypeMap = new HashSet<String>();
|
||||
sCertificateMimeTypeMap.add("application/x-x509-ca-cert");
|
||||
sCertificateMimeTypeMap.add("application/x-pkcs12");
|
||||
}
|
||||
|
||||
private static int sNativeLoaderCount;
|
||||
|
||||
@@ -318,7 +324,17 @@ class LoadListener extends Handler implements EventHandler {
|
||||
if (mMimeType.equalsIgnoreCase("text/plain") ||
|
||||
mMimeType.equalsIgnoreCase("application/octet-stream")) {
|
||||
|
||||
String newMimeType = guessMimeTypeFromExtension();
|
||||
// for attachment, use the filename in the Content-Disposition
|
||||
// to guess the mimetype
|
||||
String contentDisposition = headers.getContentDisposition();
|
||||
String url = null;
|
||||
if (contentDisposition != null) {
|
||||
url = URLUtil.parseContentDisposition(contentDisposition);
|
||||
}
|
||||
if (url == null) {
|
||||
url = mUrl;
|
||||
}
|
||||
String newMimeType = guessMimeTypeFromExtension(url);
|
||||
if (newMimeType != null) {
|
||||
mMimeType = newMimeType;
|
||||
}
|
||||
@@ -936,7 +952,7 @@ class LoadListener extends Handler implements EventHandler {
|
||||
|
||||
// This commits the headers without checking the response status code.
|
||||
private void commitHeaders() {
|
||||
if (mIsMainPageLoader && CERT_MIMETYPE.equals(mMimeType)) {
|
||||
if (mIsMainPageLoader && sCertificateMimeTypeMap.contains(mMimeType)) {
|
||||
// In the case of downloading certificate, we will save it to the
|
||||
// Keystore in commitLoad. Do not call webcore.
|
||||
return;
|
||||
@@ -982,7 +998,7 @@ class LoadListener extends Handler implements EventHandler {
|
||||
private void commitLoad() {
|
||||
if (mCancelled) return;
|
||||
|
||||
if (mIsMainPageLoader && CERT_MIMETYPE.equals(mMimeType)) {
|
||||
if (mIsMainPageLoader && sCertificateMimeTypeMap.contains(mMimeType)) {
|
||||
// In the case of downloading certificate, we will save it to the
|
||||
// Keystore and stop the current loading so that it will not
|
||||
// generate a new history page
|
||||
@@ -1409,7 +1425,7 @@ class LoadListener extends Handler implements EventHandler {
|
||||
// of frames. If no content-type was specified, it is fine to
|
||||
// default to text/html.
|
||||
mMimeType = "text/html";
|
||||
String newMimeType = guessMimeTypeFromExtension();
|
||||
String newMimeType = guessMimeTypeFromExtension(mUrl);
|
||||
if (newMimeType != null) {
|
||||
mMimeType = newMimeType;
|
||||
}
|
||||
@@ -1419,15 +1435,15 @@ class LoadListener extends Handler implements EventHandler {
|
||||
/**
|
||||
* guess MIME type based on the file extension.
|
||||
*/
|
||||
private String guessMimeTypeFromExtension() {
|
||||
private String guessMimeTypeFromExtension(String url) {
|
||||
// PENDING: need to normalize url
|
||||
if (WebView.LOGV_ENABLED) {
|
||||
Log.v(LOGTAG, "guessMimeTypeFromExtension: mURL = " + mUrl);
|
||||
Log.v(LOGTAG, "guessMimeTypeFromExtension: url = " + url);
|
||||
}
|
||||
|
||||
String mimeType =
|
||||
MimeTypeMap.getSingleton().getMimeTypeFromExtension(
|
||||
MimeTypeMap.getFileExtensionFromUrl(mUrl));
|
||||
MimeTypeMap.getFileExtensionFromUrl(url));
|
||||
|
||||
if (mimeType != null) {
|
||||
// XXX: Until the servers send us either correct xhtml or
|
||||
|
||||
@@ -335,6 +335,7 @@ public /* package */ class MimeTypeMap {
|
||||
sMimeTypeMap.loadEntry("application/x-object", "o", false);
|
||||
sMimeTypeMap.loadEntry("application/x-oz-application", "oza",
|
||||
false);
|
||||
sMimeTypeMap.loadEntry("application/x-pkcs12", "p12", false);
|
||||
sMimeTypeMap.loadEntry("application/x-pkcs7-certreqresp", "p7r",
|
||||
false);
|
||||
sMimeTypeMap.loadEntry("application/x-pkcs7-crl", "crl", false);
|
||||
|
||||
@@ -348,7 +348,7 @@ public final class URLUtil {
|
||||
* This header provides a filename for content that is going to be
|
||||
* downloaded to the file system. We only support the attachment type.
|
||||
*/
|
||||
private static String parseContentDisposition(String contentDisposition) {
|
||||
static String parseContentDisposition(String contentDisposition) {
|
||||
try {
|
||||
Matcher m = CONTENT_DISPOSITION_PATTERN.matcher(contentDisposition);
|
||||
if (m.find()) {
|
||||
|
||||
@@ -17,13 +17,13 @@
|
||||
package android.webkit;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.provider.Checkin;
|
||||
|
||||
import java.lang.SecurityException;
|
||||
import android.content.pm.PackageManager;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@@ -176,6 +176,9 @@ public class WebSettings {
|
||||
private boolean mBuiltInZoomControls = false;
|
||||
private boolean mAllowFileAccess = true;
|
||||
|
||||
// The Gears permissions manager. Only in Donut.
|
||||
static GearsPermissionsManager sGearsPermissionsManager;
|
||||
|
||||
// Class to handle messages before WebCore is ready.
|
||||
private class EventHandler {
|
||||
// Message id for syncing
|
||||
@@ -1148,6 +1151,7 @@ public class WebSettings {
|
||||
if (WebView.DEBUG) {
|
||||
junit.framework.Assert.assertTrue(frame.mNativeFrame != 0);
|
||||
}
|
||||
checkGearsPermissions();
|
||||
nativeSync(frame.mNativeFrame);
|
||||
mSyncPending = false;
|
||||
mEventHandler.createHandler();
|
||||
@@ -1163,6 +1167,23 @@ public class WebSettings {
|
||||
return size;
|
||||
}
|
||||
|
||||
private void checkGearsPermissions() {
|
||||
// Did we already check the permissions at startup?
|
||||
if (sGearsPermissionsManager != null) {
|
||||
return;
|
||||
}
|
||||
// Is the pluginsPath sane?
|
||||
String pluginsPath = getPluginsPath();
|
||||
if (pluginsPath == null || pluginsPath.length() == 0) {
|
||||
// We don't yet have a meaningful plugin path, so
|
||||
// we can't do anything about the Gears permissions.
|
||||
return;
|
||||
}
|
||||
sGearsPermissionsManager =
|
||||
new GearsPermissionsManager(mContext, pluginsPath);
|
||||
sGearsPermissionsManager.doCheckAndStartObserver();
|
||||
}
|
||||
|
||||
/* Post a SYNC message to handle syncing the native settings. */
|
||||
private synchronized void postSync() {
|
||||
// Only post if a sync is not pending
|
||||
|
||||
Reference in New Issue
Block a user