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 "chrome/browser/extensions/user_script_loader.h" | 5 #include "chrome/browser/extensions/user_script_loader.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/memory/shared_memory.h" | |
15 #include "base/version.h" | 14 #include "base/version.h" |
16 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
17 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
18 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
20 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
21 #include "extensions/browser/component_extension_resource_manager.h" | |
22 #include "extensions/browser/content_verifier.h" | 20 #include "extensions/browser/content_verifier.h" |
23 #include "extensions/browser/extension_registry.h" | |
24 #include "extensions/browser/extension_system.h" | |
25 #include "extensions/browser/extensions_browser_client.h" | |
26 #include "extensions/common/extension_messages.h" | 21 #include "extensions/common/extension_messages.h" |
27 #include "extensions/common/file_util.h" | 22 #include "extensions/common/file_util.h" |
28 #include "extensions/common/manifest_handlers/default_locale_handler.h" | |
29 #include "extensions/common/message_bundle.h" | |
30 #include "extensions/common/one_shot_event.h" | |
31 #include "ui/base/resource/resource_bundle.h" | |
32 | 23 |
33 using content::BrowserThread; | 24 using content::BrowserThread; |
34 using extensions::ExtensionsBrowserClient; | |
35 | 25 |
36 namespace extensions { | 26 namespace extensions { |
37 | 27 |
38 namespace { | 28 namespace { |
39 | 29 |
40 typedef base::Callback< | 30 using LoadScriptsCallback = |
41 void(scoped_ptr<UserScriptList>, scoped_ptr<base::SharedMemory>)> | 31 base::Callback<void(scoped_ptr<UserScriptList>, |
42 LoadScriptsCallback; | 32 scoped_ptr<base::SharedMemory>)>; |
43 | 33 |
44 void VerifyContent(scoped_refptr<ContentVerifier> verifier, | 34 UserScriptLoader::SubstitutionMap* GetLocalizationMessages( |
45 const ExtensionId& extension_id, | 35 const UserScriptLoader::HostsInfo& hosts_info, |
46 const base::FilePath& extension_root, | 36 const HostID& host_id) { |
47 const base::FilePath& relative_path, | 37 UserScriptLoader::HostsInfo::const_iterator iter = hosts_info.find(host_id); |
48 const std::string& content) { | 38 if (iter == hosts_info.end()) |
49 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 39 return nullptr; |
50 scoped_refptr<ContentVerifyJob> job( | 40 return file_util::LoadMessageBundleSubstitutionMap( |
51 verifier->CreateJobFor(extension_id, extension_root, relative_path)); | 41 iter->second.first, host_id.id(), iter->second.second); |
52 if (job.get()) { | |
53 job->Start(); | |
54 job->BytesRead(content.size(), content.data()); | |
55 job->DoneReading(); | |
56 } | |
57 } | 42 } |
58 | 43 |
59 bool LoadScriptContent(const ExtensionId& extension_id, | 44 void LoadUserScripts( |
60 UserScript::File* script_file, | 45 UserScriptList* user_scripts, |
61 const SubstitutionMap* localization_messages, | 46 const UserScriptLoader::HostsInfo& hosts_info, |
62 scoped_refptr<ContentVerifier> verifier) { | 47 const std::set<int>& added_script_ids, |
63 std::string content; | 48 const scoped_refptr<ContentVerifier>& verifier, |
64 const base::FilePath& path = ExtensionResource::GetFilePath( | 49 UserScriptLoader::LoadUserScriptsContentFunction callback) { |
65 script_file->extension_root(), | |
66 script_file->relative_path(), | |
67 ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT); | |
68 if (path.empty()) { | |
69 int resource_id; | |
70 if (ExtensionsBrowserClient::Get()->GetComponentExtensionResourceManager()-> | |
71 IsComponentExtensionResource(script_file->extension_root(), | |
72 script_file->relative_path(), | |
73 &resource_id)) { | |
74 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
75 content = rb.GetRawDataResource(resource_id).as_string(); | |
76 } else { | |
77 LOG(WARNING) << "Failed to get file path to " | |
78 << script_file->relative_path().value() << " from " | |
79 << script_file->extension_root().value(); | |
80 return false; | |
81 } | |
82 } else { | |
83 if (!base::ReadFileToString(path, &content)) { | |
84 LOG(WARNING) << "Failed to load user script file: " << path.value(); | |
85 return false; | |
86 } | |
87 if (verifier.get()) { | |
88 content::BrowserThread::PostTask(content::BrowserThread::IO, | |
89 FROM_HERE, | |
90 base::Bind(&VerifyContent, | |
91 verifier, | |
92 extension_id, | |
93 script_file->extension_root(), | |
94 script_file->relative_path(), | |
95 content)); | |
96 } | |
97 } | |
98 | |
99 // Localize the content. | |
100 if (localization_messages) { | |
101 std::string error; | |
102 MessageBundle::ReplaceMessagesWithExternalDictionary( | |
103 *localization_messages, &content, &error); | |
104 if (!error.empty()) { | |
105 LOG(WARNING) << "Failed to replace messages in script: " << error; | |
106 } | |
107 } | |
108 | |
109 // Remove BOM from the content. | |
110 std::string::size_type index = content.find(base::kUtf8ByteOrderMark); | |
111 if (index == 0) { | |
112 script_file->set_content(content.substr(strlen(base::kUtf8ByteOrderMark))); | |
113 } else { | |
114 script_file->set_content(content); | |
115 } | |
116 | |
117 return true; | |
118 } | |
119 | |
120 SubstitutionMap* GetLocalizationMessages(const ExtensionsInfo& extensions_info, | |
121 const ExtensionId& extension_id) { | |
122 ExtensionsInfo::const_iterator iter = extensions_info.find(extension_id); | |
123 if (iter == extensions_info.end()) | |
124 return NULL; | |
125 return file_util::LoadMessageBundleSubstitutionMap( | |
126 iter->second.first, extension_id, iter->second.second); | |
127 } | |
128 | |
129 void LoadUserScripts(UserScriptList* user_scripts, | |
130 const ExtensionsInfo& extensions_info, | |
131 const std::set<int>& added_script_ids, | |
132 ContentVerifier* verifier) { | |
133 for (UserScriptList::iterator script = user_scripts->begin(); | 50 for (UserScriptList::iterator script = user_scripts->begin(); |
134 script != user_scripts->end(); | 51 script != user_scripts->end(); |
135 ++script) { | 52 ++script) { |
136 if (added_script_ids.count(script->id()) == 0) | 53 if (added_script_ids.count(script->id()) == 0) |
137 continue; | 54 continue; |
138 scoped_ptr<SubstitutionMap> localization_messages( | 55 scoped_ptr<UserScriptLoader::SubstitutionMap> localization_messages( |
139 GetLocalizationMessages(extensions_info, script->extension_id())); | 56 GetLocalizationMessages(hosts_info, script->host_id())); |
140 for (size_t k = 0; k < script->js_scripts().size(); ++k) { | 57 for (size_t k = 0; k < script->js_scripts().size(); ++k) { |
141 UserScript::File& script_file = script->js_scripts()[k]; | 58 UserScript::File& script_file = script->js_scripts()[k]; |
142 if (script_file.GetContent().empty()) | 59 if (script_file.GetContent().empty()) |
143 LoadScriptContent(script->extension_id(), &script_file, NULL, verifier); | 60 callback.Run(script->host_id(), &script_file, NULL, verifier); |
144 } | 61 } |
145 for (size_t k = 0; k < script->css_scripts().size(); ++k) { | 62 for (size_t k = 0; k < script->css_scripts().size(); ++k) { |
146 UserScript::File& script_file = script->css_scripts()[k]; | 63 UserScript::File& script_file = script->css_scripts()[k]; |
147 if (script_file.GetContent().empty()) | 64 if (script_file.GetContent().empty()) |
148 LoadScriptContent(script->extension_id(), | 65 callback.Run(script->host_id(), &script_file, |
149 &script_file, | 66 localization_messages.get(), verifier); |
150 localization_messages.get(), | |
151 verifier); | |
152 } | 67 } |
153 } | 68 } |
154 } | 69 } |
155 | 70 |
156 // Pickle user scripts and return pointer to the shared memory. | 71 // Pickle user scripts and return pointer to the shared memory. |
157 scoped_ptr<base::SharedMemory> Serialize(const UserScriptList& scripts) { | 72 scoped_ptr<base::SharedMemory> Serialize(const UserScriptList& scripts) { |
158 Pickle pickle; | 73 Pickle pickle; |
159 pickle.WriteSizeT(scripts.size()); | 74 pickle.WriteSizeT(scripts.size()); |
160 for (UserScriptList::const_iterator script = scripts.begin(); | 75 for (UserScriptList::const_iterator script = scripts.begin(); |
161 script != scripts.end(); | 76 script != scripts.end(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 108 |
194 base::SharedMemoryHandle readonly_handle; | 109 base::SharedMemoryHandle readonly_handle; |
195 if (!shared_memory.ShareReadOnlyToProcess(base::GetCurrentProcessHandle(), | 110 if (!shared_memory.ShareReadOnlyToProcess(base::GetCurrentProcessHandle(), |
196 &readonly_handle)) | 111 &readonly_handle)) |
197 return scoped_ptr<base::SharedMemory>(); | 112 return scoped_ptr<base::SharedMemory>(); |
198 | 113 |
199 return make_scoped_ptr(new base::SharedMemory(readonly_handle, | 114 return make_scoped_ptr(new base::SharedMemory(readonly_handle, |
200 /*read_only=*/true)); | 115 /*read_only=*/true)); |
201 } | 116 } |
202 | 117 |
203 void LoadScriptsOnFileThread(scoped_ptr<UserScriptList> user_scripts, | 118 void LoadScriptsOnFileThread( |
204 const ExtensionsInfo& extensions_info, | 119 scoped_ptr<UserScriptList> user_scripts, |
205 const std::set<int>& added_script_ids, | 120 const UserScriptLoader::HostsInfo& hosts_info, |
206 scoped_refptr<ContentVerifier> verifier, | 121 const std::set<int>& added_script_ids, |
207 LoadScriptsCallback callback) { | 122 const scoped_refptr<ContentVerifier>& verifier, |
| 123 UserScriptLoader::LoadUserScriptsContentFunction function, |
| 124 LoadScriptsCallback callback) { |
208 DCHECK(user_scripts.get()); | 125 DCHECK(user_scripts.get()); |
209 LoadUserScripts( | 126 LoadUserScripts(user_scripts.get(), hosts_info, added_script_ids, |
210 user_scripts.get(), extensions_info, added_script_ids, verifier.get()); | 127 verifier, function); |
211 scoped_ptr<base::SharedMemory> memory = Serialize(*user_scripts); | 128 scoped_ptr<base::SharedMemory> memory = Serialize(*user_scripts); |
212 BrowserThread::PostTask( | 129 BrowserThread::PostTask( |
213 BrowserThread::UI, | 130 BrowserThread::UI, |
214 FROM_HERE, | 131 FROM_HERE, |
215 base::Bind(callback, base::Passed(&user_scripts), base::Passed(&memory))); | 132 base::Bind(callback, base::Passed(&user_scripts), base::Passed(&memory))); |
216 } | 133 } |
217 | 134 |
218 // Helper function to parse greasesmonkey headers | 135 // Helper function to parse greasesmonkey headers |
219 bool GetDeclarationValue(const base::StringPiece& line, | 136 bool GetDeclarationValue(const base::StringPiece& line, |
220 const base::StringPiece& prefix, | 137 const base::StringPiece& prefix, |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 } | 240 } |
324 | 241 |
325 // If no patterns were specified, default to @include *. This is what | 242 // If no patterns were specified, default to @include *. This is what |
326 // Greasemonkey does. | 243 // Greasemonkey does. |
327 if (script->globs().empty() && script->url_patterns().is_empty()) | 244 if (script->globs().empty() && script->url_patterns().is_empty()) |
328 script->add_glob("*"); | 245 script->add_glob("*"); |
329 | 246 |
330 return true; | 247 return true; |
331 } | 248 } |
332 | 249 |
333 // static | |
334 void UserScriptLoader::LoadScriptsForTest(UserScriptList* user_scripts) { | 250 void UserScriptLoader::LoadScriptsForTest(UserScriptList* user_scripts) { |
335 ExtensionsInfo info; | 251 HostsInfo info; |
336 std::set<int> added_script_ids; | 252 std::set<int> added_script_ids; |
337 for (UserScriptList::iterator it = user_scripts->begin(); | 253 for (UserScriptList::iterator it = user_scripts->begin(); |
338 it != user_scripts->end(); | 254 it != user_scripts->end(); |
339 ++it) { | 255 ++it) { |
340 added_script_ids.insert(it->id()); | 256 added_script_ids.insert(it->id()); |
341 } | 257 } |
342 LoadUserScripts( | 258 LoadUserScripts(user_scripts, info, added_script_ids, |
343 user_scripts, info, added_script_ids, NULL /* no verifier for testing */); | 259 NULL /* no verifier for testing */, |
| 260 GetLoadUserScriptsFunction()); |
344 } | 261 } |
345 | 262 |
346 UserScriptLoader::UserScriptLoader(Profile* profile, | 263 UserScriptLoader::UserScriptLoader( |
347 const ExtensionId& owner_extension_id, | 264 Profile* profile, |
348 bool listen_for_extension_system_loaded) | 265 const HostID& host_id, |
| 266 const scoped_refptr<ContentVerifier>& content_verifier) |
349 : user_scripts_(new UserScriptList()), | 267 : user_scripts_(new UserScriptList()), |
350 clear_scripts_(false), | 268 clear_scripts_(false), |
351 extension_system_ready_(false), | 269 ready_(false), |
352 pending_load_(false), | 270 pending_load_(false), |
353 profile_(profile), | 271 profile_(profile), |
354 owner_extension_id_(owner_extension_id), | 272 host_id_(host_id), |
355 extension_registry_observer_(this), | 273 content_verifier_(content_verifier), |
356 weak_factory_(this) { | 274 weak_factory_(this) { |
357 extension_registry_observer_.Add(ExtensionRegistry::Get(profile)); | |
358 if (listen_for_extension_system_loaded) { | |
359 ExtensionSystem::Get(profile_)->ready().Post( | |
360 FROM_HERE, | |
361 base::Bind(&UserScriptLoader::OnExtensionSystemReady, | |
362 weak_factory_.GetWeakPtr())); | |
363 } else { | |
364 extension_system_ready_ = true; | |
365 } | |
366 registrar_.Add(this, | 275 registrar_.Add(this, |
367 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 276 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
368 content::NotificationService::AllBrowserContextsAndSources()); | 277 content::NotificationService::AllBrowserContextsAndSources()); |
369 } | 278 } |
370 | 279 |
371 UserScriptLoader::~UserScriptLoader() { | 280 UserScriptLoader::~UserScriptLoader() { |
372 } | 281 } |
373 | 282 |
374 void UserScriptLoader::AddScripts(const std::set<UserScript>& scripts) { | 283 void UserScriptLoader::AddScripts(const std::set<UserScript>& scripts) { |
375 for (std::set<UserScript>::const_iterator it = scripts.begin(); | 284 for (std::set<UserScript>::const_iterator it = scripts.begin(); |
(...skipping 25 matching lines...) Expand all Loading... |
401 void UserScriptLoader::Observe(int type, | 310 void UserScriptLoader::Observe(int type, |
402 const content::NotificationSource& source, | 311 const content::NotificationSource& source, |
403 const content::NotificationDetails& details) { | 312 const content::NotificationDetails& details) { |
404 DCHECK_EQ(type, content::NOTIFICATION_RENDERER_PROCESS_CREATED); | 313 DCHECK_EQ(type, content::NOTIFICATION_RENDERER_PROCESS_CREATED); |
405 content::RenderProcessHost* process = | 314 content::RenderProcessHost* process = |
406 content::Source<content::RenderProcessHost>(source).ptr(); | 315 content::Source<content::RenderProcessHost>(source).ptr(); |
407 Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext()); | 316 Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext()); |
408 if (!profile_->IsSameProfile(profile)) | 317 if (!profile_->IsSameProfile(profile)) |
409 return; | 318 return; |
410 if (scripts_ready()) { | 319 if (scripts_ready()) { |
411 SendUpdate(process, | 320 SendUpdate(process, shared_memory_.get(), |
412 shared_memory_.get(), | 321 std::set<HostID>()); // Include all hosts. |
413 std::set<ExtensionId>()); // Include all extensions. | |
414 } | 322 } |
415 } | 323 } |
416 | 324 |
417 void UserScriptLoader::OnExtensionUnloaded( | |
418 content::BrowserContext* browser_context, | |
419 const Extension* extension, | |
420 UnloadedExtensionInfo::Reason reason) { | |
421 extensions_info_.erase(extension->id()); | |
422 } | |
423 | |
424 void UserScriptLoader::OnExtensionSystemReady() { | |
425 extension_system_ready_ = true; | |
426 AttemptLoad(); | |
427 } | |
428 | |
429 bool UserScriptLoader::ScriptsMayHaveChanged() const { | 325 bool UserScriptLoader::ScriptsMayHaveChanged() const { |
430 // Scripts may have changed if there are scripts added, scripts removed, or | 326 // Scripts may have changed if there are scripts added, scripts removed, or |
431 // if scripts were cleared and either: | 327 // if scripts were cleared and either: |
432 // (1) A load is in progress (which may result in a non-zero number of | 328 // (1) A load is in progress (which may result in a non-zero number of |
433 // scripts that need to be cleared), or | 329 // scripts that need to be cleared), or |
434 // (2) The current set of scripts is non-empty (so they need to be cleared). | 330 // (2) The current set of scripts is non-empty (so they need to be cleared). |
435 return (added_scripts_.size() || | 331 return (added_scripts_.size() || |
436 removed_scripts_.size() || | 332 removed_scripts_.size() || |
437 (clear_scripts_ && | 333 (clear_scripts_ && |
438 (is_loading() || user_scripts_->size()))); | 334 (is_loading() || user_scripts_->size()))); |
439 } | 335 } |
440 | 336 |
441 void UserScriptLoader::AttemptLoad() { | 337 void UserScriptLoader::AttemptLoad() { |
442 if (extension_system_ready_ && ScriptsMayHaveChanged()) { | 338 if (ready_ && ScriptsMayHaveChanged()) { |
443 if (is_loading()) | 339 if (is_loading()) |
444 pending_load_ = true; | 340 pending_load_ = true; |
445 else | 341 else |
446 StartLoad(); | 342 StartLoad(); |
447 } | 343 } |
448 } | 344 } |
449 | 345 |
450 void UserScriptLoader::StartLoad() { | 346 void UserScriptLoader::StartLoad() { |
451 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 347 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
452 DCHECK(!is_loading()); | 348 DCHECK(!is_loading()); |
(...skipping 15 matching lines...) Expand all Loading... |
468 user_scripts_->insert( | 364 user_scripts_->insert( |
469 user_scripts_->end(), added_scripts_.begin(), added_scripts_.end()); | 365 user_scripts_->end(), added_scripts_.begin(), added_scripts_.end()); |
470 | 366 |
471 std::set<int> added_script_ids; | 367 std::set<int> added_script_ids; |
472 for (std::set<UserScript>::const_iterator it = added_scripts_.begin(); | 368 for (std::set<UserScript>::const_iterator it = added_scripts_.begin(); |
473 it != added_scripts_.end(); | 369 it != added_scripts_.end(); |
474 ++it) { | 370 ++it) { |
475 added_script_ids.insert(it->id()); | 371 added_script_ids.insert(it->id()); |
476 } | 372 } |
477 | 373 |
478 // Expand |changed_extensions_| for OnScriptsLoaded, which will use it in | 374 // Expand |changed_hosts_| for OnScriptsLoaded, which will use it in |
479 // its IPC message. This must be done before we clear |added_scripts_| and | 375 // its IPC message. This must be done before we clear |added_scripts_| and |
480 // |removed_scripts_| below. | 376 // |removed_scripts_| below. |
481 std::set<UserScript> changed_scripts(added_scripts_); | 377 std::set<UserScript> changed_scripts(added_scripts_); |
482 changed_scripts.insert(removed_scripts_.begin(), removed_scripts_.end()); | 378 changed_scripts.insert(removed_scripts_.begin(), removed_scripts_.end()); |
483 ExpandChangedExtensions(changed_scripts); | 379 for (const UserScript& script : changed_scripts) |
| 380 changed_hosts_.insert(script.host_id()); |
484 | 381 |
485 // Update |extensions_info_| to contain info from every extension in | 382 // |changed_hosts_| before passing it to LoadScriptsOnFileThread. |
486 // |changed_extensions_| before passing it to LoadScriptsOnFileThread. | 383 UpdateHostsInfo(changed_hosts_); |
487 UpdateExtensionsInfo(); | |
488 | 384 |
489 BrowserThread::PostTask( | 385 BrowserThread::PostTask( |
490 BrowserThread::FILE, | 386 BrowserThread::FILE, FROM_HERE, |
491 FROM_HERE, | 387 base::Bind(&LoadScriptsOnFileThread, base::Passed(&user_scripts_), |
492 base::Bind(&LoadScriptsOnFileThread, | 388 hosts_info_, added_script_ids, content_verifier_, |
493 base::Passed(&user_scripts_), | 389 GetLoadUserScriptsFunction(), |
494 extensions_info_, | |
495 added_script_ids, | |
496 make_scoped_refptr( | |
497 ExtensionSystem::Get(profile_)->content_verifier()), | |
498 base::Bind(&UserScriptLoader::OnScriptsLoaded, | 390 base::Bind(&UserScriptLoader::OnScriptsLoaded, |
499 weak_factory_.GetWeakPtr()))); | 391 weak_factory_.GetWeakPtr()))); |
500 | 392 |
501 clear_scripts_ = false; | 393 clear_scripts_ = false; |
502 added_scripts_.clear(); | 394 added_scripts_.clear(); |
503 removed_scripts_.clear(); | 395 removed_scripts_.clear(); |
504 user_scripts_.reset(NULL); | 396 user_scripts_.reset(NULL); |
505 } | 397 } |
506 | 398 |
| 399 void UserScriptLoader::AddHostInfo(const HostID& host_id, |
| 400 const PathAndDefaultLocale& location) { |
| 401 if (hosts_info_.find(host_id) != hosts_info_.end()) |
| 402 return; |
| 403 hosts_info_[host_id] = location; |
| 404 } |
| 405 |
| 406 void UserScriptLoader::RemoveHostInfo(const HostID& host_id) { |
| 407 hosts_info_.erase(host_id); |
| 408 } |
| 409 |
| 410 void UserScriptLoader::SetReady(bool ready) { |
| 411 bool was_ready = ready_; |
| 412 ready_ = ready; |
| 413 if (ready_ && !was_ready) |
| 414 AttemptLoad(); |
| 415 } |
| 416 |
507 void UserScriptLoader::OnScriptsLoaded( | 417 void UserScriptLoader::OnScriptsLoaded( |
508 scoped_ptr<UserScriptList> user_scripts, | 418 scoped_ptr<UserScriptList> user_scripts, |
509 scoped_ptr<base::SharedMemory> shared_memory) { | 419 scoped_ptr<base::SharedMemory> shared_memory) { |
510 user_scripts_.reset(user_scripts.release()); | 420 user_scripts_.reset(user_scripts.release()); |
511 if (pending_load_) { | 421 if (pending_load_) { |
512 // While we were loading, there were further changes. Don't bother | 422 // While we were loading, there were further changes. Don't bother |
513 // notifying about these scripts and instead just immediately reload. | 423 // notifying about these scripts and instead just immediately reload. |
514 pending_load_ = false; | 424 pending_load_ = false; |
515 StartLoad(); | 425 StartLoad(); |
516 return; | 426 return; |
(...skipping 11 matching lines...) Expand all Loading... |
528 return; | 438 return; |
529 } | 439 } |
530 | 440 |
531 // We've got scripts ready to go. | 441 // We've got scripts ready to go. |
532 shared_memory_.reset(shared_memory.release()); | 442 shared_memory_.reset(shared_memory.release()); |
533 | 443 |
534 for (content::RenderProcessHost::iterator i( | 444 for (content::RenderProcessHost::iterator i( |
535 content::RenderProcessHost::AllHostsIterator()); | 445 content::RenderProcessHost::AllHostsIterator()); |
536 !i.IsAtEnd(); | 446 !i.IsAtEnd(); |
537 i.Advance()) { | 447 i.Advance()) { |
538 SendUpdate(i.GetCurrentValue(), shared_memory_.get(), changed_extensions_); | 448 SendUpdate(i.GetCurrentValue(), shared_memory_.get(), changed_hosts_); |
539 } | 449 } |
540 changed_extensions_.clear(); | 450 changed_hosts_.clear(); |
541 | 451 |
542 content::NotificationService::current()->Notify( | 452 content::NotificationService::current()->Notify( |
543 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED, | 453 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED, |
544 content::Source<Profile>(profile_), | 454 content::Source<Profile>(profile_), |
545 content::Details<base::SharedMemory>(shared_memory_.get())); | 455 content::Details<base::SharedMemory>(shared_memory_.get())); |
546 } | 456 } |
547 | 457 |
548 void UserScriptLoader::SendUpdate( | 458 void UserScriptLoader::SendUpdate(content::RenderProcessHost* process, |
549 content::RenderProcessHost* process, | 459 base::SharedMemory* shared_memory, |
550 base::SharedMemory* shared_memory, | 460 const std::set<HostID>& changed_hosts) { |
551 const std::set<ExtensionId>& changed_extensions) { | |
552 // Don't allow injection of content scripts into <webview>. | 461 // Don't allow injection of content scripts into <webview>. |
553 if (process->IsIsolatedGuest()) | 462 if (process->IsIsolatedGuest()) |
554 return; | 463 return; |
555 | 464 |
556 Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext()); | 465 Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext()); |
557 // Make sure we only send user scripts to processes in our profile. | 466 // Make sure we only send user scripts to processes in our profile. |
558 if (!profile_->IsSameProfile(profile)) | 467 if (!profile_->IsSameProfile(profile)) |
559 return; | 468 return; |
560 | 469 |
561 // If the process is being started asynchronously, early return. We'll end up | 470 // If the process is being started asynchronously, early return. We'll end up |
562 // calling InitUserScripts when it's created which will call this again. | 471 // calling InitUserScripts when it's created which will call this again. |
563 base::ProcessHandle handle = process->GetHandle(); | 472 base::ProcessHandle handle = process->GetHandle(); |
564 if (!handle) | 473 if (!handle) |
565 return; | 474 return; |
566 | 475 |
567 base::SharedMemoryHandle handle_for_process; | 476 base::SharedMemoryHandle handle_for_process; |
568 if (!shared_memory->ShareToProcess(handle, &handle_for_process)) | 477 if (!shared_memory->ShareToProcess(handle, &handle_for_process)) |
569 return; // This can legitimately fail if the renderer asserts at startup. | 478 return; // This can legitimately fail if the renderer asserts at startup. |
570 | 479 |
| 480 // TODO(hanxi): update the IPC message to send a set of HostIDs to render. |
| 481 // Also, remove this function when the refactor is done on render side. |
| 482 std::set<std::string> changed_ids_set; |
| 483 for (const HostID& id : changed_hosts) |
| 484 changed_ids_set.insert(id.id()); |
| 485 |
571 if (base::SharedMemory::IsHandleValid(handle_for_process)) { | 486 if (base::SharedMemory::IsHandleValid(handle_for_process)) { |
572 process->Send(new ExtensionMsg_UpdateUserScripts( | 487 process->Send(new ExtensionMsg_UpdateUserScripts( |
573 handle_for_process, owner_extension_id_, changed_extensions)); | 488 handle_for_process, host_id().id(), changed_ids_set)); |
574 } | 489 } |
575 } | 490 } |
576 | 491 |
577 void UserScriptLoader::ExpandChangedExtensions( | |
578 const std::set<UserScript>& scripts) { | |
579 for (std::set<UserScript>::const_iterator it = scripts.begin(); | |
580 it != scripts.end(); | |
581 ++it) { | |
582 changed_extensions_.insert(it->extension_id()); | |
583 } | |
584 } | |
585 | |
586 void UserScriptLoader::UpdateExtensionsInfo() { | |
587 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); | |
588 for (std::set<ExtensionId>::const_iterator it = changed_extensions_.begin(); | |
589 it != changed_extensions_.end(); | |
590 ++it) { | |
591 if (extensions_info_.find(*it) == extensions_info_.end()) { | |
592 const Extension* extension = | |
593 registry->GetExtensionById(*it, ExtensionRegistry::EVERYTHING); | |
594 // |changed_extensions_| may include extensions that have been removed, | |
595 // which leads to the above lookup failing. In this case, just continue. | |
596 if (!extension) | |
597 continue; | |
598 extensions_info_[*it] = ExtensionSet::ExtensionPathAndDefaultLocale( | |
599 extension->path(), LocaleInfo::GetDefaultLocale(extension)); | |
600 } | |
601 } | |
602 } | |
603 | |
604 } // namespace extensions | 492 } // namespace extensions |
OLD | NEW |