Update V8 to version 4.6.55.
[chromium-blink-merge.git] / extensions / browser / user_script_loader.cc
blob785e7c858ba3403decce133e313b70db127d9aa0
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
3 // found in the LICENSE file.
5 #include "extensions/browser/user_script_loader.h"
7 #include <set>
8 #include <string>
10 #include "base/version.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/browser/notification_types.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "extensions/browser/extensions_browser_client.h"
17 #include "extensions/browser/notification_types.h"
18 #include "extensions/common/extension_messages.h"
20 using content::BrowserThread;
21 using content::BrowserContext;
23 namespace extensions {
25 namespace {
27 // Helper function to parse greasesmonkey headers
28 bool GetDeclarationValue(const base::StringPiece& line,
29 const base::StringPiece& prefix,
30 std::string* value) {
31 base::StringPiece::size_type index = line.find(prefix);
32 if (index == base::StringPiece::npos)
33 return false;
35 std::string temp(line.data() + index + prefix.length(),
36 line.length() - index - prefix.length());
38 if (temp.empty() || !base::IsUnicodeWhitespace(temp[0]))
39 return false;
41 base::TrimWhitespaceASCII(temp, base::TRIM_ALL, value);
42 return true;
45 } // namespace
47 // static
48 bool UserScriptLoader::ParseMetadataHeader(const base::StringPiece& script_text,
49 UserScript* script) {
50 // http://wiki.greasespot.net/Metadata_block
51 base::StringPiece line;
52 size_t line_start = 0;
53 size_t line_end = line_start;
54 bool in_metadata = false;
56 static const base::StringPiece kUserScriptBegin("// ==UserScript==");
57 static const base::StringPiece kUserScriptEng("// ==/UserScript==");
58 static const base::StringPiece kNamespaceDeclaration("// @namespace");
59 static const base::StringPiece kNameDeclaration("// @name");
60 static const base::StringPiece kVersionDeclaration("// @version");
61 static const base::StringPiece kDescriptionDeclaration("// @description");
62 static const base::StringPiece kIncludeDeclaration("// @include");
63 static const base::StringPiece kExcludeDeclaration("// @exclude");
64 static const base::StringPiece kMatchDeclaration("// @match");
65 static const base::StringPiece kExcludeMatchDeclaration("// @exclude_match");
66 static const base::StringPiece kRunAtDeclaration("// @run-at");
67 static const base::StringPiece kRunAtDocumentStartValue("document-start");
68 static const base::StringPiece kRunAtDocumentEndValue("document-end");
69 static const base::StringPiece kRunAtDocumentIdleValue("document-idle");
71 while (line_start < script_text.length()) {
72 line_end = script_text.find('\n', line_start);
74 // Handle the case where there is no trailing newline in the file.
75 if (line_end == std::string::npos)
76 line_end = script_text.length() - 1;
78 line.set(script_text.data() + line_start, line_end - line_start);
80 if (!in_metadata) {
81 if (line.starts_with(kUserScriptBegin))
82 in_metadata = true;
83 } else {
84 if (line.starts_with(kUserScriptEng))
85 break;
87 std::string value;
88 if (GetDeclarationValue(line, kIncludeDeclaration, &value)) {
89 // We escape some characters that MatchPattern() considers special.
90 base::ReplaceSubstringsAfterOffset(&value, 0, "\\", "\\\\");
91 base::ReplaceSubstringsAfterOffset(&value, 0, "?", "\\?");
92 script->add_glob(value);
93 } else if (GetDeclarationValue(line, kExcludeDeclaration, &value)) {
94 base::ReplaceSubstringsAfterOffset(&value, 0, "\\", "\\\\");
95 base::ReplaceSubstringsAfterOffset(&value, 0, "?", "\\?");
96 script->add_exclude_glob(value);
97 } else if (GetDeclarationValue(line, kNamespaceDeclaration, &value)) {
98 script->set_name_space(value);
99 } else if (GetDeclarationValue(line, kNameDeclaration, &value)) {
100 script->set_name(value);
101 } else if (GetDeclarationValue(line, kVersionDeclaration, &value)) {
102 Version version(value);
103 if (version.IsValid())
104 script->set_version(version.GetString());
105 } else if (GetDeclarationValue(line, kDescriptionDeclaration, &value)) {
106 script->set_description(value);
107 } else if (GetDeclarationValue(line, kMatchDeclaration, &value)) {
108 URLPattern pattern(UserScript::ValidUserScriptSchemes());
109 if (URLPattern::PARSE_SUCCESS != pattern.Parse(value))
110 return false;
111 script->add_url_pattern(pattern);
112 } else if (GetDeclarationValue(line, kExcludeMatchDeclaration, &value)) {
113 URLPattern exclude(UserScript::ValidUserScriptSchemes());
114 if (URLPattern::PARSE_SUCCESS != exclude.Parse(value))
115 return false;
116 script->add_exclude_url_pattern(exclude);
117 } else if (GetDeclarationValue(line, kRunAtDeclaration, &value)) {
118 if (value == kRunAtDocumentStartValue)
119 script->set_run_location(UserScript::DOCUMENT_START);
120 else if (value == kRunAtDocumentEndValue)
121 script->set_run_location(UserScript::DOCUMENT_END);
122 else if (value == kRunAtDocumentIdleValue)
123 script->set_run_location(UserScript::DOCUMENT_IDLE);
124 else
125 return false;
128 // TODO(aa): Handle more types of metadata.
131 line_start = line_end + 1;
134 // If no patterns were specified, default to @include *. This is what
135 // Greasemonkey does.
136 if (script->globs().empty() && script->url_patterns().is_empty())
137 script->add_glob("*");
139 return true;
142 UserScriptLoader::UserScriptLoader(BrowserContext* browser_context,
143 const HostID& host_id)
144 : user_scripts_(new UserScriptList()),
145 clear_scripts_(false),
146 ready_(false),
147 pending_load_(false),
148 browser_context_(browser_context),
149 host_id_(host_id),
150 weak_factory_(this) {
151 registrar_.Add(this,
152 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
153 content::NotificationService::AllBrowserContextsAndSources());
156 UserScriptLoader::~UserScriptLoader() {
157 FOR_EACH_OBSERVER(Observer, observers_, OnUserScriptLoaderDestroyed(this));
160 void UserScriptLoader::AddScripts(const std::set<UserScript>& scripts) {
161 for (std::set<UserScript>::const_iterator it = scripts.begin();
162 it != scripts.end();
163 ++it) {
164 removed_scripts_.erase(*it);
165 added_scripts_.insert(*it);
167 AttemptLoad();
170 void UserScriptLoader::AddScripts(const std::set<UserScript>& scripts,
171 int render_process_id,
172 int render_view_id) {
173 AddScripts(scripts);
176 void UserScriptLoader::RemoveScripts(const std::set<UserScript>& scripts) {
177 for (std::set<UserScript>::const_iterator it = scripts.begin();
178 it != scripts.end();
179 ++it) {
180 added_scripts_.erase(*it);
181 removed_scripts_.insert(*it);
183 AttemptLoad();
186 void UserScriptLoader::ClearScripts() {
187 clear_scripts_ = true;
188 added_scripts_.clear();
189 removed_scripts_.clear();
190 AttemptLoad();
193 void UserScriptLoader::Observe(int type,
194 const content::NotificationSource& source,
195 const content::NotificationDetails& details) {
196 DCHECK_EQ(type, content::NOTIFICATION_RENDERER_PROCESS_CREATED);
197 content::RenderProcessHost* process =
198 content::Source<content::RenderProcessHost>(source).ptr();
199 if (!ExtensionsBrowserClient::Get()->IsSameContext(
200 browser_context_, process->GetBrowserContext()))
201 return;
202 if (scripts_ready()) {
203 SendUpdate(process, shared_memory_.get(),
204 std::set<HostID>()); // Include all hosts.
208 bool UserScriptLoader::ScriptsMayHaveChanged() const {
209 // Scripts may have changed if there are scripts added, scripts removed, or
210 // if scripts were cleared and either:
211 // (1) A load is in progress (which may result in a non-zero number of
212 // scripts that need to be cleared), or
213 // (2) The current set of scripts is non-empty (so they need to be cleared).
214 return (added_scripts_.size() ||
215 removed_scripts_.size() ||
216 (clear_scripts_ &&
217 (is_loading() || user_scripts_->size())));
220 void UserScriptLoader::AttemptLoad() {
221 if (ready_ && ScriptsMayHaveChanged()) {
222 if (is_loading())
223 pending_load_ = true;
224 else
225 StartLoad();
229 void UserScriptLoader::StartLoad() {
230 DCHECK_CURRENTLY_ON(BrowserThread::UI);
231 DCHECK(!is_loading());
233 // If scripts were marked for clearing before adding and removing, then clear
234 // them.
235 if (clear_scripts_) {
236 user_scripts_->clear();
237 } else {
238 for (UserScriptList::iterator it = user_scripts_->begin();
239 it != user_scripts_->end();) {
240 if (removed_scripts_.count(*it))
241 it = user_scripts_->erase(it);
242 else
243 ++it;
247 user_scripts_->insert(
248 user_scripts_->end(), added_scripts_.begin(), added_scripts_.end());
250 std::set<int> added_script_ids;
251 for (std::set<UserScript>::const_iterator it = added_scripts_.begin();
252 it != added_scripts_.end();
253 ++it) {
254 added_script_ids.insert(it->id());
257 // Expand |changed_hosts_| for OnScriptsLoaded, which will use it in
258 // its IPC message. This must be done before we clear |added_scripts_| and
259 // |removed_scripts_| below.
260 std::set<UserScript> changed_scripts(added_scripts_);
261 changed_scripts.insert(removed_scripts_.begin(), removed_scripts_.end());
262 for (const UserScript& script : changed_scripts)
263 changed_hosts_.insert(script.host_id());
265 LoadScripts(user_scripts_.Pass(), changed_hosts_, added_script_ids,
266 base::Bind(&UserScriptLoader::OnScriptsLoaded,
267 weak_factory_.GetWeakPtr()));
269 clear_scripts_ = false;
270 added_scripts_.clear();
271 removed_scripts_.clear();
272 user_scripts_.reset();
275 // static
276 scoped_ptr<base::SharedMemory> UserScriptLoader::Serialize(
277 const UserScriptList& scripts) {
278 base::Pickle pickle;
279 pickle.WriteSizeT(scripts.size());
280 for (const UserScript& script : scripts) {
281 // TODO(aa): This can be replaced by sending content script metadata to
282 // renderers along with other extension data in ExtensionMsg_Loaded.
283 // See crbug.com/70516.
284 script.Pickle(&pickle);
285 // Write scripts as 'data' so that we can read it out in the slave without
286 // allocating a new string.
287 for (const UserScript::File& script_file : script.js_scripts()) {
288 base::StringPiece contents = script_file.GetContent();
289 pickle.WriteData(contents.data(), contents.length());
291 for (const UserScript::File& script_file : script.css_scripts()) {
292 base::StringPiece contents = script_file.GetContent();
293 pickle.WriteData(contents.data(), contents.length());
297 // Create the shared memory object.
298 base::SharedMemory shared_memory;
300 base::SharedMemoryCreateOptions options;
301 options.size = pickle.size();
302 options.share_read_only = true;
303 if (!shared_memory.Create(options))
304 return scoped_ptr<base::SharedMemory>();
306 if (!shared_memory.Map(pickle.size()))
307 return scoped_ptr<base::SharedMemory>();
309 // Copy the pickle to shared memory.
310 memcpy(shared_memory.memory(), pickle.data(), pickle.size());
312 base::SharedMemoryHandle readonly_handle;
313 if (!shared_memory.ShareReadOnlyToProcess(base::GetCurrentProcessHandle(),
314 &readonly_handle))
315 return scoped_ptr<base::SharedMemory>();
317 return make_scoped_ptr(new base::SharedMemory(readonly_handle,
318 /*read_only=*/true));
321 void UserScriptLoader::AddObserver(Observer* observer) {
322 observers_.AddObserver(observer);
325 void UserScriptLoader::RemoveObserver(Observer* observer) {
326 observers_.RemoveObserver(observer);
329 void UserScriptLoader::SetReady(bool ready) {
330 bool was_ready = ready_;
331 ready_ = ready;
332 if (ready_ && !was_ready)
333 AttemptLoad();
336 void UserScriptLoader::OnScriptsLoaded(
337 scoped_ptr<UserScriptList> user_scripts,
338 scoped_ptr<base::SharedMemory> shared_memory) {
339 user_scripts_.reset(user_scripts.release());
340 if (pending_load_) {
341 // While we were loading, there were further changes. Don't bother
342 // notifying about these scripts and instead just immediately reload.
343 pending_load_ = false;
344 StartLoad();
345 return;
348 if (shared_memory.get() == NULL) {
349 // This can happen if we run out of file descriptors. In that case, we
350 // have a choice between silently omitting all user scripts for new tabs,
351 // by nulling out shared_memory_, or only silently omitting new ones by
352 // leaving the existing object in place. The second seems less bad, even
353 // though it removes the possibility that freeing the shared memory block
354 // would open up enough FDs for long enough for a retry to succeed.
356 // Pretend the extension change didn't happen.
357 return;
360 // We've got scripts ready to go.
361 shared_memory_.reset(shared_memory.release());
363 for (content::RenderProcessHost::iterator i(
364 content::RenderProcessHost::AllHostsIterator());
365 !i.IsAtEnd(); i.Advance()) {
366 SendUpdate(i.GetCurrentValue(), shared_memory_.get(), changed_hosts_);
368 changed_hosts_.clear();
370 // TODO(hanxi): Remove the NOTIFICATION_USER_SCRIPTS_UPDATED.
371 content::NotificationService::current()->Notify(
372 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED,
373 content::Source<BrowserContext>(browser_context_),
374 content::Details<base::SharedMemory>(shared_memory_.get()));
375 FOR_EACH_OBSERVER(Observer, observers_, OnScriptsLoaded(this));
378 void UserScriptLoader::SendUpdate(content::RenderProcessHost* process,
379 base::SharedMemory* shared_memory,
380 const std::set<HostID>& changed_hosts) {
381 // Don't allow injection of non-whitelisted extensions' content scripts
382 // into <webview>.
383 bool whitelisted_only = process->IsForGuestsOnly() && host_id().id().empty();
385 // Make sure we only send user scripts to processes in our browser_context.
386 if (!ExtensionsBrowserClient::Get()->IsSameContext(
387 browser_context_, process->GetBrowserContext()))
388 return;
390 // If the process is being started asynchronously, early return. We'll end up
391 // calling InitUserScripts when it's created which will call this again.
392 base::ProcessHandle handle = process->GetHandle();
393 if (!handle)
394 return;
396 base::SharedMemoryHandle handle_for_process;
397 if (!shared_memory->ShareToProcess(handle, &handle_for_process))
398 return; // This can legitimately fail if the renderer asserts at startup.
400 if (base::SharedMemory::IsHandleValid(handle_for_process)) {
401 process->Send(new ExtensionMsg_UpdateUserScripts(
402 handle_for_process, host_id(), changed_hosts, whitelisted_only));
406 } // namespace extensions