]> khamidou — GitHub mirrors - electron.git/commitdiff
Read guest view's info from WebContentsPreferences
authorCheng Zhao <zcbenz@gmail.com>
Sat, 5 Sep 2015 02:43:30 +0000 (11:43 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Sun, 6 Sep 2015 02:53:03 +0000 (10:53 +0800)
atom/browser/api/atom_api_web_view_manager.cc
atom/browser/atom_browser_client.cc
atom/browser/atom_browser_context.cc
atom/browser/lib/guest-view-manager.coffee
atom/browser/web_contents_preferences.cc
atom/browser/web_contents_preferences.h
atom/browser/web_view_manager.cc
atom/browser/web_view_manager.h

index 221d0d7118b41dba21f7cf84b0874a2c2421be1c..83287ac8b2f12b467bf8e4dff2cb36ebf3285821 100644 (file)
@@ -3,14 +3,12 @@
 // found in the LICENSE file.
 
 #include "atom/browser/api/atom_api_web_contents.h"
-#include "atom/browser/web_view_constants.h"
+#include "atom/browser/web_contents_preferences.h"
 #include "atom/browser/web_view_manager.h"
-#include "atom/common/native_mate_converters/gurl_converter.h"
+#include "atom/common/native_mate_converters/value_converter.h"
+#include "atom/common/node_includes.h"
 #include "content/public/browser/browser_context.h"
 #include "native_mate/dictionary.h"
-#include "net/base/filename_util.h"
-
-#include "atom/common/node_includes.h"
 
 namespace mate {
 
@@ -26,31 +24,6 @@ struct Converter<content::WebContents*> {
   }
 };
 
-template<>
-struct Converter<atom::WebViewManager::WebViewInfo> {
-  static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
-                     atom::WebViewManager::WebViewInfo* out) {
-    Dictionary options;
-    if (!ConvertFromV8(isolate, val, &options))
-      return false;
-
-    GURL preload_url;
-    if (!options.Get(atom::web_view::kPreloadUrl, &preload_url))
-      return false;
-
-    if (!preload_url.is_empty() &&
-        !net::FileURLToFilePath(preload_url, &(out->preload_script)))
-      return false;
-
-    return options.Get(atom::web_view::kNodeIntegration,
-                       &(out->node_integration)) &&
-           options.Get(atom::web_view::kPlugins, &(out->plugins)) &&
-           options.Get(atom::web_view::kPartitionId, &(out->partition_id)) &&
-           options.Get(atom::web_view::kDisableWebSecurity,
-                       &(out->disable_web_security));
-  }
-};
-
 }  // namespace mate
 
 namespace {
@@ -69,17 +42,13 @@ void AddGuest(int guest_instance_id,
               int element_instance_id,
               content::WebContents* embedder,
               content::WebContents* guest_web_contents,
-              atom::WebViewManager::WebViewInfo info) {
+              const base::DictionaryValue& options) {
   auto manager = GetWebViewManager(embedder);
   if (manager)
     manager->AddGuest(guest_instance_id, element_instance_id, embedder,
                       guest_web_contents);
 
-  info.guest_instance_id = guest_instance_id;
-  info.embedder = embedder;
-  auto data = new atom::WebViewManager::WebViewInfoUserData(info);
-  guest_web_contents->SetUserData(
-      atom::web_view::kWebViewInfoKeyName, data);
+  atom::WebContentsPreferences::From(guest_web_contents)->Merge(options);
 }
 
 void RemoveGuest(content::WebContents* embedder, int guest_instance_id) {
index fa9c8fb4abe28a4b24764621f14972ea1f20047e..e45caceab01e9691bbc8c99abec6135c21762948 100644 (file)
@@ -16,8 +16,6 @@
 #include "atom/browser/browser.h"
 #include "atom/browser/native_window.h"
 #include "atom/browser/web_contents_preferences.h"
-#include "atom/browser/web_view_manager.h"
-#include "atom/browser/web_view_constants.h"
 #include "atom/browser/window_list.h"
 #include "atom/common/options_switches.h"
 #include "base/command_line.h"
@@ -56,37 +54,6 @@ bool g_suppress_renderer_process_restart = false;
 // Custom schemes to be registered to standard.
 std::string g_custom_schemes = "";
 
-// Find out the owner of the child process according to |process_id|.
-enum ProcessOwner {
-  OWNER_NATIVE_WINDOW,
-  OWNER_GUEST_WEB_CONTENTS,
-  OWNER_NONE,  // it might be devtools though.
-};
-
-ProcessOwner GetProcessOwner(int process_id,
-                             NativeWindow** window,
-                             WebViewManager::WebViewInfo* info) {
-  content::WebContents* web_contents = content::WebContents::FromRenderViewHost(
-      content::RenderViewHost::FromID(process_id, kDefaultRoutingID));
-  if (!web_contents)
-    return OWNER_NONE;
-
-  // First search for NativeWindow.
-  *window = NativeWindow::FromWebContents(web_contents);
-  if (*window)
-    return OWNER_NATIVE_WINDOW;
-
-  // Then search for guest WebContents.
-  auto data = static_cast<WebViewManager::WebViewInfoUserData*>(
-      web_contents->GetUserData(web_view::kWebViewInfoKeyName));
-  if (data) {
-    *info = data->web_view_info();
-    return OWNER_GUEST_WEB_CONTENTS;
-  }
-
-  return OWNER_NONE;
-}
-
 scoped_refptr<net::X509Certificate> ImportCertFromFile(
     const base::FilePath& path) {
   if (path.empty())
@@ -162,16 +129,7 @@ void AtomBrowserClient::OverrideWebkitPrefs(
 
   // Custom preferences of guest page.
   auto web_contents = content::WebContents::FromRenderViewHost(host);
-  auto info = static_cast<WebViewManager::WebViewInfoUserData*>(
-      web_contents->GetUserData(web_view::kWebViewInfoKeyName));
-  if (info) {
-    prefs->web_security_enabled = !info->web_view_info().disable_web_security;
-    return;
-  }
-
-  NativeWindow* window = NativeWindow::FromWebContents(web_contents);
-  if (window)
-    WebContentsPreferences::OverrideWebkitPrefs(web_contents, prefs);
+  WebContentsPreferences::OverrideWebkitPrefs(web_contents, prefs);
 }
 
 std::string AtomBrowserClient::GetApplicationLocale() {
@@ -227,24 +185,14 @@ void AtomBrowserClient::AppendExtraCommandLineSwitches(
   if (ContainsKey(pending_processes_, process_id))
     process_id = pending_processes_[process_id];
 
-  NativeWindow* window;
-  WebViewManager::WebViewInfo info;
-  ProcessOwner owner = GetProcessOwner(process_id, &window, &info);
-
-  if (owner == OWNER_NATIVE_WINDOW) {
-    WebContentsPreferences::AppendExtraCommandLineSwitches(
-        window->web_contents(), command_line);
-  } else if (owner == OWNER_GUEST_WEB_CONTENTS) {
-    command_line->AppendSwitchASCII(
-        switches::kGuestInstanceID, base::IntToString(info.guest_instance_id));
-    command_line->AppendSwitchASCII(
-        switches::kNodeIntegration, info.node_integration ? "true" : "false");
-    if (info.plugins)
-      command_line->AppendSwitch(switches::kEnablePlugins);
-    if (!info.preload_script.empty())
-      command_line->AppendSwitchPath(
-          switches::kPreloadScript, info.preload_script);
-  }
+  // Get the WebContents of the render process.
+  content::WebContents* web_contents = content::WebContents::FromRenderViewHost(
+      content::RenderViewHost::FromID(process_id, kDefaultRoutingID));
+  if (!web_contents)
+    return;
+
+  WebContentsPreferences::AppendExtraCommandLineSwitches(
+      web_contents, command_line);
 }
 
 void AtomBrowserClient::DidCreatePpapiPlugin(
index 583a6324a8f5dd39aa0b970c6d85945a877750f4..43573b1c707685eeff52c0b44a6e4ee1f3aaab58 100644 (file)
@@ -150,7 +150,7 @@ AtomBrowserContext::GetDownloadManagerDelegate() {
 
 content::BrowserPluginGuestManager* AtomBrowserContext::GetGuestManager() {
   if (!guest_manager_)
-    guest_manager_.reset(new WebViewManager(this));
+    guest_manager_.reset(new WebViewManager);
   return guest_manager_.get();
 }
 
index 581274699085ec9dca1ac9f5fc3dfc0eb440c37b..961da8ad1920b3d90f01f91acd754b57d95b1de7 100644 (file)
@@ -132,12 +132,14 @@ attachGuest = (embedder, elementInstanceId, guestInstanceId, params) ->
     return unless guestInstances[oldGuestInstanceId]?
     destroyGuest embedder, oldGuestInstanceId
 
-  webViewManager.addGuest guestInstanceId, elementInstanceId, embedder, guest,
-    nodeIntegration: params.nodeintegration
-    plugins: params.plugins
-    disableWebSecurity: params.disablewebsecurity
-    preloadUrl: params.preload ? ''
-    partitionId: getPartitionId(params.partition)
+  webPreferences =
+    'guest-instance-id': guestInstanceId
+    'node-integration': params.nodeintegration ? false
+    'plugins': params.plugins
+    'web-security': !params.disablewebsecurity
+  webPreferences['preload-url'] = params.preload if params.preload
+  webPreferences['partition'] = getPartitionId(params.partition) if params.partition
+  webViewManager.addGuest guestInstanceId, elementInstanceId, embedder, guest, webPreferences
 
   guest.attachParams = params
   embedderElementsMap[key] = guestInstanceId
index 269433e4d6b3e54835d175d23fe630b3b8c5869d..ab7a700cc284961eb773e54c3a18347a9fbc7a65 100644 (file)
@@ -44,6 +44,10 @@ WebContentsPreferences::WebContentsPreferences(
 WebContentsPreferences::~WebContentsPreferences() {
 }
 
+void WebContentsPreferences::Merge(const base::DictionaryValue& extend) {
+  web_preferences_.MergeDictionary(&extend);
+}
+
 // static
 WebContentsPreferences* WebContentsPreferences::From(
     content::WebContents* web_contents) {
@@ -55,7 +59,9 @@ WebContentsPreferences* WebContentsPreferences::From(
 void WebContentsPreferences::AppendExtraCommandLineSwitches(
     content::WebContents* web_contents, base::CommandLine* command_line) {
   WebContentsPreferences* self = From(web_contents);
-  CHECK(self);
+  if (!self)
+    return;
+
   base::DictionaryValue& web_preferences = self->web_preferences_;
 
   bool b;
@@ -110,12 +116,21 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches(
       zoom_factor != 1.0)
     command_line->AppendSwitchASCII(switches::kZoomFactor,
                                     base::DoubleToString(zoom_factor));
+
+  // --guest-instance-id, which is used to identify guest WebContents.
+  int guest_instance_id;
+  if (web_preferences.GetInteger(switches::kGuestInstanceID,
+                                 &guest_instance_id))
+      command_line->AppendSwitchASCII(switches::kGuestInstanceID,
+                                      base::IntToString(guest_instance_id));
 }
 
+// static
 void WebContentsPreferences::OverrideWebkitPrefs(
     content::WebContents* web_contents, content::WebPreferences* prefs) {
   WebContentsPreferences* self = From(web_contents);
-  CHECK(self);
+  if (!self)
+    return;
 
   bool b;
   if (self->web_preferences_.GetBoolean("javascript", &b))
index a2a63415047daae28a45539d060bf43e29d4eddb..1053dd9c1087ebb47df54653d01e4fd44c93b58b 100644 (file)
@@ -37,6 +37,9 @@ class WebContentsPreferences
                          base::DictionaryValue&& web_preferences);
   ~WebContentsPreferences() override;
 
+  // $.extend(|web_preferences_|, |new_web_preferences|).
+  void Merge(const base::DictionaryValue& new_web_preferences);
+
  private:
   friend class content::WebContentsUserData<WebContentsPreferences>;
 
index 9d0b9337599fd44dbb08101abe03ea0096a5189f..d404c1a4368011301c2f133fac5e4934ada03c04 100644 (file)
@@ -5,13 +5,12 @@
 #include "atom/browser/web_view_manager.h"
 
 #include "atom/browser/atom_browser_context.h"
-#include "atom/browser/atom_browser_main_parts.h"
 #include "content/public/browser/render_process_host.h"
 #include "content/public/browser/web_contents.h"
 
 namespace atom {
 
-WebViewManager::WebViewManager(content::BrowserContext* context) {
+WebViewManager::WebViewManager() {
 }
 
 WebViewManager::~WebViewManager() {
@@ -21,7 +20,6 @@ void WebViewManager::AddGuest(int guest_instance_id,
                               int element_instance_id,
                               content::WebContents* embedder,
                               content::WebContents* web_contents) {
-  base::AutoLock auto_lock(lock_);
   web_contents_embedder_map_[guest_instance_id] = { web_contents, embedder };
 
   // Map the element in embedder to guest.
@@ -31,7 +29,6 @@ void WebViewManager::AddGuest(int guest_instance_id,
 }
 
 void WebViewManager::RemoveGuest(int guest_instance_id) {
-  base::AutoLock auto_lock(lock_);
   if (!ContainsKey(web_contents_embedder_map_, guest_instance_id))
     return;
 
index 20c044849720a562993c0f6d2c7dbbdea4ab4ea9..ff9a8ecba2abca5e8b3c5513694ec01c95190e93 100644 (file)
@@ -7,45 +7,14 @@
 
 #include <map>
 
-#include "base/files/file_path.h"
-#include "base/supports_user_data.h"
-#include "base/synchronization/lock.h"
 #include "content/public/browser/browser_plugin_guest_manager.h"
-#include "content/public/browser/site_instance.h"
-
-namespace content {
-class BrowserContext;
-class RenderProcessHost;
-}
 
 namespace atom {
 
 class WebViewManager : public content::BrowserPluginGuestManager {
  public:
-  struct WebViewInfo {
-    int guest_instance_id;
-    content::WebContents* embedder;
-    bool node_integration;
-    bool plugins;
-    bool disable_web_security;
-    base::FilePath preload_script;
-    GURL partition_id;
-  };
-
-  class WebViewInfoUserData : public base::SupportsUserData::Data {
-   public:
-    explicit WebViewInfoUserData(WebViewInfo info)
-        : web_view_info_(info) {}
-    ~WebViewInfoUserData() override {}
-
-    WebViewInfo& web_view_info() { return web_view_info_; }
-
-   private:
-    WebViewInfo web_view_info_;
-  };
-
-  explicit WebViewManager(content::BrowserContext* context);
-  virtual ~WebViewManager();
+  WebViewManager();
+  ~WebViewManager() override;
 
   void AddGuest(int guest_instance_id,
                 int element_instance_id,
@@ -90,8 +59,6 @@ class WebViewManager : public content::BrowserPluginGuestManager {
   // (embedder_process_id, element_instance_id) => guest_instance_id
   std::map<ElementInstanceKey, int> element_instance_id_to_guest_map_;
 
-  base::Lock lock_;
-
   DISALLOW_COPY_AND_ASSIGN(WebViewManager);
 };