Skip to content

Commit

Permalink
re-implement renderer->browser calls with mojo
Browse files Browse the repository at this point in the history
  • Loading branch information
brenca committed Jun 12, 2019
1 parent ba96cdb commit 0b73967
Show file tree
Hide file tree
Showing 14 changed files with 379 additions and 98 deletions.
43 changes: 10 additions & 33 deletions atom/browser/api/atom_api_web_contents.cc
Expand Up @@ -12,6 +12,7 @@
#include "atom/browser/api/atom_api_browser_window.h"
#include "atom/browser/api/atom_api_debugger.h"
#include "atom/browser/api/atom_api_session.h"
#include "atom/browser/atom_autofill_driver_factory.h"
#include "atom/browser/atom_browser_client.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
Expand Down Expand Up @@ -409,6 +410,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);
Expand Down Expand Up @@ -569,7 +571,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_)
Expand Down Expand Up @@ -1092,26 +1100,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)
Expand Down Expand Up @@ -2012,17 +2000,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();
}

v8::Local<v8::Value> WebContents::GetPreloadPath(v8::Isolate* isolate) const {
if (auto* web_preferences = WebContentsPreferences::From(web_contents())) {
base::FilePath::StringType preload;
Expand Down Expand Up @@ -2074,7 +2051,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();
Expand Down
16 changes: 2 additions & 14 deletions atom/browser/api/atom_api_web_contents.h
Expand Up @@ -14,11 +14,11 @@
#include "atom/browser/api/save_page_handler.h"
#include "atom/browser/api/trackable_object.h"
#include "atom/browser/common_web_contents_delegate.h"
#include "atom/browser/ui/autofill_popup.h"
#include "base/observer_list.h"
#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"
Expand Down Expand Up @@ -301,7 +301,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);

Expand Down Expand Up @@ -464,13 +464,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;

Expand Down Expand Up @@ -509,11 +502,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);

Expand Down
97 changes: 97 additions & 0 deletions 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<base::string16>& values,
const std::vector<base::string16>& 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
44 changes: 44 additions & 0 deletions 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 <vector>

#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<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 atom

#endif // ATOM_BROWSER_ATOM_AUTOFILL_DRIVER_H_

0 comments on commit 0b73967

Please sign in to comment.