| 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 (UrlMatchesPatterns(&url_patterns_, url)) |
| 75 return true; |
| 76 | 76 |
| 77 bool UserScript::MatchesUrl(const GURL& url) const { | 77 if (UrlMatchesGlobs(&exclude_globs_, url)) |
| 78 if (!url_patterns_.empty()) { | 78 return false; |
| 79 if (!UrlMatchesPatterns(&url_patterns_, url)) | |
| 80 return false; | |
| 81 } | |
| 82 | 79 |
| 83 if (!globs_.empty()) { | 80 if (UrlMatchesGlobs(&globs_, url)) |
| 84 if (!UrlMatchesGlobs(&globs_, url)) | 81 return true; |
| 85 return false; | |
| 86 } | |
| 87 | 82 |
| 88 if (!exclude_globs_.empty()) { | 83 return false; |
| 89 if (UrlMatchesGlobs(&exclude_globs_, url)) | |
| 90 return false; | |
| 91 } | |
| 92 | |
| 93 return true; | |
| 94 } | 84 } |
| 95 | 85 |
| 96 void UserScript::File::Pickle(::Pickle* pickle) const { | 86 void UserScript::File::Pickle(::Pickle* pickle) const { |
| 97 pickle->WriteString(url_.spec()); | 87 pickle->WriteString(url_.spec()); |
| 98 // Do not write path. It's not needed in the renderer. | 88 // Do not write path. It's not needed in the renderer. |
| 99 // Do not write content. It will be serialized by other means. | 89 // Do not write content. It will be serialized by other means. |
| 100 } | 90 } |
| 101 | 91 |
| 102 void UserScript::File::Unpickle(const ::Pickle& pickle, void** iter) { | 92 void UserScript::File::Unpickle(const ::Pickle& pickle, void** iter) { |
| 103 // Read url. | 93 // Read url. |
| 104 std::string url; | 94 std::string url; |
| 105 CHECK(pickle.ReadString(iter, &url)); | 95 CHECK(pickle.ReadString(iter, &url)); |
| 106 set_url(GURL(url)); | 96 set_url(GURL(url)); |
| 107 } | 97 } |
| 108 | 98 |
| 109 void UserScript::Pickle(::Pickle* pickle) const { | 99 void UserScript::Pickle(::Pickle* pickle) const { |
| 110 // Write simple types. | 100 // Write simple types. |
| 111 pickle->WriteInt(run_location()); | 101 pickle->WriteInt(run_location()); |
| 112 pickle->WriteString(extension_id()); | 102 pickle->WriteString(extension_id()); |
| 113 pickle->WriteBool(emulate_greasemonkey()); | 103 pickle->WriteBool(emulate_greasemonkey()); |
| 114 pickle->WriteBool(match_all_frames()); | 104 pickle->WriteBool(match_all_frames()); |
| 115 pickle->WriteBool(is_incognito_enabled()); | 105 pickle->WriteBool(is_incognito_enabled()); |
| 116 pickle->WriteBool(allow_file_access()); | |
| 117 | 106 |
| 118 // Write globs. | 107 // Write globs. |
| 119 std::vector<std::string>::const_iterator glob; | 108 std::vector<std::string>::const_iterator glob; |
| 120 pickle->WriteSize(globs_.size()); | 109 pickle->WriteSize(globs_.size()); |
| 121 for (glob = globs_.begin(); glob != globs_.end(); ++glob) { | 110 for (glob = globs_.begin(); glob != globs_.end(); ++glob) { |
| 122 pickle->WriteString(*glob); | 111 pickle->WriteString(*glob); |
| 123 } | 112 } |
| 124 pickle->WriteSize(exclude_globs_.size()); | 113 pickle->WriteSize(exclude_globs_.size()); |
| 125 for (glob = exclude_globs_.begin(); glob != exclude_globs_.end(); ++glob) { | 114 for (glob = exclude_globs_.begin(); glob != exclude_globs_.end(); ++glob) { |
| 126 pickle->WriteString(*glob); | 115 pickle->WriteString(*glob); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 153 // Read the run location. | 142 // Read the run location. |
| 154 int run_location = 0; | 143 int run_location = 0; |
| 155 CHECK(pickle.ReadInt(iter, &run_location)); | 144 CHECK(pickle.ReadInt(iter, &run_location)); |
| 156 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); | 145 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); |
| 157 run_location_ = static_cast<RunLocation>(run_location); | 146 run_location_ = static_cast<RunLocation>(run_location); |
| 158 | 147 |
| 159 CHECK(pickle.ReadString(iter, &extension_id_)); | 148 CHECK(pickle.ReadString(iter, &extension_id_)); |
| 160 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_)); | 149 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_)); |
| 161 CHECK(pickle.ReadBool(iter, &match_all_frames_)); | 150 CHECK(pickle.ReadBool(iter, &match_all_frames_)); |
| 162 CHECK(pickle.ReadBool(iter, &incognito_enabled_)); | 151 CHECK(pickle.ReadBool(iter, &incognito_enabled_)); |
| 163 CHECK(pickle.ReadBool(iter, &allow_file_access_)); | |
| 164 | 152 |
| 165 // Read globs. | 153 // Read globs. |
| 166 size_t num_globs = 0; | 154 size_t num_globs = 0; |
| 167 CHECK(pickle.ReadSize(iter, &num_globs)); | 155 CHECK(pickle.ReadSize(iter, &num_globs)); |
| 168 globs_.clear(); | 156 globs_.clear(); |
| 169 for (size_t i = 0; i < num_globs; ++i) { | 157 for (size_t i = 0; i < num_globs; ++i) { |
| 170 std::string glob; | 158 std::string glob; |
| 171 CHECK(pickle.ReadString(iter, &glob)); | 159 CHECK(pickle.ReadString(iter, &glob)); |
| 172 globs_.push_back(glob); | 160 globs_.push_back(glob); |
| 173 } | 161 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 // Read css scripts. | 197 // Read css scripts. |
| 210 size_t num_css_files = 0; | 198 size_t num_css_files = 0; |
| 211 CHECK(pickle.ReadSize(iter, &num_css_files)); | 199 CHECK(pickle.ReadSize(iter, &num_css_files)); |
| 212 css_scripts_.clear(); | 200 css_scripts_.clear(); |
| 213 for (size_t i = 0; i < num_css_files; ++i) { | 201 for (size_t i = 0; i < num_css_files; ++i) { |
| 214 File file; | 202 File file; |
| 215 file.Unpickle(pickle, iter); | 203 file.Unpickle(pickle, iter); |
| 216 css_scripts_.push_back(file); | 204 css_scripts_.push_back(file); |
| 217 } | 205 } |
| 218 } | 206 } |
| OLD | NEW |