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

Side by Side Diff: extensions/browser/extension_user_script_loader.cc

Issue 1056533002: Implement <webview>.addContentScript/removeContentScript API [2] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webview_addremove_contentscripts_2
Patch Set: Forward declaration and remove "<" overload. Created 5 years, 8 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 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 <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 scoped_refptr<ContentVerifyJob> job( 43 scoped_refptr<ContentVerifyJob> job(
44 verifier->CreateJobFor(extension_id, extension_root, relative_path)); 44 verifier->CreateJobFor(extension_id, extension_root, relative_path));
45 if (job.get()) { 45 if (job.get()) {
46 job->Start(); 46 job->Start();
47 job->BytesRead(content.size(), content.data()); 47 job->BytesRead(content.size(), content.data());
48 job->DoneReading(); 48 job->DoneReading();
49 } 49 }
50 } 50 }
51 51
52 // Loads user scripts from the extension who owns these scripts. 52 // Loads user scripts from the extension who owns these scripts.
53 bool ExtensionLoadScriptContent( 53 bool LoadScriptContent(
54 const HostID& host_id, 54 const HostID& host_id,
55 UserScript::File* script_file, 55 UserScript::File* script_file,
56 const UserScriptLoader::SubstitutionMap* localization_messages, 56 const UserScriptLoader::SubstitutionMap* localization_messages,
57 const scoped_refptr<ContentVerifier>& verifier) { 57 const scoped_refptr<ContentVerifier>& verifier) {
58 DCHECK(script_file); 58 DCHECK(script_file);
59 std::string content; 59 std::string content;
60 const base::FilePath& path = ExtensionResource::GetFilePath( 60 const base::FilePath& path = ExtensionResource::GetFilePath(
61 script_file->extension_root(), script_file->relative_path(), 61 script_file->extension_root(), script_file->relative_path(),
62 ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT); 62 ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT);
63 if (path.empty()) { 63 if (path.empty()) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 // Remove BOM from the content. 101 // Remove BOM from the content.
102 std::string::size_type index = content.find(base::kUtf8ByteOrderMark); 102 std::string::size_type index = content.find(base::kUtf8ByteOrderMark);
103 if (index == 0) 103 if (index == 0)
104 script_file->set_content(content.substr(strlen(base::kUtf8ByteOrderMark))); 104 script_file->set_content(content.substr(strlen(base::kUtf8ByteOrderMark)));
105 else 105 else
106 script_file->set_content(content); 106 script_file->set_content(content);
107 107
108 return true; 108 return true;
109 } 109 }
110 110
111 UserScriptLoader::SubstitutionMap* GetLocalizationMessages(
112 const UserScriptLoader::HostsInfo& hosts_info,
113 const HostID& host_id) {
114 UserScriptLoader::HostsInfo::const_iterator iter = hosts_info.find(host_id);
115 if (iter == hosts_info.end())
116 return nullptr;
117 return file_util::LoadMessageBundleSubstitutionMap(
118 iter->second.first, host_id.id(), iter->second.second);
119 }
120
121 void LoadUserScripts(UserScriptList* user_scripts,
122 const UserScriptLoader::HostsInfo& hosts_info,
123 const std::set<int>& added_script_ids,
124 const scoped_refptr<ContentVerifier>& verifier) {
125 for (UserScript& script : *user_scripts) {
126 if (added_script_ids.count(script.id()) == 0)
127 continue;
128 scoped_ptr<UserScriptLoader::SubstitutionMap> localization_messages(
129 GetLocalizationMessages(hosts_info, script.host_id()));
130 for (UserScript::File& script_file : script.js_scripts()) {
131 if (script_file.GetContent().empty())
132 LoadScriptContent(script.host_id(), &script_file, nullptr, verifier);
133 }
134 for (UserScript::File& script_file : script.css_scripts()) {
135 if (script_file.GetContent().empty())
136 LoadScriptContent(script.host_id(), &script_file,
137 localization_messages.get(), verifier);
138 }
139 }
140 }
141
142 void LoadScriptsOnFileThread(scoped_ptr<UserScriptList> user_scripts,
143 const UserScriptLoader::HostsInfo& hosts_info,
144 const std::set<int>& added_script_ids,
145 const scoped_refptr<ContentVerifier>& verifier,
146 UserScriptLoader::LoadScriptsCallback callback) {
147 DCHECK(user_scripts.get());
148 LoadUserScripts(user_scripts.get(), hosts_info, added_script_ids, verifier);
149 scoped_ptr<base::SharedMemory> memory =
150 UserScriptLoader::Serialize(*user_scripts);
151 content::BrowserThread::PostTask(
152 content::BrowserThread::UI, FROM_HERE,
153 base::Bind(callback, base::Passed(&user_scripts), base::Passed(&memory)));
154 }
155
111 } // namespace 156 } // namespace
112 157
113 ExtensionUserScriptLoader::ExtensionUserScriptLoader( 158 ExtensionUserScriptLoader::ExtensionUserScriptLoader(
114 BrowserContext* browser_context, 159 BrowserContext* browser_context,
115 const HostID& host_id, 160 const HostID& host_id,
116 bool listen_for_extension_system_loaded) 161 bool listen_for_extension_system_loaded)
117 : UserScriptLoader( 162 : UserScriptLoader(browser_context, host_id),
118 browser_context, 163 content_verifier_(
119 host_id,
120 ExtensionSystem::Get(browser_context)->content_verifier()), 164 ExtensionSystem::Get(browser_context)->content_verifier()),
121 extension_registry_observer_(this), 165 extension_registry_observer_(this),
122 weak_factory_(this) { 166 weak_factory_(this) {
123 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context)); 167 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context));
124 if (listen_for_extension_system_loaded) { 168 if (listen_for_extension_system_loaded) {
125 ExtensionSystem::Get(browser_context) 169 ExtensionSystem::Get(browser_context)
126 ->ready() 170 ->ready()
127 .Post(FROM_HERE, 171 .Post(FROM_HERE,
128 base::Bind(&ExtensionUserScriptLoader::OnExtensionSystemReady, 172 base::Bind(&ExtensionUserScriptLoader::OnExtensionSystemReady,
129 weak_factory_.GetWeakPtr())); 173 weak_factory_.GetWeakPtr()));
130 } else { 174 } else {
131 SetReady(true); 175 SetReady(true);
132 } 176 }
133 } 177 }
134 178
135 ExtensionUserScriptLoader::~ExtensionUserScriptLoader() { 179 ExtensionUserScriptLoader::~ExtensionUserScriptLoader() {
136 } 180 }
137 181
182 void ExtensionUserScriptLoader::LoadScriptsForTest(
183 UserScriptList* user_scripts) {
184 HostsInfo info;
185 std::set<int> added_script_ids;
186 for (UserScript& script : *user_scripts)
187 added_script_ids.insert(script.id());
188
189 LoadUserScripts(user_scripts, info, added_script_ids,
190 nullptr /* no verifier for testing */);
191 }
192
193 void ExtensionUserScriptLoader::LoadScripts(
194 scoped_ptr<UserScriptList> user_scripts,
195 const std::set<HostID>& changed_hosts,
196 const std::set<int>& added_script_ids,
197 LoadScriptsCallback callback) {
198 UpdateHostsInfo(changed_hosts);
199
200 content::BrowserThread::PostTask(
201 content::BrowserThread::FILE, FROM_HERE,
202 base::Bind(&LoadScriptsOnFileThread, base::Passed(&user_scripts),
203 hosts_info_, added_script_ids, content_verifier_, callback));
204 }
205
138 void ExtensionUserScriptLoader::UpdateHostsInfo( 206 void ExtensionUserScriptLoader::UpdateHostsInfo(
139 const std::set<HostID>& changed_hosts) { 207 const std::set<HostID>& changed_hosts) {
140 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context()); 208 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context());
141 for (const HostID& host_id : changed_hosts) { 209 for (const HostID& host_id : changed_hosts) {
142 const Extension* extension = 210 const Extension* extension =
143 registry->GetExtensionById(host_id.id(), ExtensionRegistry::ENABLED); 211 registry->GetExtensionById(host_id.id(), ExtensionRegistry::ENABLED);
144 // |changed_hosts_| may include hosts that have been removed, 212 // |changed_hosts_| may include hosts that have been removed,
145 // which leads to the above lookup failing. In this case, just continue. 213 // which leads to the above lookup failing. In this case, just continue.
146 if (!extension) 214 if (!extension)
147 continue; 215 continue;
148 AddHostInfo(host_id, ExtensionSet::ExtensionPathAndDefaultLocale( 216 if (hosts_info_.find(host_id) != hosts_info_.end())
149 extension->path(), 217 continue;
150 LocaleInfo::GetDefaultLocale(extension))); 218 hosts_info_[host_id] = ExtensionSet::ExtensionPathAndDefaultLocale(
219 extension->path(), LocaleInfo::GetDefaultLocale(extension));
151 } 220 }
152 } 221 }
153 222
154 UserScriptLoader::LoadUserScriptsContentFunction
155 ExtensionUserScriptLoader::GetLoadUserScriptsFunction() {
156 return base::Bind(&ExtensionLoadScriptContent);
157 }
158
159 void ExtensionUserScriptLoader::OnExtensionUnloaded( 223 void ExtensionUserScriptLoader::OnExtensionUnloaded(
160 content::BrowserContext* browser_context, 224 content::BrowserContext* browser_context,
161 const Extension* extension, 225 const Extension* extension,
162 UnloadedExtensionInfo::Reason reason) { 226 UnloadedExtensionInfo::Reason reason) {
163 RemoveHostInfo(HostID(HostID::EXTENSIONS, extension->id())); 227 hosts_info_.erase(HostID(HostID::EXTENSIONS, extension->id()));
164 } 228 }
165 229
166 void ExtensionUserScriptLoader::OnExtensionSystemReady() { 230 void ExtensionUserScriptLoader::OnExtensionSystemReady() {
167 SetReady(true); 231 SetReady(true);
168 } 232 }
169 233
170 } // namespace extensions 234 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698