| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/atomic_sequence_num.h" | 10 #include "base/atomic_sequence_num.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 if (canExecuteScriptEverywhere) | 65 if (canExecuteScriptEverywhere) |
| 66 return URLPattern::SCHEME_ALL; | 66 return URLPattern::SCHEME_ALL; |
| 67 int valid_schemes = kValidUserScriptSchemes; | 67 int valid_schemes = kValidUserScriptSchemes; |
| 68 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 68 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 69 switches::kExtensionsOnChromeURLs)) { | 69 switches::kExtensionsOnChromeURLs)) { |
| 70 valid_schemes &= ~URLPattern::SCHEME_CHROMEUI; | 70 valid_schemes &= ~URLPattern::SCHEME_CHROMEUI; |
| 71 } | 71 } |
| 72 return valid_schemes; | 72 return valid_schemes; |
| 73 } | 73 } |
| 74 | 74 |
| 75 UserScript::File::File(const base::FilePath& extension_root, | 75 UserScriptFileInfo::UserScriptFileInfo(const base::FilePath& extension_root, |
| 76 const base::FilePath& relative_path, | 76 const base::FilePath& relative_path, |
| 77 const GURL& url) | 77 const GURL& url) |
| 78 : extension_root_(extension_root), | 78 : extension_root_(extension_root), |
| 79 relative_path_(relative_path), | 79 relative_path_(relative_path), |
| 80 url_(url) { | 80 url_(url) {} |
| 81 |
| 82 UserScriptFileInfo::UserScriptFileInfo() {} |
| 83 |
| 84 UserScriptFileInfo::UserScriptFileInfo(const UserScriptFileInfo& other) = |
| 85 default; |
| 86 |
| 87 UserScriptFileInfo::~UserScriptFileInfo() {} |
| 88 |
| 89 void UserScriptFileInfo::Pickle(base::Pickle* pickle) const { |
| 90 pickle->WriteString(url_.spec()); |
| 91 // Do not write path. It's not needed in the renderer. |
| 92 // Do not write content. It will be serialized by other means. |
| 81 } | 93 } |
| 82 | 94 |
| 83 UserScript::File::File() {} | 95 void UserScriptFileInfo::Unpickle(const base::Pickle& pickle, |
| 96 base::PickleIterator* iter) { |
| 97 // Read the url from the pickle. |
| 98 std::string url; |
| 99 CHECK(iter->ReadString(&url)); |
| 100 set_url(GURL(url)); |
| 101 } |
| 84 | 102 |
| 85 UserScript::File::File(const File& other) = default; | 103 UserScriptInfo::UserScriptInfo() |
| 86 | 104 : run_location_(UserScript::DOCUMENT_IDLE), |
| 87 UserScript::File::~File() {} | 105 consumer_instance_type_(UserScript::TAB), |
| 88 | |
| 89 UserScript::UserScript() | |
| 90 : run_location_(DOCUMENT_IDLE), | |
| 91 consumer_instance_type_(TAB), | |
| 92 user_script_id_(-1), | 106 user_script_id_(-1), |
| 93 emulate_greasemonkey_(false), | 107 emulate_greasemonkey_(false), |
| 94 match_all_frames_(false), | 108 match_all_frames_(false), |
| 95 match_about_blank_(false), | 109 match_about_blank_(false), |
| 96 incognito_enabled_(false) {} | 110 incognito_enabled_(false) {} |
| 97 | 111 |
| 98 UserScript::UserScript(const UserScript& other) = default; | 112 UserScriptInfo::UserScriptInfo(const UserScriptInfo& other) = default; |
| 99 | 113 |
| 100 UserScript::~UserScript() { | 114 UserScriptInfo::~UserScriptInfo() {} |
| 101 } | |
| 102 | 115 |
| 103 void UserScript::add_url_pattern(const URLPattern& pattern) { | 116 ScriptMetadata::ScriptMetadata() {} |
| 117 ScriptMetadata::ScriptMetadata(const UserScriptInfo& info) |
| 118 : UserScriptInfo(info) {} |
| 119 ScriptMetadata::~ScriptMetadata() {} |
| 120 |
| 121 void UserScriptInfo::add_url_pattern(const URLPattern& pattern) { |
| 104 url_set_.AddPattern(pattern); | 122 url_set_.AddPattern(pattern); |
| 105 } | 123 } |
| 106 | 124 |
| 107 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) { | 125 void UserScriptInfo::add_exclude_url_pattern(const URLPattern& pattern) { |
| 108 exclude_url_set_.AddPattern(pattern); | 126 exclude_url_set_.AddPattern(pattern); |
| 109 } | 127 } |
| 110 | 128 |
| 111 bool UserScript::MatchesURL(const GURL& url) const { | 129 bool UserScriptInfo::MatchesURL(const GURL& url) const { |
| 112 if (!url_set_.is_empty()) { | 130 if (!url_set_.is_empty()) { |
| 113 if (!url_set_.MatchesURL(url)) | 131 if (!url_set_.MatchesURL(url)) |
| 114 return false; | 132 return false; |
| 115 } | 133 } |
| 116 | 134 |
| 117 if (!exclude_url_set_.is_empty()) { | 135 if (!exclude_url_set_.is_empty()) { |
| 118 if (exclude_url_set_.MatchesURL(url)) | 136 if (exclude_url_set_.MatchesURL(url)) |
| 119 return false; | 137 return false; |
| 120 } | 138 } |
| 121 | 139 |
| 122 if (!globs_.empty()) { | 140 if (!globs_.empty()) { |
| 123 if (!UrlMatchesGlobs(&globs_, url)) | 141 if (!UrlMatchesGlobs(&globs_, url)) |
| 124 return false; | 142 return false; |
| 125 } | 143 } |
| 126 | 144 |
| 127 if (!exclude_globs_.empty()) { | 145 if (!exclude_globs_.empty()) { |
| 128 if (UrlMatchesGlobs(&exclude_globs_, url)) | 146 if (UrlMatchesGlobs(&exclude_globs_, url)) |
| 129 return false; | 147 return false; |
| 130 } | 148 } |
| 131 | 149 |
| 132 return true; | 150 return true; |
| 133 } | 151 } |
| 134 | 152 |
| 135 void UserScript::File::Pickle(base::Pickle* pickle) const { | 153 void UserScriptInfo::Pickle(base::Pickle* pickle) const { |
| 136 pickle->WriteString(url_.spec()); | |
| 137 // Do not write path. It's not needed in the renderer. | |
| 138 // Do not write content. It will be serialized by other means. | |
| 139 } | |
| 140 | |
| 141 void UserScript::File::Unpickle(const base::Pickle& pickle, | |
| 142 base::PickleIterator* iter) { | |
| 143 // Read the url from the pickle. | |
| 144 std::string url; | |
| 145 CHECK(iter->ReadString(&url)); | |
| 146 set_url(GURL(url)); | |
| 147 } | |
| 148 | |
| 149 void UserScript::Pickle(base::Pickle* pickle) const { | |
| 150 // Write the simple types to the pickle. | 154 // Write the simple types to the pickle. |
| 151 pickle->WriteInt(run_location()); | 155 pickle->WriteInt(run_location()); |
| 152 pickle->WriteInt(user_script_id_); | 156 pickle->WriteInt(user_script_id_); |
| 153 pickle->WriteBool(emulate_greasemonkey()); | 157 pickle->WriteBool(emulate_greasemonkey()); |
| 154 pickle->WriteBool(match_all_frames()); | 158 pickle->WriteBool(match_all_frames()); |
| 155 pickle->WriteBool(match_about_blank()); | 159 pickle->WriteBool(match_about_blank()); |
| 156 pickle->WriteBool(is_incognito_enabled()); | 160 pickle->WriteBool(is_incognito_enabled()); |
| 157 | 161 |
| 158 PickleHostID(pickle, host_id_); | 162 PickleHostID(pickle, host_id_); |
| 159 pickle->WriteInt(consumer_instance_type()); | 163 pickle->WriteInt(consumer_instance_type()); |
| 160 PickleGlobs(pickle, globs_); | 164 PickleGlobs(pickle, globs_); |
| 161 PickleGlobs(pickle, exclude_globs_); | 165 PickleGlobs(pickle, exclude_globs_); |
| 162 PickleURLPatternSet(pickle, url_set_); | 166 PickleURLPatternSet(pickle, url_set_); |
| 163 PickleURLPatternSet(pickle, exclude_url_set_); | 167 PickleURLPatternSet(pickle, exclude_url_set_); |
| 164 PickleScripts(pickle, js_scripts_); | |
| 165 PickleScripts(pickle, css_scripts_); | |
| 166 } | 168 } |
| 167 | 169 |
| 168 void UserScript::PickleGlobs(base::Pickle* pickle, | 170 void UserScriptInfo::PickleGlobs(base::Pickle* pickle, |
| 169 const std::vector<std::string>& globs) const { | 171 const std::vector<std::string>& globs) const { |
| 170 pickle->WriteUInt32(globs.size()); | 172 pickle->WriteUInt32(globs.size()); |
| 171 for (std::vector<std::string>::const_iterator glob = globs.begin(); | 173 for (std::vector<std::string>::const_iterator glob = globs.begin(); |
| 172 glob != globs.end(); ++glob) { | 174 glob != globs.end(); ++glob) { |
| 173 pickle->WriteString(*glob); | 175 pickle->WriteString(*glob); |
| 174 } | 176 } |
| 175 } | 177 } |
| 176 | 178 |
| 177 void UserScript::PickleHostID(base::Pickle* pickle, | 179 void UserScriptInfo::PickleHostID(base::Pickle* pickle, |
| 178 const HostID& host_id) const { | 180 const HostID& host_id) const { |
| 179 pickle->WriteInt(host_id.type()); | 181 pickle->WriteInt(host_id.type()); |
| 180 pickle->WriteString(host_id.id()); | 182 pickle->WriteString(host_id.id()); |
| 181 } | 183 } |
| 182 | 184 |
| 183 void UserScript::PickleURLPatternSet(base::Pickle* pickle, | 185 void UserScriptInfo::PickleURLPatternSet( |
| 184 const URLPatternSet& pattern_list) const { | 186 base::Pickle* pickle, |
| 187 const URLPatternSet& pattern_list) const { |
| 185 pickle->WriteUInt32(pattern_list.patterns().size()); | 188 pickle->WriteUInt32(pattern_list.patterns().size()); |
| 186 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); | 189 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); |
| 187 pattern != pattern_list.end(); ++pattern) { | 190 pattern != pattern_list.end(); ++pattern) { |
| 188 pickle->WriteInt(pattern->valid_schemes()); | 191 pickle->WriteInt(pattern->valid_schemes()); |
| 189 pickle->WriteString(pattern->GetAsString()); | 192 pickle->WriteString(pattern->GetAsString()); |
| 190 } | 193 } |
| 191 } | 194 } |
| 192 | 195 |
| 193 void UserScript::PickleScripts(base::Pickle* pickle, | 196 void UserScriptInfo::Unpickle(const base::Pickle& pickle, |
| 194 const FileList& scripts) const { | 197 base::PickleIterator* iter) { |
| 195 pickle->WriteUInt32(scripts.size()); | |
| 196 for (FileList::const_iterator file = scripts.begin(); | |
| 197 file != scripts.end(); ++file) { | |
| 198 file->Pickle(pickle); | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 void UserScript::Unpickle(const base::Pickle& pickle, | |
| 203 base::PickleIterator* iter) { | |
| 204 // Read the run location. | 198 // Read the run location. |
| 205 int run_location = 0; | 199 int run_location = 0; |
| 206 CHECK(iter->ReadInt(&run_location)); | 200 CHECK(iter->ReadInt(&run_location)); |
| 207 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); | 201 CHECK(run_location >= 0 && run_location < UserScript::RUN_LOCATION_LAST); |
| 208 run_location_ = static_cast<RunLocation>(run_location); | 202 run_location_ = static_cast<UserScript::RunLocation>(run_location); |
| 209 | 203 |
| 210 CHECK(iter->ReadInt(&user_script_id_)); | 204 CHECK(iter->ReadInt(&user_script_id_)); |
| 211 CHECK(iter->ReadBool(&emulate_greasemonkey_)); | 205 CHECK(iter->ReadBool(&emulate_greasemonkey_)); |
| 212 CHECK(iter->ReadBool(&match_all_frames_)); | 206 CHECK(iter->ReadBool(&match_all_frames_)); |
| 213 CHECK(iter->ReadBool(&match_about_blank_)); | 207 CHECK(iter->ReadBool(&match_about_blank_)); |
| 214 CHECK(iter->ReadBool(&incognito_enabled_)); | 208 CHECK(iter->ReadBool(&incognito_enabled_)); |
| 215 | 209 |
| 216 UnpickleHostID(pickle, iter, &host_id_); | 210 UnpickleHostID(pickle, iter, &host_id_); |
| 217 | 211 |
| 218 int consumer_instance_type = 0; | 212 int consumer_instance_type = 0; |
| 219 CHECK(iter->ReadInt(&consumer_instance_type)); | 213 CHECK(iter->ReadInt(&consumer_instance_type)); |
| 220 consumer_instance_type_ = | 214 consumer_instance_type_ = |
| 221 static_cast<ConsumerInstanceType>(consumer_instance_type); | 215 static_cast<UserScript::ConsumerInstanceType>(consumer_instance_type); |
| 222 | 216 |
| 223 UnpickleGlobs(pickle, iter, &globs_); | 217 UnpickleGlobs(pickle, iter, &globs_); |
| 224 UnpickleGlobs(pickle, iter, &exclude_globs_); | 218 UnpickleGlobs(pickle, iter, &exclude_globs_); |
| 225 UnpickleURLPatternSet(pickle, iter, &url_set_); | 219 UnpickleURLPatternSet(pickle, iter, &url_set_); |
| 226 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); | 220 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); |
| 227 UnpickleScripts(pickle, iter, &js_scripts_); | |
| 228 UnpickleScripts(pickle, iter, &css_scripts_); | |
| 229 } | 221 } |
| 230 | 222 |
| 231 void UserScript::UnpickleGlobs(const base::Pickle& pickle, | 223 void UserScriptInfo::UnpickleGlobs(const base::Pickle& pickle, |
| 232 base::PickleIterator* iter, | 224 base::PickleIterator* iter, |
| 233 std::vector<std::string>* globs) { | 225 std::vector<std::string>* globs) { |
| 234 uint32_t num_globs = 0; | 226 uint32_t num_globs = 0; |
| 235 CHECK(iter->ReadUInt32(&num_globs)); | 227 CHECK(iter->ReadUInt32(&num_globs)); |
| 236 globs->clear(); | 228 globs->clear(); |
| 237 for (uint32_t i = 0; i < num_globs; ++i) { | 229 for (uint32_t i = 0; i < num_globs; ++i) { |
| 238 std::string glob; | 230 std::string glob; |
| 239 CHECK(iter->ReadString(&glob)); | 231 CHECK(iter->ReadString(&glob)); |
| 240 globs->push_back(glob); | 232 globs->push_back(glob); |
| 241 } | 233 } |
| 242 } | 234 } |
| 243 | 235 |
| 244 void UserScript::UnpickleHostID(const base::Pickle& pickle, | 236 void UserScriptInfo::UnpickleHostID(const base::Pickle& pickle, |
| 245 base::PickleIterator* iter, | 237 base::PickleIterator* iter, |
| 246 HostID* host_id) { | 238 HostID* host_id) { |
| 247 int type = 0; | 239 int type = 0; |
| 248 std::string id; | 240 std::string id; |
| 249 CHECK(iter->ReadInt(&type)); | 241 CHECK(iter->ReadInt(&type)); |
| 250 CHECK(iter->ReadString(&id)); | 242 CHECK(iter->ReadString(&id)); |
| 251 *host_id = HostID(static_cast<HostID::HostType>(type), id); | 243 *host_id = HostID(static_cast<HostID::HostType>(type), id); |
| 252 } | 244 } |
| 253 | 245 |
| 254 void UserScript::UnpickleURLPatternSet(const base::Pickle& pickle, | 246 void UserScriptInfo::UnpickleURLPatternSet(const base::Pickle& pickle, |
| 255 base::PickleIterator* iter, | 247 base::PickleIterator* iter, |
| 256 URLPatternSet* pattern_list) { | 248 URLPatternSet* pattern_list) { |
| 257 uint32_t num_patterns = 0; | 249 uint32_t num_patterns = 0; |
| 258 CHECK(iter->ReadUInt32(&num_patterns)); | 250 CHECK(iter->ReadUInt32(&num_patterns)); |
| 259 | 251 |
| 260 pattern_list->ClearPatterns(); | 252 pattern_list->ClearPatterns(); |
| 261 for (uint32_t i = 0; i < num_patterns; ++i) { | 253 for (uint32_t i = 0; i < num_patterns; ++i) { |
| 262 int valid_schemes; | 254 int valid_schemes; |
| 263 CHECK(iter->ReadInt(&valid_schemes)); | 255 CHECK(iter->ReadInt(&valid_schemes)); |
| 264 | 256 |
| 265 std::string pattern_str; | 257 std::string pattern_str; |
| 266 CHECK(iter->ReadString(&pattern_str)); | 258 CHECK(iter->ReadString(&pattern_str)); |
| 267 | 259 |
| 268 URLPattern pattern(kValidUserScriptSchemes); | 260 URLPattern pattern(kValidUserScriptSchemes); |
| 269 URLPattern::ParseResult result = pattern.Parse(pattern_str); | 261 URLPattern::ParseResult result = pattern.Parse(pattern_str); |
| 270 CHECK(URLPattern::PARSE_SUCCESS == result) << | 262 CHECK(URLPattern::PARSE_SUCCESS == result) << |
| 271 URLPattern::GetParseResultString(result) << " " << pattern_str.c_str(); | 263 URLPattern::GetParseResultString(result) << " " << pattern_str.c_str(); |
| 272 | 264 |
| 273 pattern.SetValidSchemes(valid_schemes); | 265 pattern.SetValidSchemes(valid_schemes); |
| 274 pattern_list->AddPattern(pattern); | 266 pattern_list->AddPattern(pattern); |
| 275 } | 267 } |
| 276 } | 268 } |
| 277 | 269 |
| 278 void UserScript::UnpickleScripts(const base::Pickle& pickle, | |
| 279 base::PickleIterator* iter, | |
| 280 FileList* scripts) { | |
| 281 uint32_t num_files = 0; | |
| 282 CHECK(iter->ReadUInt32(&num_files)); | |
| 283 scripts->clear(); | |
| 284 for (uint32_t i = 0; i < num_files; ++i) { | |
| 285 File file; | |
| 286 file.Unpickle(pickle, iter); | |
| 287 scripts->push_back(file); | |
| 288 } | |
| 289 } | |
| 290 | |
| 291 UserScriptIDPair::UserScriptIDPair(int id, const HostID& host_id) | 270 UserScriptIDPair::UserScriptIDPair(int id, const HostID& host_id) |
| 292 : id(id), host_id(host_id) {} | 271 : id(id), host_id(host_id) {} |
| 293 | 272 |
| 294 UserScriptIDPair::UserScriptIDPair(int id) : id(id), host_id(HostID()) {} | 273 UserScriptIDPair::UserScriptIDPair(int id) : id(id), host_id(HostID()) {} |
| 295 | 274 |
| 296 bool operator<(const UserScriptIDPair& a, const UserScriptIDPair& b) { | 275 bool operator<(const UserScriptIDPair& a, const UserScriptIDPair& b) { |
| 297 return a.id < b.id; | 276 return a.id < b.id; |
| 298 } | 277 } |
| 299 | 278 |
| 300 bool operator<(const UserScript& script1, const UserScript& script2) { | |
| 301 // The only kind of script that should be compared is the kind that has its | |
| 302 // IDs initialized to a meaningful value. | |
| 303 DCHECK(script1.id() != -1 && script2.id() != -1); | |
| 304 return script1.id() < script2.id(); | |
| 305 } | |
| 306 | |
| 307 } // namespace extensions | 279 } // namespace extensions |
| OLD | NEW |