| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.android_webview; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.content.pm.PackageManager; |
| 9 import android.os.Process; |
| 10 import android.webkit.WebSettings; |
| 11 |
| 12 import org.chromium.base.Log; |
| 13 import org.chromium.base.annotations.JNINamespace; |
| 14 |
| 15 /** |
| 16 * Stores Android WebView Service Worker specific settings. |
| 17 * |
| 18 * Methods in this class can be called from any thread, including threads create
d by |
| 19 * the client of WebView. |
| 20 */ |
| 21 @JNINamespace("android_webview") |
| 22 public class AwServiceWorkerSettings { |
| 23 private static final String LOGTAG = AwServiceWorkerSettings.class.getSimple
Name(); |
| 24 private static final boolean TRACE = false; |
| 25 |
| 26 private int mCacheMode = WebSettings.LOAD_DEFAULT; |
| 27 private boolean mAllowContentUrlAccess = true; |
| 28 private boolean mAllowFileUrlAccess = true; |
| 29 private boolean mBlockNetworkLoads; // Default depends on permission of the
embedding APK |
| 30 private boolean mAcceptThirdPartyCookies = false; |
| 31 |
| 32 // Lock to protect all settings. |
| 33 private final Object mAwServiceWorkerSettingsLock = new Object(); |
| 34 |
| 35 // Computed on construction. |
| 36 private final boolean mHasInternetPermission; |
| 37 |
| 38 public AwServiceWorkerSettings(Context context) { |
| 39 boolean hasInternetPermission = context.checkPermission( |
| 40 android.Manifest.permission.INTERNET, |
| 41 Process.myPid(), |
| 42 Process.myUid()) == PackageManager.PERMISSION_GRANTED; |
| 43 synchronized (mAwServiceWorkerSettingsLock) { |
| 44 mHasInternetPermission = hasInternetPermission; |
| 45 mBlockNetworkLoads = !hasInternetPermission; |
| 46 } |
| 47 } |
| 48 |
| 49 /** |
| 50 * See {@link android.webkit.ServiceWorkerWebSettings#setCacheMode}. |
| 51 */ |
| 52 public void setCacheMode(int mode) { |
| 53 if (TRACE) Log.d(LOGTAG, "setCacheMode=" + mode); |
| 54 synchronized (mAwServiceWorkerSettingsLock) { |
| 55 if (mCacheMode != mode) { |
| 56 mCacheMode = mode; |
| 57 } |
| 58 } |
| 59 } |
| 60 |
| 61 /** |
| 62 * See {@link android.webkit.ServiceWorkerWebSettings#getCacheMode}. |
| 63 */ |
| 64 public int getCacheMode() { |
| 65 synchronized (mAwServiceWorkerSettingsLock) { |
| 66 return mCacheMode; |
| 67 } |
| 68 } |
| 69 |
| 70 /** |
| 71 * See {@link android.webkit.ServiceWorkerWebSettings#setAllowContentAccess}
. |
| 72 */ |
| 73 public void setAllowContentAccess(boolean allow) { |
| 74 if (TRACE) Log.d(LOGTAG, "setAllowContentAccess=" + allow); |
| 75 synchronized (mAwServiceWorkerSettingsLock) { |
| 76 if (mAllowContentUrlAccess != allow) { |
| 77 mAllowContentUrlAccess = allow; |
| 78 } |
| 79 } |
| 80 } |
| 81 |
| 82 /** |
| 83 * See {@link android.webkit.ServiceWorkerWebSettings#getAllowContentAccess}
. |
| 84 */ |
| 85 public boolean getAllowContentAccess() { |
| 86 synchronized (mAwServiceWorkerSettingsLock) { |
| 87 return mAllowContentUrlAccess; |
| 88 } |
| 89 } |
| 90 |
| 91 /** |
| 92 * See {@link android.webkit.ServiceWorkerWebSettings#setAllowFileAccess}. |
| 93 */ |
| 94 public void setAllowFileAccess(boolean allow) { |
| 95 if (TRACE) Log.d(LOGTAG, "setAllowFileAccess=" + allow); |
| 96 synchronized (mAwServiceWorkerSettingsLock) { |
| 97 if (mAllowFileUrlAccess != allow) { |
| 98 mAllowFileUrlAccess = allow; |
| 99 } |
| 100 } |
| 101 } |
| 102 |
| 103 /** |
| 104 * See {@link android.webkit.ServiceWorkerWebSettings#getAllowFileAccess}. |
| 105 */ |
| 106 public boolean getAllowFileAccess() { |
| 107 synchronized (mAwServiceWorkerSettingsLock) { |
| 108 return mAllowFileUrlAccess; |
| 109 } |
| 110 } |
| 111 |
| 112 /** |
| 113 * See {@link android.webkit.ServiceWorkerWebSettings#setBlockNetworkLoads}. |
| 114 */ |
| 115 public void setBlockNetworkLoads(boolean flag) { |
| 116 if (TRACE) Log.d(LOGTAG, "setBlockNetworkLoads=" + flag); |
| 117 synchronized (mAwServiceWorkerSettingsLock) { |
| 118 if (!flag && !mHasInternetPermission) { |
| 119 throw new SecurityException("Permission denied - " |
| 120 + "application missing INTERNET permission"); |
| 121 } |
| 122 mBlockNetworkLoads = flag; |
| 123 } |
| 124 } |
| 125 |
| 126 /** |
| 127 * See {@link android.webkit.ServiceWorkerWebSettings#getBlockNetworkLoads}. |
| 128 */ |
| 129 public boolean getBlockNetworkLoads() { |
| 130 synchronized (mAwServiceWorkerSettingsLock) { |
| 131 return mBlockNetworkLoads; |
| 132 } |
| 133 } |
| 134 } |
| OLD | NEW |