OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "extensions/browser/guest_view/web_view/web_view_content_script_manager
.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/memory/linked_ptr.h" |
| 9 #include "content/public/browser/browser_context.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/navigation_details.h" |
| 12 #include "content/public/browser/render_process_host.h" |
| 13 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/browser/web_contents_observer.h" |
| 15 #include "extensions/browser/declarative_user_script_manager.h" |
| 16 #include "extensions/browser/declarative_user_script_master.h" |
| 17 #include "extensions/browser/extension_system.h" |
| 18 #include "extensions/browser/guest_view/guest_view_manager.h" |
| 19 #include "extensions/browser/guest_view/web_view/web_view_constants.h" |
| 20 #include "extensions/browser/guest_view/web_view/web_view_guest.h" |
| 21 #include "extensions/browser/guest_view/web_view/web_view_renderer_state.h" |
| 22 |
| 23 using content::BrowserThread; |
| 24 |
| 25 namespace extensions { |
| 26 |
| 27 // This observer ensures that the content scripts added by the guest are removed |
| 28 // when its embedder goes away. |
| 29 // The OwnerWebContentsObserver object will be destroyed when the embedder web |
| 30 // contents it observed is gone. |
| 31 class WebViewContentScriptManager::OwnerWebContentsObserver |
| 32 : public content::WebContentsObserver { |
| 33 public: |
| 34 OwnerWebContentsObserver(content::WebContents* embedder_web_contents, |
| 35 const HostID& host_id, |
| 36 WebViewContentScriptManager* manager) |
| 37 : WebContentsObserver(embedder_web_contents), |
| 38 host_id_(host_id), |
| 39 web_view_content_script_manager_(manager) {} |
| 40 ~OwnerWebContentsObserver() override {} |
| 41 |
| 42 // WebContentsObserver: |
| 43 void WebContentsDestroyed() override { |
| 44 // If the embedder is destroyed then remove all the content scripts of the |
| 45 // guest. |
| 46 RemoveContentScripts(); |
| 47 } |
| 48 void DidNavigateMainFrame( |
| 49 const content::LoadCommittedDetails& details, |
| 50 const content::FrameNavigateParams& params) override { |
| 51 // If the embedder navigates to a different page then destroy the guest. |
| 52 if (details.is_navigation_to_different_page()) |
| 53 RemoveContentScripts(); |
| 54 } |
| 55 void RenderProcessGone(base::TerminationStatus status) override { |
| 56 // If the embedder crashes, then destroy the guest. |
| 57 RemoveContentScripts(); |
| 58 } |
| 59 |
| 60 void add_view_instance_id(int view_instance_id) { |
| 61 view_instance_ids_.insert(view_instance_id); |
| 62 } |
| 63 |
| 64 private: |
| 65 void RemoveContentScripts() { |
| 66 DCHECK(web_view_content_script_manager_); |
| 67 |
| 68 // Step 1: removes content scripts of all the guests embedded. |
| 69 for (int view_instance_id : view_instance_ids_) { |
| 70 web_view_content_script_manager_->RemoveContentScripts( |
| 71 web_contents(), view_instance_id, host_id_, |
| 72 std::vector<std::string>()); |
| 73 } |
| 74 // Step 2: removes this observer. |
| 75 // This object can be deleted after this line. |
| 76 web_view_content_script_manager_->RemoveObserver(web_contents()); |
| 77 } |
| 78 |
| 79 HostID host_id_; |
| 80 std::set<int> view_instance_ids_; |
| 81 WebViewContentScriptManager* web_view_content_script_manager_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(OwnerWebContentsObserver); |
| 84 }; |
| 85 |
| 86 WebViewContentScriptManager::WebViewContentScriptManager( |
| 87 content::BrowserContext* browser_context) |
| 88 : browser_context_(browser_context) { |
| 89 } |
| 90 |
| 91 WebViewContentScriptManager::~WebViewContentScriptManager() { |
| 92 } |
| 93 |
| 94 WebViewContentScriptManager* WebViewContentScriptManager::Get( |
| 95 content::BrowserContext* browser_context) { |
| 96 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 97 WebViewContentScriptManager* manager = |
| 98 static_cast<WebViewContentScriptManager*>(browser_context->GetUserData( |
| 99 webview::kWebViewContentScriptManagerKeyName)); |
| 100 if (!manager) { |
| 101 manager = new WebViewContentScriptManager(browser_context); |
| 102 browser_context->SetUserData(webview::kWebViewContentScriptManagerKeyName, |
| 103 manager); |
| 104 } |
| 105 return manager; |
| 106 } |
| 107 |
| 108 void WebViewContentScriptManager::AddContentScripts( |
| 109 content::WebContents* embedder_web_contents, |
| 110 int view_instance_id, |
| 111 const HostID& host_id, |
| 112 const std::set<UserScript>& scripts) { |
| 113 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 114 DCHECK(embedder_web_contents); |
| 115 |
| 116 DeclarativeUserScriptMaster* master = |
| 117 ExtensionSystem::Get(browser_context_) |
| 118 ->declarative_user_script_manager() |
| 119 ->GetDeclarativeUserScriptMasterByID(host_id); |
| 120 DCHECK(master); |
| 121 |
| 122 // We need to update WebViewRenderState in the IO thread if the guest exists. |
| 123 std::set<int> ids_to_add; |
| 124 |
| 125 int embedder_process_id = |
| 126 embedder_web_contents->GetRenderProcessHost()->GetID(); |
| 127 GuestMapKey key = std::pair<int, int>(embedder_process_id, view_instance_id); |
| 128 GuestContentScriptMap::iterator iter = guest_content_script_map_.find(key); |
| 129 |
| 130 // Step 1: finds the entry in guest_content_script_map_ by the given |key|. |
| 131 // If there isn't any content script added for the given guest yet, insert an |
| 132 // empty map first. |
| 133 if (iter == guest_content_script_map_.end()) { |
| 134 iter = guest_content_script_map_.insert( |
| 135 iter, |
| 136 std::pair<GuestMapKey, ContentScriptMap>(key, ContentScriptMap())); |
| 137 } |
| 138 |
| 139 // Step 2: updates the guest_content_script_map_. |
| 140 ContentScriptMap& map = iter->second; |
| 141 for (const UserScript& script : scripts) { |
| 142 auto map_iter = map.find(script.name()); |
| 143 // If a content script has the same name as the new one, remove the old |
| 144 // script first, and insert the new one. |
| 145 if (map_iter != map.end()) { |
| 146 master->RemoveScript(map_iter->second); |
| 147 map.erase(map_iter); |
| 148 } |
| 149 map.insert(std::pair<std::string, UserScript>(script.name(), script)); |
| 150 ids_to_add.insert(script.id()); |
| 151 } |
| 152 |
| 153 // Step 3: adds new scripts to the master. |
| 154 master->AddScripts(scripts); |
| 155 |
| 156 // Step 4: creates owner web contents observer for the given |
| 157 // |embedder_web_contents| if it doesn't exist. |
| 158 auto observer_iter = |
| 159 owner_web_contents_observer_map_.find(embedder_web_contents); |
| 160 if (observer_iter == owner_web_contents_observer_map_.end()) { |
| 161 linked_ptr<OwnerWebContentsObserver> observer( |
| 162 new OwnerWebContentsObserver(embedder_web_contents, host_id, this)); |
| 163 observer->add_view_instance_id(view_instance_id); |
| 164 owner_web_contents_observer_map_[embedder_web_contents] = observer; |
| 165 } else { |
| 166 observer_iter->second->add_view_instance_id(view_instance_id); |
| 167 } |
| 168 |
| 169 // Step 5: updates WebViewRenderState in the IO thread. |
| 170 // It is safe to use base::Unretained(WebViewRendererState::GetInstance()) |
| 171 // since WebViewRendererState::GetInstance() always returns a Singleton of |
| 172 // WebViewRendererState. |
| 173 if (!ids_to_add.empty()) { |
| 174 content::BrowserThread::PostTask( |
| 175 content::BrowserThread::IO, FROM_HERE, |
| 176 base::Bind(&WebViewRendererState::AddContentScriptIDs, |
| 177 base::Unretained(WebViewRendererState::GetInstance()), |
| 178 embedder_process_id, view_instance_id, ids_to_add)); |
| 179 } |
| 180 } |
| 181 |
| 182 void WebViewContentScriptManager::RemoveContentScripts( |
| 183 content::WebContents* embedder_web_contents, |
| 184 int view_instance_id, |
| 185 const HostID& host_id, |
| 186 const std::vector<std::string>& script_name_list) { |
| 187 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 188 |
| 189 int embedder_process_id = |
| 190 embedder_web_contents->GetRenderProcessHost()->GetID(); |
| 191 GuestMapKey key = std::pair<int, int>(embedder_process_id, view_instance_id); |
| 192 GuestContentScriptMap::iterator script_map_iter = |
| 193 guest_content_script_map_.find(key); |
| 194 if (script_map_iter == guest_content_script_map_.end()) |
| 195 return; |
| 196 |
| 197 DeclarativeUserScriptMaster* master = |
| 198 ExtensionSystem::Get(browser_context_) |
| 199 ->declarative_user_script_manager() |
| 200 ->GetDeclarativeUserScriptMasterByID(host_id); |
| 201 CHECK(master); |
| 202 |
| 203 // We need to update WebViewRenderState in the IO thread if the guest exists. |
| 204 std::set<int> ids_to_delete; |
| 205 std::set<UserScript> scripts_to_delete; |
| 206 |
| 207 // Step 1: removes content scripts from |master| and updates |
| 208 // |guest_content_script_map_| |
| 209 std::map<std::string, UserScript>& map = script_map_iter->second; |
| 210 // If the |script_name_list| is empty, all the content scripts added by the |
| 211 // guest will be removed; otherwise, removes the scripts in the |
| 212 // |script_name_list|. |
| 213 if (script_name_list.empty()) { |
| 214 auto it = map.begin(); |
| 215 while (it != map.end()) { |
| 216 scripts_to_delete.insert(it->second); |
| 217 ids_to_delete.insert(it->second.id()); |
| 218 map.erase(it++); |
| 219 } |
| 220 } else { |
| 221 for (const std::string& name : script_name_list) { |
| 222 ContentScriptMap::iterator iter = map.find(name); |
| 223 if (iter == map.end()) |
| 224 continue; |
| 225 const UserScript& script = iter->second; |
| 226 ids_to_delete.insert(script.id()); |
| 227 scripts_to_delete.insert(script); |
| 228 map.erase(iter); |
| 229 } |
| 230 } |
| 231 |
| 232 // Step 2: removes content scripts from master. |
| 233 master->RemoveScripts(scripts_to_delete); |
| 234 |
| 235 // Step 3: updates WebViewRenderState in the IO thread. |
| 236 if (!ids_to_delete.empty()) { |
| 237 content::BrowserThread::PostTask( |
| 238 content::BrowserThread::IO, FROM_HERE, |
| 239 base::Bind(&WebViewRendererState::RemoveContentScriptIDs, |
| 240 base::Unretained(WebViewRendererState::GetInstance()), |
| 241 embedder_process_id, view_instance_id, ids_to_delete)); |
| 242 } |
| 243 } |
| 244 |
| 245 void WebViewContentScriptManager::RemoveObserver( |
| 246 content::WebContents* embedder_web_contents) { |
| 247 owner_web_contents_observer_map_.erase(embedder_web_contents); |
| 248 } |
| 249 |
| 250 std::set<int> WebViewContentScriptManager::GetContentScriptIDSet( |
| 251 int embedder_process_id, |
| 252 int view_instance_id) { |
| 253 std::set<int> ids; |
| 254 |
| 255 GuestMapKey key = std::pair<int, int>(embedder_process_id, view_instance_id); |
| 256 GuestContentScriptMap::const_iterator iter = |
| 257 guest_content_script_map_.find(key); |
| 258 if (iter == guest_content_script_map_.end()) |
| 259 return ids; |
| 260 const ContentScriptMap& map = iter->second; |
| 261 for (const auto& pair : map) |
| 262 ids.insert(pair.second.id()); |
| 263 |
| 264 return ids; |
| 265 } |
| 266 |
| 267 } // namespace extensions |
OLD | NEW |