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

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

Issue 495853002: Atomic UserScript ID generation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More UserScript IDs: int64 -> int Created 6 years, 4 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
« no previous file with comments | « extensions/common/user_script.h ('k') | extensions/renderer/user_script_injector.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/command_line.h" 8 #include "base/command_line.h"
8 #include "base/pickle.h" 9 #include "base/pickle.h"
9 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
10 #include "extensions/common/switches.h" 11 #include "extensions/common/switches.h"
11 12
12 namespace { 13 namespace {
13 14
15 base::StaticAtomicSequenceNumber g_user_script_id_generator;
Devlin 2014/08/22 18:24:21 nit: maybe a comment on why this can't just be an
Mark Dittmer 2014/08/22 18:28:56 Done.
16
14 bool UrlMatchesGlobs(const std::vector<std::string>* globs, 17 bool UrlMatchesGlobs(const std::vector<std::string>* globs,
15 const GURL& url) { 18 const GURL& url) {
16 for (std::vector<std::string>::const_iterator glob = globs->begin(); 19 for (std::vector<std::string>::const_iterator glob = globs->begin();
17 glob != globs->end(); ++glob) { 20 glob != globs->end(); ++glob) {
18 if (MatchPattern(url.spec(), *glob)) 21 if (MatchPattern(url.spec(), *glob))
19 return true; 22 return true;
20 } 23 }
21 24
22 return false; 25 return false;
23 } 26 }
24 27
25 } // namespace 28 } // namespace
26 29
27 namespace extensions { 30 namespace extensions {
28 31
29 // The bitmask for valid user script injectable schemes used by URLPattern. 32 // The bitmask for valid user script injectable schemes used by URLPattern.
30 enum { 33 enum {
31 kValidUserScriptSchemes = URLPattern::SCHEME_CHROMEUI | 34 kValidUserScriptSchemes = URLPattern::SCHEME_CHROMEUI |
32 URLPattern::SCHEME_HTTP | 35 URLPattern::SCHEME_HTTP |
33 URLPattern::SCHEME_HTTPS | 36 URLPattern::SCHEME_HTTPS |
34 URLPattern::SCHEME_FILE | 37 URLPattern::SCHEME_FILE |
35 URLPattern::SCHEME_FTP 38 URLPattern::SCHEME_FTP
36 }; 39 };
37 40
38 // static 41 // static
39 const char UserScript::kFileExtension[] = ".user.js"; 42 const char UserScript::kFileExtension[] = ".user.js";
40 43
44
45 // static
46 int UserScript::GenerateUserScriptID() {
47 return g_user_script_id_generator.GetNext();
48 }
49
41 bool UserScript::IsURLUserScript(const GURL& url, 50 bool UserScript::IsURLUserScript(const GURL& url,
42 const std::string& mime_type) { 51 const std::string& mime_type) {
43 return EndsWith(url.ExtractFileName(), kFileExtension, false) && 52 return EndsWith(url.ExtractFileName(), kFileExtension, false) &&
44 mime_type != "text/html"; 53 mime_type != "text/html";
45 } 54 }
46 55
47 // static 56 // static
48 int UserScript::ValidUserScriptSchemes(bool canExecuteScriptEverywhere) { 57 int UserScript::ValidUserScriptSchemes(bool canExecuteScriptEverywhere) {
49 if (canExecuteScriptEverywhere) 58 if (canExecuteScriptEverywhere)
50 return URLPattern::SCHEME_ALL; 59 return URLPattern::SCHEME_ALL;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 // Read the url from the pickle. 130 // Read the url from the pickle.
122 std::string url; 131 std::string url;
123 CHECK(pickle.ReadString(iter, &url)); 132 CHECK(pickle.ReadString(iter, &url));
124 set_url(GURL(url)); 133 set_url(GURL(url));
125 } 134 }
126 135
127 void UserScript::Pickle(::Pickle* pickle) const { 136 void UserScript::Pickle(::Pickle* pickle) const {
128 // Write the simple types to the pickle. 137 // Write the simple types to the pickle.
129 pickle->WriteInt(run_location()); 138 pickle->WriteInt(run_location());
130 pickle->WriteString(extension_id()); 139 pickle->WriteString(extension_id());
131 pickle->WriteInt64(user_script_id_); 140 pickle->WriteInt(user_script_id_);
132 pickle->WriteBool(emulate_greasemonkey()); 141 pickle->WriteBool(emulate_greasemonkey());
133 pickle->WriteBool(match_all_frames()); 142 pickle->WriteBool(match_all_frames());
134 pickle->WriteBool(match_about_blank()); 143 pickle->WriteBool(match_about_blank());
135 pickle->WriteBool(is_incognito_enabled()); 144 pickle->WriteBool(is_incognito_enabled());
136 145
137 PickleGlobs(pickle, globs_); 146 PickleGlobs(pickle, globs_);
138 PickleGlobs(pickle, exclude_globs_); 147 PickleGlobs(pickle, exclude_globs_);
139 PickleURLPatternSet(pickle, url_set_); 148 PickleURLPatternSet(pickle, url_set_);
140 PickleURLPatternSet(pickle, exclude_url_set_); 149 PickleURLPatternSet(pickle, exclude_url_set_);
141 PickleScripts(pickle, js_scripts_); 150 PickleScripts(pickle, js_scripts_);
(...skipping 29 matching lines...) Expand all
171 } 180 }
172 181
173 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { 182 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) {
174 // Read the run location. 183 // Read the run location.
175 int run_location = 0; 184 int run_location = 0;
176 CHECK(pickle.ReadInt(iter, &run_location)); 185 CHECK(pickle.ReadInt(iter, &run_location));
177 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); 186 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST);
178 run_location_ = static_cast<RunLocation>(run_location); 187 run_location_ = static_cast<RunLocation>(run_location);
179 188
180 CHECK(pickle.ReadString(iter, &extension_id_)); 189 CHECK(pickle.ReadString(iter, &extension_id_));
181 CHECK(pickle.ReadInt64(iter, &user_script_id_)); 190 CHECK(pickle.ReadInt(iter, &user_script_id_));
182 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_)); 191 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_));
183 CHECK(pickle.ReadBool(iter, &match_all_frames_)); 192 CHECK(pickle.ReadBool(iter, &match_all_frames_));
184 CHECK(pickle.ReadBool(iter, &match_about_blank_)); 193 CHECK(pickle.ReadBool(iter, &match_about_blank_));
185 CHECK(pickle.ReadBool(iter, &incognito_enabled_)); 194 CHECK(pickle.ReadBool(iter, &incognito_enabled_));
186 195
187 UnpickleGlobs(pickle, iter, &globs_); 196 UnpickleGlobs(pickle, iter, &globs_);
188 UnpickleGlobs(pickle, iter, &exclude_globs_); 197 UnpickleGlobs(pickle, iter, &exclude_globs_);
189 UnpickleURLPatternSet(pickle, iter, &url_set_); 198 UnpickleURLPatternSet(pickle, iter, &url_set_);
190 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); 199 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_);
191 UnpickleScripts(pickle, iter, &js_scripts_); 200 UnpickleScripts(pickle, iter, &js_scripts_);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 } 250 }
242 251
243 bool operator<(const UserScript& script1, const UserScript& script2) { 252 bool operator<(const UserScript& script1, const UserScript& script2) {
244 // The only kind of script that should be compared is the kind that has its 253 // The only kind of script that should be compared is the kind that has its
245 // IDs initialized to a meaningful value. 254 // IDs initialized to a meaningful value.
246 DCHECK(script1.id() != -1 && script2.id() != -1); 255 DCHECK(script1.id() != -1 && script2.id() != -1);
247 return script1.id() < script2.id(); 256 return script1.id() < script2.id();
248 } 257 }
249 258
250 } // namespace extensions 259 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/common/user_script.h ('k') | extensions/renderer/user_script_injector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698