OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 19 matching lines...) Expand all Loading... |
31 return false; | 31 return false; |
32 } | 32 } |
33 | 33 |
34 } // namespace | 34 } // namespace |
35 | 35 |
36 // static | 36 // static |
37 const char UserScript::kFileExtension[] = ".user.js"; | 37 const char UserScript::kFileExtension[] = ".user.js"; |
38 | 38 |
39 // static | 39 // static |
40 const int UserScript::kValidUserScriptSchemes = | 40 const int UserScript::kValidUserScriptSchemes = |
41 URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS | | 41 URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS | URLPattern::SCHEME_FTP; |
42 URLPattern::SCHEME_FILE | URLPattern::SCHEME_FTP; | |
43 | 42 |
44 bool UserScript::IsURLUserScript(const GURL& url, | 43 bool UserScript::IsURLUserScript(const GURL& url, |
45 const std::string& mime_type) { | 44 const std::string& mime_type) { |
46 return EndsWith(url.ExtractFileName(), kFileExtension, false) && | 45 return EndsWith(url.ExtractFileName(), kFileExtension, false) && |
47 mime_type != "text/html"; | 46 mime_type != "text/html"; |
48 } | 47 } |
49 | 48 |
50 UserScript::File::File(const FilePath& extension_root, | 49 UserScript::File::File(const FilePath& extension_root, |
51 const FilePath& relative_path, | 50 const FilePath& relative_path, |
52 const GURL& url) | 51 const GURL& url) |
53 : extension_root_(extension_root), | 52 : extension_root_(extension_root), |
54 relative_path_(relative_path), | 53 relative_path_(relative_path), |
55 url_(url) { | 54 url_(url) { |
56 } | 55 } |
57 | 56 |
58 UserScript::File::File() {} | 57 UserScript::File::File() {} |
59 | 58 |
60 UserScript::File::~File() {} | 59 UserScript::File::~File() {} |
61 | 60 |
62 UserScript::UserScript() | 61 UserScript::UserScript() |
63 : run_location_(DOCUMENT_IDLE), emulate_greasemonkey_(false), | 62 : run_location_(DOCUMENT_IDLE), emulate_greasemonkey_(false), |
64 match_all_frames_(false), incognito_enabled_(false), | 63 match_all_frames_(false), incognito_enabled_(false) { |
65 allow_file_access_(false) { | |
66 } | 64 } |
67 | 65 |
68 UserScript::~UserScript() { | 66 UserScript::~UserScript() { |
69 } | 67 } |
70 | 68 |
71 void UserScript::add_url_pattern(const URLPattern& pattern) { | 69 void UserScript::add_url_pattern(const URLPattern& pattern) { |
72 url_patterns_.push_back(pattern); | 70 url_patterns_.push_back(pattern); |
73 } | 71 } |
74 | 72 |
75 void UserScript::clear_url_patterns() { url_patterns_.clear(); } | 73 bool UserScript::MatchesUrl(const GURL& url) const { |
| 74 // If both URL patterns and globs are provided, require that we match both |
| 75 // sets. |
| 76 if ((!url_patterns_.empty() && !globs_.empty()) && |
| 77 (!UrlMatchesPatterns(&url_patterns_, url) || |
| 78 !UrlMatchesGlobs(&globs_, url))) |
| 79 return false; |
76 | 80 |
77 bool UserScript::MatchesUrl(const GURL& url) const { | 81 if (UrlMatchesPatterns(&url_patterns_, url)) |
78 if (!url_patterns_.empty()) { | 82 return true; |
79 if (!UrlMatchesPatterns(&url_patterns_, url)) | |
80 return false; | |
81 } | |
82 | 83 |
83 if (!globs_.empty()) { | 84 if (UrlMatchesGlobs(&exclude_globs_, url)) |
84 if (!UrlMatchesGlobs(&globs_, url)) | 85 return false; |
85 return false; | |
86 } | |
87 | 86 |
88 if (!exclude_globs_.empty()) { | 87 if (UrlMatchesGlobs(&globs_, url)) |
89 if (UrlMatchesGlobs(&exclude_globs_, url)) | 88 return true; |
90 return false; | |
91 } | |
92 | 89 |
93 return true; | 90 return false; |
94 } | 91 } |
95 | 92 |
96 void UserScript::File::Pickle(::Pickle* pickle) const { | 93 void UserScript::File::Pickle(::Pickle* pickle) const { |
97 pickle->WriteString(url_.spec()); | 94 pickle->WriteString(url_.spec()); |
98 // Do not write path. It's not needed in the renderer. | 95 // Do not write path. It's not needed in the renderer. |
99 // Do not write content. It will be serialized by other means. | 96 // Do not write content. It will be serialized by other means. |
100 } | 97 } |
101 | 98 |
102 void UserScript::File::Unpickle(const ::Pickle& pickle, void** iter) { | 99 void UserScript::File::Unpickle(const ::Pickle& pickle, void** iter) { |
103 // Read url. | 100 // Read url. |
104 std::string url; | 101 std::string url; |
105 CHECK(pickle.ReadString(iter, &url)); | 102 CHECK(pickle.ReadString(iter, &url)); |
106 set_url(GURL(url)); | 103 set_url(GURL(url)); |
107 } | 104 } |
108 | 105 |
109 void UserScript::Pickle(::Pickle* pickle) const { | 106 void UserScript::Pickle(::Pickle* pickle) const { |
110 // Write simple types. | 107 // Write simple types. |
111 pickle->WriteInt(run_location()); | 108 pickle->WriteInt(run_location()); |
112 pickle->WriteString(extension_id()); | 109 pickle->WriteString(extension_id()); |
113 pickle->WriteBool(emulate_greasemonkey()); | 110 pickle->WriteBool(emulate_greasemonkey()); |
114 pickle->WriteBool(match_all_frames()); | 111 pickle->WriteBool(match_all_frames()); |
115 pickle->WriteBool(is_incognito_enabled()); | 112 pickle->WriteBool(is_incognito_enabled()); |
116 pickle->WriteBool(allow_file_access()); | |
117 | 113 |
118 // Write globs. | 114 // Write globs. |
119 std::vector<std::string>::const_iterator glob; | 115 std::vector<std::string>::const_iterator glob; |
120 pickle->WriteSize(globs_.size()); | 116 pickle->WriteSize(globs_.size()); |
121 for (glob = globs_.begin(); glob != globs_.end(); ++glob) { | 117 for (glob = globs_.begin(); glob != globs_.end(); ++glob) { |
122 pickle->WriteString(*glob); | 118 pickle->WriteString(*glob); |
123 } | 119 } |
124 pickle->WriteSize(exclude_globs_.size()); | 120 pickle->WriteSize(exclude_globs_.size()); |
125 for (glob = exclude_globs_.begin(); glob != exclude_globs_.end(); ++glob) { | 121 for (glob = exclude_globs_.begin(); glob != exclude_globs_.end(); ++glob) { |
126 pickle->WriteString(*glob); | 122 pickle->WriteString(*glob); |
(...skipping 26 matching lines...) Expand all Loading... |
153 // Read the run location. | 149 // Read the run location. |
154 int run_location = 0; | 150 int run_location = 0; |
155 CHECK(pickle.ReadInt(iter, &run_location)); | 151 CHECK(pickle.ReadInt(iter, &run_location)); |
156 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); | 152 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); |
157 run_location_ = static_cast<RunLocation>(run_location); | 153 run_location_ = static_cast<RunLocation>(run_location); |
158 | 154 |
159 CHECK(pickle.ReadString(iter, &extension_id_)); | 155 CHECK(pickle.ReadString(iter, &extension_id_)); |
160 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_)); | 156 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_)); |
161 CHECK(pickle.ReadBool(iter, &match_all_frames_)); | 157 CHECK(pickle.ReadBool(iter, &match_all_frames_)); |
162 CHECK(pickle.ReadBool(iter, &incognito_enabled_)); | 158 CHECK(pickle.ReadBool(iter, &incognito_enabled_)); |
163 CHECK(pickle.ReadBool(iter, &allow_file_access_)); | |
164 | 159 |
165 // Read globs. | 160 // Read globs. |
166 size_t num_globs = 0; | 161 size_t num_globs = 0; |
167 CHECK(pickle.ReadSize(iter, &num_globs)); | 162 CHECK(pickle.ReadSize(iter, &num_globs)); |
168 globs_.clear(); | 163 globs_.clear(); |
169 for (size_t i = 0; i < num_globs; ++i) { | 164 for (size_t i = 0; i < num_globs; ++i) { |
170 std::string glob; | 165 std::string glob; |
171 CHECK(pickle.ReadString(iter, &glob)); | 166 CHECK(pickle.ReadString(iter, &glob)); |
172 globs_.push_back(glob); | 167 globs_.push_back(glob); |
173 } | 168 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 // Read css scripts. | 204 // Read css scripts. |
210 size_t num_css_files = 0; | 205 size_t num_css_files = 0; |
211 CHECK(pickle.ReadSize(iter, &num_css_files)); | 206 CHECK(pickle.ReadSize(iter, &num_css_files)); |
212 css_scripts_.clear(); | 207 css_scripts_.clear(); |
213 for (size_t i = 0; i < num_css_files; ++i) { | 208 for (size_t i = 0; i < num_css_files; ++i) { |
214 File file; | 209 File file; |
215 file.Unpickle(pickle, iter); | 210 file.Unpickle(pickle, iter); |
216 css_scripts_.push_back(file); | 211 css_scripts_.push_back(file); |
217 } | 212 } |
218 } | 213 } |
OLD | NEW |