2020-08-27 14:31:51 +00: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_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/electron_autofill_driver.h"
|
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
AutofillDriverFactory::~AutofillDriverFactory() = default;
|
|
|
|
|
|
|
|
// static
|
|
|
|
void AutofillDriverFactory::BindAutofillDriver(
|
2021-11-03 17:17:06 +00:00
|
|
|
mojo::PendingAssociatedReceiver<mojom::ElectronAutofillDriver>
|
|
|
|
pending_receiver,
|
2020-08-27 14:31:51 +00:00
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2021-11-03 17:17:06 +00:00
|
|
|
DCHECK(render_frame_host);
|
|
|
|
|
2020-08-27 14:31:51 +00:00
|
|
|
content::WebContents* web_contents =
|
|
|
|
content::WebContents::FromRenderFrameHost(render_frame_host);
|
2021-11-03 17:17:06 +00:00
|
|
|
DCHECK(web_contents);
|
2020-08-27 14:31:51 +00:00
|
|
|
|
2021-11-03 17:17:06 +00:00
|
|
|
AutofillDriverFactory* factory = FromWebContents(web_contents);
|
|
|
|
if (!factory) {
|
|
|
|
// The message pipe will be closed and raise a connection error to peer
|
|
|
|
// side. The peer side can reconnect later when needed.
|
2020-08-27 14:31:51 +00:00
|
|
|
return;
|
2021-11-03 17:17:06 +00:00
|
|
|
}
|
2020-08-27 14:31:51 +00:00
|
|
|
|
2021-11-03 17:17:06 +00:00
|
|
|
if (auto* driver = factory->DriverForFrame(render_frame_host))
|
|
|
|
driver->BindPendingReceiver(std::move(pending_receiver));
|
2020-08-27 14:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AutofillDriverFactory::AutofillDriverFactory(content::WebContents* web_contents)
|
2022-01-10 22:31:39 +00:00
|
|
|
: content::WebContentsObserver(web_contents),
|
|
|
|
content::WebContentsUserData<AutofillDriverFactory>(*web_contents) {}
|
2020-08-27 14:31:51 +00:00
|
|
|
|
|
|
|
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) {
|
2021-11-03 17:17:06 +00:00
|
|
|
auto insertion_result = driver_map_.emplace(render_frame_host, nullptr);
|
|
|
|
std::unique_ptr<AutofillDriver>& driver = insertion_result.first->second;
|
|
|
|
bool insertion_happened = insertion_result.second;
|
|
|
|
if (insertion_happened) {
|
|
|
|
// The `render_frame_host` may already be deleted (or be in the process of
|
|
|
|
// being deleted). In this case, we must not create a new driver. Otherwise,
|
|
|
|
// a driver might hold a deallocated RFH.
|
|
|
|
//
|
|
|
|
// For example, `render_frame_host` is deleted in the following sequence:
|
|
|
|
// 1. `render_frame_host->~RenderFrameHostImpl()` starts and marks
|
|
|
|
// `render_frame_host` as deleted.
|
|
|
|
// 2. `ContentAutofillDriverFactory::RenderFrameDeleted(render_frame_host)`
|
|
|
|
// destroys the driver of `render_frame_host`.
|
|
|
|
// 3. `SomeOtherWebContentsObserver::RenderFrameDeleted(render_frame_host)`
|
|
|
|
// calls `DriverForFrame(render_frame_host)`.
|
|
|
|
// 5. `render_frame_host->~RenderFrameHostImpl()` finishes.
|
2022-06-27 20:50:08 +00:00
|
|
|
if (render_frame_host->IsRenderFrameLive()) {
|
2021-11-03 17:17:06 +00:00
|
|
|
driver = std::make_unique<AutofillDriver>(render_frame_host);
|
|
|
|
DCHECK_EQ(driver_map_.find(render_frame_host)->second.get(),
|
|
|
|
driver.get());
|
|
|
|
} else {
|
|
|
|
driver_map_.erase(insertion_result.first);
|
|
|
|
DCHECK_EQ(driver_map_.count(render_frame_host), 0u);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DCHECK(driver.get());
|
|
|
|
return driver.get();
|
2020-08-27 14:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:51:36 +00:00
|
|
|
WEB_CONTENTS_USER_DATA_KEY_IMPL(AutofillDriverFactory);
|
2020-08-27 14:31:51 +00:00
|
|
|
|
|
|
|
} // namespace electron
|