Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 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/browser/policy/url_blacklist_manager.h" | 5 #include "chrome/browser/policy/url_blacklist_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 const int64 kUpdateDelayMs = 1000; | 27 const int64 kUpdateDelayMs = 1000; |
| 28 | 28 |
| 29 // Maximum filters per policy. Filters over this index are ignored. | 29 // Maximum filters per policy. Filters over this index are ignored. |
| 30 const size_t kMaxFiltersPerPolicy = 100; | 30 const size_t kMaxFiltersPerPolicy = 100; |
| 31 | 31 |
| 32 typedef std::vector<std::string> StringVector; | 32 typedef std::vector<std::string> StringVector; |
| 33 | 33 |
| 34 StringVector* ListValueToStringVector(const base::ListValue* list) { | 34 StringVector* ListValueToStringVector(const base::ListValue* list) { |
| 35 StringVector* vector = new StringVector; | 35 StringVector* vector = new StringVector; |
| 36 | 36 |
| 37 if (!list) | 37 if (!list) |
|
zunger
2011/10/20 22:24:03
Better to say 'list == NULL' explicitly.
Joao da Silva
2011/10/21 10:19:47
Done.
| |
| 38 return vector; | 38 return vector; |
| 39 | 39 |
| 40 vector->reserve(list->GetSize()); | 40 vector->reserve(list->GetSize()); |
| 41 std::string s; | 41 std::string s; |
| 42 for (base::ListValue::const_iterator it = list->begin(); | 42 for (base::ListValue::const_iterator it = list->begin(); |
| 43 it != list->end() && vector->size() < kMaxFiltersPerPolicy; ++it) { | 43 it != list->end() && vector->size() < kMaxFiltersPerPolicy; ++it) { |
| 44 if ((*it)->GetAsString(&s)) | 44 if ((*it)->GetAsString(&s)) |
| 45 vector->push_back(s); | 45 vector->push_back(s); |
| 46 } | 46 } |
| 47 | 47 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 65 blacklist->Allow(*it); | 65 blacklist->Allow(*it); |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 | 68 |
| 69 // A task that owns the URLBlacklist, and passes it to the URLBlacklistManager | 69 // A task that owns the URLBlacklist, and passes it to the URLBlacklistManager |
| 70 // on the IO thread, if the URLBlacklistManager still exists. | 70 // on the IO thread, if the URLBlacklistManager still exists. |
| 71 void SetBlacklistOnIO(base::WeakPtr<URLBlacklistManager> blacklist_manager, | 71 void SetBlacklistOnIO(base::WeakPtr<URLBlacklistManager> blacklist_manager, |
| 72 URLBlacklist* blacklist) { | 72 URLBlacklist* blacklist) { |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 74 if (blacklist_manager) | 74 if (blacklist_manager) |
| 75 blacklist_manager->SetBlacklist(blacklist); | 75 blacklist_manager->SetBlacklist(blacklist); |
|
zunger
2011/10/20 22:24:03
Use explicit { }'s when the composite if statement
Joao da Silva
2011/10/21 10:19:47
Done.
| |
| 76 else | 76 else |
| 77 delete blacklist; | 77 delete blacklist; |
| 78 } | 78 } |
| 79 | 79 |
| 80 } // namespace | 80 } // namespace |
| 81 | 81 |
| 82 URLBlacklist::URLBlacklist() { | 82 URLBlacklist::URLBlacklist() { |
| 83 } | 83 } |
| 84 | 84 |
| 85 URLBlacklist::~URLBlacklist() { | 85 URLBlacklist::~URLBlacklist() { |
| 86 STLDeleteValues(&host_filters_); | 86 STLDeleteValues(&host_filters_); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void URLBlacklist::AddFilter(const std::string& filter, bool block) { | 89 void URLBlacklist::AddFilter(const std::string& filter, bool block) { |
| 90 std::string scheme; | 90 std::string scheme; |
| 91 std::string host; | 91 std::string host; |
| 92 uint16 port; | 92 uint16 port; |
| 93 std::string path; | 93 std::string path; |
| 94 SchemeFlag flag; | 94 SchemeFlag flag; |
|
zunger
2011/10/20 22:24:03
Declare variables immediately before first use.
Joao da Silva
2011/10/21 10:19:47
Done.
| |
| 95 bool match_subdomains = true; | 95 bool match_subdomains = true; |
| 96 | 96 |
| 97 if (!FilterToComponents(filter, &scheme, &host, &port, &path)) { | 97 if (!FilterToComponents(filter, &scheme, &host, &port, &path)) { |
| 98 LOG(WARNING) << "Invalid filter, ignoring: " << filter; | 98 LOG(WARNING) << "Invalid filter, ignoring: " << filter; |
| 99 return; | 99 return; |
| 100 } | 100 } |
| 101 | 101 |
| 102 if (!SchemeToFlag(scheme, &flag)) { | 102 if (!SchemeToFlag(scheme, &flag)) { |
| 103 LOG(WARNING) << "Unsupported scheme in filter, ignoring filter: " << filter; | 103 LOG(WARNING) << "Unsupported scheme in filter, ignoring filter: " << filter; |
| 104 return; | 104 return; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 116 HostFilterTable::iterator host_filter = host_filters_.find(host); | 116 HostFilterTable::iterator host_filter = host_filters_.find(host); |
| 117 if (host_filter == host_filters_.end()) { | 117 if (host_filter == host_filters_.end()) { |
| 118 list = new PathFilterList; | 118 list = new PathFilterList; |
| 119 host_filters_[host] = list; | 119 host_filters_[host] = list; |
| 120 } else { | 120 } else { |
| 121 list = host_filter->second; | 121 list = host_filter->second; |
| 122 } | 122 } |
| 123 PathFilterList::iterator it; | 123 PathFilterList::iterator it; |
| 124 for (it = list->begin(); it != list->end(); ++it) { | 124 for (it = list->begin(); it != list->end(); ++it) { |
| 125 if (it->port == port && it->match_subdomains == match_subdomains && | 125 if (it->port == port && it->match_subdomains == match_subdomains && |
| 126 it->path_prefix == path) | 126 it->path_prefix == path) |
|
zunger
2011/10/20 22:24:03
Use explicit { }'s when the hypothesis takes up mo
Joao da Silva
2011/10/21 10:19:47
Done.
| |
| 127 break; | 127 break; |
| 128 } | 128 } |
| 129 PathFilter* path_filter; | 129 PathFilter* path_filter; |
| 130 if (it == list->end()) { | 130 if (it == list->end()) { |
| 131 list->push_back(PathFilter(path, port, match_subdomains)); | 131 list->push_back(PathFilter(path, port, match_subdomains)); |
| 132 path_filter = &list->back(); | 132 path_filter = &list->back(); |
| 133 } else { | 133 } else { |
| 134 path_filter = &(*it); | 134 path_filter = &(*it); |
| 135 } | 135 } |
| 136 | 136 |
| 137 if (block) | 137 if (block) |
|
zunger
2011/10/20 22:24:03
&c
Joao da Silva
2011/10/21 10:19:47
Not clear what is meant here; I've added explicit
| |
| 138 path_filter->blocked_schemes |= flag; | 138 path_filter->blocked_schemes |= flag; |
| 139 else | 139 else |
| 140 path_filter->allowed_schemes |= flag; | 140 path_filter->allowed_schemes |= flag; |
| 141 } | 141 } |
| 142 | 142 |
| 143 void URLBlacklist::Block(const std::string& filter) { | 143 void URLBlacklist::Block(const std::string& filter) { |
| 144 AddFilter(filter, true); | 144 AddFilter(filter, true); |
| 145 } | 145 } |
| 146 | 146 |
| 147 void URLBlacklist::Allow(const std::string& filter) { | 147 void URLBlacklist::Allow(const std::string& filter) { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 158 std::string host(url.host()); | 158 std::string host(url.host()); |
| 159 int int_port = url.EffectiveIntPort(); | 159 int int_port = url.EffectiveIntPort(); |
| 160 const uint16 port = int_port > 0 ? int_port : 0; | 160 const uint16 port = int_port > 0 ? int_port : 0; |
| 161 const std::string& path = url.path(); | 161 const std::string& path = url.path(); |
| 162 | 162 |
| 163 // The first iteration through the loop will be an exact host match. | 163 // The first iteration through the loop will be an exact host match. |
| 164 // Subsequent iterations are subdomain matches, and some filters don't apply | 164 // Subsequent iterations are subdomain matches, and some filters don't apply |
| 165 // to those. | 165 // to those. |
| 166 bool is_matching_subdomains = false; | 166 bool is_matching_subdomains = false; |
| 167 const bool host_is_ip = url.HostIsIPAddress(); | 167 const bool host_is_ip = url.HostIsIPAddress(); |
| 168 for (;;) { | 168 for (;;) { |
|
zunger
2011/10/20 22:24:03
while (1) is a bit more common in google3 code.
Joao da Silva
2011/10/21 10:19:47
Done.
| |
| 169 HostFilterTable::const_iterator host_filter = host_filters_.find(host); | 169 HostFilterTable::const_iterator host_filter = host_filters_.find(host); |
| 170 if (host_filter != host_filters_.end()) { | 170 if (host_filter != host_filters_.end()) { |
| 171 const PathFilterList* list = host_filter->second; | 171 const PathFilterList* list = host_filter->second; |
| 172 size_t longest_length = 0; | 172 size_t longest_length = 0; |
| 173 bool is_blocked = false; | 173 bool is_blocked = false; |
| 174 bool has_match = false; | 174 bool has_match = false; |
| 175 bool has_exact_host_match = false; | 175 bool has_exact_host_match = false; |
| 176 for (PathFilterList::const_iterator it = list->begin(); | 176 for (PathFilterList::const_iterator it = list->begin(); |
| 177 it != list->end(); ++it) { | 177 it != list->end(); ++it) { |
| 178 // Filters that apply to an exact hostname only take precedence over | 178 // Filters that apply to an exact hostname only take precedence over |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 ++i; | 231 ++i; |
| 232 host.erase(0, i); | 232 host.erase(0, i); |
| 233 } | 233 } |
| 234 | 234 |
| 235 // Default is to allow. | 235 // Default is to allow. |
| 236 return false; | 236 return false; |
| 237 } | 237 } |
| 238 | 238 |
| 239 // static | 239 // static |
| 240 bool URLBlacklist::SchemeToFlag(const std::string& scheme, SchemeFlag* flag) { | 240 bool URLBlacklist::SchemeToFlag(const std::string& scheme, SchemeFlag* flag) { |
| 241 if (scheme.empty()) | 241 if (scheme.empty()) |
|
zunger
2011/10/20 22:24:03
{ }'s
Joao da Silva
2011/10/21 10:19:47
Done.
| |
| 242 *flag = SCHEME_ALL; | 242 *flag = SCHEME_ALL; |
| 243 else if (scheme == "http") | 243 else if (scheme == "http") |
| 244 *flag = SCHEME_HTTP; | 244 *flag = SCHEME_HTTP; |
| 245 else if (scheme == "https") | 245 else if (scheme == "https") |
| 246 *flag = SCHEME_HTTPS; | 246 *flag = SCHEME_HTTPS; |
| 247 else if (scheme == "ftp") | 247 else if (scheme == "ftp") |
| 248 *flag = SCHEME_FTP; | 248 *flag = SCHEME_FTP; |
| 249 else | 249 else |
| 250 return false; | 250 return false; |
| 251 return true; | 251 return true; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 393 | 393 |
| 394 // static | 394 // static |
| 395 void URLBlacklistManager::RegisterPrefs(PrefService* pref_service) { | 395 void URLBlacklistManager::RegisterPrefs(PrefService* pref_service) { |
| 396 pref_service->RegisterListPref(prefs::kUrlBlacklist, | 396 pref_service->RegisterListPref(prefs::kUrlBlacklist, |
| 397 PrefService::UNSYNCABLE_PREF); | 397 PrefService::UNSYNCABLE_PREF); |
| 398 pref_service->RegisterListPref(prefs::kUrlWhitelist, | 398 pref_service->RegisterListPref(prefs::kUrlWhitelist, |
| 399 PrefService::UNSYNCABLE_PREF); | 399 PrefService::UNSYNCABLE_PREF); |
| 400 } | 400 } |
| 401 | 401 |
| 402 } // namespace policy | 402 } // namespace policy |
| 403 | |
| OLD | NEW |