Commit 1637edbd authored by Scott Violet's avatar Scott Violet Committed by Commit Bot

weblayer: adds plumbing for BrowserObserver

Here's the set of classes/changes:

weblayer_private.BrowserControllerClient: this is a singleton that is supplied
when creating BrowserControllerImpl. It will duplicate the C++ BrowserObserver
interface along with any other server->client calls we want.

BrowserController: supplies an implementation (as an inner class) of
BrowserControllerClient to BrowserControllerImpl. BrowserController's inner
class forwards calls to BrowserObservers.

BrowserObserverProxy: there is a C++ and Java class with this name. The two
are tightly coupled. The C++ side implements BrowserObserver and forwards to the
java side. The java side forwards to BrowserControllerClient.

BUG=none
TEST=none

Change-Id: I2817149224f4ac2045769c4ce0c3f81040dc7507
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1772501
Commit-Queue: Scott Violet <sky@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#691007}
parent 3ca9622b
...@@ -134,6 +134,10 @@ jumbo_static_library("weblayer_lib") { ...@@ -134,6 +134,10 @@ jumbo_static_library("weblayer_lib") {
"//ui/android", "//ui/android",
"//weblayer/browser/java:jni", "//weblayer/browser/java:jni",
] ]
sources += [
"browser/browser_observer_proxy.cc",
"browser/browser_observer_proxy.h",
]
} }
if (toolkit_views) { if (toolkit_views) {
......
// Copyright 2019 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.
#include "weblayer/browser/browser_observer_proxy.h"
#include "base/android/jni_string.h"
#include "url/gurl.h"
#include "weblayer/browser/browser_controller_impl.h"
#include "weblayer/browser/java/jni/BrowserObserverProxy_jni.h"
using base::android::AttachCurrentThread;
using base::android::ConvertUTF8ToJavaString;
using base::android::ScopedJavaLocalRef;
namespace weblayer {
BrowserObserverProxy::BrowserObserverProxy(
JNIEnv* env,
jobject obj,
BrowserController* browser_controller)
: browser_controller_(browser_controller), java_observer_(env, obj) {
browser_controller_->AddObserver(this);
}
BrowserObserverProxy::~BrowserObserverProxy() {
browser_controller_->RemoveObserver(this);
}
void BrowserObserverProxy::DisplayedURLChanged(const GURL& url) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jstring> jstring_url(
ConvertUTF8ToJavaString(env, url.spec()));
Java_BrowserObserverProxy_displayURLChanged(env, java_observer_, jstring_url);
}
void BrowserObserverProxy::LoadingStateChanged(bool is_loading,
bool to_different_document) {}
void BrowserObserverProxy::FirstContentfulPaint() {}
static jlong JNI_BrowserObserverProxy_CreateBrowserObsererProxy(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& proxy,
jlong browser_controller) {
return reinterpret_cast<jlong>(new BrowserObserverProxy(
env, proxy,
reinterpret_cast<BrowserControllerImpl*>(browser_controller)));
}
static void JNI_BrowserObserverProxy_DeleteBrowserObserverProxy(JNIEnv* env,
jlong proxy) {
delete reinterpret_cast<BrowserObserverProxy*>(proxy);
}
} // namespace weblayer
// Copyright 2019 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.
#ifndef WEBLAYER_BROWSER_BROWSER_OBSERVER_PROXY_H_
#define WEBLAYER_BROWSER_BROWSER_OBSERVER_PROXY_H_
#include <jni.h>
#include "base/android/scoped_java_ref.h"
#include "base/macros.h"
#include "weblayer/public/browser_observer.h"
namespace weblayer {
class BrowserController;
// BrowserObserverProxy forwards all BrowserObserver functions to the Java
// side. There is one BrowserObserverProxy per BrowserController.
class BrowserObserverProxy : public BrowserObserver {
public:
BrowserObserverProxy(JNIEnv* env,
jobject obj,
BrowserController* browser_controller);
~BrowserObserverProxy() override;
// BrowserObserver:
void DisplayedURLChanged(const GURL& url) override;
void LoadingStateChanged(bool is_loading,
bool to_different_document) override;
void FirstContentfulPaint() override;
private:
BrowserController* browser_controller_;
base::android::ScopedJavaGlobalRef<jobject> java_observer_;
DISALLOW_COPY_AND_ASSIGN(BrowserObserverProxy);
};
} // namespace weblayer
#endif // WEBLAYER_BROWSER_BROWSER_OBSERVER_PROXY_H_
...@@ -7,7 +7,9 @@ import("//build/config/android/rules.gni") ...@@ -7,7 +7,9 @@ import("//build/config/android/rules.gni")
android_library("java") { android_library("java") {
java_files = [ java_files = [
"org/chromium/weblayer_private/BrowserControllerClient.java",
"org/chromium/weblayer_private/BrowserControllerImpl.java", "org/chromium/weblayer_private/BrowserControllerImpl.java",
"org/chromium/weblayer_private/BrowserObserverProxy.java",
"org/chromium/weblayer_private/ProfileImpl.java", "org/chromium/weblayer_private/ProfileImpl.java",
"org/chromium/weblayer_private/WebLayerImpl.java", "org/chromium/weblayer_private/WebLayerImpl.java",
] ]
...@@ -24,6 +26,7 @@ android_library("java") { ...@@ -24,6 +26,7 @@ android_library("java") {
generate_jni("jni") { generate_jni("jni") {
sources = [ sources = [
"org/chromium/weblayer_private/BrowserControllerImpl.java", "org/chromium/weblayer_private/BrowserControllerImpl.java",
"org/chromium/weblayer_private/BrowserObserverProxy.java",
"org/chromium/weblayer_private/ProfileImpl.java", "org/chromium/weblayer_private/ProfileImpl.java",
] ]
} }
// Copyright 2019 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.weblayer_private;
/**
* Interface used by BrowserController to inform the client of changes. This largely duplicates the
* BrowserObserver interface, but is a singleton to avoid unnecessary IPC.
*/
public interface BrowserControllerClient { public void displayURLChanged(String url); }
...@@ -18,14 +18,13 @@ import org.chromium.ui.base.ViewAndroidDelegate; ...@@ -18,14 +18,13 @@ import org.chromium.ui.base.ViewAndroidDelegate;
@JNINamespace("weblayer") @JNINamespace("weblayer")
public final class BrowserControllerImpl { public final class BrowserControllerImpl {
// TODO: should there be one tag for all this code?
private static final String TAG = "WebLayer";
private long mNativeBrowserController; private long mNativeBrowserController;
private ActivityWindowAndroid mWindowAndroid; private ActivityWindowAndroid mWindowAndroid;
private ContentViewRenderView mContentView; private ContentViewRenderView mContentView;
private ProfileImpl mProfile; private ProfileImpl mProfile;
private WebContents mWebContents; private WebContents mWebContents;
private BrowserObserverProxy mBrowserObserverProxy;
private static class InternalAccessDelegateImpl private static class InternalAccessDelegateImpl
implements ViewEventSink.InternalAccessDelegate { implements ViewEventSink.InternalAccessDelegate {
...@@ -48,7 +47,8 @@ public final class BrowserControllerImpl { ...@@ -48,7 +47,8 @@ public final class BrowserControllerImpl {
public void onScrollChanged(int lPix, int tPix, int oldlPix, int oldtPix) {} public void onScrollChanged(int lPix, int tPix, int oldlPix, int oldtPix) {}
} }
public BrowserControllerImpl(Activity activity, ProfileImpl profile) { public BrowserControllerImpl(
Activity activity, ProfileImpl profile, BrowserControllerClient client) {
mProfile = profile; mProfile = profile;
mWindowAndroid = new ActivityWindowAndroid(activity); mWindowAndroid = new ActivityWindowAndroid(activity);
...@@ -66,9 +66,11 @@ public final class BrowserControllerImpl { ...@@ -66,9 +66,11 @@ public final class BrowserControllerImpl {
mContentView.setCurrentWebContents(mWebContents); mContentView.setCurrentWebContents(mWebContents);
mWebContents.onShow(); mWebContents.onShow();
mBrowserObserverProxy = new BrowserObserverProxy(mNativeBrowserController, client);
} }
public void destroy() { public void destroy() {
mBrowserObserverProxy.destroy();
nativeDeleteBrowserController(mNativeBrowserController); nativeDeleteBrowserController(mNativeBrowserController);
mNativeBrowserController = 0; mNativeBrowserController = 0;
} }
......
// Copyright 2019 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.weblayer_private;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
@JNINamespace("weblayer")
public final class BrowserObserverProxy {
private long mNativeBrowserObserverProxy;
private BrowserControllerClient mClient;
BrowserObserverProxy(long browserController, BrowserControllerClient client) {
mClient = client;
mNativeBrowserObserverProxy = nativeCreateBrowserObsererProxy(this, browserController);
}
public void destroy() {
nativeDeleteBrowserObserverProxy(mNativeBrowserObserverProxy);
}
@CalledByNative
private void displayURLChanged(String string) {
mClient.displayURLChanged(string);
}
private static native long nativeCreateBrowserObsererProxy(
BrowserObserverProxy proxy, long browserController);
private static native void nativeDeleteBrowserObserverProxy(long proxy);
}
...@@ -8,6 +8,7 @@ import("//build/config/android/rules.gni") ...@@ -8,6 +8,7 @@ import("//build/config/android/rules.gni")
android_library("java") { android_library("java") {
java_files = [ java_files = [
"org/chromium/weblayer/BrowserController.java", "org/chromium/weblayer/BrowserController.java",
"org/chromium/weblayer/BrowserObserver.java",
"org/chromium/weblayer/NavigationController.java", "org/chromium/weblayer/NavigationController.java",
"org/chromium/weblayer/Profile.java", "org/chromium/weblayer/Profile.java",
"org/chromium/weblayer/WebLayer.java", "org/chromium/weblayer/WebLayer.java",
......
...@@ -5,16 +5,24 @@ ...@@ -5,16 +5,24 @@
package org.chromium.weblayer; package org.chromium.weblayer;
import android.app.Activity; import android.app.Activity;
import android.net.Uri;
import org.chromium.weblayer_private.BrowserControllerClient;
import org.chromium.weblayer_private.BrowserControllerImpl; import org.chromium.weblayer_private.BrowserControllerImpl;
import java.util.concurrent.CopyOnWriteArrayList;
public final class BrowserController { public final class BrowserController {
private BrowserControllerImpl mBrowserController; private final BrowserControllerImpl mBrowserController;
private NavigationController mNavigationController; private final NavigationController mNavigationController;
// TODO(sky): copy ObserverList from base and use it instead.
private final CopyOnWriteArrayList<BrowserObserver> mObservers;
public BrowserController(Activity activity, Profile profile) { public BrowserController(Activity activity, Profile profile) {
mBrowserController = new BrowserControllerImpl(activity, profile.getProfileImpl()); mBrowserController = new BrowserControllerImpl(
activity, profile.getProfileImpl(), new BrowserClientImpl());
mNavigationController = new NavigationController(mBrowserController); mNavigationController = new NavigationController(mBrowserController);
mObservers = new CopyOnWriteArrayList<BrowserObserver>();
} }
@Override @Override
...@@ -25,4 +33,22 @@ public final class BrowserController { ...@@ -25,4 +33,22 @@ public final class BrowserController {
public NavigationController getNavigationController() { public NavigationController getNavigationController() {
return mNavigationController; return mNavigationController;
} }
public void addObserver(BrowserObserver observer) {
mObservers.add(observer);
}
public void removeObserver(BrowserObserver observer) {
mObservers.remove(observer);
}
private final class BrowserClientImpl implements BrowserControllerClient {
@Override
public void displayURLChanged(String url) {
Uri uri = Uri.parse(url);
for (BrowserObserver observer : mObservers) {
observer.displayURLChanged(uri);
}
}
}
} }
// Copyright 2019 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.weblayer;
import android.net.Uri;
/**
* Informed of interesting events that happen during the lifetime of a BrowserController.
*/
public abstract class BrowserObserver {
/**
* The Uri that should be displayed in the url-bar has updated.
*/
public void displayURLChanged(Uri url) {}
}
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