OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/common/user_script.h" | 5 #include "extensions/common/user_script.h" |
6 | 6 |
| 7 #include "base/atomic_sequence_num.h" |
7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
8 #include "base/pickle.h" | 9 #include "base/pickle.h" |
9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
10 #include "extensions/common/switches.h" | 11 #include "extensions/common/switches.h" |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
| 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; |
| 18 |
14 bool UrlMatchesGlobs(const std::vector<std::string>* globs, | 19 bool UrlMatchesGlobs(const std::vector<std::string>* globs, |
15 const GURL& url) { | 20 const GURL& url) { |
16 for (std::vector<std::string>::const_iterator glob = globs->begin(); | 21 for (std::vector<std::string>::const_iterator glob = globs->begin(); |
17 glob != globs->end(); ++glob) { | 22 glob != globs->end(); ++glob) { |
18 if (MatchPattern(url.spec(), *glob)) | 23 if (MatchPattern(url.spec(), *glob)) |
19 return true; | 24 return true; |
20 } | 25 } |
21 | 26 |
22 return false; | 27 return false; |
23 } | 28 } |
24 | 29 |
25 } // namespace | 30 } // namespace |
26 | 31 |
27 namespace extensions { | 32 namespace extensions { |
28 | 33 |
29 // The bitmask for valid user script injectable schemes used by URLPattern. | 34 // The bitmask for valid user script injectable schemes used by URLPattern. |
30 enum { | 35 enum { |
31 kValidUserScriptSchemes = URLPattern::SCHEME_CHROMEUI | | 36 kValidUserScriptSchemes = URLPattern::SCHEME_CHROMEUI | |
32 URLPattern::SCHEME_HTTP | | 37 URLPattern::SCHEME_HTTP | |
33 URLPattern::SCHEME_HTTPS | | 38 URLPattern::SCHEME_HTTPS | |
34 URLPattern::SCHEME_FILE | | 39 URLPattern::SCHEME_FILE | |
35 URLPattern::SCHEME_FTP | 40 URLPattern::SCHEME_FTP |
36 }; | 41 }; |
37 | 42 |
38 // static | 43 // static |
39 const char UserScript::kFileExtension[] = ".user.js"; | 44 const char UserScript::kFileExtension[] = ".user.js"; |
40 | 45 |
| 46 |
| 47 // static |
| 48 int UserScript::GenerateUserScriptID() { |
| 49 return g_user_script_id_generator.GetNext(); |
| 50 } |
| 51 |
41 bool UserScript::IsURLUserScript(const GURL& url, | 52 bool UserScript::IsURLUserScript(const GURL& url, |
42 const std::string& mime_type) { | 53 const std::string& mime_type) { |
43 return EndsWith(url.ExtractFileName(), kFileExtension, false) && | 54 return EndsWith(url.ExtractFileName(), kFileExtension, false) && |
44 mime_type != "text/html"; | 55 mime_type != "text/html"; |
45 } | 56 } |
46 | 57 |
47 // static | 58 // static |
48 int UserScript::ValidUserScriptSchemes(bool canExecuteScriptEverywhere) { | 59 int UserScript::ValidUserScriptSchemes(bool canExecuteScriptEverywhere) { |
49 if (canExecuteScriptEverywhere) | 60 if (canExecuteScriptEverywhere) |
50 return URLPattern::SCHEME_ALL; | 61 return URLPattern::SCHEME_ALL; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 // Read the url from the pickle. | 132 // Read the url from the pickle. |
122 std::string url; | 133 std::string url; |
123 CHECK(pickle.ReadString(iter, &url)); | 134 CHECK(pickle.ReadString(iter, &url)); |
124 set_url(GURL(url)); | 135 set_url(GURL(url)); |
125 } | 136 } |
126 | 137 |
127 void UserScript::Pickle(::Pickle* pickle) const { | 138 void UserScript::Pickle(::Pickle* pickle) const { |
128 // Write the simple types to the pickle. | 139 // Write the simple types to the pickle. |
129 pickle->WriteInt(run_location()); | 140 pickle->WriteInt(run_location()); |
130 pickle->WriteString(extension_id()); | 141 pickle->WriteString(extension_id()); |
131 pickle->WriteInt64(user_script_id_); | 142 pickle->WriteInt(user_script_id_); |
132 pickle->WriteBool(emulate_greasemonkey()); | 143 pickle->WriteBool(emulate_greasemonkey()); |
133 pickle->WriteBool(match_all_frames()); | 144 pickle->WriteBool(match_all_frames()); |
134 pickle->WriteBool(match_about_blank()); | 145 pickle->WriteBool(match_about_blank()); |
135 pickle->WriteBool(is_incognito_enabled()); | 146 pickle->WriteBool(is_incognito_enabled()); |
136 | 147 |
137 PickleGlobs(pickle, globs_); | 148 PickleGlobs(pickle, globs_); |
138 PickleGlobs(pickle, exclude_globs_); | 149 PickleGlobs(pickle, exclude_globs_); |
139 PickleURLPatternSet(pickle, url_set_); | 150 PickleURLPatternSet(pickle, url_set_); |
140 PickleURLPatternSet(pickle, exclude_url_set_); | 151 PickleURLPatternSet(pickle, exclude_url_set_); |
141 PickleScripts(pickle, js_scripts_); | 152 PickleScripts(pickle, js_scripts_); |
(...skipping 29 matching lines...) Expand all Loading... |
171 } | 182 } |
172 | 183 |
173 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { | 184 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { |
174 // Read the run location. | 185 // Read the run location. |
175 int run_location = 0; | 186 int run_location = 0; |
176 CHECK(pickle.ReadInt(iter, &run_location)); | 187 CHECK(pickle.ReadInt(iter, &run_location)); |
177 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); | 188 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); |
178 run_location_ = static_cast<RunLocation>(run_location); | 189 run_location_ = static_cast<RunLocation>(run_location); |
179 | 190 |
180 CHECK(pickle.ReadString(iter, &extension_id_)); | 191 CHECK(pickle.ReadString(iter, &extension_id_)); |
181 CHECK(pickle.ReadInt64(iter, &user_script_id_)); | 192 CHECK(pickle.ReadInt(iter, &user_script_id_)); |
182 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_)); | 193 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_)); |
183 CHECK(pickle.ReadBool(iter, &match_all_frames_)); | 194 CHECK(pickle.ReadBool(iter, &match_all_frames_)); |
184 CHECK(pickle.ReadBool(iter, &match_about_blank_)); | 195 CHECK(pickle.ReadBool(iter, &match_about_blank_)); |
185 CHECK(pickle.ReadBool(iter, &incognito_enabled_)); | 196 CHECK(pickle.ReadBool(iter, &incognito_enabled_)); |
186 | 197 |
187 UnpickleGlobs(pickle, iter, &globs_); | 198 UnpickleGlobs(pickle, iter, &globs_); |
188 UnpickleGlobs(pickle, iter, &exclude_globs_); | 199 UnpickleGlobs(pickle, iter, &exclude_globs_); |
189 UnpickleURLPatternSet(pickle, iter, &url_set_); | 200 UnpickleURLPatternSet(pickle, iter, &url_set_); |
190 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); | 201 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); |
191 UnpickleScripts(pickle, iter, &js_scripts_); | 202 UnpickleScripts(pickle, iter, &js_scripts_); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 } | 252 } |
242 | 253 |
243 bool operator<(const UserScript& script1, const UserScript& script2) { | 254 bool operator<(const UserScript& script1, const UserScript& script2) { |
244 // The only kind of script that should be compared is the kind that has its | 255 // The only kind of script that should be compared is the kind that has its |
245 // IDs initialized to a meaningful value. | 256 // IDs initialized to a meaningful value. |
246 DCHECK(script1.id() != -1 && script2.id() != -1); | 257 DCHECK(script1.id() != -1 && script2.id() != -1); |
247 return script1.id() < script2.id(); | 258 return script1.id() < script2.id(); |
248 } | 259 } |
249 | 260 |
250 } // namespace extensions | 261 } // namespace extensions |
OLD | NEW |