Chromium Code Reviews| 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 |
| 27 // static | 29 // static |
| 28 const char UserScript::kFileExtension[] = ".user.js"; | 30 const char UserScript::kFileExtension[] = ".user.js"; |
| 29 | 31 |
| 30 bool UserScript::IsURLUserScript(const GURL& url, | 32 bool UserScript::IsURLUserScript(const GURL& url, |
| 31 const std::string& mime_type) { | 33 const std::string& mime_type) { |
| 32 return EndsWith(url.ExtractFileName(), kFileExtension, false) && | 34 return EndsWith(url.ExtractFileName(), kFileExtension, false) && |
| 33 mime_type != "text/html"; | 35 mime_type != "text/html"; |
| 34 } | 36 } |
| 35 | 37 |
| 38 // static | |
| 39 int UserScript::ValidUserScriptSchemes(bool canExecuteScriptEverywhere) { | |
| 40 if (canExecuteScriptEverywhere) | |
| 41 return URLPattern::SCHEME_ALL; | |
| 42 int valid_schemes = kValidUserScriptSchemes; | |
| 43 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
| 44 switches::kExtensionsOnChromeURLs)) { | |
| 45 valid_schemes &= ~URLPattern::SCHEME_CHROMEUI; | |
| 46 } | |
| 47 return valid_schemes; | |
| 48 } | |
| 49 | |
| 36 UserScript::File::File(const base::FilePath& extension_root, | 50 UserScript::File::File(const base::FilePath& extension_root, |
| 37 const base::FilePath& relative_path, | 51 const base::FilePath& relative_path, |
| 38 const GURL& url) | 52 const GURL& url) |
| 39 : extension_root_(extension_root), | 53 : extension_root_(extension_root), |
| 40 relative_path_(relative_path), | 54 relative_path_(relative_path), |
| 41 url_(url) { | 55 url_(url) { |
| 42 } | 56 } |
| 43 | 57 |
| 44 UserScript::File::File() {} | 58 UserScript::File::File() {} |
| 45 | 59 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 glob != globs.end(); ++glob) { | 135 glob != globs.end(); ++glob) { |
| 122 pickle->WriteString(*glob); | 136 pickle->WriteString(*glob); |
| 123 } | 137 } |
| 124 } | 138 } |
| 125 | 139 |
| 126 void UserScript::PickleURLPatternSet(::Pickle* pickle, | 140 void UserScript::PickleURLPatternSet(::Pickle* pickle, |
| 127 const URLPatternSet& pattern_list) const { | 141 const URLPatternSet& pattern_list) const { |
| 128 pickle->WriteUInt64(pattern_list.patterns().size()); | 142 pickle->WriteUInt64(pattern_list.patterns().size()); |
| 129 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); | 143 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); |
| 130 pattern != pattern_list.end(); ++pattern) { | 144 pattern != pattern_list.end(); ++pattern) { |
| 145 pickle->WriteString(pattern->GetAsString()); | |
|
Matt Perry
2013/03/19 17:49:32
Why change this?
aboxhall
2013/03/20 22:04:59
Because I don't want to set valid_schemes until af
Matt Perry
2013/03/20 22:32:48
Yeah I'd prefer to keep the old pickle order, just
aboxhall
2013/03/20 22:51:48
Good call, done.
| |
| 131 pickle->WriteInt(pattern->valid_schemes()); | 146 pickle->WriteInt(pattern->valid_schemes()); |
| 132 pickle->WriteString(pattern->GetAsString()); | |
| 133 } | 147 } |
| 134 } | 148 } |
| 135 | 149 |
| 136 void UserScript::PickleScripts(::Pickle* pickle, | 150 void UserScript::PickleScripts(::Pickle* pickle, |
| 137 const FileList& scripts) const { | 151 const FileList& scripts) const { |
| 138 pickle->WriteUInt64(scripts.size()); | 152 pickle->WriteUInt64(scripts.size()); |
| 139 for (FileList::const_iterator file = scripts.begin(); | 153 for (FileList::const_iterator file = scripts.begin(); |
| 140 file != scripts.end(); ++file) { | 154 file != scripts.end(); ++file) { |
| 141 file->Pickle(pickle); | 155 file->Pickle(pickle); |
| 142 } | 156 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 } | 189 } |
| 176 | 190 |
| 177 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, | 191 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, |
| 178 PickleIterator* iter, | 192 PickleIterator* iter, |
| 179 URLPatternSet* pattern_list) { | 193 URLPatternSet* pattern_list) { |
| 180 uint64 num_patterns = 0; | 194 uint64 num_patterns = 0; |
| 181 CHECK(pickle.ReadUInt64(iter, &num_patterns)); | 195 CHECK(pickle.ReadUInt64(iter, &num_patterns)); |
| 182 | 196 |
| 183 pattern_list->ClearPatterns(); | 197 pattern_list->ClearPatterns(); |
| 184 for (uint64 i = 0; i < num_patterns; ++i) { | 198 for (uint64 i = 0; i < num_patterns; ++i) { |
| 199 std::string pattern_str; | |
| 200 URLPattern pattern(kValidUserScriptSchemes); | |
| 201 CHECK(pickle.ReadString(iter, &pattern_str)); | |
| 202 | |
| 203 URLPattern::ParseResult result = pattern.Parse(pattern_str); | |
| 204 CHECK(URLPattern::PARSE_SUCCESS == result) << | |
| 205 URLPattern::GetParseResultString(result) << " " << pattern_str.c_str(); | |
| 206 | |
| 185 int valid_schemes; | 207 int valid_schemes; |
| 186 CHECK(pickle.ReadInt(iter, &valid_schemes)); | 208 CHECK(pickle.ReadInt(iter, &valid_schemes)); |
| 187 std::string pattern_str; | 209 pattern.SetValidSchemes(valid_schemes); |
| 188 URLPattern pattern(valid_schemes); | |
| 189 CHECK(pickle.ReadString(iter, &pattern_str)); | |
| 190 | |
| 191 // We remove the file scheme if it's not actually allowed (see Extension:: | |
| 192 // LoadUserScriptHelper), but we need it temporarily while loading the | |
| 193 // pattern so that it's valid. | |
| 194 bool had_file_scheme = (valid_schemes & URLPattern::SCHEME_FILE) != 0; | |
| 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 | |
| 201 pattern_list->AddPattern(pattern); | 210 pattern_list->AddPattern(pattern); |
| 202 } | 211 } |
| 203 } | 212 } |
| 204 | 213 |
| 205 void UserScript::UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter, | 214 void UserScript::UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter, |
| 206 FileList* scripts) { | 215 FileList* scripts) { |
| 207 uint64 num_files = 0; | 216 uint64 num_files = 0; |
| 208 CHECK(pickle.ReadUInt64(iter, &num_files)); | 217 CHECK(pickle.ReadUInt64(iter, &num_files)); |
| 209 scripts->clear(); | 218 scripts->clear(); |
| 210 for (uint64 i = 0; i < num_files; ++i) { | 219 for (uint64 i = 0; i < num_files; ++i) { |
| 211 File file; | 220 File file; |
| 212 file.Unpickle(pickle, iter); | 221 file.Unpickle(pickle, iter); |
| 213 scripts->push_back(file); | 222 scripts->push_back(file); |
| 214 } | 223 } |
| 215 } | 224 } |
| 216 | 225 |
| 217 } // namespace extensions | 226 } // namespace extensions |
| OLD | NEW |