This source file includes following definitions.
- GetDeclarationValue
- ParseMetadataHeader
- StartLoad
- NotifyMaster
- LoadScriptContent
- LoadUserScripts
- GetLocalizationMessages
- Serialize
- RunLoad
- profile_
- NewScriptsAvailable
- Observe
- StartLoad
- SendUpdate
#include "chrome/browser/extensions/user_script_master.h"
#include <map>
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/pickle.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/threading/thread.h"
#include "base/version.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/extensions/image_loader.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/i18n/default_locale_handler.h"
#include "chrome/common/extensions/extension_file_util.h"
#include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
#include "chrome/common/extensions/message_bundle.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_process_host.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_resource.h"
#include "extensions/common/extension_set.h"
#include "ui/base/resource/resource_bundle.h"
using content::BrowserThread;
namespace extensions {
static bool GetDeclarationValue(const base::StringPiece& line,
const base::StringPiece& prefix,
std::string* value) {
base::StringPiece::size_type index = line.find(prefix);
if (index == base::StringPiece::npos)
return false;
std::string temp(line.data() + index + prefix.length(),
line.length() - index - prefix.length());
if (temp.empty() || !IsWhitespace(temp[0]))
return false;
base::TrimWhitespaceASCII(temp, base::TRIM_ALL, value);
return true;
}
UserScriptMaster::ScriptReloader::ScriptReloader(UserScriptMaster* master)
: master_(master) {
CHECK(BrowserThread::GetCurrentThreadIdentifier(&master_thread_id_));
}
bool UserScriptMaster::ScriptReloader::ParseMetadataHeader(
const base::StringPiece& script_text, UserScript* script) {
base::StringPiece line;
size_t line_start = 0;
size_t line_end = line_start;
bool in_metadata = false;
static const base::StringPiece kUserScriptBegin("// ==UserScript==");
static const base::StringPiece kUserScriptEng("// ==/UserScript==");
static const base::StringPiece kNamespaceDeclaration("// @namespace");
static const base::StringPiece kNameDeclaration("// @name");
static const base::StringPiece kVersionDeclaration("// @version");
static const base::StringPiece kDescriptionDeclaration("// @description");
static const base::StringPiece kIncludeDeclaration("// @include");
static const base::StringPiece kExcludeDeclaration("// @exclude");
static const base::StringPiece kMatchDeclaration("// @match");
static const base::StringPiece kExcludeMatchDeclaration("// @exclude_match");
static const base::StringPiece kRunAtDeclaration("// @run-at");
static const base::StringPiece kRunAtDocumentStartValue("document-start");
static const base::StringPiece kRunAtDocumentEndValue("document-end");
static const base::StringPiece kRunAtDocumentIdleValue("document-idle");
while (line_start < script_text.length()) {
line_end = script_text.find('\n', line_start);
if (line_end == std::string::npos)
line_end = script_text.length() - 1;
line.set(script_text.data() + line_start, line_end - line_start);
if (!in_metadata) {
if (line.starts_with(kUserScriptBegin))
in_metadata = true;
} else {
if (line.starts_with(kUserScriptEng))
break;
std::string value;
if (GetDeclarationValue(line, kIncludeDeclaration, &value)) {
ReplaceSubstringsAfterOffset(&value, 0, "\\", "\\\\");
ReplaceSubstringsAfterOffset(&value, 0, "?", "\\?");
script->add_glob(value);
} else if (GetDeclarationValue(line, kExcludeDeclaration, &value)) {
ReplaceSubstringsAfterOffset(&value, 0, "\\", "\\\\");
ReplaceSubstringsAfterOffset(&value, 0, "?", "\\?");
script->add_exclude_glob(value);
} else if (GetDeclarationValue(line, kNamespaceDeclaration, &value)) {
script->set_name_space(value);
} else if (GetDeclarationValue(line, kNameDeclaration, &value)) {
script->set_name(value);
} else if (GetDeclarationValue(line, kVersionDeclaration, &value)) {
Version version(value);
if (version.IsValid())
script->set_version(version.GetString());
} else if (GetDeclarationValue(line, kDescriptionDeclaration, &value)) {
script->set_description(value);
} else if (GetDeclarationValue(line, kMatchDeclaration, &value)) {
URLPattern pattern(UserScript::ValidUserScriptSchemes());
if (URLPattern::PARSE_SUCCESS != pattern.Parse(value))
return false;
script->add_url_pattern(pattern);
} else if (GetDeclarationValue(line, kExcludeMatchDeclaration, &value)) {
URLPattern exclude(UserScript::ValidUserScriptSchemes());
if (URLPattern::PARSE_SUCCESS != exclude.Parse(value))
return false;
script->add_exclude_url_pattern(exclude);
} else if (GetDeclarationValue(line, kRunAtDeclaration, &value)) {
if (value == kRunAtDocumentStartValue)
script->set_run_location(UserScript::DOCUMENT_START);
else if (value == kRunAtDocumentEndValue)
script->set_run_location(UserScript::DOCUMENT_END);
else if (value == kRunAtDocumentIdleValue)
script->set_run_location(UserScript::DOCUMENT_IDLE);
else
return false;
}
}
line_start = line_end + 1;
}
if (script->globs().empty() && script->url_patterns().is_empty())
script->add_glob("*");
return true;
}
void UserScriptMaster::ScriptReloader::StartLoad(
const UserScriptList& user_scripts,
const ExtensionsInfo& extensions_info_) {
AddRef();
this->extensions_info_ = extensions_info_;
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(
&UserScriptMaster::ScriptReloader::RunLoad, this, user_scripts));
}
UserScriptMaster::ScriptReloader::~ScriptReloader() {}
void UserScriptMaster::ScriptReloader::NotifyMaster(
base::SharedMemory* memory) {
if (!master_)
delete memory;
else
master_->NewScriptsAvailable(memory);
Release();
}
static bool LoadScriptContent(UserScript::File* script_file,
const SubstitutionMap* localization_messages) {
std::string content;
const base::FilePath& path = ExtensionResource::GetFilePath(
script_file->extension_root(), script_file->relative_path(),
ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT);
if (path.empty()) {
int resource_id;
if (extensions::ImageLoader::IsComponentExtensionResource(
script_file->extension_root(), script_file->relative_path(),
&resource_id)) {
const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
content = rb.GetRawDataResource(resource_id).as_string();
} else {
LOG(WARNING) << "Failed to get file path to "
<< script_file->relative_path().value() << " from "
<< script_file->extension_root().value();
return false;
}
} else {
if (!base::ReadFileToString(path, &content)) {
LOG(WARNING) << "Failed to load user script file: " << path.value();
return false;
}
}
if (localization_messages) {
std::string error;
MessageBundle::ReplaceMessagesWithExternalDictionary(
*localization_messages, &content, &error);
if (!error.empty()) {
LOG(WARNING) << "Failed to replace messages in script: " << error;
}
}
std::string::size_type index = content.find(base::kUtf8ByteOrderMark);
if (index == 0) {
script_file->set_content(content.substr(strlen(base::kUtf8ByteOrderMark)));
} else {
script_file->set_content(content);
}
return true;
}
void UserScriptMaster::ScriptReloader::LoadUserScripts(
UserScriptList* user_scripts) {
for (size_t i = 0; i < user_scripts->size(); ++i) {
UserScript& script = user_scripts->at(i);
scoped_ptr<SubstitutionMap> localization_messages(
GetLocalizationMessages(script.extension_id()));
for (size_t k = 0; k < script.js_scripts().size(); ++k) {
UserScript::File& script_file = script.js_scripts()[k];
if (script_file.GetContent().empty())
LoadScriptContent(&script_file, NULL);
}
for (size_t k = 0; k < script.css_scripts().size(); ++k) {
UserScript::File& script_file = script.css_scripts()[k];
if (script_file.GetContent().empty())
LoadScriptContent(&script_file, localization_messages.get());
}
}
}
SubstitutionMap* UserScriptMaster::ScriptReloader::GetLocalizationMessages(
std::string extension_id) {
if (extensions_info_.find(extension_id) == extensions_info_.end()) {
return NULL;
}
return extension_file_util::LoadMessageBundleSubstitutionMap(
extensions_info_[extension_id].first,
extension_id,
extensions_info_[extension_id].second);
}
static base::SharedMemory* Serialize(const UserScriptList& scripts) {
Pickle pickle;
pickle.WriteUInt64(scripts.size());
for (size_t i = 0; i < scripts.size(); i++) {
const UserScript& script = scripts[i];
script.Pickle(&pickle);
for (size_t j = 0; j < script.js_scripts().size(); j++) {
base::StringPiece contents = script.js_scripts()[j].GetContent();
pickle.WriteData(contents.data(), contents.length());
}
for (size_t j = 0; j < script.css_scripts().size(); j++) {
base::StringPiece contents = script.css_scripts()[j].GetContent();
pickle.WriteData(contents.data(), contents.length());
}
}
base::SharedMemory shared_memory;
if (!shared_memory.CreateAndMapAnonymous(pickle.size()))
return NULL;
memcpy(shared_memory.memory(), pickle.data(), pickle.size());
base::SharedMemoryHandle readonly_handle;
if (!shared_memory.ShareReadOnlyToProcess(base::GetCurrentProcessHandle(),
&readonly_handle))
return NULL;
return new base::SharedMemory(readonly_handle, true);
}
void UserScriptMaster::ScriptReloader::RunLoad(
const UserScriptList& user_scripts) {
LoadUserScripts(const_cast<UserScriptList*>(&user_scripts));
BrowserThread::PostTask(
master_thread_id_, FROM_HERE,
base::Bind(
&ScriptReloader::NotifyMaster, this, Serialize(user_scripts)));
}
UserScriptMaster::UserScriptMaster(Profile* profile)
: extensions_service_ready_(false),
pending_load_(false),
profile_(profile) {
registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
content::Source<Profile>(profile_));
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
content::Source<Profile>(profile_));
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
content::Source<Profile>(profile_));
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
content::NotificationService::AllBrowserContextsAndSources());
}
UserScriptMaster::~UserScriptMaster() {
if (script_reloader_.get())
script_reloader_->DisownMaster();
}
void UserScriptMaster::NewScriptsAvailable(base::SharedMemory* handle) {
scoped_ptr<base::SharedMemory> handle_deleter(handle);
if (pending_load_) {
pending_load_ = false;
StartLoad();
} else {
script_reloader_ = NULL;
shared_memory_.swap(handle_deleter);
for (content::RenderProcessHost::iterator i(
content::RenderProcessHost::AllHostsIterator());
!i.IsAtEnd(); i.Advance()) {
SendUpdate(i.GetCurrentValue(), handle);
}
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
content::Source<Profile>(profile_),
content::Details<base::SharedMemory>(handle));
}
}
void UserScriptMaster::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
bool should_start_load = false;
switch (type) {
case chrome::NOTIFICATION_EXTENSIONS_READY:
extensions_service_ready_ = true;
should_start_load = true;
break;
case chrome::NOTIFICATION_EXTENSION_LOADED: {
const Extension* extension =
content::Details<const Extension>(details).ptr();
extensions_info_[extension->id()] =
ExtensionSet::ExtensionPathAndDefaultLocale(
extension->path(), LocaleInfo::GetDefaultLocale(extension));
bool incognito_enabled =
util::IsIncognitoEnabled(extension->id(), profile_);
const UserScriptList& scripts =
ContentScriptsInfo::GetContentScripts(extension);
for (UserScriptList::const_iterator iter = scripts.begin();
iter != scripts.end(); ++iter) {
user_scripts_.push_back(*iter);
user_scripts_.back().set_incognito_enabled(incognito_enabled);
}
if (extensions_service_ready_)
should_start_load = true;
break;
}
case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
const Extension* extension =
content::Details<UnloadedExtensionInfo>(details)->extension;
extensions_info_.erase(extension->id());
UserScriptList new_user_scripts;
for (UserScriptList::iterator iter = user_scripts_.begin();
iter != user_scripts_.end(); ++iter) {
if (iter->extension_id() != extension->id())
new_user_scripts.push_back(*iter);
}
user_scripts_ = new_user_scripts;
should_start_load = true;
break;
}
case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
content::RenderProcessHost* process =
content::Source<content::RenderProcessHost>(source).ptr();
Profile* profile = Profile::FromBrowserContext(
process->GetBrowserContext());
if (!profile_->IsSameProfile(profile))
return;
if (ScriptsReady())
SendUpdate(process, GetSharedMemory());
break;
}
default:
DCHECK(false);
}
if (should_start_load) {
if (script_reloader_.get()) {
pending_load_ = true;
} else {
StartLoad();
}
}
}
void UserScriptMaster::StartLoad() {
if (!script_reloader_.get())
script_reloader_ = new ScriptReloader(this);
script_reloader_->StartLoad(user_scripts_, extensions_info_);
}
void UserScriptMaster::SendUpdate(content::RenderProcessHost* process,
base::SharedMemory* shared_memory) {
if (process->IsGuest())
return;
Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext());
if (!profile_->IsSameProfile(profile))
return;
base::ProcessHandle handle = process->GetHandle();
if (!handle)
return;
base::SharedMemoryHandle handle_for_process;
if (!shared_memory->ShareToProcess(handle, &handle_for_process))
return;
if (base::SharedMemory::IsHandleValid(handle_for_process))
process->Send(new ExtensionMsg_UpdateUserScripts(handle_for_process));
}
}