Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / common / user_script.cc
blob6b64741924170006e4ee28814aba76ffb5936ab8
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/pattern.h"
11 #include "base/strings/string_util.h"
12 #include "extensions/common/switches.h"
14 namespace {
16 // This cannot be a plain int or int64 because we need to generate unique IDs
17 // from multiple threads.
18 base::StaticAtomicSequenceNumber g_user_script_id_generator;
20 bool UrlMatchesGlobs(const std::vector<std::string>* globs,
21 const GURL& url) {
22 for (std::vector<std::string>::const_iterator glob = globs->begin();
23 glob != globs->end(); ++glob) {
24 if (base::MatchPattern(url.spec(), *glob))
25 return true;
28 return false;
31 } // namespace
33 namespace extensions {
35 // The bitmask for valid user script injectable schemes used by URLPattern.
36 enum {
37 kValidUserScriptSchemes = URLPattern::SCHEME_CHROMEUI |
38 URLPattern::SCHEME_HTTP |
39 URLPattern::SCHEME_HTTPS |
40 URLPattern::SCHEME_FILE |
41 URLPattern::SCHEME_FTP
44 // static
45 const char UserScript::kFileExtension[] = ".user.js";
48 // static
49 int UserScript::GenerateUserScriptID() {
50 return g_user_script_id_generator.GetNext();
53 bool UserScript::IsURLUserScript(const GURL& url,
54 const std::string& mime_type) {
55 return base::EndsWith(url.ExtractFileName(), kFileExtension,
56 base::CompareCase::INSENSITIVE_ASCII) &&
57 mime_type != "text/html";
60 // static
61 int UserScript::ValidUserScriptSchemes(bool canExecuteScriptEverywhere) {
62 if (canExecuteScriptEverywhere)
63 return URLPattern::SCHEME_ALL;
64 int valid_schemes = kValidUserScriptSchemes;
65 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
66 switches::kExtensionsOnChromeURLs)) {
67 valid_schemes &= ~URLPattern::SCHEME_CHROMEUI;
69 return valid_schemes;
72 UserScript::File::File(const base::FilePath& extension_root,
73 const base::FilePath& relative_path,
74 const GURL& url)
75 : extension_root_(extension_root),
76 relative_path_(relative_path),
77 url_(url) {
80 UserScript::File::File() {}
82 UserScript::File::~File() {}
84 UserScript::UserScript()
85 : run_location_(DOCUMENT_IDLE),
86 consumer_instance_type_(TAB),
87 user_script_id_(-1),
88 emulate_greasemonkey_(false),
89 match_all_frames_(false),
90 match_about_blank_(false),
91 incognito_enabled_(false) {}
93 UserScript::~UserScript() {
96 void UserScript::add_url_pattern(const URLPattern& pattern) {
97 url_set_.AddPattern(pattern);
100 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) {
101 exclude_url_set_.AddPattern(pattern);
104 bool UserScript::MatchesURL(const GURL& url) const {
105 if (!url_set_.is_empty()) {
106 if (!url_set_.MatchesURL(url))
107 return false;
110 if (!exclude_url_set_.is_empty()) {
111 if (exclude_url_set_.MatchesURL(url))
112 return false;
115 if (!globs_.empty()) {
116 if (!UrlMatchesGlobs(&globs_, url))
117 return false;
120 if (!exclude_globs_.empty()) {
121 if (UrlMatchesGlobs(&exclude_globs_, url))
122 return false;
125 return true;
128 void UserScript::File::Pickle(base::Pickle* pickle) const {
129 pickle->WriteString(url_.spec());
130 // Do not write path. It's not needed in the renderer.
131 // Do not write content. It will be serialized by other means.
134 void UserScript::File::Unpickle(const base::Pickle& pickle,
135 base::PickleIterator* iter) {
136 // Read the url from the pickle.
137 std::string url;
138 CHECK(iter->ReadString(&url));
139 set_url(GURL(url));
142 void UserScript::Pickle(base::Pickle* pickle) const {
143 // Write the simple types to the pickle.
144 pickle->WriteInt(run_location());
145 pickle->WriteInt(user_script_id_);
146 pickle->WriteBool(emulate_greasemonkey());
147 pickle->WriteBool(match_all_frames());
148 pickle->WriteBool(match_about_blank());
149 pickle->WriteBool(is_incognito_enabled());
151 PickleHostID(pickle, host_id_);
152 pickle->WriteInt(consumer_instance_type());
153 PickleGlobs(pickle, globs_);
154 PickleGlobs(pickle, exclude_globs_);
155 PickleURLPatternSet(pickle, url_set_);
156 PickleURLPatternSet(pickle, exclude_url_set_);
157 PickleScripts(pickle, js_scripts_);
158 PickleScripts(pickle, css_scripts_);
161 void UserScript::PickleGlobs(base::Pickle* pickle,
162 const std::vector<std::string>& globs) const {
163 pickle->WriteSizeT(globs.size());
164 for (std::vector<std::string>::const_iterator glob = globs.begin();
165 glob != globs.end(); ++glob) {
166 pickle->WriteString(*glob);
170 void UserScript::PickleHostID(base::Pickle* pickle,
171 const HostID& host_id) const {
172 pickle->WriteInt(host_id.type());
173 pickle->WriteString(host_id.id());
176 void UserScript::PickleURLPatternSet(base::Pickle* pickle,
177 const URLPatternSet& pattern_list) const {
178 pickle->WriteSizeT(pattern_list.patterns().size());
179 for (URLPatternSet::const_iterator pattern = pattern_list.begin();
180 pattern != pattern_list.end(); ++pattern) {
181 pickle->WriteInt(pattern->valid_schemes());
182 pickle->WriteString(pattern->GetAsString());
186 void UserScript::PickleScripts(base::Pickle* pickle,
187 const FileList& scripts) const {
188 pickle->WriteSizeT(scripts.size());
189 for (FileList::const_iterator file = scripts.begin();
190 file != scripts.end(); ++file) {
191 file->Pickle(pickle);
195 void UserScript::Unpickle(const base::Pickle& pickle,
196 base::PickleIterator* iter) {
197 // Read the run location.
198 int run_location = 0;
199 CHECK(iter->ReadInt(&run_location));
200 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST);
201 run_location_ = static_cast<RunLocation>(run_location);
203 CHECK(iter->ReadInt(&user_script_id_));
204 CHECK(iter->ReadBool(&emulate_greasemonkey_));
205 CHECK(iter->ReadBool(&match_all_frames_));
206 CHECK(iter->ReadBool(&match_about_blank_));
207 CHECK(iter->ReadBool(&incognito_enabled_));
209 UnpickleHostID(pickle, iter, &host_id_);
211 int consumer_instance_type = 0;
212 CHECK(iter->ReadInt(&consumer_instance_type));
213 consumer_instance_type_ =
214 static_cast<ConsumerInstanceType>(consumer_instance_type);
216 UnpickleGlobs(pickle, iter, &globs_);
217 UnpickleGlobs(pickle, iter, &exclude_globs_);
218 UnpickleURLPatternSet(pickle, iter, &url_set_);
219 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_);
220 UnpickleScripts(pickle, iter, &js_scripts_);
221 UnpickleScripts(pickle, iter, &css_scripts_);
224 void UserScript::UnpickleGlobs(const base::Pickle& pickle,
225 base::PickleIterator* iter,
226 std::vector<std::string>* globs) {
227 size_t num_globs = 0;
228 CHECK(iter->ReadSizeT(&num_globs));
229 globs->clear();
230 for (size_t i = 0; i < num_globs; ++i) {
231 std::string glob;
232 CHECK(iter->ReadString(&glob));
233 globs->push_back(glob);
237 void UserScript::UnpickleHostID(const base::Pickle& pickle,
238 base::PickleIterator* iter,
239 HostID* host_id) {
240 int type = 0;
241 std::string id;
242 CHECK(iter->ReadInt(&type));
243 CHECK(iter->ReadString(&id));
244 *host_id = HostID(static_cast<HostID::HostType>(type), id);
247 void UserScript::UnpickleURLPatternSet(const base::Pickle& pickle,
248 base::PickleIterator* iter,
249 URLPatternSet* pattern_list) {
250 size_t num_patterns = 0;
251 CHECK(iter->ReadSizeT(&num_patterns));
253 pattern_list->ClearPatterns();
254 for (size_t i = 0; i < num_patterns; ++i) {
255 int valid_schemes;
256 CHECK(iter->ReadInt(&valid_schemes));
258 std::string pattern_str;
259 CHECK(iter->ReadString(&pattern_str));
261 URLPattern pattern(kValidUserScriptSchemes);
262 URLPattern::ParseResult result = pattern.Parse(pattern_str);
263 CHECK(URLPattern::PARSE_SUCCESS == result) <<
264 URLPattern::GetParseResultString(result) << " " << pattern_str.c_str();
266 pattern.SetValidSchemes(valid_schemes);
267 pattern_list->AddPattern(pattern);
271 void UserScript::UnpickleScripts(const base::Pickle& pickle,
272 base::PickleIterator* iter,
273 FileList* scripts) {
274 size_t num_files = 0;
275 CHECK(iter->ReadSizeT(&num_files));
276 scripts->clear();
277 for (size_t i = 0; i < num_files; ++i) {
278 File file;
279 file.Unpickle(pickle, iter);
280 scripts->push_back(file);
284 bool operator<(const UserScript& script1, const UserScript& script2) {
285 // The only kind of script that should be compared is the kind that has its
286 // IDs initialized to a meaningful value.
287 DCHECK(script1.id() != -1 && script2.id() != -1);
288 return script1.id() < script2.id();
291 } // namespace extensions