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 @@ ...@@ -5,19 +5,37 @@
#include "android_webview/browser/aw_contents_lifecycle_notifier.h" #include "android_webview/browser/aw_contents_lifecycle_notifier.h"
#include "android_webview/browser_jni_headers/AwContentsLifecycleNotifier_jni.h" #include "android_webview/browser_jni_headers/AwContentsLifecycleNotifier_jni.h"
#include "content/public/browser/browser_thread.h"
using base::android::AttachCurrentThread; using base::android::AttachCurrentThread;
using content::BrowserThread;
namespace android_webview { namespace android_webview {
// static // static
void AwContentsLifecycleNotifier::OnWebViewCreated() { void AwContentsLifecycleNotifier::OnWebViewCreated() {
Java_AwContentsLifecycleNotifier_onWebViewCreated(AttachCurrentThread()); DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (getInstance().mNumWebViews++ == 0) {
Java_AwContentsLifecycleNotifier_onFirstWebViewCreated(
AttachCurrentThread());
}
} }
// static // static
void AwContentsLifecycleNotifier::OnWebViewDestroyed() { 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 } // namespace android_webview
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/android/jni_android.h" #include "base/android/jni_android.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/no_destructor.h"
namespace android_webview { namespace android_webview {
...@@ -16,7 +17,16 @@ class AwContentsLifecycleNotifier { ...@@ -16,7 +17,16 @@ class AwContentsLifecycleNotifier {
static void OnWebViewDestroyed(); static void OnWebViewDestroyed();
private: 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 } // namespace android_webview
......
...@@ -28,7 +28,7 @@ public class AwContentsLifecycleNotifier { ...@@ -28,7 +28,7 @@ public class AwContentsLifecycleNotifier {
private static final ObserverList<Observer> sLifecycleObservers = private static final ObserverList<Observer> sLifecycleObservers =
new ObserverList<Observer>(); new ObserverList<Observer>();
private static int sNumWebViews; private static boolean sHasWebViewInstances;
private AwContentsLifecycleNotifier() {} private AwContentsLifecycleNotifier() {}
...@@ -41,34 +41,28 @@ public class AwContentsLifecycleNotifier { ...@@ -41,34 +41,28 @@ public class AwContentsLifecycleNotifier {
} }
public static boolean hasWebViewInstances() { public static boolean hasWebViewInstances() {
return sNumWebViews > 0; return sHasWebViewInstances;
} }
// Called on UI thread. // Called on UI thread.
@CalledByNative @CalledByNative
private static void onWebViewCreated() { private static void onFirstWebViewCreated() {
ThreadUtils.assertOnUiThread(); ThreadUtils.assertOnUiThread();
assert sNumWebViews >= 0; sHasWebViewInstances = true;
sNumWebViews++; // first webview created, notify observers.
if (sNumWebViews == 1) { for (Observer observer : sLifecycleObservers) {
// first webview created, notify observers. observer.onFirstWebViewCreated();
for (Observer observer : sLifecycleObservers) {
observer.onFirstWebViewCreated();
}
} }
} }
// Called on UI thread. // Called on UI thread.
@CalledByNative @CalledByNative
private static void onWebViewDestroyed() { private static void onLastWebViewDestroyed() {
ThreadUtils.assertOnUiThread(); ThreadUtils.assertOnUiThread();
assert sNumWebViews > 0; sHasWebViewInstances = false;
sNumWebViews--; // last webview destroyed, notify observers.
if (sNumWebViews == 0) { for (Observer observer : sLifecycleObservers) {
// last webview destroyed, notify observers. observer.onLastWebViewDestroyed();
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