Browse Source

Update web preferences when creating with existing webContents

Kevin Sawicki 7 years ago
parent
commit
34c5abfe43
1 changed files with 28 additions and 18 deletions
  1. 28 18
      atom/browser/api/atom_api_window.cc

+ 28 - 18
atom/browser/api/atom_api_window.cc

@@ -10,6 +10,7 @@
 #include "atom/browser/api/atom_api_web_contents.h"
 #include "atom/browser/browser.h"
 #include "atom/browser/native_window.h"
+#include "atom/browser/web_contents_preferences.h"
 #include "atom/common/native_mate_converters/callback.h"
 #include "atom/common/native_mate_converters/file_path_converter.h"
 #include "atom/common/native_mate_converters/gfx_converter.h"
@@ -77,30 +78,39 @@ v8::Local<v8::Value> ToBuffer(v8::Isolate* isolate, void* val, int size) {
 Window::Window(v8::Isolate* isolate, v8::Local<v8::Object> wrapper,
                const mate::Dictionary& options) {
   mate::Handle<class WebContents> web_contents;
-  // If no WebContents was passed to the constructor, create it from options.
-  if (!options.Get("webContents", &web_contents)) {
-    // Use options.webPreferences to create WebContents.
-    mate::Dictionary web_preferences = mate::Dictionary::CreateEmpty(isolate);
-    options.Get(options::kWebPreferences, &web_preferences);
 
-    // Copy the backgroundColor to webContents.
-    v8::Local<v8::Value> value;
-    if (options.Get(options::kBackgroundColor, &value))
-      web_preferences.Set(options::kBackgroundColor, value);
+  // Use options.webPreferences in WebContents.
+  mate::Dictionary web_preferences = mate::Dictionary::CreateEmpty(isolate);
+  options.Get(options::kWebPreferences, &web_preferences);
 
-    v8::Local<v8::Value> transparent;
-    if (options.Get("transparent", &transparent))
-      web_preferences.Set("transparent", transparent);
+  // Copy the backgroundColor to webContents.
+  v8::Local<v8::Value> value;
+  if (options.Get(options::kBackgroundColor, &value))
+    web_preferences.Set(options::kBackgroundColor, value);
+
+  v8::Local<v8::Value> transparent;
+  if (options.Get("transparent", &transparent))
+    web_preferences.Set("transparent", transparent);
 
 #if defined(ENABLE_OSR)
-    // Offscreen windows are always created frameless.
-    bool offscreen;
-    if (web_preferences.Get("offscreen", &offscreen) && offscreen) {
-      auto window_options = const_cast<mate::Dictionary&>(options);
-      window_options.Set(options::kFrame, false);
-    }
+  // Offscreen windows are always created frameless.
+  bool offscreen;
+  if (web_preferences.Get("offscreen", &offscreen) && offscreen) {
+    auto window_options = const_cast<mate::Dictionary&>(options);
+    window_options.Set(options::kFrame, false);
+  }
 #endif
 
+  if (options.Get("webContents", &web_contents)) {
+    // Set webPreferences from options if using existing webContents
+    auto* existing_preferences =
+        WebContentsPreferences::FromWebContents(web_contents->web_contents());
+    base::DictionaryValue web_preferences_dict;
+    mate::ConvertFromV8(isolate, web_preferences.GetHandle(),
+                        &web_preferences_dict);
+    existing_preferences->web_preferences()->Clear();
+    existing_preferences->Merge(web_preferences_dict);
+  } else {
     // Creates the WebContents used by BrowserWindow.
     web_contents = WebContents::Create(isolate, web_preferences);
   }