]> khamidou — GitHub mirrors - electron.git/commitdiff
store webviewinfo as web contents userdata
authorRobo <hop2deep@gmail.com>
Thu, 3 Sep 2015 00:47:58 +0000 (06:17 +0530)
committerRobo <hop2deep@gmail.com>
Thu, 3 Sep 2015 15:57:50 +0000 (21:27 +0530)
atom/browser/api/atom_api_web_view_manager.cc
atom/browser/atom_browser_client.cc
atom/browser/lib/guest-view-manager.coffee
atom/browser/web_view_constants.cc [new file with mode: 0644]
atom/browser/web_view_constants.h [new file with mode: 0644]
atom/browser/web_view_guest_delegate.cc
atom/browser/web_view_manager.cc
atom/browser/web_view_manager.h
filenames.gypi
spec/webview-spec.coffee

index c69285a6980d6efa8a8606baddc4163d83cc9b63..221d0d7118b41dba21f7cf84b0874a2c2421be1c 100644 (file)
@@ -3,6 +3,7 @@
 // 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_view_manager.h"
 #include "atom/common/native_mate_converters/gurl_converter.h"
 #include "content/public/browser/browser_context.h"
@@ -34,17 +35,19 @@ struct Converter<atom::WebViewManager::WebViewInfo> {
       return false;
 
     GURL preload_url;
-    if (!options.Get("preloadUrl", &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("nodeIntegration", &(out->node_integration)) &&
-           options.Get("plugins", &(out->plugins)) &&
-           options.Get("disableWebSecurity", &(out->disable_web_security)) &&
-           options.Get("partitionId", &(out->partition_id));
+    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));
   }
 };
 
@@ -68,12 +71,15 @@ void AddGuest(int guest_instance_id,
               content::WebContents* guest_web_contents,
               atom::WebViewManager::WebViewInfo info) {
   auto manager = GetWebViewManager(embedder);
-  if (manager) {
-    info.guest_instance_id = guest_instance_id;
-    info.embedder = embedder;
+  if (manager)
     manager->AddGuest(guest_instance_id, element_instance_id, embedder,
-                      guest_web_contents, info);
-  }
+                      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);
 }
 
 void RemoveGuest(content::WebContents* embedder, int guest_instance_id) {
index 8db71fd3030d36ac9205f358bbddfc0f39d07b2a..fad952d53365cc1754afccf2166f060d9f232f1e 100644 (file)
@@ -16,6 +16,7 @@
 #include "atom/browser/browser.h"
 #include "atom/browser/native_window.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"
@@ -74,8 +75,12 @@ ProcessOwner GetProcessOwner(int process_id,
     return OWNER_NATIVE_WINDOW;
 
   // Then search for guest WebContents.
-  if (WebViewManager::GetInfoForWebContents(web_contents, info))
+  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;
 }
@@ -155,9 +160,10 @@ void AtomBrowserClient::OverrideWebkitPrefs(
 
   // Custom preferences of guest page.
   auto web_contents = content::WebContents::FromRenderViewHost(host);
-  WebViewManager::WebViewInfo info;
-  if (WebViewManager::GetInfoForWebContents(web_contents, &info)) {
-    prefs->web_security_enabled = !info.disable_web_security;
+  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;
   }
 
index 2f1c8312155a48489ee6d24166c8fe3693c8bfe5..581274699085ec9dca1ac9f5fc3dfc0eb440c37b 100644 (file)
@@ -48,6 +48,8 @@ getPartitionId = (partition) ->
       partition = partition.substring('persist:'.length)
       partitionId += 'persist?'
     else
+      # Just to differentiate from same persistant ID
+      partition += "_temp"
       partitionId += '?'
     partitionId += encodeURIComponent(partition)
   return partitionId
diff --git a/atom/browser/web_view_constants.cc b/atom/browser/web_view_constants.cc
new file mode 100644 (file)
index 0000000..6f8314c
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright (c) 2015 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include "atom/browser/web_view_constants.h"
+
+namespace atom {
+
+namespace web_view {
+
+const char kPreloadUrl[] = "preloadUrl";
+const char kNodeIntegration[] = "nodeIntegration";
+const char kPlugins[] = "plugins";
+const char kDisableWebSecurity[] = "disableWebSecurity";
+const char kPartitionId[] = "partitionId";
+
+const int kDefaultWidth = 300;
+const int kDefaultHeight = 300;
+
+const char kWebViewInfoKeyName[] = "web_view_info";
+
+}  // namespace web_view
+
+}  // namespace atom
diff --git a/atom/browser/web_view_constants.h b/atom/browser/web_view_constants.h
new file mode 100644 (file)
index 0000000..0831bdb
--- /dev/null
@@ -0,0 +1,27 @@
+// Copyright (c) 2015 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_BROWSER_WEB_VIEW_CONSTANTS_H_
+#define ATOM_BROWSER_WEB_VIEW_CONSTANTS_H_
+
+namespace atom {
+
+namespace web_view {
+
+extern const char kPreloadUrl[];
+extern const char kNodeIntegration[];
+extern const char kPlugins[];
+extern const char kDisableWebSecurity[];
+extern const char kPartitionId[];
+
+extern const int kDefaultWidth;
+extern const int kDefaultHeight;
+
+extern const char kWebViewInfoKeyName[];
+
+}  // namespace web_view
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_WEB_VIEW_CONSTANTS_H_
index 8e1810c4a39bb9301d79764cd4ed9e2d9ff18810..a954cea8dc021d76d2c31e46d15e3dc81df4e124 100644 (file)
@@ -5,6 +5,7 @@
 #include "atom/browser/web_view_guest_delegate.h"
 
 #include "atom/browser/api/atom_api_web_contents.h"
+#include "atom/browser/web_view_constants.h"
 #include "atom/common/native_mate_converters/gurl_converter.h"
 #include "content/public/browser/guest_host.h"
 #include "content/public/browser/render_frame_host.h"
 
 namespace atom {
 
-namespace {
-
-const int kDefaultWidth = 300;
-const int kDefaultHeight = 300;
-
-}  // namespace
-
 WebViewGuestDelegate::WebViewGuestDelegate()
     : guest_opaque_(true),
       guest_host_(nullptr),
@@ -178,7 +172,7 @@ gfx::Size WebViewGuestDelegate::GetDefaultSize() const {
     return embedder_web_contents_->GetRenderWidgetHostView()
                                  ->GetVisibleViewportSize();
   } else {
-    return gfx::Size(kDefaultWidth, kDefaultHeight);
+    return gfx::Size(web_view::kDefaultWidth, web_view::kDefaultHeight);
   }
 }
 
index 76915264c6fd482f1583546039265fc286f86ce6..9d0b9337599fd44dbb08101abe03ea0096a5189f 100644 (file)
 
 namespace atom {
 
-// static
-bool WebViewManager::GetInfoForWebContents(
-    const content::WebContents* web_contents, WebViewInfo* info) {
-  // use embedders' browser context to retrieve WebViewManager.
-  auto manager = static_cast<WebViewManager*>(
-      AtomBrowserMainParts::Get()->browser_context()->GetGuestManager());
-  if (!manager)
-    return false;
-  base::AutoLock auto_lock(manager->lock_);
-  auto iter = manager->webview_info_map_.find(web_contents);
-  if (iter == manager->webview_info_map_.end())
-    return false;
-  *info = iter->second;
-  return true;
-}
-
 WebViewManager::WebViewManager(content::BrowserContext* context) {
 }
 
@@ -36,11 +20,9 @@ WebViewManager::~WebViewManager() {
 void WebViewManager::AddGuest(int guest_instance_id,
                               int element_instance_id,
                               content::WebContents* embedder,
-                              content::WebContents* web_contents,
-                              const WebViewInfo& info) {
+                              content::WebContents* web_contents) {
   base::AutoLock auto_lock(lock_);
   web_contents_embedder_map_[guest_instance_id] = { web_contents, embedder };
-  webview_info_map_[web_contents] = info;
 
   // Map the element in embedder to guest.
   int owner_process_id = embedder->GetRenderProcessHost()->GetID();
@@ -53,10 +35,7 @@ void WebViewManager::RemoveGuest(int guest_instance_id) {
   if (!ContainsKey(web_contents_embedder_map_, guest_instance_id))
     return;
 
-  auto web_contents =
-      web_contents_embedder_map_[guest_instance_id].web_contents;
   web_contents_embedder_map_.erase(guest_instance_id);
-  webview_info_map_.erase(web_contents);
 
   // Remove the record of element in embedder too.
   for (const auto& element : element_instance_id_to_guest_map_)
index d8ed857b444fd85376cc402d8c694fa1973f0622..20c044849720a562993c0f6d2c7dbbdea4ab4ea9 100644 (file)
@@ -8,6 +8,7 @@
 #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"
@@ -31,10 +32,17 @@ class WebViewManager : public content::BrowserPluginGuestManager {
     GURL partition_id;
   };
 
-  // Finds the WebViewManager attached with |web_contents| and returns the
-  // WebViewInfo of it.
-  static bool GetInfoForWebContents(const content::WebContents* web_contents,
-                                    WebViewInfo* info);
+  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();
@@ -42,8 +50,7 @@ class WebViewManager : public content::BrowserPluginGuestManager {
   void AddGuest(int guest_instance_id,
                 int element_instance_id,
                 content::WebContents* embedder,
-                content::WebContents* web_contents,
-                const WebViewInfo& info);
+                content::WebContents* web_contents);
   void RemoveGuest(int guest_instance_id);
 
  protected:
@@ -83,10 +90,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_;
 
-  typedef std::map<const content::WebContents*, WebViewInfo> WebViewInfoMap;
-  // web_contents => (guest_instance_id, embedder, ...)
-  WebViewInfoMap webview_info_map_;
-
   base::Lock lock_;
 
   DISALLOW_COPY_AND_ASSIGN(WebViewManager);
index 807ccd32bb66604b447f8a7b85f24bb65d824882..2f97d01507e8ca431866ee26348ac576a4c59364 100644 (file)
       'atom/browser/ui/x/window_state_watcher.h',
       'atom/browser/ui/x/x_window_utils.cc',
       'atom/browser/ui/x/x_window_utils.h',
+      'atom/browser/web_view_constants.cc',
+      'atom/browser/web_view_constants.h',
       'atom/browser/web_view_guest_delegate.cc',
       'atom/browser/web_view_guest_delegate.h',
       'atom/browser/web_view_manager.cc',
index 486efaa6ffb4a82ec87c4713247f4a035874329c..fe281510b40eaefc51022637bb9e7db03a764f11 100644 (file)
@@ -155,6 +155,23 @@ describe '<webview> tag', ->
       document.body.appendChild webview
 
   describe 'partition attribute', ->
+    it 'inserts no node symbols when not set', (done) ->
+      webview.addEventListener 'console-message', (e) ->
+        assert.equal e.message, 'undefined undefined undefined undefined'
+        done()
+      webview.src = "file://#{fixtures}/pages/c.html"
+      webview.partition = "test"
+      document.body.appendChild webview
+
+    it 'inserts node symbols when set', (done) ->
+      webview.addEventListener 'console-message', (e) ->
+        assert.equal e.message, 'function object object'
+        done()
+      webview.setAttribute 'nodeintegration', 'on'
+      webview.src = "file://#{fixtures}/pages/d.html"
+      webview.partition = "test"
+      document.body.appendChild webview
+
     it 'isolates storage for different id', (done) ->
       listener = (e) ->
         assert.equal e.message, " 0"