OLD | NEW |
1 // Copyright (c) 2011 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/pickle.h" | 7 #include "base/pickle.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 | 9 |
10 namespace { | 10 namespace { |
11 | 11 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 | 82 |
83 return true; | 83 return true; |
84 } | 84 } |
85 | 85 |
86 void UserScript::File::Pickle(::Pickle* pickle) const { | 86 void UserScript::File::Pickle(::Pickle* pickle) const { |
87 pickle->WriteString(url_.spec()); | 87 pickle->WriteString(url_.spec()); |
88 // Do not write path. It's not needed in the renderer. | 88 // Do not write path. It's not needed in the renderer. |
89 // Do not write content. It will be serialized by other means. | 89 // Do not write content. It will be serialized by other means. |
90 } | 90 } |
91 | 91 |
92 void UserScript::File::Unpickle(const ::Pickle& pickle, void** iter) { | 92 void UserScript::File::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { |
93 // Read the url from the pickle. | 93 // Read the url from the pickle. |
94 std::string url; | 94 std::string url; |
95 CHECK(pickle.ReadString(iter, &url)); | 95 CHECK(pickle.ReadString(iter, &url)); |
96 set_url(GURL(url)); | 96 set_url(GURL(url)); |
97 } | 97 } |
98 | 98 |
99 void UserScript::Pickle(::Pickle* pickle) const { | 99 void UserScript::Pickle(::Pickle* pickle) const { |
100 // Write the simple types to the pickle. | 100 // Write the simple types to the pickle. |
101 pickle->WriteInt(run_location()); | 101 pickle->WriteInt(run_location()); |
102 pickle->WriteString(extension_id()); | 102 pickle->WriteString(extension_id()); |
(...skipping 30 matching lines...) Expand all Loading... |
133 | 133 |
134 void UserScript::PickleScripts(::Pickle* pickle, | 134 void UserScript::PickleScripts(::Pickle* pickle, |
135 const FileList& scripts) const { | 135 const FileList& scripts) const { |
136 pickle->WriteSize(scripts.size()); | 136 pickle->WriteSize(scripts.size()); |
137 for (FileList::const_iterator file = scripts.begin(); | 137 for (FileList::const_iterator file = scripts.begin(); |
138 file != scripts.end(); ++file) { | 138 file != scripts.end(); ++file) { |
139 file->Pickle(pickle); | 139 file->Pickle(pickle); |
140 } | 140 } |
141 } | 141 } |
142 | 142 |
143 void UserScript::Unpickle(const ::Pickle& pickle, void** iter) { | 143 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { |
144 // Read the run location. | 144 // Read the run location. |
145 int run_location = 0; | 145 int run_location = 0; |
146 CHECK(pickle.ReadInt(iter, &run_location)); | 146 CHECK(pickle.ReadInt(iter, &run_location)); |
147 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); | 147 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); |
148 run_location_ = static_cast<RunLocation>(run_location); | 148 run_location_ = static_cast<RunLocation>(run_location); |
149 | 149 |
150 CHECK(pickle.ReadString(iter, &extension_id_)); | 150 CHECK(pickle.ReadString(iter, &extension_id_)); |
151 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_)); | 151 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_)); |
152 CHECK(pickle.ReadBool(iter, &match_all_frames_)); | 152 CHECK(pickle.ReadBool(iter, &match_all_frames_)); |
153 CHECK(pickle.ReadBool(iter, &incognito_enabled_)); | 153 CHECK(pickle.ReadBool(iter, &incognito_enabled_)); |
154 | 154 |
155 UnpickleGlobs(pickle, iter, &globs_); | 155 UnpickleGlobs(pickle, iter, &globs_); |
156 UnpickleGlobs(pickle, iter, &exclude_globs_); | 156 UnpickleGlobs(pickle, iter, &exclude_globs_); |
157 UnpickleURLPatternSet(pickle, iter, &url_set_); | 157 UnpickleURLPatternSet(pickle, iter, &url_set_); |
158 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); | 158 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); |
159 UnpickleScripts(pickle, iter, &js_scripts_); | 159 UnpickleScripts(pickle, iter, &js_scripts_); |
160 UnpickleScripts(pickle, iter, &css_scripts_); | 160 UnpickleScripts(pickle, iter, &css_scripts_); |
161 } | 161 } |
162 | 162 |
163 void UserScript::UnpickleGlobs(const ::Pickle& pickle, void** iter, | 163 void UserScript::UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter, |
164 std::vector<std::string>* globs) { | 164 std::vector<std::string>* globs) { |
165 size_t num_globs = 0; | 165 size_t num_globs = 0; |
166 CHECK(pickle.ReadSize(iter, &num_globs)); | 166 CHECK(pickle.ReadSize(iter, &num_globs)); |
167 globs->clear(); | 167 globs->clear(); |
168 for (size_t i = 0; i < num_globs; ++i) { | 168 for (size_t i = 0; i < num_globs; ++i) { |
169 std::string glob; | 169 std::string glob; |
170 CHECK(pickle.ReadString(iter, &glob)); | 170 CHECK(pickle.ReadString(iter, &glob)); |
171 globs->push_back(glob); | 171 globs->push_back(glob); |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, void** iter, | 175 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, |
| 176 PickleIterator* iter, |
176 URLPatternSet* pattern_list) { | 177 URLPatternSet* pattern_list) { |
177 size_t num_patterns = 0; | 178 size_t num_patterns = 0; |
178 CHECK(pickle.ReadSize(iter, &num_patterns)); | 179 CHECK(pickle.ReadSize(iter, &num_patterns)); |
179 | 180 |
180 pattern_list->ClearPatterns(); | 181 pattern_list->ClearPatterns(); |
181 for (size_t i = 0; i < num_patterns; ++i) { | 182 for (size_t i = 0; i < num_patterns; ++i) { |
182 int valid_schemes; | 183 int valid_schemes; |
183 CHECK(pickle.ReadInt(iter, &valid_schemes)); | 184 CHECK(pickle.ReadInt(iter, &valid_schemes)); |
184 std::string pattern_str; | 185 std::string pattern_str; |
185 URLPattern pattern(valid_schemes); | 186 URLPattern pattern(valid_schemes); |
186 CHECK(pickle.ReadString(iter, &pattern_str)); | 187 CHECK(pickle.ReadString(iter, &pattern_str)); |
187 | 188 |
188 // We remove the file scheme if it's not actually allowed (see Extension:: | 189 // We remove the file scheme if it's not actually allowed (see Extension:: |
189 // LoadUserScriptHelper), but we need it temporarily while loading the | 190 // LoadUserScriptHelper), but we need it temporarily while loading the |
190 // pattern so that it's valid. | 191 // pattern so that it's valid. |
191 bool had_file_scheme = (valid_schemes & URLPattern::SCHEME_FILE) != 0; | 192 bool had_file_scheme = (valid_schemes & URLPattern::SCHEME_FILE) != 0; |
192 if (!had_file_scheme) | 193 if (!had_file_scheme) |
193 pattern.SetValidSchemes(valid_schemes | URLPattern::SCHEME_FILE); | 194 pattern.SetValidSchemes(valid_schemes | URLPattern::SCHEME_FILE); |
194 CHECK(URLPattern::PARSE_SUCCESS == pattern.Parse(pattern_str)); | 195 CHECK(URLPattern::PARSE_SUCCESS == pattern.Parse(pattern_str)); |
195 if (!had_file_scheme) | 196 if (!had_file_scheme) |
196 pattern.SetValidSchemes(valid_schemes); | 197 pattern.SetValidSchemes(valid_schemes); |
197 | 198 |
198 pattern_list->AddPattern(pattern); | 199 pattern_list->AddPattern(pattern); |
199 } | 200 } |
200 } | 201 } |
201 | 202 |
202 void UserScript::UnpickleScripts(const ::Pickle& pickle, void** iter, | 203 void UserScript::UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter, |
203 FileList* scripts) { | 204 FileList* scripts) { |
204 size_t num_files = 0; | 205 size_t num_files = 0; |
205 CHECK(pickle.ReadSize(iter, &num_files)); | 206 CHECK(pickle.ReadSize(iter, &num_files)); |
206 scripts->clear(); | 207 scripts->clear(); |
207 for (size_t i = 0; i < num_files; ++i) { | 208 for (size_t i = 0; i < num_files; ++i) { |
208 File file; | 209 File file; |
209 file.Unpickle(pickle, iter); | 210 file.Unpickle(pickle, iter); |
210 scripts->push_back(file); | 211 scripts->push_back(file); |
211 } | 212 } |
212 } | 213 } |
OLD | NEW |