refactor: mojofy autofill renderer->browser (#18760)

* re-implement renderer->browser calls with mojo

* lint

* fix: clean up after rebase

* lint

* fix: lazy load autofill drivers
This commit is contained in:
Heilig Benedek 2019-08-19 22:13:24 +02:00 committed by Shelley Vohr
parent 43e6d7fe88
commit b33558d822
14 changed files with 329 additions and 98 deletions

View file

@ -125,6 +125,10 @@ filenames = {
"shell/browser/auto_updater.cc",
"shell/browser/auto_updater.h",
"shell/browser/auto_updater_mac.mm",
"shell/browser/atom_autofill_driver_factory.cc",
"shell/browser/atom_autofill_driver_factory.h",
"shell/browser/atom_autofill_driver.cc",
"shell/browser/atom_autofill_driver.h",
"shell/browser/atom_blob_reader.cc",
"shell/browser/atom_blob_reader.h",
"shell/browser/atom_browser_client.cc",

View file

@ -50,6 +50,7 @@
#include "shell/browser/api/atom_api_browser_window.h"
#include "shell/browser/api/atom_api_debugger.h"
#include "shell/browser/api/atom_api_session.h"
#include "shell/browser/atom_autofill_driver_factory.h"
#include "shell/browser/atom_browser_client.h"
#include "shell/browser/atom_browser_context.h"
#include "shell/browser/atom_browser_main_parts.h"
@ -471,6 +472,7 @@ void WebContents::InitWithSessionAndOptions(
base::Unretained(this)));
bindings_.set_connection_error_handler(base::BindRepeating(
&WebContents::OnElectronBrowserConnectionError, base::Unretained(this)));
AutofillDriverFactory::CreateForWebContents(web_contents());
web_contents()->SetUserAgentOverride(GetBrowserContext()->GetUserAgent(),
false);
@ -626,7 +628,13 @@ void WebContents::SetContentsBounds(content::WebContents* source,
void WebContents::CloseContents(content::WebContents* source) {
Emit("close");
HideAutofillPopup();
auto* autofill_driver_factory =
AutofillDriverFactory::FromWebContents(web_contents());
if (autofill_driver_factory) {
autofill_driver_factory->CloseAllPopups();
}
if (managed_web_contents())
managed_web_contents()->GetView()->SetDelegate(nullptr);
for (ExtendedWebContentsObserver& observer : observers_)
@ -1153,26 +1161,6 @@ void WebContents::DevToolsClosed() {
Emit("devtools-closed");
}
void WebContents::ShowAutofillPopup(content::RenderFrameHost* frame_host,
const gfx::RectF& bounds,
const std::vector<base::string16>& values,
const std::vector<base::string16>& labels) {
bool offscreen = IsOffScreen() || (embedder_ && embedder_->IsOffScreen());
gfx::RectF popup_bounds(bounds);
content::RenderFrameHost* embedder_frame_host = nullptr;
if (embedder_) {
auto* embedder_view = embedder_->web_contents()->GetMainFrame()->GetView();
auto* view = web_contents()->GetMainFrame()->GetView();
auto offset = view->GetViewBounds().origin() -
embedder_view->GetViewBounds().origin();
popup_bounds.Offset(offset.x(), offset.y());
embedder_frame_host = embedder_->web_contents()->GetMainFrame();
}
CommonWebContentsDelegate::ShowAutofillPopup(
frame_host, embedder_frame_host, offscreen, popup_bounds, values, labels);
}
bool WebContents::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(WebContents, message)
@ -2214,17 +2202,6 @@ void WebContents::DoGetZoomLevel(DoGetZoomLevelCallback callback) {
std::move(callback).Run(GetZoomLevel());
}
void WebContents::ShowAutofillPopup(const gfx::RectF& bounds,
const std::vector<base::string16>& values,
const std::vector<base::string16>& labels) {
content::RenderFrameHost* frame_host = bindings_.dispatch_context();
ShowAutofillPopup(frame_host, bounds, values, labels);
}
void WebContents::HideAutofillPopup() {
CommonWebContentsDelegate::HideAutofillPopup();
}
std::vector<base::FilePath::StringType> WebContents::GetPreloadPaths() const {
auto result = SessionPreferences::GetValidPreloads(GetBrowserContext());
@ -2279,7 +2256,7 @@ v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
return v8::Local<v8::Value>::New(isolate, session_);
}
content::WebContents* WebContents::HostWebContents() {
content::WebContents* WebContents::HostWebContents() const {
if (!embedder_)
return nullptr;
return embedder_->web_contents();

View file

@ -14,6 +14,7 @@
#include "base/observer_list_types.h"
#include "content/common/cursors/webcursor.h"
#include "content/public/browser/keyboard_event_processing_result.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_binding_set.h"
#include "content/public/browser/web_contents_observer.h"
@ -27,7 +28,6 @@
#include "shell/browser/api/save_page_handler.h"
#include "shell/browser/api/trackable_object.h"
#include "shell/browser/common_web_contents_delegate.h"
#include "shell/browser/ui/autofill_popup.h"
#include "ui/gfx/image/image.h"
#if BUILDFLAG(ENABLE_PRINTING)
@ -305,7 +305,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
// Properties.
int32_t ID() const;
v8::Local<v8::Value> Session(v8::Isolate* isolate);
content::WebContents* HostWebContents();
content::WebContents* HostWebContents() const;
v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
v8::Local<v8::Value> Debugger(v8::Isolate* isolate);
@ -323,6 +323,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
bool EmitNavigationEvent(const std::string& event,
content::NavigationHandle* navigation_handle);
WebContents* embedder() { return embedder_; }
protected:
// Does not manage lifetime of |web_contents|.
WebContents(v8::Isolate* isolate, content::WebContents* web_contents);
@ -469,13 +471,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
void DevToolsOpened() override;
void DevToolsClosed() override;
#if defined(TOOLKIT_VIEWS)
void ShowAutofillPopup(content::RenderFrameHost* frame_host,
const gfx::RectF& bounds,
const std::vector<base::string16>& values,
const std::vector<base::string16>& labels);
#endif
private:
AtomBrowserContext* GetBrowserContext() const;
@ -514,11 +509,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
void SetTemporaryZoomLevel(double level) override;
void DoGetZoomLevel(DoGetZoomLevelCallback callback) override;
void ShowAutofillPopup(const gfx::RectF& bounds,
const std::vector<base::string16>& values,
const std::vector<base::string16>& labels) override;
void HideAutofillPopup() override;
// Called when we receive a CursorChange message from chromium.
void OnCursorChange(const content::WebCursor& cursor);

View file

@ -0,0 +1,63 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/atom_autofill_driver.h"
#include <utility>
#include "content/public/browser/render_widget_host_view.h"
#include "shell/browser/api/atom_api_web_contents.h"
#include "shell/browser/native_window.h"
namespace electron {
AutofillDriver::AutofillDriver(
content::RenderFrameHost* render_frame_host,
mojom::ElectronAutofillDriverAssociatedRequest request)
: render_frame_host_(render_frame_host), binding_(this) {
autofill_popup_.reset(new AutofillPopup());
binding_.Bind(std::move(request));
}
AutofillDriver::~AutofillDriver() {}
void AutofillDriver::ShowAutofillPopup(
const gfx::RectF& bounds,
const std::vector<base::string16>& values,
const std::vector<base::string16>& labels) {
auto* web_contents =
api::WebContents::From(
v8::Isolate::GetCurrent(),
content::WebContents::FromRenderFrameHost(render_frame_host_))
.get();
if (!web_contents || !web_contents->owner_window())
return;
auto* embedder = web_contents->embedder();
bool osr =
web_contents->IsOffScreen() || (embedder && embedder->IsOffScreen());
gfx::RectF popup_bounds(bounds);
content::RenderFrameHost* embedder_frame_host = nullptr;
if (embedder) {
auto* embedder_view = embedder->web_contents()->GetMainFrame()->GetView();
auto* view = web_contents->web_contents()->GetMainFrame()->GetView();
auto offset = view->GetViewBounds().origin() -
embedder_view->GetViewBounds().origin();
popup_bounds.Offset(offset.x(), offset.y());
embedder_frame_host = embedder->web_contents()->GetMainFrame();
}
autofill_popup_->CreateView(render_frame_host_, embedder_frame_host, osr,
web_contents->owner_window()->content_view(),
bounds);
autofill_popup_->SetItems(values, labels);
}
void AutofillDriver::HideAutofillPopup() {
if (autofill_popup_)
autofill_popup_->Hide();
}
} // namespace electron

View file

@ -0,0 +1,44 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef SHELL_BROWSER_ATOM_AUTOFILL_DRIVER_H_
#define SHELL_BROWSER_ATOM_AUTOFILL_DRIVER_H_
#include <memory>
#include <vector>
#if defined(TOOLKIT_VIEWS)
#include "shell/browser/ui/autofill_popup.h"
#endif
#include "mojo/public/cpp/bindings/associated_binding.h"
#include "shell/common/api/api.mojom.h"
namespace electron {
class AutofillDriver : public mojom::ElectronAutofillDriver {
public:
AutofillDriver(content::RenderFrameHost* render_frame_host,
mojom::ElectronAutofillDriverAssociatedRequest request);
~AutofillDriver() override;
void ShowAutofillPopup(const gfx::RectF& bounds,
const std::vector<base::string16>& values,
const std::vector<base::string16>& labels) override;
void HideAutofillPopup() override;
private:
content::RenderFrameHost* const render_frame_host_;
#if defined(TOOLKIT_VIEWS)
std::unique_ptr<AutofillPopup> autofill_popup_;
#endif
mojo::AssociatedBinding<mojom::ElectronAutofillDriver> binding_;
};
} // namespace electron
#endif // SHELL_BROWSER_ATOM_AUTOFILL_DRIVER_H_

View file

@ -0,0 +1,111 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/atom_autofill_driver_factory.h"
#include <memory>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/callback.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "shell/browser/atom_autofill_driver.h"
namespace electron {
namespace {
std::unique_ptr<AutofillDriver> CreateDriver(
content::RenderFrameHost* render_frame_host,
mojom::ElectronAutofillDriverAssociatedRequest request) {
return std::make_unique<AutofillDriver>(render_frame_host,
std::move(request));
}
} // namespace
AutofillDriverFactory::~AutofillDriverFactory() {}
// static
void AutofillDriverFactory::BindAutofillDriver(
mojom::ElectronAutofillDriverAssociatedRequest request,
content::RenderFrameHost* render_frame_host) {
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(render_frame_host);
if (!web_contents)
return;
AutofillDriverFactory* factory =
AutofillDriverFactory::FromWebContents(web_contents);
if (!factory)
return;
AutofillDriver* driver = factory->DriverForFrame(render_frame_host);
if (!driver)
factory->AddDriverForFrame(
render_frame_host,
base::BindOnce(CreateDriver, render_frame_host, std::move(request)));
}
AutofillDriverFactory::AutofillDriverFactory(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents) {
const std::vector<content::RenderFrameHost*> frames =
web_contents->GetAllFrames();
for (content::RenderFrameHost* frame : frames) {
if (frame->IsRenderFrameLive())
RenderFrameCreated(frame);
}
}
void AutofillDriverFactory::RenderFrameDeleted(
content::RenderFrameHost* render_frame_host) {
DeleteDriverForFrame(render_frame_host);
}
void AutofillDriverFactory::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
// For the purposes of this code, a navigation is not important if it has not
// committed yet or if it's in a subframe.
if (!navigation_handle->HasCommitted() ||
!navigation_handle->IsInMainFrame()) {
return;
}
CloseAllPopups();
}
AutofillDriver* AutofillDriverFactory::DriverForFrame(
content::RenderFrameHost* render_frame_host) {
auto mapping = driver_map_.find(render_frame_host);
return mapping == driver_map_.end() ? nullptr : mapping->second.get();
}
void AutofillDriverFactory::AddDriverForFrame(
content::RenderFrameHost* render_frame_host,
CreationCallback factory_method) {
auto insertion_result =
driver_map_.insert(std::make_pair(render_frame_host, nullptr));
// This can be called twice for the key representing the main frame.
if (insertion_result.second) {
insertion_result.first->second = std::move(factory_method).Run();
}
}
void AutofillDriverFactory::DeleteDriverForFrame(
content::RenderFrameHost* render_frame_host) {
driver_map_.erase(render_frame_host);
}
void AutofillDriverFactory::CloseAllPopups() {
for (auto& it : driver_map_) {
it.second->HideAutofillPopup();
}
}
WEB_CONTENTS_USER_DATA_KEY_IMPL(AutofillDriverFactory)
} // namespace electron

View file

@ -0,0 +1,57 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef SHELL_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_
#define SHELL_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_
#include <memory>
#include <unordered_map>
#include "base/callback_forward.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "shell/common/api/api.mojom.h"
namespace electron {
class AutofillDriver;
class AutofillDriverFactory
: public content::WebContentsObserver,
public content::WebContentsUserData<AutofillDriverFactory> {
public:
typedef base::OnceCallback<std::unique_ptr<AutofillDriver>()>
CreationCallback;
~AutofillDriverFactory() override;
static void BindAutofillDriver(
mojom::ElectronAutofillDriverAssociatedRequest request,
content::RenderFrameHost* render_frame_host);
// content::WebContentsObserver:
void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;
AutofillDriver* DriverForFrame(content::RenderFrameHost* render_frame_host);
void AddDriverForFrame(content::RenderFrameHost* render_frame_host,
CreationCallback factory_method);
void DeleteDriverForFrame(content::RenderFrameHost* render_frame_host);
void CloseAllPopups();
WEB_CONTENTS_USER_DATA_KEY_DECL();
private:
explicit AutofillDriverFactory(content::WebContents* web_contents);
friend class content::WebContentsUserData<AutofillDriverFactory>;
std::unordered_map<content::RenderFrameHost*, std::unique_ptr<AutofillDriver>>
driver_map_;
};
} // namespace electron
#endif // SHELL_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_

View file

@ -53,6 +53,7 @@
#include "shell/browser/api/atom_api_protocol_ns.h"
#include "shell/browser/api/atom_api_web_contents.h"
#include "shell/browser/api/atom_api_web_request_ns.h"
#include "shell/browser/atom_autofill_driver_factory.h"
#include "shell/browser/atom_browser_context.h"
#include "shell/browser/atom_browser_main_parts.h"
#include "shell/browser/atom_navigation_throttle.h"
@ -74,6 +75,7 @@
#include "shell/browser/web_contents_permission_helper.h"
#include "shell/browser/web_contents_preferences.h"
#include "shell/browser/window_list.h"
#include "shell/common/api/api.mojom.h"
#include "shell/common/application_info.h"
#include "shell/common/options_switches.h"
#include "shell/common/platform_util.h"
@ -1047,6 +1049,20 @@ bool AtomBrowserClient::PreSpawnRenderer(sandbox::TargetPolicy* policy) {
}
#endif // defined(OS_WIN)
bool AtomBrowserClient::BindAssociatedInterfaceRequestFromFrame(
content::RenderFrameHost* render_frame_host,
const std::string& interface_name,
mojo::ScopedInterfaceEndpointHandle* handle) {
if (interface_name == mojom::ElectronAutofillDriver::Name_) {
AutofillDriverFactory::BindAutofillDriver(
mojom::ElectronAutofillDriverAssociatedRequest(std::move(*handle)),
render_frame_host);
return true;
}
return false;
}
std::string AtomBrowserClient::GetApplicationLocale() {
if (BrowserThread::CurrentlyOn(BrowserThread::IO))
return g_io_thread_application_locale.Get();

View file

@ -183,6 +183,10 @@ class AtomBrowserClient : public content::ContentBrowserClient,
#if defined(OS_WIN)
bool PreSpawnRenderer(sandbox::TargetPolicy* policy) override;
#endif
bool BindAssociatedInterfaceRequestFromFrame(
content::RenderFrameHost* render_frame_host,
const std::string& interface_name,
mojo::ScopedInterfaceEndpointHandle* handle) override;
bool HandleExternalProtocol(
const GURL& url,

View file

@ -214,7 +214,6 @@ void CommonWebContentsDelegate::SetOwnerWindow(
NativeWindow* owner_window) {
if (owner_window) {
owner_window_ = owner_window->GetWeakPtr();
autofill_popup_.reset(new AutofillPopup());
NativeWindowRelay::CreateForWebContents(web_contents,
owner_window->GetWeakPtr());
} else {
@ -637,24 +636,4 @@ void CommonWebContentsDelegate::SetHtmlApiFullscreen(bool enter_fullscreen) {
native_fullscreen_ = false;
}
void CommonWebContentsDelegate::ShowAutofillPopup(
content::RenderFrameHost* frame_host,
content::RenderFrameHost* embedder_frame_host,
bool offscreen,
const gfx::RectF& bounds,
const std::vector<base::string16>& values,
const std::vector<base::string16>& labels) {
if (!owner_window())
return;
autofill_popup_->CreateView(frame_host, embedder_frame_host, offscreen,
owner_window()->content_view(), bounds);
autofill_popup_->SetItems(values, labels);
}
void CommonWebContentsDelegate::HideAutofillPopup() {
if (autofill_popup_)
autofill_popup_->Hide();
}
} // namespace electron

View file

@ -18,10 +18,6 @@
#include "shell/browser/ui/inspectable_web_contents_impl.h"
#include "shell/browser/ui/inspectable_web_contents_view_delegate.h"
#if defined(TOOLKIT_VIEWS)
#include "shell/browser/ui/autofill_popup.h"
#endif
namespace base {
class SequencedTaskRunner;
}
@ -107,15 +103,6 @@ class CommonWebContentsDelegate : public content::WebContentsDelegate,
content::WebContents* source,
const content::NativeWebKeyboardEvent& event) override;
// Autofill related events.
void ShowAutofillPopup(content::RenderFrameHost* frame_host,
content::RenderFrameHost* embedder_frame_host,
bool offscreen,
const gfx::RectF& bounds,
const std::vector<base::string16>& values,
const std::vector<base::string16>& labels);
void HideAutofillPopup();
// InspectableWebContentsDelegate:
void DevToolsSaveToFile(const std::string& url,
const std::string& content,
@ -176,9 +163,6 @@ class CommonWebContentsDelegate : public content::WebContentsDelegate,
bool native_fullscreen_ = false;
// UI related helper classes.
#if defined(TOOLKIT_VIEWS)
std::unique_ptr<AutofillPopup> autofill_popup_;
#endif
std::unique_ptr<WebDialogHelper> web_dialog_helper_;
scoped_refptr<DevToolsFileSystemIndexer> devtools_file_system_indexer_;

View file

@ -21,6 +21,11 @@ interface ElectronAutofillAgent {
AcceptDataListSuggestion(mojo_base.mojom.String16 value);
};
interface ElectronAutofillDriver {
ShowAutofillPopup(gfx.mojom.RectF bounds, array<mojo_base.mojom.String16> values, array<mojo_base.mojom.String16> labels);
HideAutofillPopup();
};
struct DraggableRegion {
bool draggable;
gfx.mojom.Rect bounds;
@ -71,8 +76,4 @@ interface ElectronBrowser {
[Sync]
DoGetZoomLevel() => (double result);
// TODO: move these into a separate interface
ShowAutofillPopup(gfx.mojom.RectF bounds, array<mojo_base.mojom.String16> values, array<mojo_base.mojom.String16> labels);
HideAutofillPopup();
};

View file

@ -9,7 +9,7 @@
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_view.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/platform/web_keyboard_event.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/web/web_document.h"
@ -182,7 +182,7 @@ bool AutofillAgent::IsUserGesture() const {
}
void AutofillAgent::HidePopup() {
GetElectronBrowser()->HideAutofillPopup();
GetAutofillDriver()->HideAutofillPopup();
}
void AutofillAgent::ShowPopup(const blink::WebFormControlElement& element,
@ -190,7 +190,7 @@ void AutofillAgent::ShowPopup(const blink::WebFormControlElement& element,
const std::vector<base::string16>& labels) {
gfx::RectF bounds =
render_frame()->GetRenderView()->ElementBoundsInWindow(element);
GetElectronBrowser()->ShowAutofillPopup(bounds, values, labels);
GetAutofillDriver()->ShowAutofillPopup(bounds, values, labels);
}
void AutofillAgent::AcceptDataListSuggestion(const base::string16& suggestion) {
@ -218,13 +218,14 @@ void AutofillAgent::DoFocusChangeComplete() {
focused_node_was_last_clicked_ = false;
}
const mojom::ElectronBrowserPtr& AutofillAgent::GetElectronBrowser() {
if (!browser_ptr_) {
render_frame()->GetRemoteInterfaces()->GetInterface(
mojo::MakeRequest(&browser_ptr_));
const mojom::ElectronAutofillDriverAssociatedPtr&
AutofillAgent::GetAutofillDriver() {
if (!autofill_driver_) {
render_frame()->GetRemoteAssociatedInterfaces()->GetInterface(
mojo::MakeRequest(&autofill_driver_));
}
return browser_ptr_;
return autofill_driver_;
}
} // namespace electron

View file

@ -67,8 +67,8 @@ class AutofillAgent : public content::RenderFrameObserver,
void DoFocusChangeComplete();
const mojom::ElectronBrowserPtr& GetElectronBrowser();
mojom::ElectronBrowserPtr browser_ptr_;
const mojom::ElectronAutofillDriverAssociatedPtr& GetAutofillDriver();
mojom::ElectronAutofillDriverAssociatedPtr autofill_driver_;
// True when the last click was on the focused node.
bool focused_node_was_last_clicked_ = false;