diff options
author | timvolodine <timvolodine@chromium.org> | 2015-08-20 13:06:04 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-08-20 20:06:33 +0000 |
commit | 7d01bcecee8bda46001ac56a5e38574a9084679b (patch) | |
tree | 2a095ca7317b7847b012f291d3c98b607808f09d /android_webview/java | |
parent | 79d5b7fe7128f8fce93a9c7c3f95049d2f11c364 (diff) | |
download | chromium_src-7d01bcecee8bda46001ac56a5e38574a9084679b.zip chromium_src-7d01bcecee8bda46001ac56a5e38574a9084679b.tar.gz chromium_src-7d01bcecee8bda46001ac56a5e38574a9084679b.tar.bz2 |
[Android WebView] Enable Network Information API
Enable Network Information API for Webview by default.
The API is *not* enabled in the following cases:
1. if the embedding application does not have sufficient permissions
(i.e. at least android.permission.ACCESS_NETWORK_STATE) and
2. when there are no WebView instances (i.e. instances of AwContents).
BUG=520088
Review URL: https://codereview.chromium.org/1297943003
Cr-Commit-Position: refs/heads/master@{#344569}
Diffstat (limited to 'android_webview/java')
-rw-r--r-- | android_webview/java/src/org/chromium/android_webview/AwWebViewLifecycleObserver.java | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/android_webview/java/src/org/chromium/android_webview/AwWebViewLifecycleObserver.java b/android_webview/java/src/org/chromium/android_webview/AwWebViewLifecycleObserver.java new file mode 100644 index 0000000..a9b52ca --- /dev/null +++ b/android_webview/java/src/org/chromium/android_webview/AwWebViewLifecycleObserver.java @@ -0,0 +1,61 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.android_webview; + +import android.content.Context; + +import org.chromium.base.ThreadUtils; +import org.chromium.base.VisibleForTesting; +import org.chromium.base.annotations.CalledByNative; +import org.chromium.base.annotations.JNINamespace; +import org.chromium.net.NetworkChangeNotifier; + +/** + * This class is intended to observe the lifetime of webviews. It receives + * a callback when the first webview instance (native aw_content) is created + * and when the last webview instance is destroyed. + */ +@JNINamespace("android_webview") +public class AwWebViewLifecycleObserver { + + private static final String TAG = "AwWebViewLifecycle"; + // Assume there is ACCESS_NETWORK_STATE permission unless told otherwise. + private static boolean sHasNetworkStatePermission = true; + + // Called on UI thread. + @CalledByNative + private static void onFirstWebViewCreated(Context appContext) { + ThreadUtils.assertOnUiThread(); + + if (sHasNetworkStatePermission) { + try { + if (!NetworkChangeNotifier.isInitialized()) { + NetworkChangeNotifier.init(appContext); + } + NetworkChangeNotifier.registerToReceiveNotificationsAlways(); + } catch (SecurityException e) { + // Cannot enable network information api. The application does not have the + // ACCESS_NETWORK_STATE permission. + sHasNetworkStatePermission = false; + } + } + } + + // Called on UI thread. + @CalledByNative + private static void onLastWebViewDestroyed(Context appContext) { + ThreadUtils.assertOnUiThread(); + + if (sHasNetworkStatePermission && NetworkChangeNotifier.isInitialized()) { + // Force unregister for network change broadcasts. + NetworkChangeNotifier.setAutoDetectConnectivityState(false); + } + } + + @VisibleForTesting + public static void setHasNetworkStatePermission(boolean allow) { + sHasNetworkStatePermission = allow; + } +} |