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"
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
,
21 for (std::vector
<std::string
>::const_iterator glob
= globs
->begin();
22 glob
!= globs
->end(); ++glob
) {
23 if (MatchPattern(url
.spec(), *glob
))
32 namespace extensions
{
34 // The bitmask for valid user script injectable schemes used by URLPattern.
36 kValidUserScriptSchemes
= URLPattern::SCHEME_CHROMEUI
|
37 URLPattern::SCHEME_HTTP
|
38 URLPattern::SCHEME_HTTPS
|
39 URLPattern::SCHEME_FILE
|
40 URLPattern::SCHEME_FTP
44 const char UserScript::kFileExtension
[] = ".user.js";
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";
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
;
70 UserScript::File::File(const base::FilePath
& extension_root
,
71 const base::FilePath
& relative_path
,
73 : extension_root_(extension_root
),
74 relative_path_(relative_path
),
78 UserScript::File::File() {}
80 UserScript::File::~File() {}
82 UserScript::UserScript()
83 : run_location_(DOCUMENT_IDLE
),
84 consumer_instance_type_(TAB
),
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
))
108 if (!exclude_url_set_
.is_empty()) {
109 if (exclude_url_set_
.MatchesURL(url
))
113 if (!globs_
.empty()) {
114 if (!UrlMatchesGlobs(&globs_
, url
))
118 if (!exclude_globs_
.empty()) {
119 if (UrlMatchesGlobs(&exclude_globs_
, url
))
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.
135 CHECK(iter
->ReadString(&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 PickleRoutingInfo(pickle
, routing_info_
);
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(::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(::Pickle
* pickle
, const HostID
& host_id
) const {
169 pickle
->WriteInt(host_id
.type());
170 pickle
->WriteString(host_id
.id());
173 void UserScript::PickleRoutingInfo(::Pickle
* pickle
,
174 const RoutingInfo
& routing_info
) const {
175 pickle
->WriteInt(routing_info
.render_process_id
);
176 pickle
->WriteInt(routing_info
.render_view_id
);
179 void UserScript::PickleURLPatternSet(::Pickle
* pickle
,
180 const URLPatternSet
& pattern_list
) const {
181 pickle
->WriteSizeT(pattern_list
.patterns().size());
182 for (URLPatternSet::const_iterator pattern
= pattern_list
.begin();
183 pattern
!= pattern_list
.end(); ++pattern
) {
184 pickle
->WriteInt(pattern
->valid_schemes());
185 pickle
->WriteString(pattern
->GetAsString());
189 void UserScript::PickleScripts(::Pickle
* pickle
,
190 const FileList
& scripts
) const {
191 pickle
->WriteSizeT(scripts
.size());
192 for (FileList::const_iterator file
= scripts
.begin();
193 file
!= scripts
.end(); ++file
) {
194 file
->Pickle(pickle
);
198 void UserScript::Unpickle(const ::Pickle
& pickle
, PickleIterator
* iter
) {
199 // Read the run location.
200 int run_location
= 0;
201 CHECK(iter
->ReadInt(&run_location
));
202 CHECK(run_location
>= 0 && run_location
< RUN_LOCATION_LAST
);
203 run_location_
= static_cast<RunLocation
>(run_location
);
205 CHECK(iter
->ReadInt(&user_script_id_
));
206 CHECK(iter
->ReadBool(&emulate_greasemonkey_
));
207 CHECK(iter
->ReadBool(&match_all_frames_
));
208 CHECK(iter
->ReadBool(&match_about_blank_
));
209 CHECK(iter
->ReadBool(&incognito_enabled_
));
211 UnpickleHostID(pickle
, iter
, &host_id_
);
213 int consumer_instance_type
= 0;
214 CHECK(iter
->ReadInt(&consumer_instance_type
));
215 consumer_instance_type_
=
216 static_cast<ConsumerInstanceType
>(consumer_instance_type
);
218 UnpickleRoutingInfo(pickle
, iter
, &routing_info_
);
219 UnpickleGlobs(pickle
, iter
, &globs_
);
220 UnpickleGlobs(pickle
, iter
, &exclude_globs_
);
221 UnpickleURLPatternSet(pickle
, iter
, &url_set_
);
222 UnpickleURLPatternSet(pickle
, iter
, &exclude_url_set_
);
223 UnpickleScripts(pickle
, iter
, &js_scripts_
);
224 UnpickleScripts(pickle
, iter
, &css_scripts_
);
227 void UserScript::UnpickleGlobs(const ::Pickle
& pickle
, PickleIterator
* iter
,
228 std::vector
<std::string
>* globs
) {
229 size_t num_globs
= 0;
230 CHECK(iter
->ReadSizeT(&num_globs
));
232 for (size_t i
= 0; i
< num_globs
; ++i
) {
234 CHECK(iter
->ReadString(&glob
));
235 globs
->push_back(glob
);
239 void UserScript::UnpickleHostID(const ::Pickle
& pickle
,
240 PickleIterator
* iter
,
244 CHECK(iter
->ReadInt(&type
));
245 CHECK(iter
->ReadString(&id
));
246 *host_id
= HostID(static_cast<HostID::HostType
>(type
), id
);
249 void UserScript::UnpickleRoutingInfo(const ::Pickle
& pickle
,
250 PickleIterator
* iter
,
251 RoutingInfo
* routing_info
) {
252 CHECK(iter
->ReadInt(&routing_info
->render_process_id
));
253 CHECK(iter
->ReadInt(&routing_info
->render_view_id
));
256 void UserScript::UnpickleURLPatternSet(const ::Pickle
& pickle
,
257 PickleIterator
* iter
,
258 URLPatternSet
* pattern_list
) {
259 size_t num_patterns
= 0;
260 CHECK(iter
->ReadSizeT(&num_patterns
));
262 pattern_list
->ClearPatterns();
263 for (size_t i
= 0; i
< num_patterns
; ++i
) {
265 CHECK(iter
->ReadInt(&valid_schemes
));
267 std::string pattern_str
;
268 CHECK(iter
->ReadString(&pattern_str
));
270 URLPattern
pattern(kValidUserScriptSchemes
);
271 URLPattern::ParseResult result
= pattern
.Parse(pattern_str
);
272 CHECK(URLPattern::PARSE_SUCCESS
== result
) <<
273 URLPattern::GetParseResultString(result
) << " " << pattern_str
.c_str();
275 pattern
.SetValidSchemes(valid_schemes
);
276 pattern_list
->AddPattern(pattern
);
280 void UserScript::UnpickleScripts(const ::Pickle
& pickle
, PickleIterator
* iter
,
282 size_t num_files
= 0;
283 CHECK(iter
->ReadSizeT(&num_files
));
285 for (size_t i
= 0; i
< num_files
; ++i
) {
287 file
.Unpickle(pickle
, iter
);
288 scripts
->push_back(file
);
292 bool operator<(const UserScript
& script1
, const UserScript
& script2
) {
293 // The only kind of script that should be compared is the kind that has its
294 // IDs initialized to a meaningful value.
295 DCHECK(script1
.id() != -1 && script2
.id() != -1);
296 return script1
.id() < script2
.id();
299 } // namespace extensions