Roll src/third_party/WebKit b3f094a:f697bbd (svn 194310:194313)
[chromium-blink-merge.git] / extensions / common / user_script.cc
blob825276fd019bc8fde2f0a862b93d901b5b4e8563
1 // Copyright 2013 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/common/user_script.h"
7 #include "base/atomic_sequence_num.h"
8 #include "base/command_line.h"
9 #include "base/pickle.h"
10 #include "base/strings/string_util.h"
11 #include "extensions/common/switches.h"
13 namespace {
15 // This cannot be a plain int or int64 because we need to generate unique IDs
16 // from multiple threads.
17 base::StaticAtomicSequenceNumber g_user_script_id_generator;
19 bool UrlMatchesGlobs(const std::vector<std::string>* globs,
20 const GURL& url) {
21 for (std::vector<std::string>::const_iterator glob = globs->begin();
22 glob != globs->end(); ++glob) {
23 if (MatchPattern(url.spec(), *glob))
24 return true;
27 return false;
30 } // namespace
32 namespace extensions {
34 // The bitmask for valid user script injectable schemes used by URLPattern.
35 enum {
36 kValidUserScriptSchemes = URLPattern::SCHEME_CHROMEUI |
37 URLPattern::SCHEME_HTTP |
38 URLPattern::SCHEME_HTTPS |
39 URLPattern::SCHEME_FILE |
40 URLPattern::SCHEME_FTP
43 // static
44 const char UserScript::kFileExtension[] = ".user.js";
47 // static
48 int UserScript::GenerateUserScriptID() {
49 return g_user_script_id_generator.GetNext();
52 bool UserScript::IsURLUserScript(const GURL& url,
53 const std::string& mime_type) {
54 return EndsWith(url.ExtractFileName(), kFileExtension, false) &&
55 mime_type != "text/html";
58 // static
59 int UserScript::ValidUserScriptSchemes(bool canExecuteScriptEverywhere) {
60 if (canExecuteScriptEverywhere)
61 return URLPattern::SCHEME_ALL;
62 int valid_schemes = kValidUserScriptSchemes;
63 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
64 switches::kExtensionsOnChromeURLs)) {
65 valid_schemes &= ~URLPattern::SCHEME_CHROMEUI;
67 return valid_schemes;
70 UserScript::File::File(const base::FilePath& extension_root,
71 const base::FilePath& relative_path,
72 const GURL& url)
73 : extension_root_(extension_root),
74 relative_path_(relative_path),
75 url_(url) {
78 UserScript::File::File() {}
80 UserScript::File::~File() {}
82 UserScript::UserScript()
83 : run_location_(DOCUMENT_IDLE),
84 consumer_instance_type_(TAB),
85 user_script_id_(-1),
86 emulate_greasemonkey_(false),
87 match_all_frames_(false),
88 match_about_blank_(false),
89 incognito_enabled_(false) {}
91 UserScript::~UserScript() {
94 void UserScript::add_url_pattern(const URLPattern& pattern) {
95 url_set_.AddPattern(pattern);
98 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) {
99 exclude_url_set_.AddPattern(pattern);
102 bool UserScript::MatchesURL(const GURL& url) const {
103 if (!url_set_.is_empty()) {
104 if (!url_set_.MatchesURL(url))
105 return false;
108 if (!exclude_url_set_.is_empty()) {
109 if (exclude_url_set_.MatchesURL(url))
110 return false;
113 if (!globs_.empty()) {
114 if (!UrlMatchesGlobs(&globs_, url))
115 return false;
118 if (!exclude_globs_.empty()) {
119 if (UrlMatchesGlobs(&exclude_globs_, url))
120 return false;
123 return true;
126 void UserScript::File::Pickle(::Pickle* pickle) const {
127 pickle->WriteString(url_.spec());
128 // Do not write path. It's not needed in the renderer.
129 // Do not write content. It will be serialized by other means.
132 void UserScript::File::Unpickle(const ::Pickle& pickle, PickleIterator* iter) {
133 // Read the url from the pickle.
134 std::string url;
135 CHECK(iter->ReadString(&url));
136 set_url(GURL(url));
139 void UserScript::Pickle(::Pickle* pickle) const {
140 // Write the simple types to the pickle.
141 pickle->WriteInt(run_location());
142 pickle->WriteInt(user_script_id_);
143 pickle->WriteBool(emulate_greasemonkey());
144 pickle->WriteBool(match_all_frames());
145 pickle->WriteBool(match_about_blank());
146 pickle->WriteBool(is_incognito_enabled());
148 PickleHostID(pickle, host_id_);
149 pickle->WriteInt(consumer_instance_type());
150 PickleGlobs(pickle, globs_);
151 PickleGlobs(pickle, exclude_globs_);
152 PickleURLPatternSet(pickle, url_set_);
153 PickleURLPatternSet(pickle, exclude_url_set_);
154 PickleScripts(pickle, js_scripts_);
155 PickleScripts(pickle, css_scripts_);
158 void UserScript::PickleGlobs(::Pickle* pickle,
159 const std::vector<std::string>& globs) const {
160 pickle->WriteSizeT(globs.size());
161 for (std::vector<std::string>::const_iterator glob = globs.begin();
162 glob != globs.end(); ++glob) {
163 pickle->WriteString(*glob);
167 void UserScript::PickleHostID(::Pickle* pickle, const HostID& host_id) const {
168 pickle->WriteInt(host_id.type());
169 pickle->WriteString(host_id.id());
172 void UserScript::PickleURLPatternSet(::Pickle* pickle,
173 const URLPatternSet& pattern_list) const {
174 pickle->WriteSizeT(pattern_list.patterns().size());
175 for (URLPatternSet::const_iterator pattern = pattern_list.begin();
176 pattern != pattern_list.end(); ++pattern) {
177 pickle->WriteInt(pattern->valid_schemes());
178 pickle->WriteString(pattern->GetAsString());
182 void UserScript::PickleScripts(::Pickle* pickle,
183 const FileList& scripts) const {
184 pickle->WriteSizeT(scripts.size());
185 for (FileList::const_iterator file = scripts.begin();
186 file != scripts.end(); ++file) {
187 file->Pickle(pickle);
191 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) {
192 // Read the run location.
193 int run_location = 0;
194 CHECK(iter->ReadInt(&run_location));
195 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST);
196 run_location_ = static_cast<RunLocation>(run_location);
198 CHECK(iter->ReadInt(&user_script_id_));
199 CHECK(iter->ReadBool(&emulate_greasemonkey_));
200 CHECK(iter->ReadBool(&match_all_frames_));
201 CHECK(iter->ReadBool(&match_about_blank_));
202 CHECK(iter->ReadBool(&incognito_enabled_));
204 UnpickleHostID(pickle, iter, &host_id_);
206 int consumer_instance_type = 0;
207 CHECK(iter->ReadInt(&consumer_instance_type));
208 consumer_instance_type_ =
209 static_cast<ConsumerInstanceType>(consumer_instance_type);
211 UnpickleGlobs(pickle, iter, &globs_);
212 UnpickleGlobs(pickle, iter, &exclude_globs_);
213 UnpickleURLPatternSet(pickle, iter, &url_set_);
214 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_);
215 UnpickleScripts(pickle, iter, &js_scripts_);
216 UnpickleScripts(pickle, iter, &css_scripts_);
219 void UserScript::UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter,
220 std::vector<std::string>* globs) {
221 size_t num_globs = 0;
222 CHECK(iter->ReadSizeT(&num_globs));
223 globs->clear();
224 for (size_t i = 0; i < num_globs; ++i) {
225 std::string glob;
226 CHECK(iter->ReadString(&glob));
227 globs->push_back(glob);
231 void UserScript::UnpickleHostID(const ::Pickle& pickle,
232 PickleIterator* iter,
233 HostID* host_id) {
234 int type = 0;
235 std::string id;
236 CHECK(iter->ReadInt(&type));
237 CHECK(iter->ReadString(&id));
238 *host_id = HostID(static_cast<HostID::HostType>(type), id);
241 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle,
242 PickleIterator* iter,
243 URLPatternSet* pattern_list) {
244 size_t num_patterns = 0;
245 CHECK(iter->ReadSizeT(&num_patterns));
247 pattern_list->ClearPatterns();
248 for (size_t i = 0; i < num_patterns; ++i) {
249 int valid_schemes;
250 CHECK(iter->ReadInt(&valid_schemes));
252 std::string pattern_str;
253 CHECK(iter->ReadString(&pattern_str));
255 URLPattern pattern(kValidUserScriptSchemes);
256 URLPattern::ParseResult result = pattern.Parse(pattern_str);
257 CHECK(URLPattern::PARSE_SUCCESS == result) <<
258 URLPattern::GetParseResultString(result) << " " << pattern_str.c_str();
260 pattern.SetValidSchemes(valid_schemes);
261 pattern_list->AddPattern(pattern);
265 void UserScript::UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter,
266 FileList* scripts) {
267 size_t num_files = 0;
268 CHECK(iter->ReadSizeT(&num_files));
269 scripts->clear();
270 for (size_t i = 0; i < num_files; ++i) {
271 File file;
272 file.Unpickle(pickle, iter);
273 scripts->push_back(file);
277 bool operator<(const UserScript& script1, const UserScript& script2) {
278 // The only kind of script that should be compared is the kind that has its
279 // IDs initialized to a meaningful value.
280 DCHECK(script1.id() != -1 && script2.id() != -1);
281 return script1.id() < script2.id();
284 } // namespace extensions