Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: extensions/browser/guest_view/web_view/web_view_guest.cc

Issue 885493007: Refactoring: de-couple Extensions from "script injection System" [render side] : 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Devlin@'s comments. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 52
53 using base::UserMetricsAction; 53 using base::UserMetricsAction;
54 using content::RenderFrameHost; 54 using content::RenderFrameHost;
55 using content::ResourceType; 55 using content::ResourceType;
56 using content::WebContents; 56 using content::WebContents;
57 57
58 namespace extensions { 58 namespace extensions {
59 59
60 namespace { 60 namespace {
61 61
62 int current_script_injection_instance_id = 0;
63
62 std::string WindowOpenDispositionToString( 64 std::string WindowOpenDispositionToString(
63 WindowOpenDisposition window_open_disposition) { 65 WindowOpenDisposition window_open_disposition) {
64 switch (window_open_disposition) { 66 switch (window_open_disposition) {
65 case IGNORE_ACTION: 67 case IGNORE_ACTION:
66 return "ignore"; 68 return "ignore";
67 case SAVE_TO_DISK: 69 case SAVE_TO_DISK:
68 return "save_to_disk"; 70 return "save_to_disk";
69 case CURRENT_TAB: 71 case CURRENT_TAB:
70 return "current_tab"; 72 return "current_tab";
71 case NEW_BACKGROUND_TAB: 73 case NEW_BACKGROUND_TAB:
(...skipping 21 matching lines...) Expand all
93 return "killed"; 95 return "killed";
94 case base::TERMINATION_STATUS_PROCESS_CRASHED: 96 case base::TERMINATION_STATUS_PROCESS_CRASHED:
95 return "crashed"; 97 return "crashed";
96 case base::TERMINATION_STATUS_MAX_ENUM: 98 case base::TERMINATION_STATUS_MAX_ENUM:
97 break; 99 break;
98 } 100 }
99 NOTREACHED() << "Unknown Termination Status."; 101 NOTREACHED() << "Unknown Termination Status.";
100 return "unknown"; 102 return "unknown";
101 } 103 }
102 104
105 int GetNextScriptInjectionInstanceID() {
106 return ++current_script_injection_instance_id;
107 }
108
103 std::string GetStoragePartitionIdFromSiteURL(const GURL& site_url) { 109 std::string GetStoragePartitionIdFromSiteURL(const GURL& site_url) {
104 const std::string& partition_id = site_url.query(); 110 const std::string& partition_id = site_url.query();
105 bool persist_storage = site_url.path().find("persist") != std::string::npos; 111 bool persist_storage = site_url.path().find("persist") != std::string::npos;
106 return (persist_storage ? webview::kPersistPrefix : "") + partition_id; 112 return (persist_storage ? webview::kPersistPrefix : "") + partition_id;
107 } 113 }
108 114
109 void ParsePartitionParam(const base::DictionaryValue& create_params, 115 void ParsePartitionParam(const base::DictionaryValue& create_params,
110 std::string* storage_partition_id, 116 std::string* storage_partition_id,
111 bool* persist_storage) { 117 bool* persist_storage) {
112 std::string partition_str; 118 std::string partition_str;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 return true; 184 return true;
179 } 185 }
180 186
181 // static 187 // static
182 const char WebViewGuest::Type[] = "webview"; 188 const char WebViewGuest::Type[] = "webview";
183 189
184 using WebViewKey = std::pair<int, int>; 190 using WebViewKey = std::pair<int, int>;
185 using WebViewKeyToIDMap = std::map<WebViewKey, int>; 191 using WebViewKeyToIDMap = std::map<WebViewKey, int>;
186 static base::LazyInstance<WebViewKeyToIDMap> web_view_key_to_id_map = 192 static base::LazyInstance<WebViewKeyToIDMap> web_view_key_to_id_map =
187 LAZY_INSTANCE_INITIALIZER; 193 LAZY_INSTANCE_INITIALIZER;
194 static base::LazyInstance<WebViewKeyToIDMap> script_injection_instance_id_map =
Devlin 2015/02/06 00:28:41 It bothers me that we have multiple different maps
Xi Han 2015/02/06 17:21:45 We would like to have just one function, but the i
Devlin 2015/02/06 18:48:36 But the RulesRegistry one is going away, right?
Xi Han 2015/02/06 19:45:33 Hmmm, I don't think so, because the declarative we
Devlin 2015/02/09 17:40:24 Ah, right. In that case, can we make GetRulesRegi
Xi Han 2015/02/09 23:28:11 As discussed offline, we will still keeps two maps
195 LAZY_INSTANCE_INITIALIZER;
188 196
189 // static 197 // static
190 int WebViewGuest::GetOrGenerateRulesRegistryID( 198 int WebViewGuest::GetOrGenerateRulesRegistryID(
191 int embedder_process_id, 199 int embedder_process_id,
192 int webview_instance_id) { 200 int webview_instance_id) {
193 bool is_web_view = embedder_process_id && webview_instance_id; 201 bool is_web_view = embedder_process_id && webview_instance_id;
194 if (!is_web_view) 202 if (!is_web_view)
195 return RulesRegistryService::kDefaultRulesRegistryID; 203 return RulesRegistryService::kDefaultRulesRegistryID;
196 204
197 WebViewKey key = std::make_pair(embedder_process_id, webview_instance_id); 205 WebViewKey key = std::make_pair(embedder_process_id, webview_instance_id);
198 auto it = web_view_key_to_id_map.Get().find(key); 206 auto it = web_view_key_to_id_map.Get().find(key);
199 if (it != web_view_key_to_id_map.Get().end()) 207 if (it != web_view_key_to_id_map.Get().end())
200 return it->second; 208 return it->second;
201 209
202 auto rph = content::RenderProcessHost::FromID(embedder_process_id); 210 auto rph = content::RenderProcessHost::FromID(embedder_process_id);
203 int rules_registry_id = 211 int rules_registry_id =
204 RulesRegistryService::Get(rph->GetBrowserContext())-> 212 RulesRegistryService::Get(rph->GetBrowserContext())->
205 GetNextRulesRegistryID(); 213 GetNextRulesRegistryID();
206 web_view_key_to_id_map.Get()[key] = rules_registry_id; 214 web_view_key_to_id_map.Get()[key] = rules_registry_id;
207 return rules_registry_id; 215 return rules_registry_id;
208 } 216 }
209 217
210 // static 218 // static
219 int WebViewGuest:: GetOrGenerateScriptInjectionInstanceID(
220 int embedder_process_id,
221 int guest_instance_id ) {
222 if (!(embedder_process_id && guest_instance_id))
223 return Host::kDefaultInstanceId;
224
225 WebViewKey key = std::make_pair(embedder_process_id, guest_instance_id);
226 WebViewKeyToIDMap& script_injection_ids =
227 script_injection_instance_id_map.Get();
228 auto it = script_injection_ids.find(key);
229 if (it != script_injection_ids.end())
230 return it->second;
231
232 int script_injection_instance_id = GetNextScriptInjectionInstanceID();
233 script_injection_ids[key] = script_injection_instance_id;
234 return script_injection_instance_id;
235 }
236
237 // static
211 int WebViewGuest::GetViewInstanceId(WebContents* contents) { 238 int WebViewGuest::GetViewInstanceId(WebContents* contents) {
212 auto guest = FromWebContents(contents); 239 auto guest = FromWebContents(contents);
213 if (!guest) 240 if (!guest)
214 return guestview::kInstanceIDNone; 241 return guestview::kInstanceIDNone;
215 242
216 return guest->view_instance_id(); 243 return guest->view_instance_id();
217 } 244 }
218 245
219 bool WebViewGuest::CanRunInDetachedState() const { 246 bool WebViewGuest::CanRunInDetachedState() const {
220 return true; 247 return true;
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 content::Source<WebContents>(web_contents())); 314 content::Source<WebContents>(web_contents()));
288 315
289 if (web_view_guest_delegate_) 316 if (web_view_guest_delegate_)
290 web_view_guest_delegate_->OnDidInitialize(); 317 web_view_guest_delegate_->OnDidInitialize();
291 AttachWebViewHelpers(web_contents()); 318 AttachWebViewHelpers(web_contents());
292 319
293 rules_registry_id_ = GetOrGenerateRulesRegistryID( 320 rules_registry_id_ = GetOrGenerateRulesRegistryID(
294 owner_web_contents()->GetRenderProcessHost()->GetID(), 321 owner_web_contents()->GetRenderProcessHost()->GetID(),
295 view_instance_id()); 322 view_instance_id());
296 323
324 GetOrGenerateScriptInjectionInstanceID(
325 owner_web_contents()->GetRenderProcessHost()->GetID(),
326 guest_instance_id());
327
297 // We must install the mapping from guests to WebViews prior to resuming 328 // We must install the mapping from guests to WebViews prior to resuming
298 // suspended resource loads so that the WebRequest API will catch resource 329 // suspended resource loads so that the WebRequest API will catch resource
299 // requests. 330 // requests.
300 PushWebViewStateToIOThread(); 331 PushWebViewStateToIOThread();
301 332
302 ApplyAttributes(create_params); 333 ApplyAttributes(create_params);
303 } 334 }
304 335
305 void WebViewGuest::AttachWebViewHelpers(WebContents* contents) { 336 void WebViewGuest::AttachWebViewHelpers(WebContents* contents) {
306 if (web_view_guest_delegate_) 337 if (web_view_guest_delegate_)
307 web_view_guest_delegate_->OnAttachWebViewHelpers(contents); 338 web_view_guest_delegate_->OnAttachWebViewHelpers(contents);
308 web_view_permission_helper_.reset(new WebViewPermissionHelper(this)); 339 web_view_permission_helper_.reset(new WebViewPermissionHelper(this));
309 } 340 }
310 341
311 void WebViewGuest::DidStopLoading() { 342 void WebViewGuest::DidStopLoading() {
312 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); 343 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
313 DispatchEventToView( 344 DispatchEventToView(
314 new GuestViewBase::Event(webview::kEventLoadStop, args.Pass())); 345 new GuestViewBase::Event(webview::kEventLoadStop, args.Pass()));
315 } 346 }
316 347
317 void WebViewGuest::EmbedderWillBeDestroyed() { 348 void WebViewGuest::EmbedderWillBeDestroyed() {
318 // Clean up rules registries for the webview. 349 // Clean up rules registries for the webview.
319 RulesRegistryService::Get(browser_context()) 350 RulesRegistryService::Get(browser_context())
320 ->RemoveRulesRegistriesByID(rules_registry_id_); 351 ->RemoveRulesRegistriesByID(rules_registry_id_);
321 WebViewKey key(owner_web_contents()->GetRenderProcessHost()->GetID(), 352 WebViewKey key(owner_web_contents()->GetRenderProcessHost()->GetID(),
322 view_instance_id()); 353 view_instance_id());
323 web_view_key_to_id_map.Get().erase(key); 354 web_view_key_to_id_map.Get().erase(key);
355 script_injection_instance_id_map.Get().erase(key);
324 356
325 content::BrowserThread::PostTask( 357 content::BrowserThread::PostTask(
326 content::BrowserThread::IO, 358 content::BrowserThread::IO,
327 FROM_HERE, 359 FROM_HERE,
328 base::Bind( 360 base::Bind(
329 &RemoveWebViewEventListenersOnIOThread, 361 &RemoveWebViewEventListenersOnIOThread,
330 browser_context(), 362 browser_context(),
331 owner_extension_id(), 363 owner_extension_id(),
332 owner_web_contents()->GetRenderProcessHost()->GetID(), 364 owner_web_contents()->GetRenderProcessHost()->GetID(),
333 view_instance_id())); 365 view_instance_id()));
(...skipping 881 matching lines...) Expand 10 before | Expand all | Expand 10 after
1215 WebViewGuest::From(owner_web_contents()->GetRenderProcessHost()->GetID(), 1247 WebViewGuest::From(owner_web_contents()->GetRenderProcessHost()->GetID(),
1216 new_window_instance_id); 1248 new_window_instance_id);
1217 if (!guest) 1249 if (!guest)
1218 return; 1250 return;
1219 1251
1220 if (!allow) 1252 if (!allow)
1221 guest->Destroy(); 1253 guest->Destroy();
1222 } 1254 }
1223 1255
1224 } // namespace extensions 1256 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698