Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
43e6d7f
commit b33558d
Showing
14 changed files
with
329 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.