Commit 5912a82a authored by Tao Bai's avatar Tao Bai Committed by Commit Bot

Move the WebView instance count to native

This
- reduces unnecessary calls from native to java.
- makes it possible to add observer in native side.

Bug: 1042048
Change-Id: I0e5c32b98adfb56ac408556f9a0fcbd209b5813c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2001747Reviewed-by: default avatarBo <boliu@chromium.org>
Commit-Queue: Tao Bai <michaelbai@chromium.org>
Cr-Commit-Position: refs/heads/master@{#732264}
parent 28814b23
......@@ -5,19 +5,37 @@
#include "android_webview/browser/aw_contents_lifecycle_notifier.h"
#include "android_webview/browser_jni_headers/AwContentsLifecycleNotifier_jni.h"
#include "content/public/browser/browser_thread.h"
using base::android::AttachCurrentThread;
using content::BrowserThread;
namespace android_webview {
// static
void AwContentsLifecycleNotifier::OnWebViewCreated() {
Java_AwContentsLifecycleNotifier_onWebViewCreated(AttachCurrentThread());
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (getInstance().mNumWebViews++ == 0) {
Java_AwContentsLifecycleNotifier_onFirstWebViewCreated(
AttachCurrentThread());
}
}
// static
void AwContentsLifecycleNotifier::OnWebViewDestroyed() {
Java_AwContentsLifecycleNotifier_onWebViewDestroyed(AttachCurrentThread());
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (--getInstance().mNumWebViews == 0) {
Java_AwContentsLifecycleNotifier_onLastWebViewDestroyed(
AttachCurrentThread());
}
}
// static
AwContentsLifecycleNotifier& AwContentsLifecycleNotifier::getInstance() {
static base::NoDestructor<AwContentsLifecycleNotifier> instance;
return *instance;
}
AwContentsLifecycleNotifier::AwContentsLifecycleNotifier() = default;
} // namespace android_webview
......@@ -7,6 +7,7 @@
#include "base/android/jni_android.h"
#include "base/macros.h"
#include "base/no_destructor.h"
namespace android_webview {
......@@ -16,7 +17,16 @@ class AwContentsLifecycleNotifier {
static void OnWebViewDestroyed();
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(AwContentsLifecycleNotifier);
friend base::NoDestructor<AwContentsLifecycleNotifier>;
static AwContentsLifecycleNotifier& getInstance();
AwContentsLifecycleNotifier();
~AwContentsLifecycleNotifier() = delete;
int mNumWebViews = 0;
DISALLOW_COPY_AND_ASSIGN(AwContentsLifecycleNotifier);
};
} // namespace android_webview
......
......@@ -28,7 +28,7 @@ public class AwContentsLifecycleNotifier {
private static final ObserverList<Observer> sLifecycleObservers =
new ObserverList<Observer>();
private static int sNumWebViews;
private static boolean sHasWebViewInstances;
private AwContentsLifecycleNotifier() {}
......@@ -41,34 +41,28 @@ public class AwContentsLifecycleNotifier {
}
public static boolean hasWebViewInstances() {
return sNumWebViews > 0;
return sHasWebViewInstances;
}
// Called on UI thread.
@CalledByNative
private static void onWebViewCreated() {
private static void onFirstWebViewCreated() {
ThreadUtils.assertOnUiThread();
assert sNumWebViews >= 0;
sNumWebViews++;
if (sNumWebViews == 1) {
// first webview created, notify observers.
for (Observer observer : sLifecycleObservers) {
observer.onFirstWebViewCreated();
}
sHasWebViewInstances = true;
// first webview created, notify observers.
for (Observer observer : sLifecycleObservers) {
observer.onFirstWebViewCreated();
}
}
// Called on UI thread.
@CalledByNative
private static void onWebViewDestroyed() {
private static void onLastWebViewDestroyed() {
ThreadUtils.assertOnUiThread();
assert sNumWebViews > 0;
sNumWebViews--;
if (sNumWebViews == 0) {
// last webview destroyed, notify observers.
for (Observer observer : sLifecycleObservers) {
observer.onLastWebViewDestroyed();
}
sHasWebViewInstances = false;
// last webview destroyed, notify observers.
for (Observer observer : sLifecycleObservers) {
observer.onLastWebViewDestroyed();
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment