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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 | 124 |
125 void UserScript::File::Pickle(::Pickle* pickle) const { | 125 void UserScript::File::Pickle(::Pickle* pickle) const { |
126 pickle->WriteString(url_.spec()); | 126 pickle->WriteString(url_.spec()); |
127 // Do not write path. It's not needed in the renderer. | 127 // Do not write path. It's not needed in the renderer. |
128 // Do not write content. It will be serialized by other means. | 128 // Do not write content. It will be serialized by other means. |
129 } | 129 } |
130 | 130 |
131 void UserScript::File::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { | 131 void UserScript::File::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { |
132 // Read the url from the pickle. | 132 // Read the url from the pickle. |
133 std::string url; | 133 std::string url; |
134 CHECK(pickle.ReadString(iter, &url)); | 134 CHECK(iter->ReadString(&url)); |
135 set_url(GURL(url)); | 135 set_url(GURL(url)); |
136 } | 136 } |
137 | 137 |
138 void UserScript::Pickle(::Pickle* pickle) const { | 138 void UserScript::Pickle(::Pickle* pickle) const { |
139 // Write the simple types to the pickle. | 139 // Write the simple types to the pickle. |
140 pickle->WriteInt(run_location()); | 140 pickle->WriteInt(run_location()); |
141 pickle->WriteString(extension_id()); | 141 pickle->WriteString(extension_id()); |
142 pickle->WriteInt(user_script_id_); | 142 pickle->WriteInt(user_script_id_); |
143 pickle->WriteBool(emulate_greasemonkey()); | 143 pickle->WriteBool(emulate_greasemonkey()); |
144 pickle->WriteBool(match_all_frames()); | 144 pickle->WriteBool(match_all_frames()); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 pickle->WriteSizeT(scripts.size()); | 177 pickle->WriteSizeT(scripts.size()); |
178 for (FileList::const_iterator file = scripts.begin(); | 178 for (FileList::const_iterator file = scripts.begin(); |
179 file != scripts.end(); ++file) { | 179 file != scripts.end(); ++file) { |
180 file->Pickle(pickle); | 180 file->Pickle(pickle); |
181 } | 181 } |
182 } | 182 } |
183 | 183 |
184 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { | 184 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { |
185 // Read the run location. | 185 // Read the run location. |
186 int run_location = 0; | 186 int run_location = 0; |
187 CHECK(pickle.ReadInt(iter, &run_location)); | 187 CHECK(iter->ReadInt(&run_location)); |
188 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); | 188 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); |
189 run_location_ = static_cast<RunLocation>(run_location); | 189 run_location_ = static_cast<RunLocation>(run_location); |
190 | 190 |
191 CHECK(pickle.ReadString(iter, &extension_id_)); | 191 CHECK(iter->ReadString(&extension_id_)); |
192 CHECK(pickle.ReadInt(iter, &user_script_id_)); | 192 CHECK(iter->ReadInt(&user_script_id_)); |
193 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_)); | 193 CHECK(iter->ReadBool(&emulate_greasemonkey_)); |
194 CHECK(pickle.ReadBool(iter, &match_all_frames_)); | 194 CHECK(iter->ReadBool(&match_all_frames_)); |
195 CHECK(pickle.ReadBool(iter, &match_about_blank_)); | 195 CHECK(iter->ReadBool(&match_about_blank_)); |
196 CHECK(pickle.ReadBool(iter, &incognito_enabled_)); | 196 CHECK(iter->ReadBool(&incognito_enabled_)); |
197 | 197 |
198 UnpickleGlobs(pickle, iter, &globs_); | 198 UnpickleGlobs(pickle, iter, &globs_); |
199 UnpickleGlobs(pickle, iter, &exclude_globs_); | 199 UnpickleGlobs(pickle, iter, &exclude_globs_); |
200 UnpickleURLPatternSet(pickle, iter, &url_set_); | 200 UnpickleURLPatternSet(pickle, iter, &url_set_); |
201 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); | 201 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); |
202 UnpickleScripts(pickle, iter, &js_scripts_); | 202 UnpickleScripts(pickle, iter, &js_scripts_); |
203 UnpickleScripts(pickle, iter, &css_scripts_); | 203 UnpickleScripts(pickle, iter, &css_scripts_); |
204 } | 204 } |
205 | 205 |
206 void UserScript::UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter, | 206 void UserScript::UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter, |
207 std::vector<std::string>* globs) { | 207 std::vector<std::string>* globs) { |
208 size_t num_globs = 0; | 208 size_t num_globs = 0; |
209 CHECK(pickle.ReadSizeT(iter, &num_globs)); | 209 CHECK(iter->ReadSizeT(&num_globs)); |
210 globs->clear(); | 210 globs->clear(); |
211 for (size_t i = 0; i < num_globs; ++i) { | 211 for (size_t i = 0; i < num_globs; ++i) { |
212 std::string glob; | 212 std::string glob; |
213 CHECK(pickle.ReadString(iter, &glob)); | 213 CHECK(iter->ReadString(&glob)); |
214 globs->push_back(glob); | 214 globs->push_back(glob); |
215 } | 215 } |
216 } | 216 } |
217 | 217 |
218 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, | 218 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, |
219 PickleIterator* iter, | 219 PickleIterator* iter, |
220 URLPatternSet* pattern_list) { | 220 URLPatternSet* pattern_list) { |
221 size_t num_patterns = 0; | 221 size_t num_patterns = 0; |
222 CHECK(pickle.ReadSizeT(iter, &num_patterns)); | 222 CHECK(iter->ReadSizeT(&num_patterns)); |
223 | 223 |
224 pattern_list->ClearPatterns(); | 224 pattern_list->ClearPatterns(); |
225 for (size_t i = 0; i < num_patterns; ++i) { | 225 for (size_t i = 0; i < num_patterns; ++i) { |
226 int valid_schemes; | 226 int valid_schemes; |
227 CHECK(pickle.ReadInt(iter, &valid_schemes)); | 227 CHECK(iter->ReadInt(&valid_schemes)); |
228 | 228 |
229 std::string pattern_str; | 229 std::string pattern_str; |
230 CHECK(pickle.ReadString(iter, &pattern_str)); | 230 CHECK(iter->ReadString(&pattern_str)); |
231 | 231 |
232 URLPattern pattern(kValidUserScriptSchemes); | 232 URLPattern pattern(kValidUserScriptSchemes); |
233 URLPattern::ParseResult result = pattern.Parse(pattern_str); | 233 URLPattern::ParseResult result = pattern.Parse(pattern_str); |
234 CHECK(URLPattern::PARSE_SUCCESS == result) << | 234 CHECK(URLPattern::PARSE_SUCCESS == result) << |
235 URLPattern::GetParseResultString(result) << " " << pattern_str.c_str(); | 235 URLPattern::GetParseResultString(result) << " " << pattern_str.c_str(); |
236 | 236 |
237 pattern.SetValidSchemes(valid_schemes); | 237 pattern.SetValidSchemes(valid_schemes); |
238 pattern_list->AddPattern(pattern); | 238 pattern_list->AddPattern(pattern); |
239 } | 239 } |
240 } | 240 } |
241 | 241 |
242 void UserScript::UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter, | 242 void UserScript::UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter, |
243 FileList* scripts) { | 243 FileList* scripts) { |
244 size_t num_files = 0; | 244 size_t num_files = 0; |
245 CHECK(pickle.ReadSizeT(iter, &num_files)); | 245 CHECK(iter->ReadSizeT(&num_files)); |
246 scripts->clear(); | 246 scripts->clear(); |
247 for (size_t i = 0; i < num_files; ++i) { | 247 for (size_t i = 0; i < num_files; ++i) { |
248 File file; | 248 File file; |
249 file.Unpickle(pickle, iter); | 249 file.Unpickle(pickle, iter); |
250 scripts->push_back(file); | 250 scripts->push_back(file); |
251 } | 251 } |
252 } | 252 } |
253 | 253 |
254 bool operator<(const UserScript& script1, const UserScript& script2) { | 254 bool operator<(const UserScript& script1, const UserScript& script2) { |
255 // 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 |
256 // IDs initialized to a meaningful value. | 256 // IDs initialized to a meaningful value. |
257 DCHECK(script1.id() != -1 && script2.id() != -1); | 257 DCHECK(script1.id() != -1 && script2.id() != -1); |
258 return script1.id() < script2.id(); | 258 return script1.id() < script2.id(); |
259 } | 259 } |
260 | 260 |
261 } // namespace extensions | 261 } // namespace extensions |
OLD | NEW |