| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/extension_user_script_loader.h" | 5 #include "extensions/browser/extension_user_script_loader.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 verifier->CreateJobFor(extension_id, extension_root, relative_path)); | 49 verifier->CreateJobFor(extension_id, extension_root, relative_path)); |
| 50 if (job.get()) { | 50 if (job.get()) { |
| 51 job->Start(); | 51 job->Start(); |
| 52 job->BytesRead(content.size(), content.data()); | 52 job->BytesRead(content.size(), content.data()); |
| 53 job->DoneReading(); | 53 job->DoneReading(); |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 // Loads user scripts from the extension who owns these scripts. | 57 // Loads user scripts from the extension who owns these scripts. |
| 58 bool LoadScriptContent(const HostID& host_id, | 58 bool LoadScriptContent(const HostID& host_id, |
| 59 UserScript::File* script_file, | 59 BrowserScriptFile* script_file, |
| 60 const SubstitutionMap* localization_messages, | 60 const SubstitutionMap* localization_messages, |
| 61 const scoped_refptr<ContentVerifier>& verifier) { | 61 const scoped_refptr<ContentVerifier>& verifier) { |
| 62 DCHECK(script_file); | 62 DCHECK(script_file); |
| 63 std::string content; | 63 std::string content; |
| 64 const base::FilePath& path = ExtensionResource::GetFilePath( | 64 const base::FilePath& path = ExtensionResource::GetFilePath( |
| 65 script_file->extension_root(), script_file->relative_path(), | 65 script_file->extension_root(), script_file->relative_path(), |
| 66 ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT); | 66 ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT); |
| 67 if (path.empty()) { | 67 if (path.empty()) { |
| 68 int resource_id = 0; | 68 int resource_id = 0; |
| 69 if (ExtensionsBrowserClient::Get() | 69 if (ExtensionsBrowserClient::Get() |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 const ExtensionUserScriptLoader::HostsInfo& hosts_info, | 117 const ExtensionUserScriptLoader::HostsInfo& hosts_info, |
| 118 const HostID& host_id) { | 118 const HostID& host_id) { |
| 119 ExtensionUserScriptLoader::HostsInfo::const_iterator iter = | 119 ExtensionUserScriptLoader::HostsInfo::const_iterator iter = |
| 120 hosts_info.find(host_id); | 120 hosts_info.find(host_id); |
| 121 if (iter == hosts_info.end()) | 121 if (iter == hosts_info.end()) |
| 122 return nullptr; | 122 return nullptr; |
| 123 return file_util::LoadMessageBundleSubstitutionMap( | 123 return file_util::LoadMessageBundleSubstitutionMap( |
| 124 iter->second.first, host_id.id(), iter->second.second); | 124 iter->second.first, host_id.id(), iter->second.second); |
| 125 } | 125 } |
| 126 | 126 |
| 127 void LoadUserScripts(UserScriptList* user_scripts, | 127 void LoadUserScripts(BrowserUserScriptList* user_scripts, |
| 128 const ExtensionUserScriptLoader::HostsInfo& hosts_info, | 128 const ExtensionUserScriptLoader::HostsInfo& hosts_info, |
| 129 const std::set<int>& added_script_ids, | 129 const std::set<int>& added_script_ids, |
| 130 const scoped_refptr<ContentVerifier>& verifier) { | 130 const scoped_refptr<ContentVerifier>& verifier) { |
| 131 for (UserScript& script : *user_scripts) { | 131 for (const std::unique_ptr<BrowserUserScript>& script : *user_scripts) { |
| 132 if (added_script_ids.count(script.id()) == 0) | 132 if (added_script_ids.count(script->id()) == 0) |
| 133 continue; | 133 continue; |
| 134 for (UserScript::File& script_file : script.js_scripts()) { | 134 for (const std::unique_ptr<BrowserScriptFile>& file : |
| 135 if (script_file.GetContent().empty()) | 135 script->js_scripts()) { |
| 136 LoadScriptContent(script.host_id(), &script_file, nullptr, verifier); | 136 if (file->GetContent().empty()) |
| 137 LoadScriptContent(script->host_id(), file.get(), nullptr, verifier); |
| 137 } | 138 } |
| 138 if (script.css_scripts().size() > 0) { | 139 if (script->css_scripts().size() > 0) { |
| 139 std::unique_ptr<SubstitutionMap> localization_messages( | 140 std::unique_ptr<SubstitutionMap> localization_messages( |
| 140 GetLocalizationMessages(hosts_info, script.host_id())); | 141 GetLocalizationMessages(hosts_info, script->host_id())); |
| 141 for (UserScript::File& script_file : script.css_scripts()) { | 142 for (const std::unique_ptr<BrowserScriptFile>& file : |
| 142 if (script_file.GetContent().empty()) { | 143 script->css_scripts()) { |
| 143 LoadScriptContent(script.host_id(), &script_file, | 144 if (file->GetContent().empty()) { |
| 145 LoadScriptContent(script->host_id(), file.get(), |
| 144 localization_messages.get(), verifier); | 146 localization_messages.get(), verifier); |
| 145 } | 147 } |
| 146 } | 148 } |
| 147 } | 149 } |
| 148 } | 150 } |
| 149 } | 151 } |
| 150 | 152 |
| 151 void LoadScriptsOnFileThread( | 153 void LoadScriptsOnFileThread( |
| 152 std::unique_ptr<UserScriptList> user_scripts, | 154 std::unique_ptr<BrowserUserScriptList> user_scripts, |
| 153 const ExtensionUserScriptLoader::HostsInfo& hosts_info, | 155 const ExtensionUserScriptLoader::HostsInfo& hosts_info, |
| 154 const std::set<int>& added_script_ids, | 156 const std::set<int>& added_script_ids, |
| 155 const scoped_refptr<ContentVerifier>& verifier, | 157 const scoped_refptr<ContentVerifier>& verifier, |
| 156 UserScriptLoader::LoadScriptsCallback callback) { | 158 UserScriptLoader::LoadScriptsCallback callback) { |
| 157 DCHECK(user_scripts.get()); | 159 DCHECK(user_scripts.get()); |
| 158 LoadUserScripts(user_scripts.get(), hosts_info, added_script_ids, verifier); | 160 LoadUserScripts(user_scripts.get(), hosts_info, added_script_ids, verifier); |
| 159 std::unique_ptr<base::SharedMemory> memory = | 161 std::unique_ptr<base::SharedMemory> memory = |
| 160 UserScriptLoader::Serialize(*user_scripts); | 162 UserScriptLoader::Serialize(*user_scripts); |
| 161 content::BrowserThread::PostTask( | 163 content::BrowserThread::PostTask( |
| 162 content::BrowserThread::UI, FROM_HERE, | 164 content::BrowserThread::UI, FROM_HERE, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 183 weak_factory_.GetWeakPtr())); | 185 weak_factory_.GetWeakPtr())); |
| 184 } else { | 186 } else { |
| 185 SetReady(true); | 187 SetReady(true); |
| 186 } | 188 } |
| 187 } | 189 } |
| 188 | 190 |
| 189 ExtensionUserScriptLoader::~ExtensionUserScriptLoader() { | 191 ExtensionUserScriptLoader::~ExtensionUserScriptLoader() { |
| 190 } | 192 } |
| 191 | 193 |
| 192 void ExtensionUserScriptLoader::LoadScriptsForTest( | 194 void ExtensionUserScriptLoader::LoadScriptsForTest( |
| 193 UserScriptList* user_scripts) { | 195 BrowserUserScriptList* user_scripts) { |
| 194 HostsInfo info; | 196 HostsInfo info; |
| 195 std::set<int> added_script_ids; | 197 std::set<int> added_script_ids; |
| 196 for (UserScript& script : *user_scripts) | 198 for (const std::unique_ptr<BrowserUserScript>& script : *user_scripts) |
| 197 added_script_ids.insert(script.id()); | 199 added_script_ids.insert(script->id()); |
| 198 | 200 |
| 199 LoadUserScripts(user_scripts, info, added_script_ids, | 201 LoadUserScripts(user_scripts, info, added_script_ids, |
| 200 nullptr /* no verifier for testing */); | 202 nullptr /* no verifier for testing */); |
| 201 } | 203 } |
| 202 | 204 |
| 203 void ExtensionUserScriptLoader::LoadScripts( | 205 void ExtensionUserScriptLoader::LoadScripts( |
| 204 std::unique_ptr<UserScriptList> user_scripts, | 206 std::unique_ptr<BrowserUserScriptList> user_scripts, |
| 205 const std::set<HostID>& changed_hosts, | 207 const std::set<HostID>& changed_hosts, |
| 206 const std::set<int>& added_script_ids, | 208 const std::set<int>& added_script_ids, |
| 207 LoadScriptsCallback callback) { | 209 LoadScriptsCallback callback) { |
| 208 UpdateHostsInfo(changed_hosts); | 210 UpdateHostsInfo(changed_hosts); |
| 209 | 211 |
| 210 content::BrowserThread::PostTask( | 212 content::BrowserThread::PostTask( |
| 211 content::BrowserThread::FILE, FROM_HERE, | 213 content::BrowserThread::FILE, FROM_HERE, |
| 212 base::Bind(&LoadScriptsOnFileThread, base::Passed(&user_scripts), | 214 base::Bind(&LoadScriptsOnFileThread, base::Passed(&user_scripts), |
| 213 hosts_info_, added_script_ids, content_verifier_, callback)); | 215 hosts_info_, added_script_ids, content_verifier_, callback)); |
| 214 } | 216 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 235 const Extension* extension, | 237 const Extension* extension, |
| 236 UnloadedExtensionInfo::Reason reason) { | 238 UnloadedExtensionInfo::Reason reason) { |
| 237 hosts_info_.erase(HostID(HostID::EXTENSIONS, extension->id())); | 239 hosts_info_.erase(HostID(HostID::EXTENSIONS, extension->id())); |
| 238 } | 240 } |
| 239 | 241 |
| 240 void ExtensionUserScriptLoader::OnExtensionSystemReady() { | 242 void ExtensionUserScriptLoader::OnExtensionSystemReady() { |
| 241 SetReady(true); | 243 SetReady(true); |
| 242 } | 244 } |
| 243 | 245 |
| 244 } // namespace extensions | 246 } // namespace extensions |
| OLD | NEW |