| 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/atomic_sequence_num.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 } | 116 } |
| 117 | 117 |
| 118 if (!exclude_globs_.empty()) { | 118 if (!exclude_globs_.empty()) { |
| 119 if (UrlMatchesGlobs(&exclude_globs_, url)) | 119 if (UrlMatchesGlobs(&exclude_globs_, url)) |
| 120 return false; | 120 return false; |
| 121 } | 121 } |
| 122 | 122 |
| 123 return true; | 123 return true; |
| 124 } | 124 } |
| 125 | 125 |
| 126 void UserScript::File::Pickle(::Pickle* pickle) const { | 126 void UserScript::File::Pickle(base::Pickle* pickle) const { |
| 127 pickle->WriteString(url_.spec()); | 127 pickle->WriteString(url_.spec()); |
| 128 // Do not write path. It's not needed in the renderer. | 128 // Do not write path. It's not needed in the renderer. |
| 129 // Do not write content. It will be serialized by other means. | 129 // Do not write content. It will be serialized by other means. |
| 130 } | 130 } |
| 131 | 131 |
| 132 void UserScript::File::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { | 132 void UserScript::File::Unpickle(const base::Pickle& pickle, |
| 133 base::PickleIterator* iter) { |
| 133 // Read the url from the pickle. | 134 // Read the url from the pickle. |
| 134 std::string url; | 135 std::string url; |
| 135 CHECK(iter->ReadString(&url)); | 136 CHECK(iter->ReadString(&url)); |
| 136 set_url(GURL(url)); | 137 set_url(GURL(url)); |
| 137 } | 138 } |
| 138 | 139 |
| 139 void UserScript::Pickle(::Pickle* pickle) const { | 140 void UserScript::Pickle(base::Pickle* pickle) const { |
| 140 // Write the simple types to the pickle. | 141 // Write the simple types to the pickle. |
| 141 pickle->WriteInt(run_location()); | 142 pickle->WriteInt(run_location()); |
| 142 pickle->WriteInt(user_script_id_); | 143 pickle->WriteInt(user_script_id_); |
| 143 pickle->WriteBool(emulate_greasemonkey()); | 144 pickle->WriteBool(emulate_greasemonkey()); |
| 144 pickle->WriteBool(match_all_frames()); | 145 pickle->WriteBool(match_all_frames()); |
| 145 pickle->WriteBool(match_about_blank()); | 146 pickle->WriteBool(match_about_blank()); |
| 146 pickle->WriteBool(is_incognito_enabled()); | 147 pickle->WriteBool(is_incognito_enabled()); |
| 147 | 148 |
| 148 PickleHostID(pickle, host_id_); | 149 PickleHostID(pickle, host_id_); |
| 149 pickle->WriteInt(consumer_instance_type()); | 150 pickle->WriteInt(consumer_instance_type()); |
| 150 PickleGlobs(pickle, globs_); | 151 PickleGlobs(pickle, globs_); |
| 151 PickleGlobs(pickle, exclude_globs_); | 152 PickleGlobs(pickle, exclude_globs_); |
| 152 PickleURLPatternSet(pickle, url_set_); | 153 PickleURLPatternSet(pickle, url_set_); |
| 153 PickleURLPatternSet(pickle, exclude_url_set_); | 154 PickleURLPatternSet(pickle, exclude_url_set_); |
| 154 PickleScripts(pickle, js_scripts_); | 155 PickleScripts(pickle, js_scripts_); |
| 155 PickleScripts(pickle, css_scripts_); | 156 PickleScripts(pickle, css_scripts_); |
| 156 } | 157 } |
| 157 | 158 |
| 158 void UserScript::PickleGlobs(::Pickle* pickle, | 159 void UserScript::PickleGlobs(base::Pickle* pickle, |
| 159 const std::vector<std::string>& globs) const { | 160 const std::vector<std::string>& globs) const { |
| 160 pickle->WriteSizeT(globs.size()); | 161 pickle->WriteSizeT(globs.size()); |
| 161 for (std::vector<std::string>::const_iterator glob = globs.begin(); | 162 for (std::vector<std::string>::const_iterator glob = globs.begin(); |
| 162 glob != globs.end(); ++glob) { | 163 glob != globs.end(); ++glob) { |
| 163 pickle->WriteString(*glob); | 164 pickle->WriteString(*glob); |
| 164 } | 165 } |
| 165 } | 166 } |
| 166 | 167 |
| 167 void UserScript::PickleHostID(::Pickle* pickle, const HostID& host_id) const { | 168 void UserScript::PickleHostID(base::Pickle* pickle, |
| 169 const HostID& host_id) const { |
| 168 pickle->WriteInt(host_id.type()); | 170 pickle->WriteInt(host_id.type()); |
| 169 pickle->WriteString(host_id.id()); | 171 pickle->WriteString(host_id.id()); |
| 170 } | 172 } |
| 171 | 173 |
| 172 void UserScript::PickleURLPatternSet(::Pickle* pickle, | 174 void UserScript::PickleURLPatternSet(base::Pickle* pickle, |
| 173 const URLPatternSet& pattern_list) const { | 175 const URLPatternSet& pattern_list) const { |
| 174 pickle->WriteSizeT(pattern_list.patterns().size()); | 176 pickle->WriteSizeT(pattern_list.patterns().size()); |
| 175 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); | 177 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); |
| 176 pattern != pattern_list.end(); ++pattern) { | 178 pattern != pattern_list.end(); ++pattern) { |
| 177 pickle->WriteInt(pattern->valid_schemes()); | 179 pickle->WriteInt(pattern->valid_schemes()); |
| 178 pickle->WriteString(pattern->GetAsString()); | 180 pickle->WriteString(pattern->GetAsString()); |
| 179 } | 181 } |
| 180 } | 182 } |
| 181 | 183 |
| 182 void UserScript::PickleScripts(::Pickle* pickle, | 184 void UserScript::PickleScripts(base::Pickle* pickle, |
| 183 const FileList& scripts) const { | 185 const FileList& scripts) const { |
| 184 pickle->WriteSizeT(scripts.size()); | 186 pickle->WriteSizeT(scripts.size()); |
| 185 for (FileList::const_iterator file = scripts.begin(); | 187 for (FileList::const_iterator file = scripts.begin(); |
| 186 file != scripts.end(); ++file) { | 188 file != scripts.end(); ++file) { |
| 187 file->Pickle(pickle); | 189 file->Pickle(pickle); |
| 188 } | 190 } |
| 189 } | 191 } |
| 190 | 192 |
| 191 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { | 193 void UserScript::Unpickle(const base::Pickle& pickle, |
| 194 base::PickleIterator* iter) { |
| 192 // Read the run location. | 195 // Read the run location. |
| 193 int run_location = 0; | 196 int run_location = 0; |
| 194 CHECK(iter->ReadInt(&run_location)); | 197 CHECK(iter->ReadInt(&run_location)); |
| 195 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); | 198 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); |
| 196 run_location_ = static_cast<RunLocation>(run_location); | 199 run_location_ = static_cast<RunLocation>(run_location); |
| 197 | 200 |
| 198 CHECK(iter->ReadInt(&user_script_id_)); | 201 CHECK(iter->ReadInt(&user_script_id_)); |
| 199 CHECK(iter->ReadBool(&emulate_greasemonkey_)); | 202 CHECK(iter->ReadBool(&emulate_greasemonkey_)); |
| 200 CHECK(iter->ReadBool(&match_all_frames_)); | 203 CHECK(iter->ReadBool(&match_all_frames_)); |
| 201 CHECK(iter->ReadBool(&match_about_blank_)); | 204 CHECK(iter->ReadBool(&match_about_blank_)); |
| 202 CHECK(iter->ReadBool(&incognito_enabled_)); | 205 CHECK(iter->ReadBool(&incognito_enabled_)); |
| 203 | 206 |
| 204 UnpickleHostID(pickle, iter, &host_id_); | 207 UnpickleHostID(pickle, iter, &host_id_); |
| 205 | 208 |
| 206 int consumer_instance_type = 0; | 209 int consumer_instance_type = 0; |
| 207 CHECK(iter->ReadInt(&consumer_instance_type)); | 210 CHECK(iter->ReadInt(&consumer_instance_type)); |
| 208 consumer_instance_type_ = | 211 consumer_instance_type_ = |
| 209 static_cast<ConsumerInstanceType>(consumer_instance_type); | 212 static_cast<ConsumerInstanceType>(consumer_instance_type); |
| 210 | 213 |
| 211 UnpickleGlobs(pickle, iter, &globs_); | 214 UnpickleGlobs(pickle, iter, &globs_); |
| 212 UnpickleGlobs(pickle, iter, &exclude_globs_); | 215 UnpickleGlobs(pickle, iter, &exclude_globs_); |
| 213 UnpickleURLPatternSet(pickle, iter, &url_set_); | 216 UnpickleURLPatternSet(pickle, iter, &url_set_); |
| 214 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); | 217 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); |
| 215 UnpickleScripts(pickle, iter, &js_scripts_); | 218 UnpickleScripts(pickle, iter, &js_scripts_); |
| 216 UnpickleScripts(pickle, iter, &css_scripts_); | 219 UnpickleScripts(pickle, iter, &css_scripts_); |
| 217 } | 220 } |
| 218 | 221 |
| 219 void UserScript::UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter, | 222 void UserScript::UnpickleGlobs(const base::Pickle& pickle, |
| 223 base::PickleIterator* iter, |
| 220 std::vector<std::string>* globs) { | 224 std::vector<std::string>* globs) { |
| 221 size_t num_globs = 0; | 225 size_t num_globs = 0; |
| 222 CHECK(iter->ReadSizeT(&num_globs)); | 226 CHECK(iter->ReadSizeT(&num_globs)); |
| 223 globs->clear(); | 227 globs->clear(); |
| 224 for (size_t i = 0; i < num_globs; ++i) { | 228 for (size_t i = 0; i < num_globs; ++i) { |
| 225 std::string glob; | 229 std::string glob; |
| 226 CHECK(iter->ReadString(&glob)); | 230 CHECK(iter->ReadString(&glob)); |
| 227 globs->push_back(glob); | 231 globs->push_back(glob); |
| 228 } | 232 } |
| 229 } | 233 } |
| 230 | 234 |
| 231 void UserScript::UnpickleHostID(const ::Pickle& pickle, | 235 void UserScript::UnpickleHostID(const base::Pickle& pickle, |
| 232 PickleIterator* iter, | 236 base::PickleIterator* iter, |
| 233 HostID* host_id) { | 237 HostID* host_id) { |
| 234 int type = 0; | 238 int type = 0; |
| 235 std::string id; | 239 std::string id; |
| 236 CHECK(iter->ReadInt(&type)); | 240 CHECK(iter->ReadInt(&type)); |
| 237 CHECK(iter->ReadString(&id)); | 241 CHECK(iter->ReadString(&id)); |
| 238 *host_id = HostID(static_cast<HostID::HostType>(type), id); | 242 *host_id = HostID(static_cast<HostID::HostType>(type), id); |
| 239 } | 243 } |
| 240 | 244 |
| 241 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, | 245 void UserScript::UnpickleURLPatternSet(const base::Pickle& pickle, |
| 242 PickleIterator* iter, | 246 base::PickleIterator* iter, |
| 243 URLPatternSet* pattern_list) { | 247 URLPatternSet* pattern_list) { |
| 244 size_t num_patterns = 0; | 248 size_t num_patterns = 0; |
| 245 CHECK(iter->ReadSizeT(&num_patterns)); | 249 CHECK(iter->ReadSizeT(&num_patterns)); |
| 246 | 250 |
| 247 pattern_list->ClearPatterns(); | 251 pattern_list->ClearPatterns(); |
| 248 for (size_t i = 0; i < num_patterns; ++i) { | 252 for (size_t i = 0; i < num_patterns; ++i) { |
| 249 int valid_schemes; | 253 int valid_schemes; |
| 250 CHECK(iter->ReadInt(&valid_schemes)); | 254 CHECK(iter->ReadInt(&valid_schemes)); |
| 251 | 255 |
| 252 std::string pattern_str; | 256 std::string pattern_str; |
| 253 CHECK(iter->ReadString(&pattern_str)); | 257 CHECK(iter->ReadString(&pattern_str)); |
| 254 | 258 |
| 255 URLPattern pattern(kValidUserScriptSchemes); | 259 URLPattern pattern(kValidUserScriptSchemes); |
| 256 URLPattern::ParseResult result = pattern.Parse(pattern_str); | 260 URLPattern::ParseResult result = pattern.Parse(pattern_str); |
| 257 CHECK(URLPattern::PARSE_SUCCESS == result) << | 261 CHECK(URLPattern::PARSE_SUCCESS == result) << |
| 258 URLPattern::GetParseResultString(result) << " " << pattern_str.c_str(); | 262 URLPattern::GetParseResultString(result) << " " << pattern_str.c_str(); |
| 259 | 263 |
| 260 pattern.SetValidSchemes(valid_schemes); | 264 pattern.SetValidSchemes(valid_schemes); |
| 261 pattern_list->AddPattern(pattern); | 265 pattern_list->AddPattern(pattern); |
| 262 } | 266 } |
| 263 } | 267 } |
| 264 | 268 |
| 265 void UserScript::UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter, | 269 void UserScript::UnpickleScripts(const base::Pickle& pickle, |
| 270 base::PickleIterator* iter, |
| 266 FileList* scripts) { | 271 FileList* scripts) { |
| 267 size_t num_files = 0; | 272 size_t num_files = 0; |
| 268 CHECK(iter->ReadSizeT(&num_files)); | 273 CHECK(iter->ReadSizeT(&num_files)); |
| 269 scripts->clear(); | 274 scripts->clear(); |
| 270 for (size_t i = 0; i < num_files; ++i) { | 275 for (size_t i = 0; i < num_files; ++i) { |
| 271 File file; | 276 File file; |
| 272 file.Unpickle(pickle, iter); | 277 file.Unpickle(pickle, iter); |
| 273 scripts->push_back(file); | 278 scripts->push_back(file); |
| 274 } | 279 } |
| 275 } | 280 } |
| 276 | 281 |
| 277 bool operator<(const UserScript& script1, const UserScript& script2) { | 282 bool operator<(const UserScript& script1, const UserScript& script2) { |
| 278 // The only kind of script that should be compared is the kind that has its | 283 // The only kind of script that should be compared is the kind that has its |
| 279 // IDs initialized to a meaningful value. | 284 // IDs initialized to a meaningful value. |
| 280 DCHECK(script1.id() != -1 && script2.id() != -1); | 285 DCHECK(script1.id() != -1 && script2.id() != -1); |
| 281 return script1.id() < script2.id(); | 286 return script1.id() < script2.id(); |
| 282 } | 287 } |
| 283 | 288 |
| 284 } // namespace extensions | 289 } // namespace extensions |
| OLD | NEW |