Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Side by Side Diff: extensions/common/user_script.cc

Issue 822453002: Introduce HostID and de-couple Extensions from "script injection System" [browser side] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Another round of comments of Devlin@. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 UserScript::File::File() {} 78 UserScript::File::File() {}
79 79
80 UserScript::File::~File() {} 80 UserScript::File::~File() {}
81 81
82 UserScript::UserScript() 82 UserScript::UserScript()
83 : run_location_(DOCUMENT_IDLE), 83 : run_location_(DOCUMENT_IDLE),
84 user_script_id_(-1), 84 user_script_id_(-1),
85 emulate_greasemonkey_(false), 85 emulate_greasemonkey_(false),
86 match_all_frames_(false), 86 match_all_frames_(false),
87 match_about_blank_(false), 87 match_about_blank_(false),
88 incognito_enabled_(false) {} 88 incognito_enabled_(false) {
89 }
89 90
90 UserScript::~UserScript() { 91 UserScript::~UserScript() {
91 } 92 }
92 93
93 void UserScript::add_url_pattern(const URLPattern& pattern) { 94 void UserScript::add_url_pattern(const URLPattern& pattern) {
94 url_set_.AddPattern(pattern); 95 url_set_.AddPattern(pattern);
95 } 96 }
96 97
97 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) { 98 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) {
98 exclude_url_set_.AddPattern(pattern); 99 exclude_url_set_.AddPattern(pattern);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 void UserScript::File::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { 132 void UserScript::File::Unpickle(const ::Pickle& pickle, PickleIterator* iter) {
132 // Read the url from the pickle. 133 // Read the url from the pickle.
133 std::string url; 134 std::string url;
134 CHECK(iter->ReadString(&url)); 135 CHECK(iter->ReadString(&url));
135 set_url(GURL(url)); 136 set_url(GURL(url));
136 } 137 }
137 138
138 void UserScript::Pickle(::Pickle* pickle) const { 139 void UserScript::Pickle(::Pickle* pickle) const {
139 // Write the simple types to the pickle. 140 // Write the simple types to the pickle.
140 pickle->WriteInt(run_location()); 141 pickle->WriteInt(run_location());
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());
145 pickle->WriteBool(match_about_blank()); 145 pickle->WriteBool(match_about_blank());
146 pickle->WriteBool(is_incognito_enabled()); 146 pickle->WriteBool(is_incognito_enabled());
147 147
148 PickleConsumerID(pickle, consumer_id_);
148 PickleGlobs(pickle, globs_); 149 PickleGlobs(pickle, globs_);
149 PickleGlobs(pickle, exclude_globs_); 150 PickleGlobs(pickle, exclude_globs_);
150 PickleURLPatternSet(pickle, url_set_); 151 PickleURLPatternSet(pickle, url_set_);
151 PickleURLPatternSet(pickle, exclude_url_set_); 152 PickleURLPatternSet(pickle, exclude_url_set_);
152 PickleScripts(pickle, js_scripts_); 153 PickleScripts(pickle, js_scripts_);
153 PickleScripts(pickle, css_scripts_); 154 PickleScripts(pickle, css_scripts_);
154 } 155 }
155 156
156 void UserScript::PickleGlobs(::Pickle* pickle, 157 void UserScript::PickleGlobs(::Pickle* pickle,
157 const std::vector<std::string>& globs) const { 158 const std::vector<std::string>& globs) const {
158 pickle->WriteSizeT(globs.size()); 159 pickle->WriteSizeT(globs.size());
159 for (std::vector<std::string>::const_iterator glob = globs.begin(); 160 for (std::vector<std::string>::const_iterator glob = globs.begin();
160 glob != globs.end(); ++glob) { 161 glob != globs.end(); ++glob) {
161 pickle->WriteString(*glob); 162 pickle->WriteString(*glob);
162 } 163 }
163 } 164 }
164 165
166 void UserScript::PickleConsumerID(::Pickle* pickle,
167 const ConsumerID& consumer_id) const {
168 pickle->WriteInt(consumer_id.host_type());
169 pickle->WriteString(consumer_id.host_id());
170 pickle->WriteInt(consumer_id.instance_type());
171 pickle->WriteInt(consumer_id.instance_id());
172 }
173
165 void UserScript::PickleURLPatternSet(::Pickle* pickle, 174 void UserScript::PickleURLPatternSet(::Pickle* pickle,
166 const URLPatternSet& pattern_list) const { 175 const URLPatternSet& pattern_list) const {
167 pickle->WriteSizeT(pattern_list.patterns().size()); 176 pickle->WriteSizeT(pattern_list.patterns().size());
168 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); 177 for (URLPatternSet::const_iterator pattern = pattern_list.begin();
169 pattern != pattern_list.end(); ++pattern) { 178 pattern != pattern_list.end(); ++pattern) {
170 pickle->WriteInt(pattern->valid_schemes()); 179 pickle->WriteInt(pattern->valid_schemes());
171 pickle->WriteString(pattern->GetAsString()); 180 pickle->WriteString(pattern->GetAsString());
172 } 181 }
173 } 182 }
174 183
175 void UserScript::PickleScripts(::Pickle* pickle, 184 void UserScript::PickleScripts(::Pickle* pickle,
176 const FileList& scripts) const { 185 const FileList& scripts) const {
177 pickle->WriteSizeT(scripts.size()); 186 pickle->WriteSizeT(scripts.size());
178 for (FileList::const_iterator file = scripts.begin(); 187 for (FileList::const_iterator file = scripts.begin();
179 file != scripts.end(); ++file) { 188 file != scripts.end(); ++file) {
180 file->Pickle(pickle); 189 file->Pickle(pickle);
181 } 190 }
182 } 191 }
183 192
184 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { 193 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) {
185 // Read the run location. 194 // Read the run location.
186 int run_location = 0; 195 int run_location = 0;
187 CHECK(iter->ReadInt(&run_location)); 196 CHECK(iter->ReadInt(&run_location));
188 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); 197 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST);
189 run_location_ = static_cast<RunLocation>(run_location); 198 run_location_ = static_cast<RunLocation>(run_location);
190 199
191 CHECK(iter->ReadString(&extension_id_));
192 CHECK(iter->ReadInt(&user_script_id_)); 200 CHECK(iter->ReadInt(&user_script_id_));
193 CHECK(iter->ReadBool(&emulate_greasemonkey_)); 201 CHECK(iter->ReadBool(&emulate_greasemonkey_));
194 CHECK(iter->ReadBool(&match_all_frames_)); 202 CHECK(iter->ReadBool(&match_all_frames_));
195 CHECK(iter->ReadBool(&match_about_blank_)); 203 CHECK(iter->ReadBool(&match_about_blank_));
196 CHECK(iter->ReadBool(&incognito_enabled_)); 204 CHECK(iter->ReadBool(&incognito_enabled_));
197 205
206 UnpickleConsumerID(pickle, iter, &consumer_id_);
198 UnpickleGlobs(pickle, iter, &globs_); 207 UnpickleGlobs(pickle, iter, &globs_);
199 UnpickleGlobs(pickle, iter, &exclude_globs_); 208 UnpickleGlobs(pickle, iter, &exclude_globs_);
200 UnpickleURLPatternSet(pickle, iter, &url_set_); 209 UnpickleURLPatternSet(pickle, iter, &url_set_);
201 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); 210 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_);
202 UnpickleScripts(pickle, iter, &js_scripts_); 211 UnpickleScripts(pickle, iter, &js_scripts_);
203 UnpickleScripts(pickle, iter, &css_scripts_); 212 UnpickleScripts(pickle, iter, &css_scripts_);
204 } 213 }
205 214
206 void UserScript::UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter, 215 void UserScript::UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter,
207 std::vector<std::string>* globs) { 216 std::vector<std::string>* globs) {
208 size_t num_globs = 0; 217 size_t num_globs = 0;
209 CHECK(iter->ReadSizeT(&num_globs)); 218 CHECK(iter->ReadSizeT(&num_globs));
210 globs->clear(); 219 globs->clear();
211 for (size_t i = 0; i < num_globs; ++i) { 220 for (size_t i = 0; i < num_globs; ++i) {
212 std::string glob; 221 std::string glob;
213 CHECK(iter->ReadString(&glob)); 222 CHECK(iter->ReadString(&glob));
214 globs->push_back(glob); 223 globs->push_back(glob);
215 } 224 }
216 } 225 }
217 226
227 void UserScript::UnpickleConsumerID(const ::Pickle& pickle,
228 PickleIterator* iter,
229 ConsumerID* consumer_id) {
230 int host_type = 0;
231 std::string host_id;
232 int instance_type = 0;
233 int instance_id = 0;
234 CHECK(iter->ReadInt(&host_type));
235 CHECK(iter->ReadString(&host_id));
236 CHECK(iter->ReadInt(&instance_type));
237 CHECK(iter->ReadInt(&instance_id));
238 *consumer_id = ConsumerID(
239 static_cast<ConsumerID::HostType>(host_type), host_id,
240 static_cast<ConsumerID::InstanceType>(instance_type), instance_id);
241 }
242
218 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, 243 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle,
219 PickleIterator* iter, 244 PickleIterator* iter,
220 URLPatternSet* pattern_list) { 245 URLPatternSet* pattern_list) {
221 size_t num_patterns = 0; 246 size_t num_patterns = 0;
222 CHECK(iter->ReadSizeT(&num_patterns)); 247 CHECK(iter->ReadSizeT(&num_patterns));
223 248
224 pattern_list->ClearPatterns(); 249 pattern_list->ClearPatterns();
225 for (size_t i = 0; i < num_patterns; ++i) { 250 for (size_t i = 0; i < num_patterns; ++i) {
226 int valid_schemes; 251 int valid_schemes;
227 CHECK(iter->ReadInt(&valid_schemes)); 252 CHECK(iter->ReadInt(&valid_schemes));
(...skipping 24 matching lines...) Expand all
252 } 277 }
253 278
254 bool operator<(const UserScript& script1, const UserScript& script2) { 279 bool operator<(const UserScript& script1, const UserScript& script2) {
255 // The only kind of script that should be compared is the kind that has its 280 // The only kind of script that should be compared is the kind that has its
256 // IDs initialized to a meaningful value. 281 // IDs initialized to a meaningful value.
257 DCHECK(script1.id() != -1 && script2.id() != -1); 282 DCHECK(script1.id() != -1 && script2.id() != -1);
258 return script1.id() < script2.id(); 283 return script1.id() < script2.id();
259 } 284 }
260 285
261 } // namespace extensions 286 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698