Fix content_browsertests on Linux GN build.
[chromium-blink-merge.git] / extensions / common / user_script.cc
blobb8ba22e1d7639523157f3cf99abbafe666117255
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 user_script_id_(-1),
85 emulate_greasemonkey_(false),
86 match_all_frames_(false),
87 match_about_blank_(false),
88 incognito_enabled_(false) {}
90 UserScript::~UserScript() {
93 void UserScript::add_url_pattern(const URLPattern& pattern) {
94 url_set_.AddPattern(pattern);
97 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) {
98 exclude_url_set_.AddPattern(pattern);
101 bool UserScript::MatchesURL(const GURL& url) const {
102 if (!url_set_.is_empty()) {
103 if (!url_set_.MatchesURL(url))
104 return false;
107 if (!exclude_url_set_.is_empty()) {
108 if (exclude_url_set_.MatchesURL(url))
109 return false;
112 if (!globs_.empty()) {
113 if (!UrlMatchesGlobs(&globs_, url))
114 return false;
117 if (!exclude_globs_.empty()) {
118 if (UrlMatchesGlobs(&exclude_globs_, url))
119 return false;
122 return true;
125 void UserScript::File::Pickle(::Pickle* pickle) const {
126 pickle->WriteString(url_.spec());
127 // Do not write path. It's not needed in the renderer.
128 // Do not write content. It will be serialized by other means.
131 void UserScript::File::Unpickle(const ::Pickle& pickle, PickleIterator* iter) {
132 // Read the url from the pickle.
133 std::string url;
134 CHECK(iter->ReadString(&url));
135 set_url(GURL(url));
138 void UserScript::Pickle(::Pickle* pickle) const {
139 // Write the simple types to the pickle.
140 pickle->WriteInt(run_location());
141 pickle->WriteInt(user_script_id_);
142 pickle->WriteBool(emulate_greasemonkey());
143 pickle->WriteBool(match_all_frames());
144 pickle->WriteBool(match_about_blank());
145 pickle->WriteBool(is_incognito_enabled());
147 PickleHostID(pickle, host_id_);
148 PickleGlobs(pickle, globs_);
149 PickleGlobs(pickle, exclude_globs_);
150 PickleURLPatternSet(pickle, url_set_);
151 PickleURLPatternSet(pickle, exclude_url_set_);
152 PickleScripts(pickle, js_scripts_);
153 PickleScripts(pickle, css_scripts_);
156 void UserScript::PickleGlobs(::Pickle* pickle,
157 const std::vector<std::string>& globs) const {
158 pickle->WriteSizeT(globs.size());
159 for (std::vector<std::string>::const_iterator glob = globs.begin();
160 glob != globs.end(); ++glob) {
161 pickle->WriteString(*glob);
165 void UserScript::PickleHostID(::Pickle* pickle, const HostID& host_id) const {
166 pickle->WriteInt(host_id.type());
167 pickle->WriteString(host_id.id());
170 void UserScript::PickleURLPatternSet(::Pickle* pickle,
171 const URLPatternSet& pattern_list) const {
172 pickle->WriteSizeT(pattern_list.patterns().size());
173 for (URLPatternSet::const_iterator pattern = pattern_list.begin();
174 pattern != pattern_list.end(); ++pattern) {
175 pickle->WriteInt(pattern->valid_schemes());
176 pickle->WriteString(pattern->GetAsString());
180 void UserScript::PickleScripts(::Pickle* pickle,
181 const FileList& scripts) const {
182 pickle->WriteSizeT(scripts.size());
183 for (FileList::const_iterator file = scripts.begin();
184 file != scripts.end(); ++file) {
185 file->Pickle(pickle);
189 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) {
190 // Read the run location.
191 int run_location = 0;
192 CHECK(iter->ReadInt(&run_location));
193 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST);
194 run_location_ = static_cast<RunLocation>(run_location);
196 CHECK(iter->ReadInt(&user_script_id_));
197 CHECK(iter->ReadBool(&emulate_greasemonkey_));
198 CHECK(iter->ReadBool(&match_all_frames_));
199 CHECK(iter->ReadBool(&match_about_blank_));
200 CHECK(iter->ReadBool(&incognito_enabled_));
202 UnpickleHostID(pickle, iter, &host_id_);
203 UnpickleGlobs(pickle, iter, &globs_);
204 UnpickleGlobs(pickle, iter, &exclude_globs_);
205 UnpickleURLPatternSet(pickle, iter, &url_set_);
206 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_);
207 UnpickleScripts(pickle, iter, &js_scripts_);
208 UnpickleScripts(pickle, iter, &css_scripts_);
211 void UserScript::UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter,
212 std::vector<std::string>* globs) {
213 size_t num_globs = 0;
214 CHECK(iter->ReadSizeT(&num_globs));
215 globs->clear();
216 for (size_t i = 0; i < num_globs; ++i) {
217 std::string glob;
218 CHECK(iter->ReadString(&glob));
219 globs->push_back(glob);
223 void UserScript::UnpickleHostID(const ::Pickle& pickle,
224 PickleIterator* iter,
225 HostID* host_id) {
226 int type = 0;
227 std::string id;
228 CHECK(iter->ReadInt(&type));
229 CHECK(iter->ReadString(&id));
230 *host_id = HostID(static_cast<HostID::HostType>(type), id);
233 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle,
234 PickleIterator* iter,
235 URLPatternSet* pattern_list) {
236 size_t num_patterns = 0;
237 CHECK(iter->ReadSizeT(&num_patterns));
239 pattern_list->ClearPatterns();
240 for (size_t i = 0; i < num_patterns; ++i) {
241 int valid_schemes;
242 CHECK(iter->ReadInt(&valid_schemes));
244 std::string pattern_str;
245 CHECK(iter->ReadString(&pattern_str));
247 URLPattern pattern(kValidUserScriptSchemes);
248 URLPattern::ParseResult result = pattern.Parse(pattern_str);
249 CHECK(URLPattern::PARSE_SUCCESS == result) <<
250 URLPattern::GetParseResultString(result) << " " << pattern_str.c_str();
252 pattern.SetValidSchemes(valid_schemes);
253 pattern_list->AddPattern(pattern);
257 void UserScript::UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter,
258 FileList* scripts) {
259 size_t num_files = 0;
260 CHECK(iter->ReadSizeT(&num_files));
261 scripts->clear();
262 for (size_t i = 0; i < num_files; ++i) {
263 File file;
264 file.Unpickle(pickle, iter);
265 scripts->push_back(file);
269 bool operator<(const UserScript& script1, const UserScript& script2) {
270 // The only kind of script that should be compared is the kind that has its
271 // IDs initialized to a meaningful value.
272 DCHECK(script1.id() != -1 && script2.id() != -1);
273 return script1.id() < script2.id();
276 } // namespace extensions