Commit 740c80c0 authored by Jinsuk Kim's avatar Jinsuk Kim Committed by Commit Bot

Android: Init SelectPopup from native

SelectPopup is the only object invoked from native before Java
side initialization is performed, which led to adding a call in
|WebContentsImpl.initialize| to trigger the init operation. This
CL removes the explicit init by having native do it in a lazy
manner before invoking Java side |show|.

Bug: 868238
Change-Id: I8691607472bd8432f4ff3379b9e1452bb76ca21c
Reviewed-on: https://chromium-review.googlesource.com/1155270
Commit-Queue: Jinsuk Kim <jinsukkim@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#579325}
parent 29364f25
......@@ -40,27 +40,14 @@ enum PopupItemType {
} // namespace
jlong JNI_SelectPopup_Init(JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jobject>& jweb_contents) {
WebContents* web_contents = WebContents::FromJavaWebContents(jweb_contents);
DCHECK(web_contents);
auto* wc_impl = static_cast<WebContentsImpl*>(web_contents);
auto* wcv = static_cast<WebContentsViewAndroid*>(wc_impl->GetView());
// Owned by |WebContentsViewAndroid|.
auto select_popup = std::make_unique<SelectPopup>(env, obj, wc_impl);
SelectPopup* select_popup_ptr = select_popup.get();
wcv->SetSelectPopup(std::move(select_popup));
return reinterpret_cast<intptr_t>(select_popup_ptr);
SelectPopup::SelectPopup(WebContentsImpl* web_contents)
: web_contents_(web_contents) {
JNIEnv* env = AttachCurrentThread();
java_obj_ = JavaObjectWeakGlobalRef(
env, Java_SelectPopup_create(env, web_contents_->GetJavaWebContents(),
reinterpret_cast<intptr_t>(this)));
}
SelectPopup::SelectPopup(JNIEnv* env,
const JavaParamRef<jobject>& obj,
WebContentsImpl* web_contents)
: web_contents_(web_contents), java_obj_(env, obj) {}
SelectPopup::~SelectPopup() {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> j_obj = java_obj_.get(env);
......
......@@ -23,9 +23,7 @@ struct MenuItem;
class SelectPopup {
public:
SelectPopup(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
WebContentsImpl* web_contents);
explicit SelectPopup(WebContentsImpl* web_contents);
~SelectPopup();
// Creates a popup menu with |items|.
......
......@@ -107,11 +107,6 @@ void WebContentsViewAndroid::SetContentUiEventHandler(
content_ui_event_handler_ = std::move(handler);
}
void WebContentsViewAndroid::SetSelectPopup(
std::unique_ptr<SelectPopup> select_popup) {
select_popup_ = std::move(select_popup);
}
void WebContentsViewAndroid::SetOverscrollRefreshHandler(
std::unique_ptr<ui::OverscrollRefreshHandler> overscroll_refresh_handler) {
overscroll_refresh_handler_ = std::move(overscroll_refresh_handler);
......@@ -323,6 +318,12 @@ void WebContentsViewAndroid::ShowContextMenu(
delegate_->ShowContextMenu(render_frame_host, params);
}
SelectPopup* WebContentsViewAndroid::GetSelectPopup() {
if (!select_popup_)
select_popup_ = std::make_unique<SelectPopup>(web_contents_);
return select_popup_.get();
}
void WebContentsViewAndroid::ShowPopupMenu(
RenderFrameHost* render_frame_host,
const gfx::Rect& bounds,
......@@ -332,15 +333,12 @@ void WebContentsViewAndroid::ShowPopupMenu(
const std::vector<MenuItem>& items,
bool right_aligned,
bool allow_multiple_selection) {
if (select_popup_) {
select_popup_->ShowMenu(render_frame_host, bounds, items, selected_item,
allow_multiple_selection, right_aligned);
}
GetSelectPopup()->ShowMenu(render_frame_host, bounds, items, selected_item,
allow_multiple_selection, right_aligned);
}
void WebContentsViewAndroid::HidePopupMenu() {
if (select_popup_)
select_popup_->HideMenu();
GetSelectPopup()->HideMenu();
}
void WebContentsViewAndroid::StartDragging(
......
......@@ -38,9 +38,6 @@ class WebContentsViewAndroid : public WebContentsView,
void SetContentUiEventHandler(std::unique_ptr<ContentUiEventHandler> handler);
// Sets the object that show/hide popup view for <select> tag.
void SetSelectPopup(std::unique_ptr<SelectPopup> select_popup);
void set_synchronous_compositor_client(SynchronousCompositorClient* client) {
synchronous_compositor_client_ = client;
}
......@@ -142,6 +139,8 @@ class WebContentsViewAndroid : public WebContentsView,
void OnDragEnded();
void OnSystemDragEnded();
SelectPopup* GetSelectPopup();
// The WebContents whose contents we display.
WebContentsImpl* web_contents_;
......
......@@ -64,6 +64,13 @@ public class SelectPopup
return webContents.getOrSetUserData(SelectPopup.class, UserDataFactoryLazyHolder.INSTANCE);
}
@CalledByNative
private static SelectPopup create(WebContents webContents, long nativePtr) {
SelectPopup selectPopup = fromWebContents(webContents);
selectPopup.mNativeSelectPopup = nativePtr;
return selectPopup;
}
/**
* Create {@link SelectPopup} instance.
* @param webContents WebContents instance.
......@@ -74,7 +81,6 @@ public class SelectPopup
assert viewDelegate != null;
mContainerView = viewDelegate.getContainerView();
viewDelegate.addObserver(this);
mNativeSelectPopup = nativeInit(mWebContents);
PopupController.register(mWebContents, this);
WindowEventObserverManager.from(mWebContents).addObserver(this);
}
......@@ -188,7 +194,6 @@ public class SelectPopup
mPopupView = null;
}
private native long nativeInit(WebContents webContents);
private native void nativeSelectMenuItems(
long nativeSelectPopup, long nativeSelectPopupSourceFrame, int[] indices);
}
......@@ -205,9 +205,6 @@ public class WebContentsImpl implements WebContents, RenderFrameHostDelegate, Wi
setViewAndroidDelegate(viewDelegate);
setTopLevelNativeWindow(windowAndroid);
// Internally registers the handler that controls showing <select> HTML elements.
SelectPopup.fromWebContents(this);
ViewEventSinkImpl.from(this).setAccessDelegate(accessDelegate);
getRenderCoordinates().setDeviceScaleFactor(windowAndroid.getDisplay().getDipScale());
}
......
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