NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / extensions / user_script_master.cc
blobb0c219893019958b07ab4751a15980954e853301
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/user_script_master.h"
7 #include <map>
8 #include <string>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/file_util.h"
13 #include "base/files/file_path.h"
14 #include "base/pickle.h"
15 #include "base/stl_util.h"
16 #include "base/strings/string_util.h"
17 #include "base/threading/thread.h"
18 #include "base/version.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/extensions/extension_util.h"
22 #include "chrome/browser/extensions/image_loader.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/common/extensions/api/i18n/default_locale_handler.h"
25 #include "chrome/common/extensions/extension_file_util.h"
26 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
27 #include "chrome/common/extensions/message_bundle.h"
28 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/render_process_host.h"
30 #include "extensions/browser/extension_system.h"
31 #include "extensions/common/extension.h"
32 #include "extensions/common/extension_resource.h"
33 #include "extensions/common/extension_set.h"
34 #include "ui/base/resource/resource_bundle.h"
36 using content::BrowserThread;
38 namespace extensions {
40 // Helper function to parse greasesmonkey headers
41 static bool GetDeclarationValue(const base::StringPiece& line,
42 const base::StringPiece& prefix,
43 std::string* value) {
44 base::StringPiece::size_type index = line.find(prefix);
45 if (index == base::StringPiece::npos)
46 return false;
48 std::string temp(line.data() + index + prefix.length(),
49 line.length() - index - prefix.length());
51 if (temp.empty() || !IsWhitespace(temp[0]))
52 return false;
54 TrimWhitespaceASCII(temp, TRIM_ALL, value);
55 return true;
58 UserScriptMaster::ScriptReloader::ScriptReloader(UserScriptMaster* master)
59 : master_(master) {
60 CHECK(BrowserThread::GetCurrentThreadIdentifier(&master_thread_id_));
63 // static
64 bool UserScriptMaster::ScriptReloader::ParseMetadataHeader(
65 const base::StringPiece& script_text, UserScript* script) {
66 // http://wiki.greasespot.net/Metadata_block
67 base::StringPiece line;
68 size_t line_start = 0;
69 size_t line_end = line_start;
70 bool in_metadata = false;
72 static const base::StringPiece kUserScriptBegin("// ==UserScript==");
73 static const base::StringPiece kUserScriptEng("// ==/UserScript==");
74 static const base::StringPiece kNamespaceDeclaration("// @namespace");
75 static const base::StringPiece kNameDeclaration("// @name");
76 static const base::StringPiece kVersionDeclaration("// @version");
77 static const base::StringPiece kDescriptionDeclaration("// @description");
78 static const base::StringPiece kIncludeDeclaration("// @include");
79 static const base::StringPiece kExcludeDeclaration("// @exclude");
80 static const base::StringPiece kMatchDeclaration("// @match");
81 static const base::StringPiece kExcludeMatchDeclaration("// @exclude_match");
82 static const base::StringPiece kRunAtDeclaration("// @run-at");
83 static const base::StringPiece kRunAtDocumentStartValue("document-start");
84 static const base::StringPiece kRunAtDocumentEndValue("document-end");
85 static const base::StringPiece kRunAtDocumentIdleValue("document-idle");
87 while (line_start < script_text.length()) {
88 line_end = script_text.find('\n', line_start);
90 // Handle the case where there is no trailing newline in the file.
91 if (line_end == std::string::npos)
92 line_end = script_text.length() - 1;
94 line.set(script_text.data() + line_start, line_end - line_start);
96 if (!in_metadata) {
97 if (line.starts_with(kUserScriptBegin))
98 in_metadata = true;
99 } else {
100 if (line.starts_with(kUserScriptEng))
101 break;
103 std::string value;
104 if (GetDeclarationValue(line, kIncludeDeclaration, &value)) {
105 // We escape some characters that MatchPattern() considers special.
106 ReplaceSubstringsAfterOffset(&value, 0, "\\", "\\\\");
107 ReplaceSubstringsAfterOffset(&value, 0, "?", "\\?");
108 script->add_glob(value);
109 } else if (GetDeclarationValue(line, kExcludeDeclaration, &value)) {
110 ReplaceSubstringsAfterOffset(&value, 0, "\\", "\\\\");
111 ReplaceSubstringsAfterOffset(&value, 0, "?", "\\?");
112 script->add_exclude_glob(value);
113 } else if (GetDeclarationValue(line, kNamespaceDeclaration, &value)) {
114 script->set_name_space(value);
115 } else if (GetDeclarationValue(line, kNameDeclaration, &value)) {
116 script->set_name(value);
117 } else if (GetDeclarationValue(line, kVersionDeclaration, &value)) {
118 Version version(value);
119 if (version.IsValid())
120 script->set_version(version.GetString());
121 } else if (GetDeclarationValue(line, kDescriptionDeclaration, &value)) {
122 script->set_description(value);
123 } else if (GetDeclarationValue(line, kMatchDeclaration, &value)) {
124 URLPattern pattern(UserScript::ValidUserScriptSchemes());
125 if (URLPattern::PARSE_SUCCESS != pattern.Parse(value))
126 return false;
127 script->add_url_pattern(pattern);
128 } else if (GetDeclarationValue(line, kExcludeMatchDeclaration, &value)) {
129 URLPattern exclude(UserScript::ValidUserScriptSchemes());
130 if (URLPattern::PARSE_SUCCESS != exclude.Parse(value))
131 return false;
132 script->add_exclude_url_pattern(exclude);
133 } else if (GetDeclarationValue(line, kRunAtDeclaration, &value)) {
134 if (value == kRunAtDocumentStartValue)
135 script->set_run_location(UserScript::DOCUMENT_START);
136 else if (value == kRunAtDocumentEndValue)
137 script->set_run_location(UserScript::DOCUMENT_END);
138 else if (value == kRunAtDocumentIdleValue)
139 script->set_run_location(UserScript::DOCUMENT_IDLE);
140 else
141 return false;
144 // TODO(aa): Handle more types of metadata.
147 line_start = line_end + 1;
150 // If no patterns were specified, default to @include *. This is what
151 // Greasemonkey does.
152 if (script->globs().empty() && script->url_patterns().is_empty())
153 script->add_glob("*");
155 return true;
158 void UserScriptMaster::ScriptReloader::StartLoad(
159 const UserScriptList& user_scripts,
160 const ExtensionsInfo& extensions_info_) {
161 // Add a reference to ourselves to keep ourselves alive while we're running.
162 // Balanced by NotifyMaster().
163 AddRef();
165 this->extensions_info_ = extensions_info_;
166 BrowserThread::PostTask(
167 BrowserThread::FILE, FROM_HERE,
168 base::Bind(
169 &UserScriptMaster::ScriptReloader::RunLoad, this, user_scripts));
172 UserScriptMaster::ScriptReloader::~ScriptReloader() {}
174 void UserScriptMaster::ScriptReloader::NotifyMaster(
175 base::SharedMemory* memory) {
176 // The master went away, so these new scripts aren't useful anymore.
177 if (!master_)
178 delete memory;
179 else
180 master_->NewScriptsAvailable(memory);
182 // Drop our self-reference.
183 // Balances StartLoad().
184 Release();
187 static bool LoadScriptContent(UserScript::File* script_file,
188 const SubstitutionMap* localization_messages) {
189 std::string content;
190 const base::FilePath& path = ExtensionResource::GetFilePath(
191 script_file->extension_root(), script_file->relative_path(),
192 ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT);
193 if (path.empty()) {
194 int resource_id;
195 if (extensions::ImageLoader::IsComponentExtensionResource(
196 script_file->extension_root(), script_file->relative_path(),
197 &resource_id)) {
198 const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
199 content = rb.GetRawDataResource(resource_id).as_string();
200 } else {
201 LOG(WARNING) << "Failed to get file path to "
202 << script_file->relative_path().value() << " from "
203 << script_file->extension_root().value();
204 return false;
206 } else {
207 if (!base::ReadFileToString(path, &content)) {
208 LOG(WARNING) << "Failed to load user script file: " << path.value();
209 return false;
213 // Localize the content.
214 if (localization_messages) {
215 std::string error;
216 MessageBundle::ReplaceMessagesWithExternalDictionary(
217 *localization_messages, &content, &error);
218 if (!error.empty()) {
219 LOG(WARNING) << "Failed to replace messages in script: " << error;
223 // Remove BOM from the content.
224 std::string::size_type index = content.find(base::kUtf8ByteOrderMark);
225 if (index == 0) {
226 script_file->set_content(content.substr(strlen(base::kUtf8ByteOrderMark)));
227 } else {
228 script_file->set_content(content);
231 return true;
234 void UserScriptMaster::ScriptReloader::LoadUserScripts(
235 UserScriptList* user_scripts) {
236 for (size_t i = 0; i < user_scripts->size(); ++i) {
237 UserScript& script = user_scripts->at(i);
238 scoped_ptr<SubstitutionMap> localization_messages(
239 GetLocalizationMessages(script.extension_id()));
240 for (size_t k = 0; k < script.js_scripts().size(); ++k) {
241 UserScript::File& script_file = script.js_scripts()[k];
242 if (script_file.GetContent().empty())
243 LoadScriptContent(&script_file, NULL);
245 for (size_t k = 0; k < script.css_scripts().size(); ++k) {
246 UserScript::File& script_file = script.css_scripts()[k];
247 if (script_file.GetContent().empty())
248 LoadScriptContent(&script_file, localization_messages.get());
253 SubstitutionMap* UserScriptMaster::ScriptReloader::GetLocalizationMessages(
254 std::string extension_id) {
255 if (extensions_info_.find(extension_id) == extensions_info_.end()) {
256 return NULL;
259 return extension_file_util::LoadMessageBundleSubstitutionMap(
260 extensions_info_[extension_id].first,
261 extension_id,
262 extensions_info_[extension_id].second);
265 // Pickle user scripts and return pointer to the shared memory.
266 static base::SharedMemory* Serialize(const UserScriptList& scripts) {
267 Pickle pickle;
268 pickle.WriteUInt64(scripts.size());
269 for (size_t i = 0; i < scripts.size(); i++) {
270 const UserScript& script = scripts[i];
271 // TODO(aa): This can be replaced by sending content script metadata to
272 // renderers along with other extension data in ExtensionMsg_Loaded.
273 // See crbug.com/70516.
274 script.Pickle(&pickle);
275 // Write scripts as 'data' so that we can read it out in the slave without
276 // allocating a new string.
277 for (size_t j = 0; j < script.js_scripts().size(); j++) {
278 base::StringPiece contents = script.js_scripts()[j].GetContent();
279 pickle.WriteData(contents.data(), contents.length());
281 for (size_t j = 0; j < script.css_scripts().size(); j++) {
282 base::StringPiece contents = script.css_scripts()[j].GetContent();
283 pickle.WriteData(contents.data(), contents.length());
287 // Create the shared memory object.
288 base::SharedMemory shared_memory;
290 if (!shared_memory.CreateAndMapAnonymous(pickle.size()))
291 return NULL;
293 // Copy the pickle to shared memory.
294 memcpy(shared_memory.memory(), pickle.data(), pickle.size());
296 base::SharedMemoryHandle readonly_handle;
297 if (!shared_memory.ShareReadOnlyToProcess(base::GetCurrentProcessHandle(),
298 &readonly_handle))
299 return NULL;
301 return new base::SharedMemory(readonly_handle, /*read_only=*/true);
304 // This method will be called on the file thread.
305 void UserScriptMaster::ScriptReloader::RunLoad(
306 const UserScriptList& user_scripts) {
307 LoadUserScripts(const_cast<UserScriptList*>(&user_scripts));
309 // Scripts now contains list of up-to-date scripts. Load the content in the
310 // shared memory and let the master know it's ready. We need to post the task
311 // back even if no scripts ware found to balance the AddRef/Release calls.
312 BrowserThread::PostTask(
313 master_thread_id_, FROM_HERE,
314 base::Bind(
315 &ScriptReloader::NotifyMaster, this, Serialize(user_scripts)));
319 UserScriptMaster::UserScriptMaster(Profile* profile)
320 : extensions_service_ready_(false),
321 pending_load_(false),
322 profile_(profile) {
323 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
324 content::Source<Profile>(profile_));
325 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
326 content::Source<Profile>(profile_));
327 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
328 content::Source<Profile>(profile_));
329 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
330 content::NotificationService::AllBrowserContextsAndSources());
333 UserScriptMaster::~UserScriptMaster() {
334 if (script_reloader_.get())
335 script_reloader_->DisownMaster();
338 void UserScriptMaster::NewScriptsAvailable(base::SharedMemory* handle) {
339 // Ensure handle is deleted or released.
340 scoped_ptr<base::SharedMemory> handle_deleter(handle);
342 if (pending_load_) {
343 // While we were loading, there were further changes. Don't bother
344 // notifying about these scripts and instead just immediately reload.
345 pending_load_ = false;
346 StartLoad();
347 } else {
348 // We're no longer loading.
349 script_reloader_ = NULL;
350 // We've got scripts ready to go.
351 shared_memory_.swap(handle_deleter);
353 for (content::RenderProcessHost::iterator i(
354 content::RenderProcessHost::AllHostsIterator());
355 !i.IsAtEnd(); i.Advance()) {
356 SendUpdate(i.GetCurrentValue(), handle);
359 content::NotificationService::current()->Notify(
360 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
361 content::Source<Profile>(profile_),
362 content::Details<base::SharedMemory>(handle));
366 void UserScriptMaster::Observe(int type,
367 const content::NotificationSource& source,
368 const content::NotificationDetails& details) {
369 bool should_start_load = false;
370 switch (type) {
371 case chrome::NOTIFICATION_EXTENSIONS_READY:
372 extensions_service_ready_ = true;
373 should_start_load = true;
374 break;
375 case chrome::NOTIFICATION_EXTENSION_LOADED: {
376 // Add any content scripts inside the extension.
377 const Extension* extension =
378 content::Details<const Extension>(details).ptr();
379 extensions_info_[extension->id()] =
380 ExtensionSet::ExtensionPathAndDefaultLocale(
381 extension->path(), LocaleInfo::GetDefaultLocale(extension));
382 bool incognito_enabled =
383 util::IsIncognitoEnabled(extension->id(), profile_);
384 const UserScriptList& scripts =
385 ContentScriptsInfo::GetContentScripts(extension);
386 for (UserScriptList::const_iterator iter = scripts.begin();
387 iter != scripts.end(); ++iter) {
388 user_scripts_.push_back(*iter);
389 user_scripts_.back().set_incognito_enabled(incognito_enabled);
391 if (extensions_service_ready_)
392 should_start_load = true;
393 break;
395 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
396 // Remove any content scripts.
397 const Extension* extension =
398 content::Details<UnloadedExtensionInfo>(details)->extension;
399 extensions_info_.erase(extension->id());
400 UserScriptList new_user_scripts;
401 for (UserScriptList::iterator iter = user_scripts_.begin();
402 iter != user_scripts_.end(); ++iter) {
403 if (iter->extension_id() != extension->id())
404 new_user_scripts.push_back(*iter);
406 user_scripts_ = new_user_scripts;
407 should_start_load = true;
408 break;
410 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
411 content::RenderProcessHost* process =
412 content::Source<content::RenderProcessHost>(source).ptr();
413 Profile* profile = Profile::FromBrowserContext(
414 process->GetBrowserContext());
415 if (!profile_->IsSameProfile(profile))
416 return;
417 if (ScriptsReady())
418 SendUpdate(process, GetSharedMemory());
419 break;
421 default:
422 DCHECK(false);
425 if (should_start_load) {
426 if (script_reloader_.get()) {
427 pending_load_ = true;
428 } else {
429 StartLoad();
434 void UserScriptMaster::StartLoad() {
435 if (!script_reloader_.get())
436 script_reloader_ = new ScriptReloader(this);
438 script_reloader_->StartLoad(user_scripts_, extensions_info_);
441 void UserScriptMaster::SendUpdate(content::RenderProcessHost* process,
442 base::SharedMemory* shared_memory) {
443 // Don't allow injection of content scripts into <webview>.
444 if (process->IsGuest())
445 return;
447 Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext());
448 // Make sure we only send user scripts to processes in our profile.
449 if (!profile_->IsSameProfile(profile))
450 return;
452 // If the process is being started asynchronously, early return. We'll end up
453 // calling InitUserScripts when it's created which will call this again.
454 base::ProcessHandle handle = process->GetHandle();
455 if (!handle)
456 return;
458 base::SharedMemoryHandle handle_for_process;
459 if (!shared_memory->ShareToProcess(handle, &handle_for_process))
460 return; // This can legitimately fail if the renderer asserts at startup.
462 if (base::SharedMemory::IsHandleValid(handle_for_process))
463 process->Send(new ExtensionMsg_UpdateUserScripts(handle_for_process));
466 } // namespace extensions