]> khamidou — GitHub mirrors - electron.git/commitdiff
Inherit parent window's options in window.open
authorCheng Zhao <zcbenz@gmail.com>
Tue, 22 Sep 2015 13:56:56 +0000 (21:56 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 22 Sep 2015 14:32:10 +0000 (22:32 +0800)
atom/browser/api/atom_api_web_contents.cc
atom/browser/api/atom_api_web_contents.h
atom/browser/api/atom_api_window.cc
atom/browser/lib/guest-window-manager.coffee
atom/browser/web_contents_preferences.cc
atom/browser/web_contents_preferences.h

index 79f359ae8d739e14775633d5831bf9b73ebfbff9..bdeb4ed4f873b94adef22da0a37caeef926dd645 100644 (file)
@@ -228,7 +228,7 @@ WebContents::WebContents(v8::Isolate* isolate,
   AttachAsUserData(web_contents);
   InitWithWebContents(web_contents);
 
-  // Save the preferences.
+  // Save the preferences in C++.
   base::DictionaryValue web_preferences;
   mate::ConvertFromV8(isolate, options.GetHandle(), &web_preferences);
   new WebContentsPreferences(web_contents, &web_preferences);
@@ -887,6 +887,12 @@ bool WebContents::IsGuest() const {
   return type_ == WEB_VIEW;
 }
 
+v8::Local<v8::Value> WebContents::GetWebPreferences(v8::Isolate* isolate) {
+  WebContentsPreferences* web_preferences =
+      WebContentsPreferences::FromWebContents(web_contents());
+  return mate::ConvertToV8(isolate, *web_preferences->web_preferences());
+}
+
 mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
     v8::Isolate* isolate) {
   if (template_.IsEmpty())
@@ -942,6 +948,7 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
         .SetMethod("setSize", &WebContents::SetSize)
         .SetMethod("setAllowTransparency", &WebContents::SetAllowTransparency)
         .SetMethod("isGuest", &WebContents::IsGuest)
+        .SetMethod("getWebPreferences", &WebContents::GetWebPreferences)
         .SetMethod("hasServiceWorker", &WebContents::HasServiceWorker)
         .SetMethod("unregisterServiceWorker",
                    &WebContents::UnregisterServiceWorker)
@@ -995,7 +1002,7 @@ mate::Handle<WebContents> WebContents::CreateFrom(
 // static
 mate::Handle<WebContents> WebContents::Create(
     v8::Isolate* isolate, const mate::Dictionary& options) {
-  auto handle =  mate::CreateHandle(isolate, new WebContents(isolate, options));
+  auto handle = mate::CreateHandle(isolate, new WebContents(isolate, options));
   g_wrap_web_contents.Run(handle.ToV8());
   return handle;
 }
index c8ea6908bc5b78af08dbb89f5a24fc3cee6db474..91750ac6136c198f4791e0b4431ad1d51303531b 100644 (file)
@@ -132,6 +132,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
   void SetAllowTransparency(bool allow);
   bool IsGuest() const;
 
+  // Returns the web preferences of current WebContents.
+  v8::Local<v8::Value> GetWebPreferences(v8::Isolate* isolate);
+
  protected:
   explicit WebContents(content::WebContents* web_contents);
   WebContents(v8::Isolate* isolate, const mate::Dictionary& options);
index 3a44115da264cd45b438b4a67d878fb327152cae..4d866d18503c788b1abaef289e81260243d6d6a8 100644 (file)
@@ -82,6 +82,10 @@ Window::Window(v8::Isolate* isolate, const mate::Dictionary& options) {
   web_contents_.Reset(isolate, web_contents.ToV8());
   api_web_contents_ = web_contents.get();
 
+  // Keep a copy of the options for later use.
+  mate::Dictionary(isolate, web_contents->GetWrapper(isolate)).Set(
+      "browserWindowOptions", options);
+
   // Creates BrowserWindow.
   window_.reset(NativeWindow::Create(web_contents->managed_web_contents(),
                                      options));
index add366eca8127fde881244cc71d7d85155da3c49..2dc5056a57ea4935e99301d17fe1c658ca7cfa94 100644 (file)
@@ -4,6 +4,17 @@ BrowserWindow = require 'browser-window'
 
 frameToGuest = {}
 
+# Merge |options| with the |embedder|'s window's options.
+mergeBrowserWindowOptions = (embedder, options) ->
+  if embedder.browserWindowOptions?
+    # Inherit the original options if it is a BrowserWindow.
+    options.__proto__ = embedder.browserWindowOptions
+  else
+    # Or only inherit web-preferences if it is a webview.
+    options['web-preferences'] ?= {}
+    options['web-preferences'].__proto__ = embedder.getWebPreferences()
+  options
+
 # Create a new guest created by |embedder| with |options|.
 createGuest = (embedder, url, frameName, options) ->
   guest = frameToGuest[frameName]
@@ -11,7 +22,7 @@ createGuest = (embedder, url, frameName, options) ->
     guest.loadUrl url
     return guest.id
 
-  guest = new BrowserWindow(options)
+  guest = new BrowserWindow(mergeBrowserWindowOptions(embedder, options))
   guest.loadUrl url
 
   # Remember the embedder, will be used by window.opener methods.
index d34b67d785037747ff0c6ffb497eaffb1021c3c2..2856598c0bc0cb380bf2076a543da5b17c0098d7 100644 (file)
@@ -40,6 +40,9 @@ WebContentsPreferences::WebContentsPreferences(
     base::DictionaryValue* web_preferences) {
   web_preferences_.Swap(web_preferences);
   web_contents->SetUserData(UserDataKey(), this);
+
+  // The "isGuest" is not a preferences field.
+  web_preferences_.Remove("isGuest", nullptr);
 }
 
 WebContentsPreferences::~WebContentsPreferences() {
index 83b485f449bc7b3d8682724cf65c24ef4296ba80..3e36df02147829ed548ba79faddd3295b59ae295 100644 (file)
@@ -37,6 +37,9 @@ class WebContentsPreferences
   // $.extend(|web_preferences_|, |new_web_preferences|).
   void Merge(const base::DictionaryValue& new_web_preferences);
 
+  // Returns the web preferences.
+  base::DictionaryValue* web_preferences() { return &web_preferences_; }
+
  private:
   friend class content::WebContentsUserData<WebContentsPreferences>;