OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/common/extensions/user_script.h" | 5 #include "chrome/common/extensions/user_script.h" |
6 | 6 |
| 7 #include "base/command_line.h" |
7 #include "base/pickle.h" | 8 #include "base/pickle.h" |
8 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "chrome/common/chrome_switches.h" |
9 | 11 |
10 namespace { | 12 namespace { |
11 | 13 |
12 bool UrlMatchesGlobs(const std::vector<std::string>* globs, | 14 bool UrlMatchesGlobs(const std::vector<std::string>* globs, |
13 const GURL& url) { | 15 const GURL& url) { |
14 for (std::vector<std::string>::const_iterator glob = globs->begin(); | 16 for (std::vector<std::string>::const_iterator glob = globs->begin(); |
15 glob != globs->end(); ++glob) { | 17 glob != globs->end(); ++glob) { |
16 if (MatchPattern(url.spec(), *glob)) | 18 if (MatchPattern(url.spec(), *glob)) |
17 return true; | 19 return true; |
18 } | 20 } |
19 | 21 |
20 return false; | 22 return false; |
21 } | 23 } |
22 | 24 |
23 } // namespace | 25 } // namespace |
24 | 26 |
25 namespace extensions { | 27 namespace extensions { |
26 | 28 |
| 29 // The bitmask for valid user script injectable schemes used by URLPattern. |
| 30 enum { |
| 31 kValidUserScriptSchemes = URLPattern::SCHEME_CHROMEUI | |
| 32 URLPattern::SCHEME_HTTP | |
| 33 URLPattern::SCHEME_HTTPS | |
| 34 URLPattern::SCHEME_FILE | |
| 35 URLPattern::SCHEME_FTP |
| 36 }; |
| 37 |
27 // static | 38 // static |
28 const char UserScript::kFileExtension[] = ".user.js"; | 39 const char UserScript::kFileExtension[] = ".user.js"; |
29 | 40 |
30 bool UserScript::IsURLUserScript(const GURL& url, | 41 bool UserScript::IsURLUserScript(const GURL& url, |
31 const std::string& mime_type) { | 42 const std::string& mime_type) { |
32 return EndsWith(url.ExtractFileName(), kFileExtension, false) && | 43 return EndsWith(url.ExtractFileName(), kFileExtension, false) && |
33 mime_type != "text/html"; | 44 mime_type != "text/html"; |
34 } | 45 } |
35 | 46 |
| 47 // static |
| 48 int UserScript::ValidUserScriptSchemes(bool canExecuteScriptEverywhere) { |
| 49 if (canExecuteScriptEverywhere) |
| 50 return URLPattern::SCHEME_ALL; |
| 51 int valid_schemes = kValidUserScriptSchemes; |
| 52 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 53 switches::kExtensionsOnChromeURLs)) { |
| 54 valid_schemes &= ~URLPattern::SCHEME_CHROMEUI; |
| 55 } |
| 56 return valid_schemes; |
| 57 } |
| 58 |
36 UserScript::File::File(const base::FilePath& extension_root, | 59 UserScript::File::File(const base::FilePath& extension_root, |
37 const base::FilePath& relative_path, | 60 const base::FilePath& relative_path, |
38 const GURL& url) | 61 const GURL& url) |
39 : extension_root_(extension_root), | 62 : extension_root_(extension_root), |
40 relative_path_(relative_path), | 63 relative_path_(relative_path), |
41 url_(url) { | 64 url_(url) { |
42 } | 65 } |
43 | 66 |
44 UserScript::File::File() {} | 67 UserScript::File::File() {} |
45 | 68 |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, | 200 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, |
178 PickleIterator* iter, | 201 PickleIterator* iter, |
179 URLPatternSet* pattern_list) { | 202 URLPatternSet* pattern_list) { |
180 uint64 num_patterns = 0; | 203 uint64 num_patterns = 0; |
181 CHECK(pickle.ReadUInt64(iter, &num_patterns)); | 204 CHECK(pickle.ReadUInt64(iter, &num_patterns)); |
182 | 205 |
183 pattern_list->ClearPatterns(); | 206 pattern_list->ClearPatterns(); |
184 for (uint64 i = 0; i < num_patterns; ++i) { | 207 for (uint64 i = 0; i < num_patterns; ++i) { |
185 int valid_schemes; | 208 int valid_schemes; |
186 CHECK(pickle.ReadInt(iter, &valid_schemes)); | 209 CHECK(pickle.ReadInt(iter, &valid_schemes)); |
| 210 |
187 std::string pattern_str; | 211 std::string pattern_str; |
188 URLPattern pattern(valid_schemes); | |
189 CHECK(pickle.ReadString(iter, &pattern_str)); | 212 CHECK(pickle.ReadString(iter, &pattern_str)); |
190 | 213 |
191 // We remove the file scheme if it's not actually allowed (see Extension:: | 214 URLPattern pattern(kValidUserScriptSchemes); |
192 // LoadUserScriptHelper), but we need it temporarily while loading the | 215 URLPattern::ParseResult result = pattern.Parse(pattern_str); |
193 // pattern so that it's valid. | 216 CHECK(URLPattern::PARSE_SUCCESS == result) << |
194 bool had_file_scheme = (valid_schemes & URLPattern::SCHEME_FILE) != 0; | 217 URLPattern::GetParseResultString(result) << " " << pattern_str.c_str(); |
195 if (!had_file_scheme) | |
196 pattern.SetValidSchemes(valid_schemes | URLPattern::SCHEME_FILE); | |
197 CHECK(URLPattern::PARSE_SUCCESS == pattern.Parse(pattern_str)); | |
198 if (!had_file_scheme) | |
199 pattern.SetValidSchemes(valid_schemes); | |
200 | 218 |
| 219 pattern.SetValidSchemes(valid_schemes); |
201 pattern_list->AddPattern(pattern); | 220 pattern_list->AddPattern(pattern); |
202 } | 221 } |
203 } | 222 } |
204 | 223 |
205 void UserScript::UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter, | 224 void UserScript::UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter, |
206 FileList* scripts) { | 225 FileList* scripts) { |
207 uint64 num_files = 0; | 226 uint64 num_files = 0; |
208 CHECK(pickle.ReadUInt64(iter, &num_files)); | 227 CHECK(pickle.ReadUInt64(iter, &num_files)); |
209 scripts->clear(); | 228 scripts->clear(); |
210 for (uint64 i = 0; i < num_files; ++i) { | 229 for (uint64 i = 0; i < num_files; ++i) { |
211 File file; | 230 File file; |
212 file.Unpickle(pickle, iter); | 231 file.Unpickle(pickle, iter); |
213 scripts->push_back(file); | 232 scripts->push_back(file); |
214 } | 233 } |
215 } | 234 } |
216 | 235 |
217 } // namespace extensions | 236 } // namespace extensions |
OLD | NEW |