Remove unneeded filename rules for "ozone" and "evdev"
[chromium-blink-merge.git] / extensions / common / user_script.cc
blobddcfd8077777e9844c72dc22919c55fa34042993
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(base::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 base::Pickle& pickle,
133 base::PickleIterator* iter) {
134 // Read the url from the pickle.
135 std::string url;
136 CHECK(iter->ReadString(&url));
137 set_url(GURL(url));
140 void UserScript::Pickle(base::Pickle* pickle) const {
141 // Write the simple types to the pickle.
142 pickle->WriteInt(run_location());
143 pickle->WriteInt(user_script_id_);
144 pickle->WriteBool(emulate_greasemonkey());
145 pickle->WriteBool(match_all_frames());
146 pickle->WriteBool(match_about_blank());
147 pickle->WriteBool(is_incognito_enabled());
149 PickleHostID(pickle, host_id_);
150 pickle->WriteInt(consumer_instance_type());
151 PickleGlobs(pickle, globs_);
152 PickleGlobs(pickle, exclude_globs_);
153 PickleURLPatternSet(pickle, url_set_);
154 PickleURLPatternSet(pickle, exclude_url_set_);
155 PickleScripts(pickle, js_scripts_);
156 PickleScripts(pickle, css_scripts_);
159 void UserScript::PickleGlobs(base::Pickle* pickle,
160 const std::vector<std::string>& globs) const {
161 pickle->WriteSizeT(globs.size());
162 for (std::vector<std::string>::const_iterator glob = globs.begin();
163 glob != globs.end(); ++glob) {
164 pickle->WriteString(*glob);
168 void UserScript::PickleHostID(base::Pickle* pickle,
169 const HostID& host_id) const {
170 pickle->WriteInt(host_id.type());
171 pickle->WriteString(host_id.id());
174 void UserScript::PickleURLPatternSet(base::Pickle* pickle,
175 const URLPatternSet& pattern_list) const {
176 pickle->WriteSizeT(pattern_list.patterns().size());
177 for (URLPatternSet::const_iterator pattern = pattern_list.begin();
178 pattern != pattern_list.end(); ++pattern) {
179 pickle->WriteInt(pattern->valid_schemes());
180 pickle->WriteString(pattern->GetAsString());
184 void UserScript::PickleScripts(base::Pickle* pickle,
185 const FileList& scripts) const {
186 pickle->WriteSizeT(scripts.size());
187 for (FileList::const_iterator file = scripts.begin();
188 file != scripts.end(); ++file) {
189 file->Pickle(pickle);
193 void UserScript::Unpickle(const base::Pickle& pickle,
194 base::PickleIterator* iter) {
195 // Read the run location.
196 int run_location = 0;
197 CHECK(iter->ReadInt(&run_location));
198 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST);
199 run_location_ = static_cast<RunLocation>(run_location);
201 CHECK(iter->ReadInt(&user_script_id_));
202 CHECK(iter->ReadBool(&emulate_greasemonkey_));
203 CHECK(iter->ReadBool(&match_all_frames_));
204 CHECK(iter->ReadBool(&match_about_blank_));
205 CHECK(iter->ReadBool(&incognito_enabled_));
207 UnpickleHostID(pickle, iter, &host_id_);
209 int consumer_instance_type = 0;
210 CHECK(iter->ReadInt(&consumer_instance_type));
211 consumer_instance_type_ =
212 static_cast<ConsumerInstanceType>(consumer_instance_type);
214 UnpickleGlobs(pickle, iter, &globs_);
215 UnpickleGlobs(pickle, iter, &exclude_globs_);
216 UnpickleURLPatternSet(pickle, iter, &url_set_);
217 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_);
218 UnpickleScripts(pickle, iter, &js_scripts_);
219 UnpickleScripts(pickle, iter, &css_scripts_);
222 void UserScript::UnpickleGlobs(const base::Pickle& pickle,
223 base::PickleIterator* iter,
224 std::vector<std::string>* globs) {
225 size_t num_globs = 0;
226 CHECK(iter->ReadSizeT(&num_globs));
227 globs->clear();
228 for (size_t i = 0; i < num_globs; ++i) {
229 std::string glob;
230 CHECK(iter->ReadString(&glob));
231 globs->push_back(glob);
235 void UserScript::UnpickleHostID(const base::Pickle& pickle,
236 base::PickleIterator* iter,
237 HostID* host_id) {
238 int type = 0;
239 std::string id;
240 CHECK(iter->ReadInt(&type));
241 CHECK(iter->ReadString(&id));
242 *host_id = HostID(static_cast<HostID::HostType>(type), id);
245 void UserScript::UnpickleURLPatternSet(const base::Pickle& pickle,
246 base::PickleIterator* iter,
247 URLPatternSet* pattern_list) {
248 size_t num_patterns = 0;
249 CHECK(iter->ReadSizeT(&num_patterns));
251 pattern_list->ClearPatterns();
252 for (size_t i = 0; i < num_patterns; ++i) {
253 int valid_schemes;
254 CHECK(iter->ReadInt(&valid_schemes));
256 std::string pattern_str;
257 CHECK(iter->ReadString(&pattern_str));
259 URLPattern pattern(kValidUserScriptSchemes);
260 URLPattern::ParseResult result = pattern.Parse(pattern_str);
261 CHECK(URLPattern::PARSE_SUCCESS == result) <<
262 URLPattern::GetParseResultString(result) << " " << pattern_str.c_str();
264 pattern.SetValidSchemes(valid_schemes);
265 pattern_list->AddPattern(pattern);
269 void UserScript::UnpickleScripts(const base::Pickle& pickle,
270 base::PickleIterator* iter,
271 FileList* scripts) {
272 size_t num_files = 0;
273 CHECK(iter->ReadSizeT(&num_files));
274 scripts->clear();
275 for (size_t i = 0; i < num_files; ++i) {
276 File file;
277 file.Unpickle(pickle, iter);
278 scripts->push_back(file);
282 bool operator<(const UserScript& script1, const UserScript& script2) {
283 // The only kind of script that should be compared is the kind that has its
284 // IDs initialized to a meaningful value.
285 DCHECK(script1.id() != -1 && script2.id() != -1);
286 return script1.id() < script2.id();
289 } // namespace extensions