2020-08-27 17:31:51 +03:00
|
|
|
// 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/electron_autofill_driver.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "content/public/browser/render_widget_host_view.h"
|
|
|
|
#include "shell/browser/api/electron_api_web_contents.h"
|
|
|
|
#include "shell/browser/javascript_environment.h"
|
2023-06-06 11:21:42 +03:00
|
|
|
#include "shell/browser/native_browser_view.h"
|
2020-08-27 17:31:51 +03:00
|
|
|
#include "shell/browser/native_window.h"
|
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
2021-11-03 20:17:06 +03:00
|
|
|
AutofillDriver::AutofillDriver(content::RenderFrameHost* render_frame_host)
|
|
|
|
: render_frame_host_(render_frame_host) {
|
2020-08-27 17:31:51 +03:00
|
|
|
autofill_popup_ = std::make_unique<AutofillPopup>();
|
2022-06-28 11:08:55 +03:00
|
|
|
}
|
2020-08-27 17:31:51 +03:00
|
|
|
|
|
|
|
AutofillDriver::~AutofillDriver() = default;
|
|
|
|
|
2021-11-03 20:17:06 +03:00
|
|
|
void AutofillDriver::BindPendingReceiver(
|
|
|
|
mojo::PendingAssociatedReceiver<mojom::ElectronAutofillDriver>
|
|
|
|
pending_receiver) {
|
|
|
|
receiver_.Bind(std::move(pending_receiver));
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:31:51 +03:00
|
|
|
void AutofillDriver::ShowAutofillPopup(
|
|
|
|
const gfx::RectF& bounds,
|
2021-03-16 19:18:45 +03:00
|
|
|
const std::vector<std::u16string>& values,
|
|
|
|
const std::vector<std::u16string>& labels) {
|
2020-08-27 17:31:51 +03:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
auto* web_contents = api::WebContents::From(
|
|
|
|
content::WebContents::FromRenderFrameHost(render_frame_host_));
|
2023-06-06 11:21:42 +03:00
|
|
|
if (!web_contents)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto* owner_window = web_contents->owner_window();
|
|
|
|
if (!owner_window)
|
2020-08-27 17:31:51 +03:00
|
|
|
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) {
|
2022-06-27 23:50:08 +03:00
|
|
|
auto* embedder_view =
|
|
|
|
embedder->web_contents()->GetPrimaryMainFrame()->GetView();
|
|
|
|
auto* view = web_contents->web_contents()->GetPrimaryMainFrame()->GetView();
|
2020-08-27 17:31:51 +03:00
|
|
|
auto offset = view->GetViewBounds().origin() -
|
|
|
|
embedder_view->GetViewBounds().origin();
|
|
|
|
popup_bounds.Offset(offset);
|
2022-06-27 23:50:08 +03:00
|
|
|
embedder_frame_host = embedder->web_contents()->GetPrimaryMainFrame();
|
2020-08-27 17:31:51 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 11:21:42 +03:00
|
|
|
// Ensure that if the WebContents belongs to a BrowserView,
|
|
|
|
// the popup is positioned relative to the BrowserView's bounds.
|
|
|
|
for (NativeBrowserView* bv : owner_window->browser_views()) {
|
|
|
|
auto* iwc = bv->GetInspectableWebContents();
|
|
|
|
if (!iwc)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto* awc = api::WebContents::From(iwc->GetWebContents());
|
|
|
|
if (awc == web_contents) {
|
|
|
|
auto bv_origin = bv->GetBounds().origin();
|
|
|
|
popup_bounds.Offset(gfx::Vector2dF(bv_origin.x(), bv_origin.y()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:31:51 +03:00
|
|
|
autofill_popup_->CreateView(render_frame_host_, embedder_frame_host, osr,
|
2023-06-06 11:21:42 +03:00
|
|
|
owner_window->content_view(), popup_bounds);
|
2020-08-27 17:31:51 +03:00
|
|
|
autofill_popup_->SetItems(values, labels);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutofillDriver::HideAutofillPopup() {
|
|
|
|
if (autofill_popup_)
|
|
|
|
autofill_popup_->Hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace electron
|