From 9201d6b6c33ed4dca900772f067fa089d8e8dadf Mon Sep 17 00:00:00 2001 From: Heilig Benedek Date: Thu, 13 Jun 2019 00:54:21 +0200 Subject: [PATCH 1/5] re-implement renderer->browser calls with mojo --- atom/browser/atom_autofill_driver.cc | 97 +++++++++++++ atom/browser/atom_autofill_driver.h | 44 ++++++ atom/browser/atom_autofill_driver_factory.cc | 129 ++++++++++++++++++ atom/browser/atom_autofill_driver_factory.h | 56 ++++++++ filenames.gni | 4 + shell/browser/api/atom_api_web_contents.cc | 32 ++--- shell/browser/api/atom_api_web_contents.h | 15 +- shell/browser/atom_browser_client.cc | 17 +++ shell/browser/atom_browser_client.h | 4 + shell/browser/common_web_contents_delegate.cc | 1 - shell/browser/common_web_contents_delegate.h | 12 -- shell/common/api/api.mojom | 9 +- shell/renderer/atom_autofill_agent.cc | 17 +-- shell/renderer/atom_autofill_agent.h | 4 +- 14 files changed, 379 insertions(+), 62 deletions(-) create mode 100644 atom/browser/atom_autofill_driver.cc create mode 100644 atom/browser/atom_autofill_driver.h create mode 100644 atom/browser/atom_autofill_driver_factory.cc create mode 100644 atom/browser/atom_autofill_driver_factory.h diff --git a/atom/browser/atom_autofill_driver.cc b/atom/browser/atom_autofill_driver.cc new file mode 100644 index 0000000000000..8c9e38c3db637 --- /dev/null +++ b/atom/browser/atom_autofill_driver.cc @@ -0,0 +1,97 @@ +// 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 "atom/browser/atom_autofill_driver.h" + +#include "atom/browser/api/atom_api_web_contents.h" +#include "atom/browser/native_window.h" +#include "content/public/browser/render_widget_host_view.h" + +namespace atom { + +namespace { + +const api::WebContents* WebContentsFromContentWebContents( + content::WebContents* web_contents) { + auto api_web_contents = + api::WebContents::From(v8::Isolate::GetCurrent(), web_contents); + if (!api_web_contents.IsEmpty()) { + return api_web_contents.get(); + } + + return nullptr; +} + +const api::WebContents* WebContentsFromRenderFrameHost( + content::RenderFrameHost* render_frame_host) { + content::WebContents* web_contents = + content::WebContents::FromRenderFrameHost(render_frame_host); + + if (web_contents) { + return WebContentsFromContentWebContents(web_contents); + } + + return nullptr; +} + +const api::WebContents* EmbedderFromWebContents( + const api::WebContents* web_contents) { + auto* embedder_web_contents = web_contents->HostWebContents(); + + if (embedder_web_contents) { + return WebContentsFromContentWebContents(embedder_web_contents); + } + + return nullptr; +} + +} // namespace + +AutofillDriver::AutofillDriver(content::RenderFrameHost* render_frame_host) + : render_frame_host_(render_frame_host), binding_(this) { + autofill_popup_.reset(new AutofillPopup()); +} + +AutofillDriver::~AutofillDriver() {} + +void AutofillDriver::BindRequest( + mojom::ElectronAutofillDriverAssociatedRequest request) { + binding_.Bind(std::move(request)); +} + +void AutofillDriver::ShowAutofillPopup( + const gfx::RectF& bounds, + const std::vector& values, + const std::vector& labels) { + auto* web_contents = WebContentsFromRenderFrameHost(render_frame_host_); + if (!web_contents || !web_contents->owner_window()) + return; + + auto* embedder = EmbedderFromWebContents(web_contents); + + 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 atom diff --git a/atom/browser/atom_autofill_driver.h b/atom/browser/atom_autofill_driver.h new file mode 100644 index 0000000000000..7af1426d21b72 --- /dev/null +++ b/atom/browser/atom_autofill_driver.h @@ -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 ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_H_ +#define ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_H_ + +#include + +#if defined(TOOLKIT_VIEWS) +#include "atom/browser/ui/autofill_popup.h" +#endif + +#include "atom/common/api/api.mojom.h" +#include "mojo/public/cpp/bindings/associated_binding.h" + +namespace atom { + +class AutofillDriver : public mojom::ElectronAutofillDriver { + public: + AutofillDriver(content::RenderFrameHost* render_frame_host); + + ~AutofillDriver() override; + + void BindRequest(mojom::ElectronAutofillDriverAssociatedRequest request); + + void ShowAutofillPopup(const gfx::RectF& bounds, + const std::vector& values, + const std::vector& labels) override; + void HideAutofillPopup() override; + + private: + content::RenderFrameHost* const render_frame_host_; + +#if defined(TOOLKIT_VIEWS) + std::unique_ptr autofill_popup_; +#endif + + mojo::AssociatedBinding binding_; +}; + +} // namespace atom + +#endif // ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_H_ diff --git a/atom/browser/atom_autofill_driver_factory.cc b/atom/browser/atom_autofill_driver_factory.cc new file mode 100644 index 0000000000000..8c6ef9bf72b2c --- /dev/null +++ b/atom/browser/atom_autofill_driver_factory.cc @@ -0,0 +1,129 @@ +// 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 "atom/browser/atom_autofill_driver_factory.h" + +#include "atom/browser/atom_autofill_driver.h" +#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" + +namespace atom { + +namespace { + +std::unique_ptr CreateDriver( + content::RenderFrameHost* render_frame_host) { + return std::make_unique(render_frame_host); +} + +} // namespace + +const char + AutofillDriverFactory::kAtomAutofillDriverFactoryWebContentsUserDataKey[] = + "atom_web_contents_autofill_driver_factory"; + +AutofillDriverFactory::~AutofillDriverFactory() {} + +// static +void AutofillDriverFactory::CreateForWebContents( + content::WebContents* contents) { + if (FromWebContents(contents)) + return; + + auto new_factory = std::make_unique(contents); + const std::vector frames = + contents->GetAllFrames(); + for (content::RenderFrameHost* frame : frames) { + if (frame->IsRenderFrameLive()) + new_factory->RenderFrameCreated(frame); + } + + contents->SetUserData(kAtomAutofillDriverFactoryWebContentsUserDataKey, + std::move(new_factory)); +} + +// static +AutofillDriverFactory* AutofillDriverFactory::FromWebContents( + content::WebContents* contents) { + return static_cast( + contents->GetUserData(kAtomAutofillDriverFactoryWebContentsUserDataKey)); +} + +// 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) + driver->BindRequest(std::move(request)); +} + +AutofillDriverFactory::AutofillDriverFactory(content::WebContents* web_contents) + : content::WebContentsObserver(web_contents) {} + +void AutofillDriverFactory::RenderFrameCreated( + content::RenderFrameHost* render_frame_host) { + AddDriverForFrame(render_frame_host, + base::Bind(CreateDriver, render_frame_host)); +} + +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, + base::Callback()> 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 = 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(); + } +} + +} // namespace atom diff --git a/atom/browser/atom_autofill_driver_factory.h b/atom/browser/atom_autofill_driver_factory.h new file mode 100644 index 0000000000000..3feadf7b8da17 --- /dev/null +++ b/atom/browser/atom_autofill_driver_factory.h @@ -0,0 +1,56 @@ +// 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 ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_ +#define ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_ + +#include + +#include "base/callback_forward.h" +#include "base/supports_user_data.h" +#include "content/public/browser/web_contents_observer.h" +#include "electron/atom/common/api/api.mojom.h" + +namespace atom { + +class AutofillDriver; + +class AutofillDriverFactory : public content::WebContentsObserver, + public base::SupportsUserData::Data { + public: + AutofillDriverFactory(content::WebContents* web_contents); + + ~AutofillDriverFactory() override; + + static void CreateForWebContents(content::WebContents* contents); + + static AutofillDriverFactory* FromWebContents(content::WebContents* contents); + static void BindAutofillDriver( + mojom::ElectronAutofillDriverAssociatedRequest request, + content::RenderFrameHost* render_frame_host); + + // content::WebContentsObserver: + void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override; + 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, + base::Callback()> factory_method); + void DeleteDriverForFrame(content::RenderFrameHost* render_frame_host); + + void CloseAllPopups(); + + static const char kAtomAutofillDriverFactoryWebContentsUserDataKey[]; + + private: + std::unordered_map> + driver_map_; +}; + +} // namespace atom + +#endif // ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_ diff --git a/filenames.gni b/filenames.gni index 589f6b8866821..228df4d4cb726 100644 --- a/filenames.gni +++ b/filenames.gni @@ -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", diff --git a/shell/browser/api/atom_api_web_contents.cc b/shell/browser/api/atom_api_web_contents.cc index 66644b7ea587f..3cfa96b6124fd 100644 --- a/shell/browser/api/atom_api_web_contents.cc +++ b/shell/browser/api/atom_api_web_contents.cc @@ -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))); + atom::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& values, - const std::vector& 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) @@ -2279,7 +2267,7 @@ v8::Local WebContents::Session(v8::Isolate* isolate) { return v8::Local::New(isolate, session_); } -content::WebContents* WebContents::HostWebContents() { +content::WebContents* WebContents::HostWebContents() const { if (!embedder_) return nullptr; return embedder_->web_contents(); diff --git a/shell/browser/api/atom_api_web_contents.h b/shell/browser/api/atom_api_web_contents.h index c9067a4b6b238..1a43c48552f23 100644 --- a/shell/browser/api/atom_api_web_contents.h +++ b/shell/browser/api/atom_api_web_contents.h @@ -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" @@ -305,7 +306,7 @@ class WebContents : public mate::TrackableObject, // Properties. int32_t ID() const; v8::Local Session(v8::Isolate* isolate); - content::WebContents* HostWebContents(); + content::WebContents* HostWebContents() const; v8::Local DevToolsWebContents(v8::Isolate* isolate); v8::Local Debugger(v8::Isolate* isolate); @@ -469,13 +470,6 @@ class WebContents : public mate::TrackableObject, void DevToolsOpened() override; void DevToolsClosed() override; -#if defined(TOOLKIT_VIEWS) - void ShowAutofillPopup(content::RenderFrameHost* frame_host, - const gfx::RectF& bounds, - const std::vector& values, - const std::vector& labels); -#endif - private: AtomBrowserContext* GetBrowserContext() const; @@ -514,11 +508,6 @@ class WebContents : public mate::TrackableObject, void SetTemporaryZoomLevel(double level) override; void DoGetZoomLevel(DoGetZoomLevelCallback callback) override; - void ShowAutofillPopup(const gfx::RectF& bounds, - const std::vector& values, - const std::vector& labels) override; - void HideAutofillPopup() override; - // Called when we receive a CursorChange message from chromium. void OnCursorChange(const content::WebCursor& cursor); diff --git a/shell/browser/atom_browser_client.cc b/shell/browser/atom_browser_client.cc index e2056ac8926ad..d13c1c1e9e1cc 100644 --- a/shell/browser/atom_browser_client.cc +++ b/shell/browser/atom_browser_client.cc @@ -39,6 +39,7 @@ #include "content/public/common/service_names.mojom.h" #include "content/public/common/url_constants.h" #include "content/public/common/web_preferences.h" +#include "electron/atom/common/api/api.mojom.h" #include "electron/buildflags/buildflags.h" #include "electron/grit/electron_resources.h" #include "net/base/escape.h" @@ -53,6 +54,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" @@ -1049,6 +1051,21 @@ 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 == atom::mojom::ElectronAutofillDriver::Name_) { + atom::AutofillDriverFactory::BindAutofillDriver( + atom::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(); diff --git a/shell/browser/atom_browser_client.h b/shell/browser/atom_browser_client.h index 3eca46f7f9a2a..8580ac328158f 100644 --- a/shell/browser/atom_browser_client.h +++ b/shell/browser/atom_browser_client.h @@ -186,6 +186,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, diff --git a/shell/browser/common_web_contents_delegate.cc b/shell/browser/common_web_contents_delegate.cc index d7b774ef1a492..989277eed2765 100644 --- a/shell/browser/common_web_contents_delegate.cc +++ b/shell/browser/common_web_contents_delegate.cc @@ -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 { diff --git a/shell/browser/common_web_contents_delegate.h b/shell/browser/common_web_contents_delegate.h index 245655d14daab..a1bbdc8c68d8e 100644 --- a/shell/browser/common_web_contents_delegate.h +++ b/shell/browser/common_web_contents_delegate.h @@ -107,15 +107,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& values, - const std::vector& labels); - void HideAutofillPopup(); - // InspectableWebContentsDelegate: void DevToolsSaveToFile(const std::string& url, const std::string& content, @@ -176,9 +167,6 @@ class CommonWebContentsDelegate : public content::WebContentsDelegate, bool native_fullscreen_ = false; // UI related helper classes. -#if defined(TOOLKIT_VIEWS) - std::unique_ptr autofill_popup_; -#endif std::unique_ptr web_dialog_helper_; scoped_refptr devtools_file_system_indexer_; diff --git a/shell/common/api/api.mojom b/shell/common/api/api.mojom index a549fad71d197..1ea6345399ef5 100644 --- a/shell/common/api/api.mojom +++ b/shell/common/api/api.mojom @@ -21,6 +21,11 @@ interface ElectronAutofillAgent { AcceptDataListSuggestion(mojo_base.mojom.String16 value); }; +interface ElectronAutofillDriver { + ShowAutofillPopup(gfx.mojom.RectF bounds, array values, array 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 values, array labels); - HideAutofillPopup(); }; diff --git a/shell/renderer/atom_autofill_agent.cc b/shell/renderer/atom_autofill_agent.cc index e111ea68df51b..4a158df530984 100644 --- a/shell/renderer/atom_autofill_agent.cc +++ b/shell/renderer/atom_autofill_agent.cc @@ -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& 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 diff --git a/shell/renderer/atom_autofill_agent.h b/shell/renderer/atom_autofill_agent.h index e172cbaba622d..81026ae7c18e0 100644 --- a/shell/renderer/atom_autofill_agent.h +++ b/shell/renderer/atom_autofill_agent.h @@ -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; From 8774cf35d0ad6d1b4edf2425bc2e3d3434d94976 Mon Sep 17 00:00:00 2001 From: Heilig Benedek Date: Thu, 13 Jun 2019 01:11:03 +0200 Subject: [PATCH 2/5] lint --- atom/browser/atom_autofill_driver.cc | 2 ++ atom/browser/atom_autofill_driver.h | 4 ++-- atom/browser/atom_autofill_driver_factory.cc | 4 ++++ atom/browser/atom_autofill_driver_factory.h | 3 ++- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/atom/browser/atom_autofill_driver.cc b/atom/browser/atom_autofill_driver.cc index 8c9e38c3db637..1571cc7e6e1fc 100644 --- a/atom/browser/atom_autofill_driver.cc +++ b/atom/browser/atom_autofill_driver.cc @@ -4,6 +4,8 @@ #include "atom/browser/atom_autofill_driver.h" +#include + #include "atom/browser/api/atom_api_web_contents.h" #include "atom/browser/native_window.h" #include "content/public/browser/render_widget_host_view.h" diff --git a/atom/browser/atom_autofill_driver.h b/atom/browser/atom_autofill_driver.h index 7af1426d21b72..11f86c8fb9b75 100644 --- a/atom/browser/atom_autofill_driver.h +++ b/atom/browser/atom_autofill_driver.h @@ -5,7 +5,7 @@ #ifndef ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_H_ #define ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_H_ -#include +#include #if defined(TOOLKIT_VIEWS) #include "atom/browser/ui/autofill_popup.h" @@ -18,7 +18,7 @@ namespace atom { class AutofillDriver : public mojom::ElectronAutofillDriver { public: - AutofillDriver(content::RenderFrameHost* render_frame_host); + explicit AutofillDriver(content::RenderFrameHost* render_frame_host); ~AutofillDriver() override; diff --git a/atom/browser/atom_autofill_driver_factory.cc b/atom/browser/atom_autofill_driver_factory.cc index 8c6ef9bf72b2c..447a55387e89b 100644 --- a/atom/browser/atom_autofill_driver_factory.cc +++ b/atom/browser/atom_autofill_driver_factory.cc @@ -4,6 +4,10 @@ #include "atom/browser/atom_autofill_driver_factory.h" +#include +#include +#include + #include "atom/browser/atom_autofill_driver.h" #include "base/bind.h" #include "base/callback.h" diff --git a/atom/browser/atom_autofill_driver_factory.h b/atom/browser/atom_autofill_driver_factory.h index 3feadf7b8da17..b7ed467fbb1ec 100644 --- a/atom/browser/atom_autofill_driver_factory.h +++ b/atom/browser/atom_autofill_driver_factory.h @@ -5,6 +5,7 @@ #ifndef ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_ #define ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_ +#include #include #include "base/callback_forward.h" @@ -19,7 +20,7 @@ class AutofillDriver; class AutofillDriverFactory : public content::WebContentsObserver, public base::SupportsUserData::Data { public: - AutofillDriverFactory(content::WebContents* web_contents); + explicit AutofillDriverFactory(content::WebContents* web_contents); ~AutofillDriverFactory() override; From 9d5251e836790d2e5243aad159b089f6c3654432 Mon Sep 17 00:00:00 2001 From: Heilig Benedek Date: Sun, 11 Aug 2019 02:06:12 +0200 Subject: [PATCH 3/5] fix: clean up after rebase --- shell/browser/api/atom_api_web_contents.cc | 13 +---- shell/browser/api/atom_api_web_contents.h | 3 +- .../browser/atom_autofill_driver.cc | 56 ++++--------------- .../browser/atom_autofill_driver.h | 8 +-- .../browser/atom_autofill_driver_factory.cc | 48 +++++----------- .../browser/atom_autofill_driver_factory.h | 23 ++++---- shell/browser/atom_browser_client.cc | 9 ++- shell/browser/common_web_contents_delegate.cc | 20 ------- shell/browser/common_web_contents_delegate.h | 4 -- 9 files changed, 47 insertions(+), 137 deletions(-) rename {atom => shell}/browser/atom_autofill_driver.cc (56%) rename {atom => shell}/browser/atom_autofill_driver.h (86%) rename {atom => shell}/browser/atom_autofill_driver_factory.cc (73%) rename {atom => shell}/browser/atom_autofill_driver_factory.h (73%) diff --git a/shell/browser/api/atom_api_web_contents.cc b/shell/browser/api/atom_api_web_contents.cc index 3cfa96b6124fd..91c501eb24856 100644 --- a/shell/browser/api/atom_api_web_contents.cc +++ b/shell/browser/api/atom_api_web_contents.cc @@ -472,7 +472,7 @@ void WebContents::InitWithSessionAndOptions( base::Unretained(this))); bindings_.set_connection_error_handler(base::BindRepeating( &WebContents::OnElectronBrowserConnectionError, base::Unretained(this))); - atom::AutofillDriverFactory::CreateForWebContents(web_contents()); + AutofillDriverFactory::CreateForWebContents(web_contents()); web_contents()->SetUserAgentOverride(GetBrowserContext()->GetUserAgent(), false); @@ -2202,17 +2202,6 @@ void WebContents::DoGetZoomLevel(DoGetZoomLevelCallback callback) { std::move(callback).Run(GetZoomLevel()); } -void WebContents::ShowAutofillPopup(const gfx::RectF& bounds, - const std::vector& values, - const std::vector& labels) { - content::RenderFrameHost* frame_host = bindings_.dispatch_context(); - ShowAutofillPopup(frame_host, bounds, values, labels); -} - -void WebContents::HideAutofillPopup() { - CommonWebContentsDelegate::HideAutofillPopup(); -} - std::vector WebContents::GetPreloadPaths() const { auto result = SessionPreferences::GetValidPreloads(GetBrowserContext()); diff --git a/shell/browser/api/atom_api_web_contents.h b/shell/browser/api/atom_api_web_contents.h index 1a43c48552f23..6ad3bf5663ab4 100644 --- a/shell/browser/api/atom_api_web_contents.h +++ b/shell/browser/api/atom_api_web_contents.h @@ -28,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) @@ -324,6 +323,8 @@ class WebContents : public mate::TrackableObject, 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); diff --git a/atom/browser/atom_autofill_driver.cc b/shell/browser/atom_autofill_driver.cc similarity index 56% rename from atom/browser/atom_autofill_driver.cc rename to shell/browser/atom_autofill_driver.cc index 1571cc7e6e1fc..78e7e82ff05fa 100644 --- a/atom/browser/atom_autofill_driver.cc +++ b/shell/browser/atom_autofill_driver.cc @@ -2,53 +2,15 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#include "atom/browser/atom_autofill_driver.h" +#include "shell/browser/atom_autofill_driver.h" #include -#include "atom/browser/api/atom_api_web_contents.h" -#include "atom/browser/native_window.h" #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 atom { - -namespace { - -const api::WebContents* WebContentsFromContentWebContents( - content::WebContents* web_contents) { - auto api_web_contents = - api::WebContents::From(v8::Isolate::GetCurrent(), web_contents); - if (!api_web_contents.IsEmpty()) { - return api_web_contents.get(); - } - - return nullptr; -} - -const api::WebContents* WebContentsFromRenderFrameHost( - content::RenderFrameHost* render_frame_host) { - content::WebContents* web_contents = - content::WebContents::FromRenderFrameHost(render_frame_host); - - if (web_contents) { - return WebContentsFromContentWebContents(web_contents); - } - - return nullptr; -} - -const api::WebContents* EmbedderFromWebContents( - const api::WebContents* web_contents) { - auto* embedder_web_contents = web_contents->HostWebContents(); - - if (embedder_web_contents) { - return WebContentsFromContentWebContents(embedder_web_contents); - } - - return nullptr; -} - -} // namespace +namespace electron { AutofillDriver::AutofillDriver(content::RenderFrameHost* render_frame_host) : render_frame_host_(render_frame_host), binding_(this) { @@ -66,11 +28,15 @@ void AutofillDriver::ShowAutofillPopup( const gfx::RectF& bounds, const std::vector& values, const std::vector& labels) { - auto* web_contents = WebContentsFromRenderFrameHost(render_frame_host_); + 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 = EmbedderFromWebContents(web_contents); + auto* embedder = web_contents->embedder(); bool osr = web_contents->IsOffScreen() || (embedder && embedder->IsOffScreen()); @@ -96,4 +62,4 @@ void AutofillDriver::HideAutofillPopup() { autofill_popup_->Hide(); } -} // namespace atom +} // namespace electron diff --git a/atom/browser/atom_autofill_driver.h b/shell/browser/atom_autofill_driver.h similarity index 86% rename from atom/browser/atom_autofill_driver.h rename to shell/browser/atom_autofill_driver.h index 11f86c8fb9b75..ec8eebaeac3bd 100644 --- a/atom/browser/atom_autofill_driver.h +++ b/shell/browser/atom_autofill_driver.h @@ -8,13 +8,13 @@ #include #if defined(TOOLKIT_VIEWS) -#include "atom/browser/ui/autofill_popup.h" +#include "shell/browser/ui/autofill_popup.h" #endif -#include "atom/common/api/api.mojom.h" #include "mojo/public/cpp/bindings/associated_binding.h" +#include "shell/common/api/api.mojom.h" -namespace atom { +namespace electron { class AutofillDriver : public mojom::ElectronAutofillDriver { public: @@ -39,6 +39,6 @@ class AutofillDriver : public mojom::ElectronAutofillDriver { mojo::AssociatedBinding binding_; }; -} // namespace atom +} // namespace electron #endif // ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_H_ diff --git a/atom/browser/atom_autofill_driver_factory.cc b/shell/browser/atom_autofill_driver_factory.cc similarity index 73% rename from atom/browser/atom_autofill_driver_factory.cc rename to shell/browser/atom_autofill_driver_factory.cc index 447a55387e89b..d922b925c52a3 100644 --- a/atom/browser/atom_autofill_driver_factory.cc +++ b/shell/browser/atom_autofill_driver_factory.cc @@ -2,20 +2,20 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#include "atom/browser/atom_autofill_driver_factory.h" +#include "shell/browser/atom_autofill_driver_factory.h" #include #include #include -#include "atom/browser/atom_autofill_driver.h" #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 atom { +namespace electron { namespace { @@ -26,37 +26,8 @@ std::unique_ptr CreateDriver( } // namespace -const char - AutofillDriverFactory::kAtomAutofillDriverFactoryWebContentsUserDataKey[] = - "atom_web_contents_autofill_driver_factory"; - AutofillDriverFactory::~AutofillDriverFactory() {} -// static -void AutofillDriverFactory::CreateForWebContents( - content::WebContents* contents) { - if (FromWebContents(contents)) - return; - - auto new_factory = std::make_unique(contents); - const std::vector frames = - contents->GetAllFrames(); - for (content::RenderFrameHost* frame : frames) { - if (frame->IsRenderFrameLive()) - new_factory->RenderFrameCreated(frame); - } - - contents->SetUserData(kAtomAutofillDriverFactoryWebContentsUserDataKey, - std::move(new_factory)); -} - -// static -AutofillDriverFactory* AutofillDriverFactory::FromWebContents( - content::WebContents* contents) { - return static_cast( - contents->GetUserData(kAtomAutofillDriverFactoryWebContentsUserDataKey)); -} - // static void AutofillDriverFactory::BindAutofillDriver( mojom::ElectronAutofillDriverAssociatedRequest request, @@ -77,7 +48,14 @@ void AutofillDriverFactory::BindAutofillDriver( } AutofillDriverFactory::AutofillDriverFactory(content::WebContents* web_contents) - : content::WebContentsObserver(web_contents) {} + : content::WebContentsObserver(web_contents) { + const std::vector frames = + web_contents->GetAllFrames(); + for (content::RenderFrameHost* frame : frames) { + if (frame->IsRenderFrameLive()) + RenderFrameCreated(frame); + } +} void AutofillDriverFactory::RenderFrameCreated( content::RenderFrameHost* render_frame_host) { @@ -130,4 +108,6 @@ void AutofillDriverFactory::CloseAllPopups() { } } -} // namespace atom +WEB_CONTENTS_USER_DATA_KEY_IMPL(AutofillDriverFactory) + +} // namespace electron diff --git a/atom/browser/atom_autofill_driver_factory.h b/shell/browser/atom_autofill_driver_factory.h similarity index 73% rename from atom/browser/atom_autofill_driver_factory.h rename to shell/browser/atom_autofill_driver_factory.h index b7ed467fbb1ec..7bfac379246bd 100644 --- a/atom/browser/atom_autofill_driver_factory.h +++ b/shell/browser/atom_autofill_driver_factory.h @@ -9,24 +9,20 @@ #include #include "base/callback_forward.h" -#include "base/supports_user_data.h" #include "content/public/browser/web_contents_observer.h" -#include "electron/atom/common/api/api.mojom.h" +#include "content/public/browser/web_contents_user_data.h" +#include "shell/common/api/api.mojom.h" -namespace atom { +namespace electron { class AutofillDriver; -class AutofillDriverFactory : public content::WebContentsObserver, - public base::SupportsUserData::Data { +class AutofillDriverFactory + : public content::WebContentsObserver, + public content::WebContentsUserData { public: - explicit AutofillDriverFactory(content::WebContents* web_contents); - ~AutofillDriverFactory() override; - static void CreateForWebContents(content::WebContents* contents); - - static AutofillDriverFactory* FromWebContents(content::WebContents* contents); static void BindAutofillDriver( mojom::ElectronAutofillDriverAssociatedRequest request, content::RenderFrameHost* render_frame_host); @@ -45,13 +41,16 @@ class AutofillDriverFactory : public content::WebContentsObserver, void CloseAllPopups(); - static const char kAtomAutofillDriverFactoryWebContentsUserDataKey[]; + WEB_CONTENTS_USER_DATA_KEY_DECL(); private: + explicit AutofillDriverFactory(content::WebContents* web_contents); + friend class content::WebContentsUserData; + std::unordered_map> driver_map_; }; -} // namespace atom +} // namespace electron #endif // ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_ diff --git a/shell/browser/atom_browser_client.cc b/shell/browser/atom_browser_client.cc index d13c1c1e9e1cc..3858e906cbd46 100644 --- a/shell/browser/atom_browser_client.cc +++ b/shell/browser/atom_browser_client.cc @@ -39,7 +39,6 @@ #include "content/public/common/service_names.mojom.h" #include "content/public/common/url_constants.h" #include "content/public/common/web_preferences.h" -#include "electron/atom/common/api/api.mojom.h" #include "electron/buildflags/buildflags.h" #include "electron/grit/electron_resources.h" #include "net/base/escape.h" @@ -76,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" @@ -1055,10 +1055,9 @@ bool AtomBrowserClient::BindAssociatedInterfaceRequestFromFrame( content::RenderFrameHost* render_frame_host, const std::string& interface_name, mojo::ScopedInterfaceEndpointHandle* handle) { - if (interface_name == atom::mojom::ElectronAutofillDriver::Name_) { - atom::AutofillDriverFactory::BindAutofillDriver( - atom::mojom::ElectronAutofillDriverAssociatedRequest( - std::move(*handle)), + if (interface_name == mojom::ElectronAutofillDriver::Name_) { + AutofillDriverFactory::BindAutofillDriver( + mojom::ElectronAutofillDriverAssociatedRequest(std::move(*handle)), render_frame_host); return true; } diff --git a/shell/browser/common_web_contents_delegate.cc b/shell/browser/common_web_contents_delegate.cc index 989277eed2765..21e9e5cdd2cca 100644 --- a/shell/browser/common_web_contents_delegate.cc +++ b/shell/browser/common_web_contents_delegate.cc @@ -636,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& values, - const std::vector& 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 diff --git a/shell/browser/common_web_contents_delegate.h b/shell/browser/common_web_contents_delegate.h index a1bbdc8c68d8e..a583f8f84c462 100644 --- a/shell/browser/common_web_contents_delegate.h +++ b/shell/browser/common_web_contents_delegate.h @@ -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; } From 0ac0bd245050e73dba5fefb629cf2722b3467cf7 Mon Sep 17 00:00:00 2001 From: Heilig Benedek Date: Sun, 11 Aug 2019 13:48:48 +0200 Subject: [PATCH 4/5] lint --- shell/browser/atom_autofill_driver.h | 7 ++++--- shell/browser/atom_autofill_driver_factory.h | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/shell/browser/atom_autofill_driver.h b/shell/browser/atom_autofill_driver.h index ec8eebaeac3bd..8be0afd14a3e2 100644 --- a/shell/browser/atom_autofill_driver.h +++ b/shell/browser/atom_autofill_driver.h @@ -2,10 +2,11 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#ifndef ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_H_ -#define ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_H_ +#ifndef SHELL_BROWSER_ATOM_AUTOFILL_DRIVER_H_ +#define SHELL_BROWSER_ATOM_AUTOFILL_DRIVER_H_ #include +#include #if defined(TOOLKIT_VIEWS) #include "shell/browser/ui/autofill_popup.h" @@ -41,4 +42,4 @@ class AutofillDriver : public mojom::ElectronAutofillDriver { } // namespace electron -#endif // ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_H_ +#endif // SHELL_BROWSER_ATOM_AUTOFILL_DRIVER_H_ diff --git a/shell/browser/atom_autofill_driver_factory.h b/shell/browser/atom_autofill_driver_factory.h index 7bfac379246bd..3aeab5b4da416 100644 --- a/shell/browser/atom_autofill_driver_factory.h +++ b/shell/browser/atom_autofill_driver_factory.h @@ -2,8 +2,8 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#ifndef ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_ -#define ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_ +#ifndef SHELL_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_ +#define SHELL_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_ #include #include @@ -53,4 +53,4 @@ class AutofillDriverFactory } // namespace electron -#endif // ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_ +#endif // SHELL_BROWSER_ATOM_AUTOFILL_DRIVER_FACTORY_H_ From a79b94d5ec0fb3a4bfdabb6d7fe700f53ecc2b3d Mon Sep 17 00:00:00 2001 From: Heilig Benedek Date: Wed, 14 Aug 2019 01:23:20 +0200 Subject: [PATCH 5/5] fix: lazy load autofill drivers --- shell/browser/atom_autofill_driver.cc | 10 ++++----- shell/browser/atom_autofill_driver.h | 5 ++--- shell/browser/atom_autofill_driver_factory.cc | 22 +++++++++---------- shell/browser/atom_autofill_driver_factory.h | 9 ++++---- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/shell/browser/atom_autofill_driver.cc b/shell/browser/atom_autofill_driver.cc index 78e7e82ff05fa..dd6bedf4d41ec 100644 --- a/shell/browser/atom_autofill_driver.cc +++ b/shell/browser/atom_autofill_driver.cc @@ -12,18 +12,16 @@ namespace electron { -AutofillDriver::AutofillDriver(content::RenderFrameHost* render_frame_host) +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::BindRequest( - mojom::ElectronAutofillDriverAssociatedRequest request) { - binding_.Bind(std::move(request)); -} - void AutofillDriver::ShowAutofillPopup( const gfx::RectF& bounds, const std::vector& values, diff --git a/shell/browser/atom_autofill_driver.h b/shell/browser/atom_autofill_driver.h index 8be0afd14a3e2..b30521491d6b6 100644 --- a/shell/browser/atom_autofill_driver.h +++ b/shell/browser/atom_autofill_driver.h @@ -19,12 +19,11 @@ namespace electron { class AutofillDriver : public mojom::ElectronAutofillDriver { public: - explicit AutofillDriver(content::RenderFrameHost* render_frame_host); + AutofillDriver(content::RenderFrameHost* render_frame_host, + mojom::ElectronAutofillDriverAssociatedRequest request); ~AutofillDriver() override; - void BindRequest(mojom::ElectronAutofillDriverAssociatedRequest request); - void ShowAutofillPopup(const gfx::RectF& bounds, const std::vector& values, const std::vector& labels) override; diff --git a/shell/browser/atom_autofill_driver_factory.cc b/shell/browser/atom_autofill_driver_factory.cc index d922b925c52a3..bcac60d8c4beb 100644 --- a/shell/browser/atom_autofill_driver_factory.cc +++ b/shell/browser/atom_autofill_driver_factory.cc @@ -20,8 +20,10 @@ namespace electron { namespace { std::unique_ptr CreateDriver( - content::RenderFrameHost* render_frame_host) { - return std::make_unique(render_frame_host); + content::RenderFrameHost* render_frame_host, + mojom::ElectronAutofillDriverAssociatedRequest request) { + return std::make_unique(render_frame_host, + std::move(request)); } } // namespace @@ -43,8 +45,10 @@ void AutofillDriverFactory::BindAutofillDriver( return; AutofillDriver* driver = factory->DriverForFrame(render_frame_host); - if (driver) - driver->BindRequest(std::move(request)); + if (!driver) + factory->AddDriverForFrame( + render_frame_host, + base::BindOnce(CreateDriver, render_frame_host, std::move(request))); } AutofillDriverFactory::AutofillDriverFactory(content::WebContents* web_contents) @@ -57,12 +61,6 @@ AutofillDriverFactory::AutofillDriverFactory(content::WebContents* web_contents) } } -void AutofillDriverFactory::RenderFrameCreated( - content::RenderFrameHost* render_frame_host) { - AddDriverForFrame(render_frame_host, - base::Bind(CreateDriver, render_frame_host)); -} - void AutofillDriverFactory::RenderFrameDeleted( content::RenderFrameHost* render_frame_host) { DeleteDriverForFrame(render_frame_host); @@ -88,12 +86,12 @@ AutofillDriver* AutofillDriverFactory::DriverForFrame( void AutofillDriverFactory::AddDriverForFrame( content::RenderFrameHost* render_frame_host, - base::Callback()> factory_method) { + 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 = factory_method.Run(); + insertion_result.first->second = std::move(factory_method).Run(); } } diff --git a/shell/browser/atom_autofill_driver_factory.h b/shell/browser/atom_autofill_driver_factory.h index 3aeab5b4da416..bcf0b4a29cacc 100644 --- a/shell/browser/atom_autofill_driver_factory.h +++ b/shell/browser/atom_autofill_driver_factory.h @@ -21,6 +21,9 @@ class AutofillDriverFactory : public content::WebContentsObserver, public content::WebContentsUserData { public: + typedef base::OnceCallback()> + CreationCallback; + ~AutofillDriverFactory() override; static void BindAutofillDriver( @@ -28,15 +31,13 @@ class AutofillDriverFactory content::RenderFrameHost* render_frame_host); // content::WebContentsObserver: - void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override; 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, - base::Callback()> factory_method); + void AddDriverForFrame(content::RenderFrameHost* render_frame_host, + CreationCallback factory_method); void DeleteDriverForFrame(content::RenderFrameHost* render_frame_host); void CloseAllPopups();