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

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

Issue 340057: Add first-class support for user scripts (Closed)
Patch Set: newness Created 11 years, 1 month 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 2009 The Chromium Authors. All rights reserved. 1 // Copyright 2009 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 bool UserScript::MatchesUrl(const GURL& url) const { 10 namespace {
11 for (std::vector<std::string>::const_iterator glob = globs_.begin(); 11 static bool UrlMatchesPatterns(const UserScript::PatternList* patterns,
12 glob != globs_.end(); ++glob) { 12 const GURL& url) {
13 if (MatchPattern(url.spec(), *glob)) 13 for (UserScript::PatternList::const_iterator pattern = patterns->begin();
14 return true; 14 pattern != patterns->end(); ++pattern) {
15 }
16
17 for (PatternList::const_iterator pattern = url_patterns_.begin();
18 pattern != url_patterns_.end(); ++pattern) {
19 if (pattern->MatchesUrl(url)) 15 if (pattern->MatchesUrl(url))
20 return true; 16 return true;
21 } 17 }
22 18
23 return false; 19 return false;
24 } 20 }
25 21
22 static bool UrlMatchesGlobs(const std::vector<std::string>* globs,
23 const GURL& url) {
24 for (std::vector<std::string>::const_iterator glob = globs->begin();
25 glob != globs->end(); ++glob) {
26 if (MatchPattern(url.spec(), *glob))
27 return true;
28 }
29
30 return false;
31 }
32 }
33
34 const char UserScript::kFileExtension[] = ".user.js";
35
36 bool UserScript::HasUserScriptFileExtension(const GURL& url) {
37 return EndsWith(url.ExtractFileName(), kFileExtension, false);
38 }
39
40 bool UserScript::HasUserScriptFileExtension(const FilePath& path) {
41 static FilePath extension(FilePath().AppendASCII(kFileExtension));
42 return EndsWith(path.BaseName().value(), extension.value(), false);
43 }
44
45 bool UserScript::MatchesUrl(const GURL& url) const {
46 if (url_patterns_.size() > 0) {
47 if (!UrlMatchesPatterns(&url_patterns_, url))
48 return false;
49 }
50
51 if (globs_.size() > 0) {
52 if (!UrlMatchesGlobs(&globs_, url))
53 return false;
54 }
55
56 if (exclude_globs_.size() > 0) {
57 if (UrlMatchesGlobs(&exclude_globs_, url))
58 return false;
59 }
60
61 return true;
62 }
63
26 void UserScript::File::Pickle(::Pickle* pickle) const { 64 void UserScript::File::Pickle(::Pickle* pickle) const {
27 pickle->WriteString(url_.spec()); 65 pickle->WriteString(url_.spec());
28 // Do not write path. It's not needed in the renderer. 66 // Do not write path. It's not needed in the renderer.
29 // Do not write content. It will be serialized by other means. 67 // Do not write content. It will be serialized by other means.
30 } 68 }
31 69
32 void UserScript::File::Unpickle(const ::Pickle& pickle, void** iter) { 70 void UserScript::File::Unpickle(const ::Pickle& pickle, void** iter) {
33 // Read url. 71 // Read url.
34 std::string url; 72 std::string url;
35 CHECK(pickle.ReadString(iter, &url)); 73 CHECK(pickle.ReadString(iter, &url));
36 set_url(GURL(url)); 74 set_url(GURL(url));
37 } 75 }
38 76
39 void UserScript::Pickle(::Pickle* pickle) const { 77 void UserScript::Pickle(::Pickle* pickle) const {
40 // Write the run location. 78 // Write the run location.
41 pickle->WriteInt(run_location()); 79 pickle->WriteInt(run_location());
42 80
43 // Write the extension id. 81 // Write the extension id.
44 pickle->WriteString(extension_id()); 82 pickle->WriteString(extension_id());
45 83
84 // Write Greasemonkey emulation.
85 pickle->WriteBool(emulate_greasemonkey());
86
46 // Write globs. 87 // Write globs.
88 std::vector<std::string>::const_iterator glob;
47 pickle->WriteSize(globs_.size()); 89 pickle->WriteSize(globs_.size());
48 for (std::vector<std::string>::const_iterator glob = globs_.begin(); 90 for (glob = globs_.begin(); glob != globs_.end(); ++glob) {
49 glob != globs_.end(); ++glob) { 91 pickle->WriteString(*glob);
92 }
93 pickle->WriteSize(exclude_globs_.size());
94 for (glob = exclude_globs_.begin(); glob != exclude_globs_.end(); ++glob) {
50 pickle->WriteString(*glob); 95 pickle->WriteString(*glob);
51 } 96 }
52 97
53 // Write url patterns. 98 // Write url patterns.
54 pickle->WriteSize(url_patterns_.size()); 99 pickle->WriteSize(url_patterns_.size());
55 for (PatternList::const_iterator pattern = url_patterns_.begin(); 100 for (PatternList::const_iterator pattern = url_patterns_.begin();
56 pattern != url_patterns_.end(); ++pattern) { 101 pattern != url_patterns_.end(); ++pattern) {
57 pickle->WriteString(pattern->GetAsString()); 102 pickle->WriteString(pattern->GetAsString());
58 } 103 }
59 104
(...skipping 15 matching lines...) Expand all
75 void UserScript::Unpickle(const ::Pickle& pickle, void** iter) { 120 void UserScript::Unpickle(const ::Pickle& pickle, void** iter) {
76 // Read the run location. 121 // Read the run location.
77 int run_location = 0; 122 int run_location = 0;
78 CHECK(pickle.ReadInt(iter, &run_location)); 123 CHECK(pickle.ReadInt(iter, &run_location));
79 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); 124 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST);
80 run_location_ = static_cast<RunLocation>(run_location); 125 run_location_ = static_cast<RunLocation>(run_location);
81 126
82 // Read the extension ID. 127 // Read the extension ID.
83 CHECK(pickle.ReadString(iter, &extension_id_)); 128 CHECK(pickle.ReadString(iter, &extension_id_));
84 129
130 // Read Greasemonkey emulation.
131 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_));
132
85 // Read globs. 133 // Read globs.
86 size_t num_globs = 0; 134 size_t num_globs = 0;
87 CHECK(pickle.ReadSize(iter, &num_globs)); 135 CHECK(pickle.ReadSize(iter, &num_globs));
88
89 globs_.clear(); 136 globs_.clear();
90 for (size_t i = 0; i < num_globs; ++i) { 137 for (size_t i = 0; i < num_globs; ++i) {
91 std::string glob; 138 std::string glob;
92 CHECK(pickle.ReadString(iter, &glob)); 139 CHECK(pickle.ReadString(iter, &glob));
93 globs_.push_back(glob); 140 globs_.push_back(glob);
94 } 141 }
95 142
143 CHECK(pickle.ReadSize(iter, &num_globs));
144 exclude_globs_.clear();
145 for (size_t i = 0; i < num_globs; ++i) {
146 std::string glob;
147 CHECK(pickle.ReadString(iter, &glob));
148 exclude_globs_.push_back(glob);
149 }
150
96 // Read url patterns. 151 // Read url patterns.
97 size_t num_patterns = 0; 152 size_t num_patterns = 0;
98 CHECK(pickle.ReadSize(iter, &num_patterns)); 153 CHECK(pickle.ReadSize(iter, &num_patterns));
99 154
100 url_patterns_.clear(); 155 url_patterns_.clear();
101 for (size_t i = 0; i < num_patterns; ++i) { 156 for (size_t i = 0; i < num_patterns; ++i) {
102 std::string pattern_str; 157 std::string pattern_str;
103 URLPattern pattern; 158 URLPattern pattern;
104 CHECK(pickle.ReadString(iter, &pattern_str)); 159 CHECK(pickle.ReadString(iter, &pattern_str));
105 CHECK(pattern.Parse(pattern_str)); 160 CHECK(pattern.Parse(pattern_str));
(...skipping 13 matching lines...) Expand all
119 // Read css scripts. 174 // Read css scripts.
120 size_t num_css_files = 0; 175 size_t num_css_files = 0;
121 CHECK(pickle.ReadSize(iter, &num_css_files)); 176 CHECK(pickle.ReadSize(iter, &num_css_files));
122 css_scripts_.clear(); 177 css_scripts_.clear();
123 for (size_t i = 0; i < num_css_files; ++i) { 178 for (size_t i = 0; i < num_css_files; ++i) {
124 File file; 179 File file;
125 file.Unpickle(pickle, iter); 180 file.Unpickle(pickle, iter);
126 css_scripts_.push_back(file); 181 css_scripts_.push_back(file);
127 } 182 }
128 } 183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698