diff --git a/atom.gyp b/atom.gyp index 7fb533745345..c0fac768b04b 100644 --- a/atom.gyp +++ b/atom.gyp @@ -192,8 +192,6 @@ 'atom/browser/ui/x/x_window_utils.h', 'atom/browser/web_view/web_view_manager.cc', 'atom/browser/web_view/web_view_manager.h', - 'atom/browser/web_view/web_view_renderer_state.cc', - 'atom/browser/web_view/web_view_renderer_state.h', 'atom/browser/web_dialog_helper.cc', 'atom/browser/web_dialog_helper.h', 'atom/browser/window_list.cc', diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 4d11322950ff..167a304bac03 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -7,7 +7,7 @@ #include "atom/browser/atom_browser_context.h" #include "atom/browser/native_window.h" #include "atom/browser/web_dialog_helper.h" -#include "atom/browser/web_view/web_view_renderer_state.h" +#include "atom/browser/web_view/web_view_manager.h" #include "atom/common/api/api_messages.h" #include "atom/common/native_mate_converters/gfx_converter.h" #include "atom/common/native_mate_converters/gurl_converter.h" @@ -39,9 +39,10 @@ v8::Persistent 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(); - WebViewRendererState::WebViewInfo info; - if (!WebViewRendererState::GetInstance()->GetInfo(guest_process_id, &info)) + WebViewManager::WebViewInfo info; + if (!static_cast(manager)->GetInfo(guest_process_id, &info)) return nullptr; return NativeWindow::FromRenderView( info.embedder->GetRenderProcessHost()->GetID(), diff --git a/atom/browser/atom_browser_client.cc b/atom/browser/atom_browser_client.cc index 4a5d2d968bed..6a974df9c0fa 100644 --- a/atom/browser/atom_browser_client.cc +++ b/atom/browser/atom_browser_client.cc @@ -9,7 +9,7 @@ #include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/atom_speech_recognition_manager_delegate.h" #include "atom/browser/native_window.h" -#include "atom/browser/web_view/web_view_renderer_state.h" +#include "atom/browser/web_view/web_view_manager.h" #include "atom/browser/window_list.h" #include "atom/common/options_switches.h" #include "base/command_line.h" @@ -44,6 +44,19 @@ 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(manager)->GetInfo(host->GetID(), info); +} + } // namespace AtomBrowserClient::AtomBrowserClient() @@ -97,9 +110,8 @@ void AtomBrowserClient::OverrideWebkitPrefs( } // Custom preferences of guest page. - int guest_process_id = render_view_host->GetProcess()->GetID(); - WebViewRendererState::WebViewInfo info; - if (WebViewRendererState::GetInstance()->GetInfo(guest_process_id, &info)) { + WebViewManager::WebViewInfo info; + if (GetWebViewInfo(render_view_host->GetProcess(), &info)) { prefs->web_security_enabled = !info.disable_web_security; return; } @@ -154,8 +166,9 @@ void AtomBrowserClient::AppendExtraCommandLineSwitches( window->AppendExtraCommandLineSwitches(command_line, child_process_id); } else { // Append commnad line arguments for guest web view. - WebViewRendererState::WebViewInfo info; - if (WebViewRendererState::GetInstance()->GetInfo(child_process_id, &info)) { + auto child_process = content::RenderProcessHost::FromID(child_process_id); + WebViewManager::WebViewInfo info; + if (GetWebViewInfo(child_process, &info)) { command_line->AppendSwitchASCII( switches::kGuestInstanceID, base::IntToString(info.guest_instance_id)); diff --git a/atom/browser/web_view/web_view_manager.cc b/atom/browser/web_view/web_view_manager.cc index 9298ec05f44b..f744ed3e08c2 100644 --- a/atom/browser/web_view/web_view_manager.cc +++ b/atom/browser/web_view/web_view_manager.cc @@ -6,7 +6,6 @@ #include "atom/browser/api/atom_api_web_contents.h" #include "atom/browser/atom_browser_context.h" -#include "atom/browser/web_view/web_view_renderer_state.h" #include "atom/common/native_mate_converters/gurl_converter.h" #include "base/bind.h" #include "base/stl_util.h" @@ -32,15 +31,20 @@ struct Converter { }; template<> -struct Converter { +struct Converter { static bool FromV8(v8::Isolate* isolate, v8::Handle val, - atom::WebViewManager::WebViewOptions* out) { + atom::WebViewManager::WebViewInfo* out) { Dictionary options; if (!ConvertFromV8(isolate, val, &options)) return false; - return options.Get("nodeIntegration", &(out->node_integration)) && + + GURL preload_url; + if (!options.Get("preloadUrl", &preload_url)) + return false; + + return net::FileURLToFilePath(preload_url, &(out->preload_script)) && + options.Get("nodeIntegration", &(out->node_integration)) && options.Get("plugins", &(out->plugins)) && - options.Get("preloadUrl", &(out->preload_url)) && options.Get("disableWebSecurity", &(out->disable_web_security)); } }; @@ -59,20 +63,14 @@ void WebViewManager::AddGuest(int guest_instance_id, int element_instance_id, content::WebContents* embedder, content::WebContents* web_contents, - const WebViewOptions& options) { + WebViewInfo info) { + base::AutoLock auto_lock(lock_); web_contents_map_[guest_instance_id] = { web_contents, embedder }; - WebViewRendererState::WebViewInfo web_view_info = { - guest_instance_id, - embedder, - options.node_integration, - options.plugins, - options.disable_web_security, - }; - net::FileURLToFilePath(options.preload_url, &web_view_info.preload_script); - WebViewRendererState::GetInstance()->AddGuest( - web_contents->GetRenderProcessHost()->GetID(), - web_view_info); + int guest_process_id = web_contents->GetRenderProcessHost()->GetID(); + info.guest_instance_id = guest_instance_id; + info.embedder = embedder; + webview_info_map_[guest_process_id] = info; // Map the element in embedder to guest. ElementInstanceKey key(embedder, element_instance_id); @@ -80,16 +78,17 @@ void WebViewManager::AddGuest(int guest_instance_id, } void WebViewManager::RemoveGuest(int guest_instance_id) { + base::AutoLock auto_lock(lock_); if (!ContainsKey(web_contents_map_, guest_instance_id)) { return; } auto web_contents = web_contents_map_[guest_instance_id].web_contents; - WebViewRendererState::GetInstance()->RemoveGuest( - web_contents->GetRenderProcessHost()->GetID()); - web_contents_map_.erase(guest_instance_id); + int guest_process_id = web_contents->GetRenderProcessHost()->GetID(); + webview_info_map_.erase(guest_process_id); + // Remove the record of element in embedder too. for (const auto& element : element_instance_id_to_guest_map_) if (element.second == guest_instance_id) { @@ -98,6 +97,16 @@ void WebViewManager::RemoveGuest(int guest_instance_id) { } } +bool WebViewManager::GetInfo(int guest_process_id, WebViewInfo* webview_info) { + base::AutoLock auto_lock(lock_); + WebViewInfoMap::iterator iter = webview_info_map_.find(guest_process_id); + if (iter != webview_info_map_.end()) { + *webview_info = iter->second; + return true; + } + return false; +} + content::WebContents* WebViewManager::GetGuestByInstanceID( content::WebContents* embedder, int element_instance_id) { diff --git a/atom/browser/web_view/web_view_manager.h b/atom/browser/web_view/web_view_manager.h index bc58bd34f49d..5b26bef9d996 100644 --- a/atom/browser/web_view/web_view_manager.h +++ b/atom/browser/web_view/web_view_manager.h @@ -7,6 +7,8 @@ #include +#include "base/files/file_path.h" +#include "base/synchronization/lock.h" #include "content/public/browser/browser_plugin_guest_manager.h" #include "url/gurl.h" @@ -21,20 +23,26 @@ class WebViewManager : public content::BrowserPluginGuestManager { explicit WebViewManager(content::BrowserContext* context); virtual ~WebViewManager(); - struct WebViewOptions { + struct WebViewInfo { + int guest_instance_id; + content::WebContents* embedder; bool node_integration; bool plugins; bool disable_web_security; - GURL preload_url; + base::FilePath preload_script; }; void AddGuest(int guest_instance_id, int element_instance_id, content::WebContents* embedder, content::WebContents* web_contents, - const WebViewOptions& options); + WebViewInfo info); void RemoveGuest(int guest_instance_id); + // Looks up the information for the embedder for a given render + // view, if one exists. Called on the IO thread. + bool GetInfo(int guest_process_id, WebViewInfo* webview_info); + protected: // content::BrowserPluginGuestManager: content::WebContents* GetGuestByInstanceID( @@ -76,6 +84,11 @@ class WebViewManager : public content::BrowserPluginGuestManager { }; std::map element_instance_id_to_guest_map_; + typedef std::map WebViewInfoMap; + WebViewInfoMap webview_info_map_; + + base::Lock lock_; + DISALLOW_COPY_AND_ASSIGN(WebViewManager); }; diff --git a/atom/browser/web_view/web_view_renderer_state.cc b/atom/browser/web_view/web_view_renderer_state.cc deleted file mode 100644 index 2df9d3ade098..000000000000 --- a/atom/browser/web_view/web_view_renderer_state.cc +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2014 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/web_view_renderer_state.h" - -#include "content/public/browser/browser_thread.h" - -namespace atom { - -// static -WebViewRendererState* WebViewRendererState::GetInstance() { - return Singleton::get(); -} - -WebViewRendererState::WebViewRendererState() { -} - -WebViewRendererState::~WebViewRendererState() { -} - -void WebViewRendererState::AddGuest(int guest_process_id, - const WebViewInfo& webview_info) { - base::AutoLock auto_lock(lock_); - webview_info_map_[guest_process_id] = webview_info; -} - -void WebViewRendererState::RemoveGuest(int guest_process_id) { - base::AutoLock auto_lock(lock_); - webview_info_map_.erase(guest_process_id); -} - -bool WebViewRendererState::GetInfo(int guest_process_id, - WebViewInfo* webview_info) { - base::AutoLock auto_lock(lock_); - WebViewInfoMap::iterator iter = webview_info_map_.find(guest_process_id); - if (iter != webview_info_map_.end()) { - *webview_info = iter->second; - return true; - } - return false; -} - -} // namespace atom diff --git a/atom/browser/web_view/web_view_renderer_state.h b/atom/browser/web_view/web_view_renderer_state.h deleted file mode 100644 index 27edbcfbf159..000000000000 --- a/atom/browser/web_view/web_view_renderer_state.h +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright (c) 2014 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_WEB_VIEW_RENDERER_STATE_H_ -#define ATOM_BROWSER_WEB_VIEW_WEB_VIEW_RENDERER_STATE_H_ - -#include -#include -#include - -#include "base/files/file_path.h" -#include "base/memory/singleton.h" -#include "base/synchronization/lock.h" - -namespace content { -class WebContents; -} - -namespace atom { - -class WebViewManager; - -// This class keeps track of renderer state for use on the IO thread. -// All methods should be called on the IO thread. -class WebViewRendererState { - public: - struct WebViewInfo { - int guest_instance_id; - content::WebContents* embedder; - bool node_integration; - bool plugins; - bool disable_web_security; - base::FilePath preload_script; - }; - - static WebViewRendererState* GetInstance(); - - // Looks up the information for the embedder for a given render - // view, if one exists. Called on the IO thread. - bool GetInfo(int guest_process_id, WebViewInfo* webview_info); - - private: - friend class WebViewManager; - friend struct DefaultSingletonTraits; - - typedef std::map WebViewInfoMap; - - WebViewRendererState(); - ~WebViewRendererState(); - - // Adds or removes a guest render process from the set. - void AddGuest(int render_process_id, const WebViewInfo& webview_info); - void RemoveGuest(int render_process_id); - - WebViewInfoMap webview_info_map_; - base::Lock lock_; - - DISALLOW_COPY_AND_ASSIGN(WebViewRendererState); -}; - -} // namespace atom - -#endif // ATOM_BROWSER_WEB_VIEW_WEB_VIEW_RENDERER_STATE_H_