]> khamidou — GitHub mirrors - electron.git/commitdiff
Avoid using global BrowserContext
authorCheng Zhao <zcbenz@gmail.com>
Wed, 4 Feb 2015 23:08:29 +0000 (15:08 -0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 4 Feb 2015 23:08:29 +0000 (15:08 -0800)
atom/browser/api/atom_api_web_contents.cc
atom/browser/atom_browser_client.cc
atom/browser/web_view/web_view_manager.cc
atom/browser/web_view/web_view_manager.h

index 167a304bac034d435d21bd93f3363e9cb3555799..9e49691b2ce5dc9b6b7127a6190afd41142608ec 100644 (file)
@@ -39,10 +39,9 @@ v8::Persistent<v8::ObjectTemplate> template_;
 
 // Get the window that has the |guest| embedded.
 NativeWindow* GetWindowFromGuest(const content::WebContents* guest) {
-  auto manager = AtomBrowserContext::Get()->GetGuestManager();
-  int guest_process_id = guest->GetRenderProcessHost()->GetID();
+  auto process = guest->GetRenderProcessHost();
   WebViewManager::WebViewInfo info;
-  if (!static_cast<WebViewManager*>(manager)->GetInfo(guest_process_id, &info))
+  if (WebViewManager::GetInfoForProcess(process, &info))
     return nullptr;
   return NativeWindow::FromRenderView(
       info.embedder->GetRenderProcessHost()->GetID(),
index 6a974df9c0fa716860bfeee509067b48387c60c1..6a92ab996e4e06556a332455b57e0c4ac81e731a 100644 (file)
@@ -44,19 +44,6 @@ struct FindByProcessId {
   int child_process_id_;
 };
 
-bool GetWebViewInfo(content::RenderProcessHost* host,
-                    WebViewManager::WebViewInfo* info) {
-  if (!host)
-    return false;
-  auto context = host->GetBrowserContext();
-  if (!context)
-    return false;
-  auto manager = context->GetGuestManager();
-  if (!manager)
-    return false;
-  return static_cast<WebViewManager*>(manager)->GetInfo(host->GetID(), info);
-}
-
 }  // namespace
 
 AtomBrowserClient::AtomBrowserClient()
@@ -110,15 +97,15 @@ void AtomBrowserClient::OverrideWebkitPrefs(
   }
 
   // Custom preferences of guest page.
+  auto process = render_view_host->GetProcess();
   WebViewManager::WebViewInfo info;
-  if (GetWebViewInfo(render_view_host->GetProcess(), &info)) {
+  if (WebViewManager::GetInfoForProcess(process, &info)) {
     prefs->web_security_enabled = !info.disable_web_security;
     return;
   }
 
   NativeWindow* window = NativeWindow::FromRenderView(
-      render_view_host->GetProcess()->GetID(),
-      render_view_host->GetRoutingID());
+      process->GetID(), render_view_host->GetRoutingID());
   if (window)
     window->OverrideWebkitPrefs(url, prefs);
 }
@@ -168,7 +155,7 @@ void AtomBrowserClient::AppendExtraCommandLineSwitches(
     // Append commnad line arguments for guest web view.
     auto child_process = content::RenderProcessHost::FromID(child_process_id);
     WebViewManager::WebViewInfo info;
-    if (GetWebViewInfo(child_process, &info)) {
+    if (WebViewManager::GetInfoForProcess(child_process, &info)) {
       command_line->AppendSwitchASCII(
           switches::kGuestInstanceID,
           base::IntToString(info.guest_instance_id));
index f744ed3e08c29669bdadc7bcd6bb11c6d13b21c7..6d614574d5d90ae83cd8d7e2b947d9fcb4850f2e 100644 (file)
@@ -42,8 +42,11 @@ struct Converter<atom::WebViewManager::WebViewInfo> {
     if (!options.Get("preloadUrl", &preload_url))
       return false;
 
-    return net::FileURLToFilePath(preload_url, &(out->preload_script)) &&
-           options.Get("nodeIntegration", &(out->node_integration)) &&
+    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));
   }
@@ -53,6 +56,20 @@ struct Converter<atom::WebViewManager::WebViewInfo> {
 
 namespace atom {
 
+// static
+bool WebViewManager::GetInfoForProcess(content::RenderProcessHost* process,
+                                       WebViewInfo* info) {
+  if (!process)
+    return false;
+  auto context = process->GetBrowserContext();
+  if (!context)
+    return false;
+  auto manager = context->GetGuestManager();
+  if (!manager)
+    return false;
+  return static_cast<WebViewManager*>(manager)->GetInfo(process->GetID(), info);
+}
+
 WebViewManager::WebViewManager(content::BrowserContext* context) {
 }
 
index 5b26bef9d996829d47c76ef7f020aae9465a6fb7..b9ef039495dc721c03e1a1ac4413c356313d0d86 100644 (file)
 
 namespace content {
 class BrowserContext;
+class RenderProcessHost;
 }
 
 namespace atom {
 
 class WebViewManager : public content::BrowserPluginGuestManager {
  public:
-  explicit WebViewManager(content::BrowserContext* context);
-  virtual ~WebViewManager();
-
   struct WebViewInfo {
     int guest_instance_id;
     content::WebContents* embedder;
@@ -32,6 +30,14 @@ class WebViewManager : public content::BrowserPluginGuestManager {
     base::FilePath preload_script;
   };
 
+  // Finds the WebViewManager attached with |process| and returns the
+  // WebViewInfo of it.
+  static bool GetInfoForProcess(content::RenderProcessHost* process,
+                                WebViewInfo* info);
+
+  explicit WebViewManager(content::BrowserContext* context);
+  virtual ~WebViewManager();
+
   void AddGuest(int guest_instance_id,
                 int element_instance_id,
                 content::WebContents* embedder,