Commit bf073a04 authored by Shakti Sahu's avatar Shakti Sahu Committed by Commit Bot

Thin webview : Added class to show WebContents

This CL creates a ThinWebView class that can display a WebContents on
an android surface. It also provides method to the user to attach a
ContentView that can be used to provide user interactability.

Bug: 980323
Change-Id: If99b98b695a512daa693c343c07ba4717af6ca52
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1684331
Commit-Queue: Shakti Sahu <shaktisahu@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Reviewed-by: default avatarKhushal <khushalsagar@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#680129}
parent 1a5a8494
......@@ -15,22 +15,29 @@ source_set("thin_webview") {
}
android_library("java") {
java_files =
[ "java/src/org/chromium/chrome/browser/thinwebview/CompositorView.java" ]
java_files = [
"java/src/org/chromium/chrome/browser/thinwebview/CompositorView.java",
"java/src/org/chromium/chrome/browser/thinwebview/ThinWebView.java",
]
deps = [
"//base:base_java",
"//content/public/android:content_java",
"//ui/android:ui_java",
]
}
android_library("factory_java") {
java_files = [ "java/src/org/chromium/chrome/browser/thinwebview/CompositorViewFactory.java" ]
java_files = [
"java/src/org/chromium/chrome/browser/thinwebview/CompositorViewFactory.java",
"java/src/org/chromium/chrome/browser/thinwebview/ThinWebViewFactory.java",
]
deps = [
":java",
"internal:internal_java",
"//base:base_java",
"//content/public/android:content_java",
"//ui/android:ui_java",
]
}
include_rules = [
"+content/public/android/java/src/org/chromium/content_public",
]
......@@ -9,6 +9,8 @@ static_library("internal") {
sources = [
"compositor_view_impl.cc",
"compositor_view_impl.h",
"thin_webview.cc",
"thin_webview.h",
]
deps = [
......@@ -26,7 +28,10 @@ static_library("internal") {
}
android_library("internal_java") {
java_files = [ "java/src/org/chromium/chrome/browser/thinwebview/internal/CompositorViewImpl.java" ]
java_files = [
"java/src/org/chromium/chrome/browser/thinwebview/internal/CompositorViewImpl.java",
"java/src/org/chromium/chrome/browser/thinwebview/internal/ThinWebViewImpl.java",
]
deps = [
"//base:base_java",
......@@ -40,5 +45,6 @@ generate_jni("jni_headers") {
visibility = [ ":*" ]
sources = [
"java/src/org/chromium/chrome/browser/thinwebview/internal/CompositorViewImpl.java",
"java/src/org/chromium/chrome/browser/thinwebview/internal/ThinWebViewImpl.java",
]
}
include_rules = [
"+cc/layers",
"+content/public/android/java/src/org/chromium/content_public",
]
// 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.chrome.browser.thinwebview.internal;
import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.chrome.browser.thinwebview.CompositorView;
import org.chromium.chrome.browser.thinwebview.ThinWebView;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.WindowAndroid;
/**
* An android view backed by a {@link Surface} that is able to display a live {@link WebContents}.
*/
@JNINamespace("thin_webview::android")
public class ThinWebViewImpl extends FrameLayout implements ThinWebView {
private final CompositorView mCompositorView;
private final long mNativeThinWebViewImpl;
private WebContents mWebContents;
private View mContentView;
/**
* Creates a {@link ThinWebViewImpl} backed by a {@link Surface}.
* @param context The Context to create this view.
* @param windowAndroid The associated {@code WindowAndroid} on which the view is to be
* displayed.
*/
public ThinWebViewImpl(Context context, WindowAndroid windowAndroid) {
super(context);
mCompositorView = new CompositorViewImpl(context, windowAndroid);
LayoutParams layoutParams = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
addView(mCompositorView.getView(), layoutParams);
mNativeThinWebViewImpl = nativeInit(mCompositorView, windowAndroid);
}
@Override
public View getView() {
return this;
}
@Override
public void attachWebContents(WebContents webContents, @Nullable View contentView) {
mWebContents = webContents;
setContentView(contentView);
nativeSetWebContents(mNativeThinWebViewImpl, mWebContents);
mWebContents.onShow();
}
@Override
public void destroy() {
mCompositorView.destroy();
if (mNativeThinWebViewImpl != 0) nativeDestroy(mNativeThinWebViewImpl);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (w != oldw || h != oldh) {
nativeSizeChanged(mNativeThinWebViewImpl, w, h);
}
}
private void setContentView(View contentView) {
if (mContentView == contentView) return;
if (mContentView != null) {
assert getChildCount() > 1;
removeViewAt(1);
}
mContentView = contentView;
if (mContentView != null) addView(mContentView, 1);
}
private native long nativeInit(CompositorView compositorView, WindowAndroid windowAndroid);
private native void nativeDestroy(long nativeThinWebView);
private native void nativeSetWebContents(long nativeThinWebView, WebContents webContents);
private native void nativeSizeChanged(long nativeThinWebView, int width, int height);
}
// 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 "chrome/browser/android/thin_webview/internal/thin_webview.h"
#include "base/android/jni_android.h"
#include "cc/layers/layer.h"
#include "chrome/browser/android/thin_webview/internal/jni_headers/ThinWebViewImpl_jni.h"
#include "content/public/browser/web_contents.h"
using base::android::JavaParamRef;
namespace thin_webview {
namespace android {
jlong JNI_ThinWebViewImpl_Init(JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jobject>& jcompositor_view,
const JavaParamRef<jobject>& jwindow_android) {
CompositorView* compositor_view =
CompositorViewImpl::FromJavaObject(jcompositor_view);
ui::WindowAndroid* window_android =
ui::WindowAndroid::FromJavaWindowAndroid(jwindow_android);
ThinWebView* view =
new ThinWebView(env, obj, compositor_view, window_android);
return reinterpret_cast<intptr_t>(view);
}
ThinWebView::ThinWebView(JNIEnv* env,
jobject obj,
CompositorView* compositor_view,
ui::WindowAndroid* window_android)
: obj_(env, obj),
compositor_view_(compositor_view),
window_android_(window_android),
web_contents_(nullptr) {}
ThinWebView::~ThinWebView() {}
void ThinWebView::Destroy(JNIEnv* env, const JavaParamRef<jobject>& object) {
delete this;
}
void ThinWebView::SetWebContents(JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jobject>& jweb_contents) {
content::WebContents* web_contents =
content::WebContents::FromJavaWebContents(jweb_contents);
SetWebContents(web_contents);
}
void ThinWebView::SetWebContents(content::WebContents* web_contents) {
DCHECK(web_contents);
web_contents_ = web_contents;
ui::ViewAndroid* view_android = web_contents_->GetNativeView();
if (view_android->parent() != window_android_) {
window_android_->AddChild(view_android);
}
compositor_view_->SetRootLayer(web_contents_->GetNativeView()->GetLayer());
ResizeWebContents(view_size_);
}
void ThinWebView::SizeChanged(JNIEnv* env,
const JavaParamRef<jobject>& object,
jint width,
jint height) {
view_size_ = gfx::Size(width, height);
// TODO(shaktisahu): If we want to use a different size for WebContents, e.g.
// showing full screen contents instead inside this view, don't do the resize.
if (web_contents_)
ResizeWebContents(view_size_);
}
void ThinWebView::ResizeWebContents(const gfx::Size& size) {
if (!web_contents_)
return;
web_contents_->GetNativeView()->OnPhysicalBackingSizeChanged(size);
web_contents_->GetNativeView()->OnSizeChanged(size.width(), size.height());
}
} // namespace android
} // namespace thin_webview
// 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 CHROME_BROWSER_ANDROID_THIN_WEBVIEW_INTERNAL_THIN_WEBVIEW_H_
#define CHROME_BROWSER_ANDROID_THIN_WEBVIEW_INTERNAL_THIN_WEBVIEW_H_
#include <jni.h>
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "base/macros.h"
#include "chrome/browser/android/thin_webview/internal/compositor_view_impl.h"
#include "content/public/browser/web_contents.h"
#include "ui/android/window_android.h"
namespace thin_webview {
namespace android {
// Native counterpart of ThinWebViewImpl.java.
class ThinWebView {
public:
ThinWebView(JNIEnv* env,
jobject obj,
CompositorView* compositor_view,
ui::WindowAndroid* window_android);
~ThinWebView();
void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& object);
void SetWebContents(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& object,
const base::android::JavaParamRef<jobject>& jweb_contents);
void SizeChanged(JNIEnv* env,
const base::android::JavaParamRef<jobject>& object,
jint width,
jint height);
private:
void SetWebContents(content::WebContents* web_contents);
void ResizeWebContents(const gfx::Size& size);
base::android::ScopedJavaGlobalRef<jobject> obj_;
CompositorView* compositor_view_;
ui::WindowAndroid* window_android_;
content::WebContents* web_contents_;
gfx::Size view_size_;
DISALLOW_COPY_AND_ASSIGN(ThinWebView);
};
} // namespace android
} // namespace thin_webview
#endif // CHROME_BROWSER_ANDROID_THIN_WEBVIEW_INTERNAL_THIN_WEBVIEW_H_
// 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.chrome.browser.thinwebview;
import android.support.annotation.Nullable;
import android.view.View;
import org.chromium.content_public.browser.WebContents;
/**
* An android view backed by a {@link Surface} that is able to display a cc::Layer. Either, a {@link
* TextureView} or {@link SurfaceView} can be used to provide the surface. The cc::Layer should be
* provided in the native.
*/
public interface ThinWebView {
/**
*@return The android {@link View} representing this widget.
*/
View getView();
/**
* Method to be called to display the contents of a {@link WebContents} on the surface. The user
* interactability is provided through the {@code contentView}.
* @param webContents A {@link WebContents} for providing the contents to be rendered.
* @param contentView A {@link ContentView} that can handle user inputs.
*/
void attachWebContents(WebContents webContents, @Nullable View contentView);
/**
* Should be called for cleanup when the CompositorView instance is no longer used.
*/
void destroy();
}
// 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.chrome.browser.thinwebview;
import android.content.Context;
import org.chromium.chrome.browser.thinwebview.internal.ThinWebViewImpl;
import org.chromium.ui.base.WindowAndroid;
/**
* Factory for creating a {@link ThinWebView}.
*/
public class ThinWebViewFactory {
/**
* Creates a {@link ThinWebView} backed by a {@link Surface}. The surface is provided by
* a either a {@link TextureView} or {@link SurfaceView}.
* @param context The context to create this view.
* @param windowAndroid The associated {@code WindowAndroid} on which the view is to be
* displayed.
*/
public static ThinWebView create(Context context, WindowAndroid windowAndroid) {
return new ThinWebViewImpl(context, windowAndroid);
}
}
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