Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / user_script_master.cc
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.
4
5 #include "chrome/browser/extensions/user_script_master.h"
6
7 #include <map>
8 #include <string>
9 #include <vector>
10
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"
35
36 using content::BrowserThread;
37
38 namespace extensions {
39
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;
47
48   std::string temp(line.data() + index + prefix.length(),
49                    line.length() - index - prefix.length());
50
51   if (temp.empty() || !IsWhitespace(temp[0]))
52     return false;
53
54   base::TrimWhitespaceASCII(temp, base::TRIM_ALL, value);
55   return true;
56 }
57
58 UserScriptMaster::ScriptReloader::ScriptReloader(UserScriptMaster* master)
59     : master_(master) {
60   CHECK(BrowserThread::GetCurrentThreadIdentifier(&master_thread_id_));
61 }
62
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;
71
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");
86
87   while (line_start < script_text.length()) {
88     line_end = script_text.find('\n', line_start);
89
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;
93
94     line.set(script_text.data() + line_start, line_end - line_start);
95
96     if (!in_metadata) {
97       if (line.starts_with(kUserScriptBegin))
98         in_metadata = true;
99     } else {
100       if (line.starts_with(kUserScriptEng))
101         break;
102
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;
142       }
143
144       // TODO(aa): Handle more types of metadata.
145     }
146
147     line_start = line_end + 1;
148   }
149
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("*");
154
155   return true;
156 }
157
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();
164
165   this->extensions_info_ = extensions_info_;
166   BrowserThread::PostTask(
167       BrowserThread::FILE, FROM_HERE,
168       base::Bind(
169           &UserScriptMaster::ScriptReloader::RunLoad, this, user_scripts));
170 }
171
172 UserScriptMaster::ScriptReloader::~ScriptReloader() {}
173
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);
181
182   // Drop our self-reference.
183   // Balances StartLoad().
184   Release();
185 }
186
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;
205     }
206   } else {
207     if (!base::ReadFileToString(path, &content)) {
208       LOG(WARNING) << "Failed to load user script file: " << path.value();
209       return false;
210     }
211   }
212
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;
220     }
221   }
222
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);
229   }
230
231   return true;
232 }
233
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);
244     }
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());
249     }
250   }
251 }
252
253 SubstitutionMap* UserScriptMaster::ScriptReloader::GetLocalizationMessages(
254     std::string extension_id) {
255   if (extensions_info_.find(extension_id) == extensions_info_.end()) {
256     return NULL;
257   }
258
259   return extension_file_util::LoadMessageBundleSubstitutionMap(
260       extensions_info_[extension_id].first,
261       extension_id,
262       extensions_info_[extension_id].second);
263 }
264
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());
280     }
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());
284     }
285   }
286
287   // Create the shared memory object.
288   base::SharedMemory shared_memory;
289
290   base::SharedMemoryCreateOptions options;
291   options.size = pickle.size();
292   options.share_read_only = true;
293   if (!shared_memory.Create(options))
294     return NULL;
295
296   if (!shared_memory.Map(pickle.size()))
297     return NULL;
298
299   // Copy the pickle to shared memory.
300   memcpy(shared_memory.memory(), pickle.data(), pickle.size());
301
302   base::SharedMemoryHandle readonly_handle;
303   if (!shared_memory.ShareReadOnlyToProcess(base::GetCurrentProcessHandle(),
304                                             &readonly_handle))
305     return NULL;
306
307   return new base::SharedMemory(readonly_handle, /*read_only=*/true);
308 }
309
310 // This method will be called on the file thread.
311 void UserScriptMaster::ScriptReloader::RunLoad(
312     const UserScriptList& user_scripts) {
313   LoadUserScripts(const_cast<UserScriptList*>(&user_scripts));
314
315   // Scripts now contains list of up-to-date scripts. Load the content in the
316   // shared memory and let the master know it's ready. We need to post the task
317   // back even if no scripts ware found to balance the AddRef/Release calls.
318   BrowserThread::PostTask(
319       master_thread_id_, FROM_HERE,
320       base::Bind(
321           &ScriptReloader::NotifyMaster, this, Serialize(user_scripts)));
322 }
323
324
325 UserScriptMaster::UserScriptMaster(Profile* profile)
326     : extensions_service_ready_(false),
327       pending_load_(false),
328       profile_(profile) {
329   registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
330                  content::Source<Profile>(profile_));
331   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
332                  content::Source<Profile>(profile_));
333   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
334                  content::Source<Profile>(profile_));
335   registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
336                  content::NotificationService::AllBrowserContextsAndSources());
337 }
338
339 UserScriptMaster::~UserScriptMaster() {
340   if (script_reloader_.get())
341     script_reloader_->DisownMaster();
342 }
343
344 void UserScriptMaster::NewScriptsAvailable(base::SharedMemory* handle) {
345   // Ensure handle is deleted or released.
346   scoped_ptr<base::SharedMemory> handle_deleter(handle);
347
348   if (pending_load_) {
349     // While we were loading, there were further changes.  Don't bother
350     // notifying about these scripts and instead just immediately reload.
351     pending_load_ = false;
352     StartLoad();
353   } else {
354     // We're no longer loading.
355     script_reloader_ = NULL;
356     // We've got scripts ready to go.
357     shared_memory_.swap(handle_deleter);
358
359     for (content::RenderProcessHost::iterator i(
360             content::RenderProcessHost::AllHostsIterator());
361          !i.IsAtEnd(); i.Advance()) {
362       SendUpdate(i.GetCurrentValue(), handle);
363     }
364
365     content::NotificationService::current()->Notify(
366         chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
367         content::Source<Profile>(profile_),
368         content::Details<base::SharedMemory>(handle));
369   }
370 }
371
372 void UserScriptMaster::Observe(int type,
373                                const content::NotificationSource& source,
374                                const content::NotificationDetails& details) {
375   bool should_start_load = false;
376   switch (type) {
377     case chrome::NOTIFICATION_EXTENSIONS_READY:
378       extensions_service_ready_ = true;
379       should_start_load = true;
380       break;
381     case chrome::NOTIFICATION_EXTENSION_LOADED: {
382       // Add any content scripts inside the extension.
383       const Extension* extension =
384           content::Details<const Extension>(details).ptr();
385       extensions_info_[extension->id()] =
386           ExtensionSet::ExtensionPathAndDefaultLocale(
387               extension->path(), LocaleInfo::GetDefaultLocale(extension));
388       bool incognito_enabled =
389           util::IsIncognitoEnabled(extension->id(), profile_);
390       const UserScriptList& scripts =
391           ContentScriptsInfo::GetContentScripts(extension);
392       for (UserScriptList::const_iterator iter = scripts.begin();
393            iter != scripts.end(); ++iter) {
394         user_scripts_.push_back(*iter);
395         user_scripts_.back().set_incognito_enabled(incognito_enabled);
396       }
397       if (extensions_service_ready_)
398         should_start_load = true;
399       break;
400     }
401     case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
402       // Remove any content scripts.
403       const Extension* extension =
404           content::Details<UnloadedExtensionInfo>(details)->extension;
405       extensions_info_.erase(extension->id());
406       UserScriptList new_user_scripts;
407       for (UserScriptList::iterator iter = user_scripts_.begin();
408            iter != user_scripts_.end(); ++iter) {
409         if (iter->extension_id() != extension->id())
410           new_user_scripts.push_back(*iter);
411       }
412       user_scripts_ = new_user_scripts;
413       should_start_load = true;
414       break;
415     }
416     case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
417       content::RenderProcessHost* process =
418           content::Source<content::RenderProcessHost>(source).ptr();
419       Profile* profile = Profile::FromBrowserContext(
420           process->GetBrowserContext());
421       if (!profile_->IsSameProfile(profile))
422         return;
423       if (ScriptsReady())
424         SendUpdate(process, GetSharedMemory());
425       break;
426     }
427     default:
428       DCHECK(false);
429   }
430
431   if (should_start_load) {
432     if (script_reloader_.get()) {
433       pending_load_ = true;
434     } else {
435       StartLoad();
436     }
437   }
438 }
439
440 void UserScriptMaster::StartLoad() {
441   if (!script_reloader_.get())
442     script_reloader_ = new ScriptReloader(this);
443
444   script_reloader_->StartLoad(user_scripts_, extensions_info_);
445 }
446
447 void UserScriptMaster::SendUpdate(content::RenderProcessHost* process,
448                                   base::SharedMemory* shared_memory) {
449   // Don't allow injection of content scripts into <webview>.
450   if (process->IsGuest())
451     return;
452
453   Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext());
454   // Make sure we only send user scripts to processes in our profile.
455   if (!profile_->IsSameProfile(profile))
456     return;
457
458   // If the process is being started asynchronously, early return.  We'll end up
459   // calling InitUserScripts when it's created which will call this again.
460   base::ProcessHandle handle = process->GetHandle();
461   if (!handle)
462     return;
463
464   base::SharedMemoryHandle handle_for_process;
465   if (!shared_memory->ShareToProcess(handle, &handle_for_process))
466     return;  // This can legitimately fail if the renderer asserts at startup.
467
468   if (base::SharedMemory::IsHandleValid(handle_for_process))
469     process->Send(new ExtensionMsg_UpdateUserScripts(handle_for_process));
470 }
471
472 }  // namespace extensions