Index: content/public/android/java/src/org/chromium/content/browser/webcontents/WebContentsImpl.java |
diff --git a/content/public/android/java/src/org/chromium/content/browser/webcontents/WebContentsImpl.java b/content/public/android/java/src/org/chromium/content/browser/webcontents/WebContentsImpl.java |
index 049e5af8d0ea21e9ed7813af43f522d757c165e0..f7a82e6992d0722a52d2bfc47728b1fcb3211cd7 100644 |
--- a/content/public/android/java/src/org/chromium/content/browser/webcontents/WebContentsImpl.java |
+++ b/content/public/android/java/src/org/chromium/content/browser/webcontents/WebContentsImpl.java |
@@ -10,6 +10,7 @@ import org.chromium.content_public.browser.JavaScriptCallback; |
import org.chromium.content_public.browser.NavigationController; |
import org.chromium.content_public.browser.NavigationTransitionDelegate; |
import org.chromium.content_public.browser.WebContents; |
+import org.chromium.content_public.browser.WebContentsObserver; |
/** |
* The WebContentsImpl Java wrapper to allow communicating with the native WebContentsImpl |
@@ -23,6 +24,9 @@ import org.chromium.content_public.browser.WebContents; |
private long mNativeWebContentsAndroid; |
private NavigationController mNavigationController; |
+ // Lazily created proxy observer for handling all Java-based WebContentsObservers. |
+ private WebContentsObserverProxy mObserverProxy; |
+ |
private NavigationTransitionDelegate mNavigationTransitionDelegate = null; |
private WebContentsImpl( |
@@ -41,6 +45,10 @@ import org.chromium.content_public.browser.WebContents; |
private void clearNativePtr() { |
mNativeWebContentsAndroid = 0; |
mNavigationController = null; |
+ if (mObserverProxy != null) { |
+ mObserverProxy.destroy(); |
+ mObserverProxy = null; |
+ } |
} |
@CalledByNative |
@@ -312,6 +320,23 @@ import org.chromium.content_public.browser.WebContents; |
callback.handleJavaScriptResult(jsonResult); |
} |
+ @Override |
+ public void addObserver(WebContentsObserver observer) { |
+ assert mNativeWebContentsAndroid != 0; |
+ if (mObserverProxy == null) mObserverProxy = new WebContentsObserverProxy(this); |
+ mObserverProxy.addObserver(observer); |
+ } |
+ |
+ @Override |
+ public void removeObserver(WebContentsObserver observer) { |
+ if (mObserverProxy == null) return; |
+ mObserverProxy.removeObserver(observer); |
+ if (!mObserverProxy.hasObservers()) { |
+ mObserverProxy.destroy(); |
+ mObserverProxy = null; |
+ } |
+ } |
+ |
// This is static to avoid exposing a public destroy method on the native side of this class. |
private static native void nativeDestroyWebContents(long webContentsAndroidPtr); |