Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/guest_view/web_view/web_view_guest.h" | 5 #include "extensions/browser/guest_view/web_view/web_view_guest.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 7 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 8 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 10 #include "content/public/browser/browser_context.h" | 11 #include "content/public/browser/browser_context.h" |
| 11 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/child_process_security_policy.h" | 13 #include "content/public/browser/child_process_security_policy.h" |
| 13 #include "content/public/browser/native_web_keyboard_event.h" | 14 #include "content/public/browser/native_web_keyboard_event.h" |
| 14 #include "content/public/browser/navigation_entry.h" | 15 #include "content/public/browser/navigation_entry.h" |
| 15 #include "content/public/browser/notification_details.h" | 16 #include "content/public/browser/notification_details.h" |
| 16 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 | 53 |
| 53 using base::UserMetricsAction; | 54 using base::UserMetricsAction; |
| 54 using content::RenderFrameHost; | 55 using content::RenderFrameHost; |
| 55 using content::ResourceType; | 56 using content::ResourceType; |
| 56 using content::WebContents; | 57 using content::WebContents; |
| 57 | 58 |
| 58 namespace extensions { | 59 namespace extensions { |
| 59 | 60 |
| 60 namespace { | 61 namespace { |
| 61 | 62 |
| 63 using GetNextUniqueIDFunction = base::Callback<int(void)>; | |
|
Devlin
2015/02/11 00:00:48
remove
Xi Han
2015/02/11 16:00:23
Done.
| |
| 64 | |
| 65 using WebViewKey = std::pair<int, int>; | |
| 66 using WebViewKeyToIDMap = std::map<WebViewKey, int>; | |
| 67 base::LazyInstance<WebViewKeyToIDMap> web_view_key_to_id_map = | |
| 68 LAZY_INSTANCE_INITIALIZER; | |
| 69 base::LazyInstance<WebViewKeyToIDMap> unique_instance_id_map = | |
| 70 LAZY_INSTANCE_INITIALIZER; | |
| 71 | |
| 72 int current_instance_id = 0; | |
| 73 | |
| 62 std::string WindowOpenDispositionToString( | 74 std::string WindowOpenDispositionToString( |
| 63 WindowOpenDisposition window_open_disposition) { | 75 WindowOpenDisposition window_open_disposition) { |
| 64 switch (window_open_disposition) { | 76 switch (window_open_disposition) { |
| 65 case IGNORE_ACTION: | 77 case IGNORE_ACTION: |
| 66 return "ignore"; | 78 return "ignore"; |
| 67 case SAVE_TO_DISK: | 79 case SAVE_TO_DISK: |
| 68 return "save_to_disk"; | 80 return "save_to_disk"; |
| 69 case CURRENT_TAB: | 81 case CURRENT_TAB: |
| 70 return "current_tab"; | 82 return "current_tab"; |
| 71 case NEW_BACKGROUND_TAB: | 83 case NEW_BACKGROUND_TAB: |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 93 return "killed"; | 105 return "killed"; |
| 94 case base::TERMINATION_STATUS_PROCESS_CRASHED: | 106 case base::TERMINATION_STATUS_PROCESS_CRASHED: |
| 95 return "crashed"; | 107 return "crashed"; |
| 96 case base::TERMINATION_STATUS_MAX_ENUM: | 108 case base::TERMINATION_STATUS_MAX_ENUM: |
| 97 break; | 109 break; |
| 98 } | 110 } |
| 99 NOTREACHED() << "Unknown Termination Status."; | 111 NOTREACHED() << "Unknown Termination Status."; |
| 100 return "unknown"; | 112 return "unknown"; |
| 101 } | 113 } |
| 102 | 114 |
| 115 bool GetIDFromMap(const WebViewKey& key, | |
|
Devlin
2015/02/11 00:00:48
Add a comment - something like "Looks up the id fo
Xi Han
2015/02/11 16:00:23
Thanks:)
| |
| 116 const WebViewKeyToIDMap& map, | |
|
Devlin
2015/02/11 00:00:48
indentation
Xi Han
2015/02/11 16:00:23
Done.
| |
| 117 int default_id, | |
| 118 int* result) { | |
| 119 WebViewKeyToIDMap::const_iterator iter = map.end(); | |
| 120 if (!key.first || !key.second) | |
| 121 *result = default_id; | |
| 122 else if ((iter = map.find(key)) != map.end()) | |
| 123 *result = iter->second; | |
| 124 else | |
| 125 return false; | |
| 126 return true; | |
| 127 } | |
| 128 | |
| 103 std::string GetStoragePartitionIdFromSiteURL(const GURL& site_url) { | 129 std::string GetStoragePartitionIdFromSiteURL(const GURL& site_url) { |
| 104 const std::string& partition_id = site_url.query(); | 130 const std::string& partition_id = site_url.query(); |
| 105 bool persist_storage = site_url.path().find("persist") != std::string::npos; | 131 bool persist_storage = site_url.path().find("persist") != std::string::npos; |
| 106 return (persist_storage ? webview::kPersistPrefix : "") + partition_id; | 132 return (persist_storage ? webview::kPersistPrefix : "") + partition_id; |
| 107 } | 133 } |
| 108 | 134 |
| 109 void ParsePartitionParam(const base::DictionaryValue& create_params, | 135 void ParsePartitionParam(const base::DictionaryValue& create_params, |
| 110 std::string* storage_partition_id, | 136 std::string* storage_partition_id, |
| 111 bool* persist_storage) { | 137 bool* persist_storage) { |
| 112 std::string partition_str; | 138 std::string partition_str; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 // The partition name is user supplied value, which we have encoded when the | 200 // The partition name is user supplied value, which we have encoded when the |
| 175 // URL was created, so it needs to be decoded. | 201 // URL was created, so it needs to be decoded. |
| 176 *partition_name = | 202 *partition_name = |
| 177 net::UnescapeURLComponent(site.query(), net::UnescapeRule::NORMAL); | 203 net::UnescapeURLComponent(site.query(), net::UnescapeRule::NORMAL); |
| 178 return true; | 204 return true; |
| 179 } | 205 } |
| 180 | 206 |
| 181 // static | 207 // static |
| 182 const char WebViewGuest::Type[] = "webview"; | 208 const char WebViewGuest::Type[] = "webview"; |
| 183 | 209 |
| 184 using WebViewKey = std::pair<int, int>; | |
| 185 using WebViewKeyToIDMap = std::map<WebViewKey, int>; | |
| 186 static base::LazyInstance<WebViewKeyToIDMap> web_view_key_to_id_map = | |
| 187 LAZY_INSTANCE_INITIALIZER; | |
| 188 | |
| 189 // static | 210 // static |
| 190 int WebViewGuest::GetOrGenerateRulesRegistryID( | 211 int WebViewGuest::GetOrGenerateRulesRegistryID( |
| 191 int embedder_process_id, | 212 int embedder_process_id, |
| 192 int webview_instance_id) { | 213 int web_view_instance_id) { |
| 193 bool is_web_view = embedder_process_id && webview_instance_id; | 214 WebViewKey key(embedder_process_id, web_view_instance_id); |
| 194 if (!is_web_view) | 215 WebViewKeyToIDMap& map = web_view_key_to_id_map.Get(); |
| 195 return RulesRegistryService::kDefaultRulesRegistryID; | 216 int id = 0; |
| 196 | 217 if (!GetIDFromMap( |
| 197 WebViewKey key = std::make_pair(embedder_process_id, webview_instance_id); | 218 key, map, RulesRegistryService::kDefaultRulesRegistryID, &id)) { |
| 198 auto it = web_view_key_to_id_map.Get().find(key); | 219 auto rph = content::RenderProcessHost::FromID(embedder_process_id); |
| 199 if (it != web_view_key_to_id_map.Get().end()) | 220 id = RulesRegistryService::Get(rph->GetBrowserContext())-> |
| 200 return it->second; | 221 GetNextRulesRegistryID(); |
| 201 | 222 map[key] = id; |
| 202 auto rph = content::RenderProcessHost::FromID(embedder_process_id); | 223 } |
| 203 int rules_registry_id = | 224 return id; |
| 204 RulesRegistryService::Get(rph->GetBrowserContext())-> | |
| 205 GetNextRulesRegistryID(); | |
| 206 web_view_key_to_id_map.Get()[key] = rules_registry_id; | |
| 207 return rules_registry_id; | |
| 208 } | 225 } |
| 209 | 226 |
| 210 // static | 227 // static |
| 228 int WebViewGuest::GetOrGenerateUniqueInstanceID( | |
| 229 int embedder_process_id, | |
| 230 int guest_instance_id ) { | |
| 231 WebViewKey key(embedder_process_id, guest_instance_id); | |
| 232 WebViewKeyToIDMap& map = unique_instance_id_map.Get(); | |
| 233 int id = 0; | |
| 234 if (!GetIDFromMap(key, map, HostID::kDefaultInstanceId, &id)) { | |
| 235 id = ++current_instance_id; | |
| 236 map[key] = id; | |
| 237 } | |
| 238 return id; | |
| 239 } | |
| 240 | |
| 241 // static | |
| 211 int WebViewGuest::GetViewInstanceId(WebContents* contents) { | 242 int WebViewGuest::GetViewInstanceId(WebContents* contents) { |
| 212 auto guest = FromWebContents(contents); | 243 auto guest = FromWebContents(contents); |
| 213 if (!guest) | 244 if (!guest) |
| 214 return guestview::kInstanceIDNone; | 245 return guestview::kInstanceIDNone; |
| 215 | 246 |
| 216 return guest->view_instance_id(); | 247 return guest->view_instance_id(); |
| 217 } | 248 } |
| 218 | 249 |
| 219 bool WebViewGuest::CanRunInDetachedState() const { | 250 bool WebViewGuest::CanRunInDetachedState() const { |
| 220 return true; | 251 return true; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 287 content::Source<WebContents>(web_contents())); | 318 content::Source<WebContents>(web_contents())); |
| 288 | 319 |
| 289 if (web_view_guest_delegate_) | 320 if (web_view_guest_delegate_) |
| 290 web_view_guest_delegate_->OnDidInitialize(); | 321 web_view_guest_delegate_->OnDidInitialize(); |
| 291 AttachWebViewHelpers(web_contents()); | 322 AttachWebViewHelpers(web_contents()); |
| 292 | 323 |
| 293 rules_registry_id_ = GetOrGenerateRulesRegistryID( | 324 rules_registry_id_ = GetOrGenerateRulesRegistryID( |
| 294 owner_web_contents()->GetRenderProcessHost()->GetID(), | 325 owner_web_contents()->GetRenderProcessHost()->GetID(), |
| 295 view_instance_id()); | 326 view_instance_id()); |
| 296 | 327 |
| 328 GetOrGenerateUniqueInstanceID( | |
| 329 owner_web_contents()->GetRenderProcessHost()->GetID(), | |
| 330 guest_instance_id()); | |
| 331 | |
| 297 // We must install the mapping from guests to WebViews prior to resuming | 332 // We must install the mapping from guests to WebViews prior to resuming |
| 298 // suspended resource loads so that the WebRequest API will catch resource | 333 // suspended resource loads so that the WebRequest API will catch resource |
| 299 // requests. | 334 // requests. |
| 300 PushWebViewStateToIOThread(); | 335 PushWebViewStateToIOThread(); |
| 301 | 336 |
| 302 ApplyAttributes(create_params); | 337 ApplyAttributes(create_params); |
| 303 } | 338 } |
| 304 | 339 |
| 305 void WebViewGuest::AttachWebViewHelpers(WebContents* contents) { | 340 void WebViewGuest::AttachWebViewHelpers(WebContents* contents) { |
| 306 if (web_view_guest_delegate_) | 341 if (web_view_guest_delegate_) |
| 307 web_view_guest_delegate_->OnAttachWebViewHelpers(contents); | 342 web_view_guest_delegate_->OnAttachWebViewHelpers(contents); |
| 308 web_view_permission_helper_.reset(new WebViewPermissionHelper(this)); | 343 web_view_permission_helper_.reset(new WebViewPermissionHelper(this)); |
| 309 } | 344 } |
| 310 | 345 |
| 311 void WebViewGuest::DidStopLoading() { | 346 void WebViewGuest::DidStopLoading() { |
| 312 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 347 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
| 313 DispatchEventToView( | 348 DispatchEventToView( |
| 314 new GuestViewBase::Event(webview::kEventLoadStop, args.Pass())); | 349 new GuestViewBase::Event(webview::kEventLoadStop, args.Pass())); |
| 315 } | 350 } |
| 316 | 351 |
| 317 void WebViewGuest::EmbedderWillBeDestroyed() { | 352 void WebViewGuest::EmbedderWillBeDestroyed() { |
| 318 // Clean up rules registries for the webview. | 353 // Clean up rules registries for the webview. |
| 319 RulesRegistryService::Get(browser_context()) | 354 RulesRegistryService::Get(browser_context()) |
| 320 ->RemoveRulesRegistriesByID(rules_registry_id_); | 355 ->RemoveRulesRegistriesByID(rules_registry_id_); |
| 321 WebViewKey key(owner_web_contents()->GetRenderProcessHost()->GetID(), | 356 WebViewKey key(owner_web_contents()->GetRenderProcessHost()->GetID(), |
| 322 view_instance_id()); | 357 view_instance_id()); |
| 323 web_view_key_to_id_map.Get().erase(key); | 358 web_view_key_to_id_map.Get().erase(key); |
| 359 unique_instance_id_map.Get().erase(key); | |
| 324 | 360 |
| 325 content::BrowserThread::PostTask( | 361 content::BrowserThread::PostTask( |
| 326 content::BrowserThread::IO, | 362 content::BrowserThread::IO, |
| 327 FROM_HERE, | 363 FROM_HERE, |
| 328 base::Bind( | 364 base::Bind( |
| 329 &RemoveWebViewEventListenersOnIOThread, | 365 &RemoveWebViewEventListenersOnIOThread, |
| 330 browser_context(), | 366 browser_context(), |
| 331 owner_extension_id(), | 367 owner_extension_id(), |
| 332 owner_web_contents()->GetRenderProcessHost()->GetID(), | 368 owner_web_contents()->GetRenderProcessHost()->GetID(), |
| 333 view_instance_id())); | 369 view_instance_id())); |
| (...skipping 881 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1215 WebViewGuest::From(owner_web_contents()->GetRenderProcessHost()->GetID(), | 1251 WebViewGuest::From(owner_web_contents()->GetRenderProcessHost()->GetID(), |
| 1216 new_window_instance_id); | 1252 new_window_instance_id); |
| 1217 if (!guest) | 1253 if (!guest) |
| 1218 return; | 1254 return; |
| 1219 | 1255 |
| 1220 if (!allow) | 1256 if (!allow) |
| 1221 guest->Destroy(); | 1257 guest->Destroy(); |
| 1222 } | 1258 } |
| 1223 | 1259 |
| 1224 } // namespace extensions | 1260 } // namespace extensions |
| OLD | NEW |