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/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "content/public/browser/browser_context.h" | 10 #include "content/public/browser/browser_context.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 #include "content/public/browser/site_instance.h" | 23 #include "content/public/browser/site_instance.h" |
| 24 #include "content/public/browser/storage_partition.h" | 24 #include "content/public/browser/storage_partition.h" |
| 25 #include "content/public/browser/user_metrics.h" | 25 #include "content/public/browser/user_metrics.h" |
| 26 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 27 #include "content/public/browser/web_contents_delegate.h" | 27 #include "content/public/browser/web_contents_delegate.h" |
| 28 #include "content/public/common/media_stream_request.h" | 28 #include "content/public/common/media_stream_request.h" |
| 29 #include "content/public/common/page_zoom.h" | 29 #include "content/public/common/page_zoom.h" |
| 30 #include "content/public/common/result_codes.h" | 30 #include "content/public/common/result_codes.h" |
| 31 #include "content/public/common/stop_find_action.h" | 31 #include "content/public/common/stop_find_action.h" |
| 32 #include "content/public/common/url_constants.h" | 32 #include "content/public/common/url_constants.h" |
| 33 #include "extensions/browser/api/declarative/rules_registry_service.h" | |
| 33 #include "extensions/browser/api/extensions_api_client.h" | 34 #include "extensions/browser/api/extensions_api_client.h" |
| 34 #include "extensions/browser/api/web_request/web_request_api.h" | 35 #include "extensions/browser/api/web_request/web_request_api.h" |
| 35 #include "extensions/browser/api/web_view/web_view_internal_api.h" | 36 #include "extensions/browser/api/web_view/web_view_internal_api.h" |
| 36 #include "extensions/browser/extension_system.h" | 37 #include "extensions/browser/extension_system.h" |
| 37 #include "extensions/browser/guest_view/guest_view_manager.h" | 38 #include "extensions/browser/guest_view/guest_view_manager.h" |
| 38 #include "extensions/browser/guest_view/web_view/web_view_constants.h" | 39 #include "extensions/browser/guest_view/web_view/web_view_constants.h" |
| 39 #include "extensions/browser/guest_view/web_view/web_view_permission_helper.h" | 40 #include "extensions/browser/guest_view/web_view/web_view_permission_helper.h" |
| 40 #include "extensions/browser/guest_view/web_view/web_view_permission_types.h" | 41 #include "extensions/browser/guest_view/web_view/web_view_permission_types.h" |
| 41 #include "extensions/browser/guest_view/web_view/web_view_renderer_state.h" | 42 #include "extensions/browser/guest_view/web_view/web_view_renderer_state.h" |
| 42 #include "extensions/common/constants.h" | 43 #include "extensions/common/constants.h" |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 // The partition name is user supplied value, which we have encoded when the | 174 // The partition name is user supplied value, which we have encoded when the |
| 174 // URL was created, so it needs to be decoded. | 175 // URL was created, so it needs to be decoded. |
| 175 *partition_name = | 176 *partition_name = |
| 176 net::UnescapeURLComponent(site.query(), net::UnescapeRule::NORMAL); | 177 net::UnescapeURLComponent(site.query(), net::UnescapeRule::NORMAL); |
| 177 return true; | 178 return true; |
| 178 } | 179 } |
| 179 | 180 |
| 180 // static | 181 // static |
| 181 const char WebViewGuest::Type[] = "webview"; | 182 const char WebViewGuest::Type[] = "webview"; |
| 182 | 183 |
| 184 typedef std::pair<int, int> WebViewKey; | |
| 185 typedef std::map<WebViewKey, int> WebViewKeyToIDMap; | |
| 186 typedef std::map<int, WebViewKey> IDToWebViewKeyMap; | |
| 187 static base::LazyInstance<WebViewKeyToIDMap> web_view_key_to_id_map = | |
| 188 LAZY_INSTANCE_INITIALIZER; | |
| 189 static base::LazyInstance<IDToWebViewKeyMap> id_to_web_view_key_map = | |
| 190 LAZY_INSTANCE_INITIALIZER; | |
| 191 | |
| 192 // static | |
| 193 int WebViewGuest::GetOrGenerateRulesRegistryID( | |
| 194 int embedder_process_id, | |
| 195 int webview_instance_id, | |
| 196 content::BrowserContext* browser_context) { | |
| 197 bool is_web_view = embedder_process_id && webview_instance_id; | |
| 198 if (!is_web_view) | |
| 199 return RulesRegistryService::kDefaultRulesRegistryID; | |
| 200 | |
| 201 WebViewKey key = std::make_pair(embedder_process_id, webview_instance_id); | |
| 202 WebViewKeyToIDMap::iterator it = web_view_key_to_id_map.Get().find(key); | |
|
Fady Samuel
2014/11/28 11:34:24
nit: Use auto.
auto it = web_view_key_to_id_map.G
Xi Han
2014/11/28 16:12:56
Done. Do we use auto whenever possible?
| |
| 203 if (it != web_view_key_to_id_map.Get().end()) { | |
|
Fady Samuel
2014/11/28 11:34:25
No need for a new block.
Xi Han
2014/11/28 16:12:56
Done.
| |
| 204 return it->second; | |
| 205 } | |
| 206 int rules_registry_id = | |
| 207 RulesRegistryService::Get(browser_context)->GetNextRulesRegistryID(); | |
| 208 web_view_key_to_id_map.Get()[key] = rules_registry_id; | |
| 209 id_to_web_view_key_map.Get()[rules_registry_id] = key; | |
| 210 return rules_registry_id; | |
| 211 } | |
| 212 | |
| 183 // static | 213 // static |
| 184 int WebViewGuest::GetViewInstanceId(WebContents* contents) { | 214 int WebViewGuest::GetViewInstanceId(WebContents* contents) { |
| 185 WebViewGuest* guest = FromWebContents(contents); | 215 WebViewGuest* guest = FromWebContents(contents); |
| 186 if (!guest) | 216 if (!guest) |
| 187 return guestview::kInstanceIDNone; | 217 return guestview::kInstanceIDNone; |
| 188 | 218 |
| 189 return guest->view_instance_id(); | 219 return guest->view_instance_id(); |
| 190 } | 220 } |
| 191 | 221 |
| 192 const char* WebViewGuest::GetAPINamespace() const { | 222 const char* WebViewGuest::GetAPINamespace() const { |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 void WebViewGuest::DidStopLoading() { | 370 void WebViewGuest::DidStopLoading() { |
| 341 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 371 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
| 342 DispatchEventToEmbedder( | 372 DispatchEventToEmbedder( |
| 343 new GuestViewBase::Event(webview::kEventLoadStop, args.Pass())); | 373 new GuestViewBase::Event(webview::kEventLoadStop, args.Pass())); |
| 344 } | 374 } |
| 345 | 375 |
| 346 void WebViewGuest::EmbedderWillBeDestroyed() { | 376 void WebViewGuest::EmbedderWillBeDestroyed() { |
| 347 if (web_view_guest_delegate_) | 377 if (web_view_guest_delegate_) |
| 348 web_view_guest_delegate_->OnEmbedderWillBeDestroyed(); | 378 web_view_guest_delegate_->OnEmbedderWillBeDestroyed(); |
| 349 | 379 |
| 380 // Clean up rules registries for the webview. | |
| 381 RulesRegistryService::Get(browser_context()) | |
| 382 ->RemoveRulesRegistryID(rules_registry_id_); | |
| 383 IDToWebViewKeyMap::iterator it = | |
| 384 id_to_web_view_key_map.Get().find(rules_registry_id_); | |
|
Fady Samuel
2014/11/28 11:34:25
Hmm, I don't think we need IDToWebViewKeyMap? A We
Xi Han
2014/11/28 16:12:56
You are right. It was used before I move the "remo
| |
| 385 if (it != id_to_web_view_key_map.Get().end()) { | |
| 386 web_view_key_to_id_map.Get().erase(it->second); | |
| 387 id_to_web_view_key_map.Get().erase(it); | |
| 388 } | |
| 389 | |
| 350 content::BrowserThread::PostTask( | 390 content::BrowserThread::PostTask( |
| 351 content::BrowserThread::IO, | 391 content::BrowserThread::IO, |
| 352 FROM_HERE, | 392 FROM_HERE, |
| 353 base::Bind( | 393 base::Bind( |
| 354 &RemoveWebViewEventListenersOnIOThread, | 394 &RemoveWebViewEventListenersOnIOThread, |
| 355 browser_context(), | 395 browser_context(), |
| 356 embedder_extension_id(), | 396 embedder_extension_id(), |
| 357 embedder_render_process_id(), | 397 embedder_render_process_id(), |
| 358 view_instance_id())); | 398 view_instance_id())); |
| 359 } | 399 } |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 661 content::StoragePartition::OriginMatcherFunction(), | 701 content::StoragePartition::OriginMatcherFunction(), |
| 662 remove_since, | 702 remove_since, |
| 663 base::Time::Now(), | 703 base::Time::Now(), |
| 664 callback); | 704 callback); |
| 665 return true; | 705 return true; |
| 666 } | 706 } |
| 667 | 707 |
| 668 WebViewGuest::WebViewGuest(content::BrowserContext* browser_context, | 708 WebViewGuest::WebViewGuest(content::BrowserContext* browser_context, |
| 669 int guest_instance_id) | 709 int guest_instance_id) |
| 670 : GuestView<WebViewGuest>(browser_context, guest_instance_id), | 710 : GuestView<WebViewGuest>(browser_context, guest_instance_id), |
| 711 rules_registry_id_(0), | |
|
Fady Samuel
2014/11/28 11:34:24
Use the kDefaultRulesRegistryID constant here?
Xi Han
2014/11/28 16:12:56
Define kInvalidRulesRegistryID in RulesRegistrySer
| |
| 671 find_helper_(this), | 712 find_helper_(this), |
| 672 is_overriding_user_agent_(false), | 713 is_overriding_user_agent_(false), |
| 673 guest_opaque_(true), | 714 guest_opaque_(true), |
| 674 javascript_dialog_helper_(this), | 715 javascript_dialog_helper_(this), |
| 675 weak_ptr_factory_(this) { | 716 weak_ptr_factory_(this) { |
| 676 web_view_guest_delegate_.reset( | 717 web_view_guest_delegate_.reset( |
| 677 ExtensionsAPIClient::Get()->CreateWebViewGuestDelegate(this)); | 718 ExtensionsAPIClient::Get()->CreateWebViewGuestDelegate(this)); |
| 678 } | 719 } |
| 679 | 720 |
| 680 WebViewGuest::~WebViewGuest() { | 721 WebViewGuest::~WebViewGuest() { |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 805 site_url, &partition_domain, &partition_id, &in_memory)) { | 846 site_url, &partition_domain, &partition_id, &in_memory)) { |
| 806 NOTREACHED(); | 847 NOTREACHED(); |
| 807 return; | 848 return; |
| 808 } | 849 } |
| 809 | 850 |
| 810 WebViewRendererState::WebViewInfo web_view_info; | 851 WebViewRendererState::WebViewInfo web_view_info; |
| 811 web_view_info.embedder_process_id = embedder_render_process_id(); | 852 web_view_info.embedder_process_id = embedder_render_process_id(); |
| 812 web_view_info.instance_id = view_instance_id(); | 853 web_view_info.instance_id = view_instance_id(); |
| 813 web_view_info.partition_id = partition_id; | 854 web_view_info.partition_id = partition_id; |
| 814 web_view_info.embedder_extension_id = embedder_extension_id(); | 855 web_view_info.embedder_extension_id = embedder_extension_id(); |
| 856 web_view_info.rules_registry_id = rules_registry_id_; | |
| 815 | 857 |
| 816 content::BrowserThread::PostTask( | 858 content::BrowserThread::PostTask( |
| 817 content::BrowserThread::IO, | 859 content::BrowserThread::IO, |
| 818 FROM_HERE, | 860 FROM_HERE, |
| 819 base::Bind(&WebViewRendererState::AddGuest, | 861 base::Bind(&WebViewRendererState::AddGuest, |
| 820 base::Unretained(WebViewRendererState::GetInstance()), | 862 base::Unretained(WebViewRendererState::GetInstance()), |
| 821 web_contents()->GetRenderProcessHost()->GetID(), | 863 web_contents()->GetRenderProcessHost()->GetID(), |
| 822 web_contents()->GetRoutingID(), | 864 web_contents()->GetRoutingID(), |
| 823 web_view_info)); | 865 web_view_info)); |
| 824 } | 866 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 877 bool user_gesture, | 919 bool user_gesture, |
| 878 bool last_unlocked_by_target, | 920 bool last_unlocked_by_target, |
| 879 const base::Callback<void(bool)>& callback) { | 921 const base::Callback<void(bool)>& callback) { |
| 880 web_view_permission_helper_->RequestPointerLockPermission( | 922 web_view_permission_helper_->RequestPointerLockPermission( |
| 881 user_gesture, | 923 user_gesture, |
| 882 last_unlocked_by_target, | 924 last_unlocked_by_target, |
| 883 callback); | 925 callback); |
| 884 } | 926 } |
| 885 | 927 |
| 886 void WebViewGuest::WillAttachToEmbedder() { | 928 void WebViewGuest::WillAttachToEmbedder() { |
| 929 int embedder_process_id = | |
| 930 embedder_web_contents()->GetRenderProcessHost()->GetID(); | |
| 931 WebViewKey key(embedder_process_id, view_instance_id()); | |
| 932 WebViewKeyToIDMap::iterator it = web_view_key_to_id_map.Get().find(key); | |
| 933 if (it != web_view_key_to_id_map.Get().end()) | |
| 934 rules_registry_id_ = web_view_key_to_id_map.Get().find(key)->second; | |
|
Fady Samuel
2014/11/28 11:34:25
Hmm, what if add rules after attachment?
I feel l
Xi Han
2014/11/28 16:12:56
This is a good point. Updated.
| |
| 935 | |
| 887 // We must install the mapping from guests to WebViews prior to resuming | 936 // We must install the mapping from guests to WebViews prior to resuming |
| 888 // suspended resource loads so that the WebRequest API will catch resource | 937 // suspended resource loads so that the WebRequest API will catch resource |
| 889 // requests. | 938 // requests. |
| 890 PushWebViewStateToIOThread(); | 939 PushWebViewStateToIOThread(); |
| 891 } | 940 } |
| 892 | 941 |
| 893 content::JavaScriptDialogManager* WebViewGuest::GetJavaScriptDialogManager( | 942 content::JavaScriptDialogManager* WebViewGuest::GetJavaScriptDialogManager( |
| 894 WebContents* source) { | 943 WebContents* source) { |
| 895 return &javascript_dialog_helper_; | 944 return &javascript_dialog_helper_; |
| 896 } | 945 } |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1219 WebViewGuest* guest = | 1268 WebViewGuest* guest = |
| 1220 WebViewGuest::From(embedder_render_process_id(), new_window_instance_id); | 1269 WebViewGuest::From(embedder_render_process_id(), new_window_instance_id); |
| 1221 if (!guest) | 1270 if (!guest) |
| 1222 return; | 1271 return; |
| 1223 | 1272 |
| 1224 if (!allow) | 1273 if (!allow) |
| 1225 guest->Destroy(); | 1274 guest->Destroy(); |
| 1226 } | 1275 } |
| 1227 | 1276 |
| 1228 } // namespace extensions | 1277 } // namespace extensions |
| OLD | NEW |