Move MatchPattern to its own header and the base namespace.
[chromium-blink-merge.git] / extensions / common / user_script.cc
blobb3d82bba795a04871921edebe6adc55552311be2
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, false) &&
56 mime_type != "text/html";
59 // static
60 int UserScript::ValidUserScriptSchemes(bool canExecuteScriptEverywhere) {
61 if (canExecuteScriptEverywhere)
62 return URLPattern::SCHEME_ALL;
63 int valid_schemes = kValidUserScriptSchemes;
64 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
65 switches::kExtensionsOnChromeURLs)) {
66 valid_schemes &= ~URLPattern::SCHEME_CHROMEUI;
68 return valid_schemes;
71 UserScript::File::File(const base::FilePath& extension_root,
72 const base::FilePath& relative_path,
73 const GURL& url)
74 : extension_root_(extension_root),
75 relative_path_(relative_path),
76 url_(url) {
79 UserScript::File::File() {}
81 UserScript::File::~File() {}
83 UserScript::UserScript()
84 : run_location_(DOCUMENT_IDLE),
85 consumer_instance_type_(TAB),
86 user_script_id_(-1),
87 emulate_greasemonkey_(false),
88 match_all_frames_(false),
89 match_about_blank_(false),
90 incognito_enabled_(false) {}
92 UserScript::~UserScript() {
95 void UserScript::add_url_pattern(const URLPattern& pattern) {
96 url_set_.AddPattern(pattern);
99 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) {
100 exclude_url_set_.AddPattern(pattern);
103 bool UserScript::MatchesURL(const GURL& url) const {
104 if (!url_set_.is_empty()) {
105 if (!url_set_.MatchesURL(url))
106 return false;
109 if (!exclude_url_set_.is_empty()) {
110 if (exclude_url_set_.MatchesURL(url))
111 return false;
114 if (!globs_.empty()) {
115 if (!UrlMatchesGlobs(&globs_, url))
116 return false;
119 if (!exclude_globs_.empty()) {
120 if (UrlMatchesGlobs(&exclude_globs_, url))
121 return false;
124 return true;
127 void UserScript::File::Pickle(base::Pickle* pickle) const {
128 pickle->WriteString(url_.spec());
129 // Do not write path. It's not needed in the renderer.
130 // Do not write content. It will be serialized by other means.
133 void UserScript::File::Unpickle(const base::Pickle& pickle,
134 base::PickleIterator* iter) {
135 // Read the url from the pickle.
136 std::string url;
137 CHECK(iter->ReadString(&url));
138 set_url(GURL(url));
141 void UserScript::Pickle(base::Pickle* pickle) const {
142 // Write the simple types to the pickle.
143 pickle->WriteInt(run_location());
144 pickle->WriteInt(user_script_id_);
145 pickle->WriteBool(emulate_greasemonkey());
146 pickle->WriteBool(match_all_frames());
147 pickle->WriteBool(match_about_blank());
148 pickle->WriteBool(is_incognito_enabled());
150 PickleHostID(pickle, host_id_);
151 pickle->WriteInt(consumer_instance_type());
152 PickleGlobs(pickle, globs_);
153 PickleGlobs(pickle, exclude_globs_);
154 PickleURLPatternSet(pickle, url_set_);
155 PickleURLPatternSet(pickle, exclude_url_set_);
156 PickleScripts(pickle, js_scripts_);
157 PickleScripts(pickle, css_scripts_);
160 void UserScript::PickleGlobs(base::Pickle* pickle,
161 const std::vector<std::string>& globs) const {
162 pickle->WriteSizeT(globs.size());
163 for (std::vector<std::string>::const_iterator glob = globs.begin();
164 glob != globs.end(); ++glob) {
165 pickle->WriteString(*glob);
169 void UserScript::PickleHostID(base::Pickle* pickle,
170 const HostID& host_id) const {
171 pickle->WriteInt(host_id.type());
172 pickle->WriteString(host_id.id());
175 void UserScript::PickleURLPatternSet(base::Pickle* pickle,
176 const URLPatternSet& pattern_list) const {
177 pickle->WriteSizeT(pattern_list.patterns().size());
178 for (URLPatternSet::const_iterator pattern = pattern_list.begin();
179 pattern != pattern_list.end(); ++pattern) {
180 pickle->WriteInt(pattern->valid_schemes());
181 pickle->WriteString(pattern->GetAsString());
185 void UserScript::PickleScripts(base::Pickle* pickle,
186 const FileList& scripts) const {
187 pickle->WriteSizeT(scripts.size());
188 for (FileList::const_iterator file = scripts.begin();
189 file != scripts.end(); ++file) {
190 file->Pickle(pickle);
194 void UserScript::Unpickle(const base::Pickle& pickle,
195 base::PickleIterator* iter) {
196 // Read the run location.
197 int run_location = 0;
198 CHECK(iter->ReadInt(&run_location));
199 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST);
200 run_location_ = static_cast<RunLocation>(run_location);
202 CHECK(iter->ReadInt(&user_script_id_));
203 CHECK(iter->ReadBool(&emulate_greasemonkey_));
204 CHECK(iter->ReadBool(&match_all_frames_));
205 CHECK(iter->ReadBool(&match_about_blank_));
206 CHECK(iter->ReadBool(&incognito_enabled_));
208 UnpickleHostID(pickle, iter, &host_id_);
210 int consumer_instance_type = 0;
211 CHECK(iter->ReadInt(&consumer_instance_type));
212 consumer_instance_type_ =
213 static_cast<ConsumerInstanceType>(consumer_instance_type);
215 UnpickleGlobs(pickle, iter, &globs_);
216 UnpickleGlobs(pickle, iter, &exclude_globs_);
217 UnpickleURLPatternSet(pickle, iter, &url_set_);
218 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_);
219 UnpickleScripts(pickle, iter, &js_scripts_);
220 UnpickleScripts(pickle, iter, &css_scripts_);
223 void UserScript::UnpickleGlobs(const base::Pickle& pickle,
224 base::PickleIterator* iter,
225 std::vector<std::string>* globs) {
226 size_t num_globs = 0;
227 CHECK(iter->ReadSizeT(&num_globs));
228 globs->clear();
229 for (size_t i = 0; i < num_globs; ++i) {
230 std::string glob;
231 CHECK(iter->ReadString(&glob));
232 globs->push_back(glob);
236 void UserScript::UnpickleHostID(const base::Pickle& pickle,
237 base::PickleIterator* iter,
238 HostID* host_id) {
239 int type = 0;
240 std::string id;
241 CHECK(iter->ReadInt(&type));
242 CHECK(iter->ReadString(&id));
243 *host_id = HostID(static_cast<HostID::HostType>(type), id);
246 void UserScript::UnpickleURLPatternSet(const base::Pickle& pickle,
247 base::PickleIterator* iter,
248 URLPatternSet* pattern_list) {
249 size_t num_patterns = 0;
250 CHECK(iter->ReadSizeT(&num_patterns));
252 pattern_list->ClearPatterns();
253 for (size_t i = 0; i < num_patterns; ++i) {
254 int valid_schemes;
255 CHECK(iter->ReadInt(&valid_schemes));
257 std::string pattern_str;
258 CHECK(iter->ReadString(&pattern_str));
260 URLPattern pattern(kValidUserScriptSchemes);
261 URLPattern::ParseResult result = pattern.Parse(pattern_str);
262 CHECK(URLPattern::PARSE_SUCCESS == result) <<
263 URLPattern::GetParseResultString(result) << " " << pattern_str.c_str();
265 pattern.SetValidSchemes(valid_schemes);
266 pattern_list->AddPattern(pattern);
270 void UserScript::UnpickleScripts(const base::Pickle& pickle,
271 base::PickleIterator* iter,
272 FileList* scripts) {
273 size_t num_files = 0;
274 CHECK(iter->ReadSizeT(&num_files));
275 scripts->clear();
276 for (size_t i = 0; i < num_files; ++i) {
277 File file;
278 file.Unpickle(pickle, iter);
279 scripts->push_back(file);
283 bool operator<(const UserScript& script1, const UserScript& script2) {
284 // The only kind of script that should be compared is the kind that has its
285 // IDs initialized to a meaningful value.
286 DCHECK(script1.id() != -1 && script2.id() != -1);
287 return script1.id() < script2.id();
290 } // namespace extensions