feat: add webContents.setWindowOpenHandler API (#24517)
Co-authored-by: Jeremy Rose <jeremya@chromium.org>
This commit is contained in:
parent
6b222a2d8a
commit
0b85fdf26c
56 changed files with 2087 additions and 885 deletions
|
@ -784,14 +784,13 @@ void Session::CreateInterruptedDownload(const gin_helper::Dictionary& options) {
|
|||
length, last_modified, etag, base::Time::FromDoubleT(start_time)));
|
||||
}
|
||||
|
||||
void Session::SetPreloads(
|
||||
const std::vector<base::FilePath::StringType>& preloads) {
|
||||
void Session::SetPreloads(const std::vector<base::FilePath>& preloads) {
|
||||
auto* prefs = SessionPreferences::FromBrowserContext(browser_context());
|
||||
DCHECK(prefs);
|
||||
prefs->set_preloads(preloads);
|
||||
}
|
||||
|
||||
std::vector<base::FilePath::StringType> Session::GetPreloads() const {
|
||||
std::vector<base::FilePath> Session::GetPreloads() const {
|
||||
auto* prefs = SessionPreferences::FromBrowserContext(browser_context());
|
||||
DCHECK(prefs);
|
||||
return prefs->preloads();
|
||||
|
|
|
@ -115,8 +115,8 @@ class Session : public gin::Wrappable<Session>,
|
|||
const std::string& uuid);
|
||||
void DownloadURL(const GURL& url);
|
||||
void CreateInterruptedDownload(const gin_helper::Dictionary& options);
|
||||
void SetPreloads(const std::vector<base::FilePath::StringType>& preloads);
|
||||
std::vector<base::FilePath::StringType> GetPreloads() const;
|
||||
void SetPreloads(const std::vector<base::FilePath>& preloads);
|
||||
std::vector<base::FilePath> GetPreloads() const;
|
||||
v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
|
||||
v8::Local<v8::Value> Protocol(v8::Isolate* isolate);
|
||||
v8::Local<v8::Value> ServiceWorkerContext(v8::Isolate* isolate);
|
||||
|
|
|
@ -639,7 +639,11 @@ void WebContents::InitWithSessionAndOptions(
|
|||
prefs->caret_blink_interval = *interval;
|
||||
|
||||
// Save the preferences in C++.
|
||||
new WebContentsPreferences(web_contents(), options);
|
||||
// If there's already a WebContentsPreferences object, we created it as part
|
||||
// of the webContents.setWindowOpenHandler path, so don't overwrite it.
|
||||
if (!WebContentsPreferences::From(web_contents())) {
|
||||
new WebContentsPreferences(web_contents(), options);
|
||||
}
|
||||
// Trigger re-calculation of webkit prefs.
|
||||
web_contents()->NotifyPreferencesChanged();
|
||||
|
||||
|
@ -778,18 +782,44 @@ void WebContents::WebContentsCreatedWithFullParams(
|
|||
tracker->referrer = params.referrer.To<content::Referrer>();
|
||||
tracker->raw_features = params.raw_features;
|
||||
tracker->body = params.body;
|
||||
|
||||
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
||||
v8::Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
|
||||
gin_helper::Dictionary dict;
|
||||
gin::ConvertFromV8(isolate, pending_child_web_preferences_.Get(isolate),
|
||||
&dict);
|
||||
pending_child_web_preferences_.Reset();
|
||||
|
||||
// Associate the preferences passed in via `setWindowOpenHandler` with the
|
||||
// content::WebContents that was just created for the child window. These
|
||||
// preferences will be picked up by the RenderWidgetHost via its call to the
|
||||
// delegate's OverrideWebkitPrefs.
|
||||
new WebContentsPreferences(new_contents, dict);
|
||||
}
|
||||
|
||||
bool WebContents::IsWebContentsCreationOverridden(
|
||||
content::SiteInstance* source_site_instance,
|
||||
content::mojom::WindowContainerType window_container_type,
|
||||
const GURL& opener_url,
|
||||
const std::string& frame_name,
|
||||
const GURL& target_url) {
|
||||
if (Emit("-will-add-new-contents", target_url, frame_name)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
const content::mojom::CreateNewWindowParams& params) {
|
||||
bool default_prevented = Emit("-will-add-new-contents", params.target_url,
|
||||
params.frame_name, params.raw_features);
|
||||
// If the app prevented the default, redirect to CreateCustomWebContents,
|
||||
// which always returns nullptr, which will result in the window open being
|
||||
// prevented (window.open() will return null in the renderer).
|
||||
return default_prevented;
|
||||
}
|
||||
|
||||
void WebContents::SetNextChildWebPreferences(
|
||||
const gin_helper::Dictionary preferences) {
|
||||
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
||||
v8::Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
// Store these prefs for when Chrome calls WebContentsCreatedWithFullParams
|
||||
// with the new child contents.
|
||||
pending_child_web_preferences_.Reset(isolate, preferences.GetHandle());
|
||||
}
|
||||
|
||||
content::WebContents* WebContents::CreateCustomWebContents(
|
||||
|
@ -1076,7 +1106,7 @@ void WebContents::RenderViewDeleted(content::RenderViewHost* render_view_host) {
|
|||
if (web_contents()->GetRenderViewHost() == render_view_host) {
|
||||
// When the RVH that has been deleted is the current RVH it means that the
|
||||
// the web contents are being closed. This is communicated by this event.
|
||||
// Currently tracked by guest-window-manager.js to destroy the
|
||||
// Currently tracked by guest-window-manager.ts to destroy the
|
||||
// BrowserWindow.
|
||||
Emit("current-render-view-deleted",
|
||||
render_view_host->GetProcess()->GetID());
|
||||
|
@ -2738,11 +2768,11 @@ void WebContents::DoGetZoomLevel(DoGetZoomLevelCallback callback) {
|
|||
std::move(callback).Run(GetZoomLevel());
|
||||
}
|
||||
|
||||
std::vector<base::FilePath::StringType> WebContents::GetPreloadPaths() const {
|
||||
std::vector<base::FilePath> WebContents::GetPreloadPaths() const {
|
||||
auto result = SessionPreferences::GetValidPreloads(GetBrowserContext());
|
||||
|
||||
if (auto* web_preferences = WebContentsPreferences::From(web_contents())) {
|
||||
base::FilePath::StringType preload;
|
||||
base::FilePath preload;
|
||||
if (web_preferences->GetPreloadPath(&preload)) {
|
||||
result.emplace_back(preload);
|
||||
}
|
||||
|
@ -3017,6 +3047,8 @@ v8::Local<v8::ObjectTemplate> WebContents::FillObjectTemplate(
|
|||
.SetMethod("_getPrinters", &WebContents::GetPrinterList)
|
||||
.SetMethod("_printToPDF", &WebContents::PrintToPDF)
|
||||
#endif
|
||||
.SetMethod("_setNextChildWebPreferences",
|
||||
&WebContents::SetNextChildWebPreferences)
|
||||
.SetMethod("addWorkSpace", &WebContents::AddWorkSpace)
|
||||
.SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace)
|
||||
.SetMethod("showDefinitionForSelection",
|
||||
|
|
|
@ -252,6 +252,8 @@ class WebContents : public gin::Wrappable<WebContents>,
|
|||
v8::Local<v8::Promise> PrintToPDF(base::DictionaryValue settings);
|
||||
#endif
|
||||
|
||||
void SetNextChildWebPreferences(const gin_helper::Dictionary);
|
||||
|
||||
// DevTools workspace api.
|
||||
void AddWorkSpace(gin::Arguments* args, const base::FilePath& path);
|
||||
void RemoveWorkSpace(gin::Arguments* args, const base::FilePath& path);
|
||||
|
@ -354,7 +356,7 @@ class WebContents : public gin::Wrappable<WebContents>,
|
|||
const scoped_refptr<network::ResourceRequestBody>& body);
|
||||
|
||||
// Returns the preload script path of current WebContents.
|
||||
std::vector<base::FilePath::StringType> GetPreloadPaths() const;
|
||||
std::vector<base::FilePath> GetPreloadPaths() const;
|
||||
|
||||
// Returns the web preferences of current WebContents.
|
||||
v8::Local<v8::Value> GetWebPreferences(v8::Isolate* isolate) const;
|
||||
|
@ -456,8 +458,7 @@ class WebContents : public gin::Wrappable<WebContents>,
|
|||
content::SiteInstance* source_site_instance,
|
||||
content::mojom::WindowContainerType window_container_type,
|
||||
const GURL& opener_url,
|
||||
const std::string& frame_name,
|
||||
const GURL& target_url) override;
|
||||
const content::mojom::CreateNewWindowParams& params) override;
|
||||
content::WebContents* CreateCustomWebContents(
|
||||
content::RenderFrameHost* opener,
|
||||
content::SiteInstance* source_site_instance,
|
||||
|
@ -679,6 +680,8 @@ class WebContents : public gin::Wrappable<WebContents>,
|
|||
// Observers of this WebContents.
|
||||
base::ObserverList<ExtendedWebContentsObserver> observers_;
|
||||
|
||||
v8::Global<v8::Value> pending_child_web_preferences_;
|
||||
|
||||
bool initially_shown_ = true;
|
||||
|
||||
service_manager::BinderRegistryWithArgs<content::RenderFrameHost*> registry_;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue