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" | |
11 #include "base/values.h" | 10 #include "base/values.h" |
12 #include "chrome/browser/net/url_fixer_upper.h" | 11 #include "chrome/browser/net/url_fixer_upper.h" |
13 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
15 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
16 #include "content/browser/browser_thread.h" | 15 #include "content/browser/browser_thread.h" |
17 #include "content/common/notification_details.h" | 16 #include "content/common/notification_details.h" |
18 #include "content/common/notification_source.h" | 17 #include "content/common/notification_source.h" |
19 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
20 | 19 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 | 78 |
80 } // namespace | 79 } // namespace |
81 | 80 |
82 URLBlacklist::URLBlacklist() { | 81 URLBlacklist::URLBlacklist() { |
83 } | 82 } |
84 | 83 |
85 URLBlacklist::~URLBlacklist() { | 84 URLBlacklist::~URLBlacklist() { |
86 STLDeleteValues(&host_filters_); | 85 STLDeleteValues(&host_filters_); |
87 } | 86 } |
88 | 87 |
89 void URLBlacklist::AddFilter(const std::string& filter, bool block) { | |
90 std::string scheme; | |
91 std::string host; | |
92 uint16 port; | |
93 std::string path; | |
94 SchemeFlag flag; | |
95 bool match_subdomains = true; | |
96 | |
97 if (!FilterToComponents(filter, &scheme, &host, &port, &path)) { | |
98 LOG(WARNING) << "Invalid filter, ignoring: " << filter; | |
99 return; | |
100 } | |
101 | |
102 if (!SchemeToFlag(scheme, &flag)) { | |
103 LOG(WARNING) << "Unsupported scheme in filter, ignoring filter: " << filter; | |
104 return; | |
105 } | |
106 | |
107 // Special syntax to disable subdomain matching. | |
108 if (!host.empty() && host[0] == '.') { | |
109 host.erase(0, 1); | |
110 match_subdomains = false; | |
111 } | |
112 | |
113 // Try to find an existing PathFilter with the same path prefix, port and | |
114 // match_subdomains value. | |
115 PathFilterList* list; | |
116 HostFilterTable::iterator host_filter = host_filters_.find(host); | |
117 if (host_filter == host_filters_.end()) { | |
118 list = new PathFilterList; | |
119 host_filters_[host] = list; | |
120 } else { | |
121 list = host_filter->second; | |
122 } | |
123 PathFilterList::iterator it; | |
124 for (it = list->begin(); it != list->end(); ++it) { | |
125 if (it->port == port && it->match_subdomains == match_subdomains && | |
126 it->path_prefix == path) | |
127 break; | |
128 } | |
129 PathFilter* path_filter; | |
130 if (it == list->end()) { | |
131 list->push_back(PathFilter(path, port, match_subdomains)); | |
132 path_filter = &list->back(); | |
133 } else { | |
134 path_filter = &(*it); | |
135 } | |
136 | |
137 if (block) | |
138 path_filter->blocked_schemes |= flag; | |
139 else | |
140 path_filter->allowed_schemes |= flag; | |
141 } | |
142 | |
143 void URLBlacklist::Block(const std::string& filter) { | 88 void URLBlacklist::Block(const std::string& filter) { |
144 AddFilter(filter, true); | 89 AddFilter(filter, true); |
145 } | 90 } |
146 | 91 |
147 void URLBlacklist::Allow(const std::string& filter) { | 92 void URLBlacklist::Allow(const std::string& filter) { |
148 AddFilter(filter, false); | 93 AddFilter(filter, false); |
149 } | 94 } |
150 | 95 |
151 bool URLBlacklist::IsURLBlocked(const GURL& url) const { | 96 bool URLBlacklist::IsURLBlocked(const GURL& url) const { |
152 SchemeFlag flag = SCHEME_ALL; | 97 SchemeFlag flag = SCHEME_ALL; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 } | 233 } |
289 | 234 |
290 if (parsed.path.is_nonempty()) | 235 if (parsed.path.is_nonempty()) |
291 path->assign(filter, parsed.path.begin, parsed.path.len); | 236 path->assign(filter, parsed.path.begin, parsed.path.len); |
292 else | 237 else |
293 path->clear(); | 238 path->clear(); |
294 | 239 |
295 return true; | 240 return true; |
296 } | 241 } |
297 | 242 |
| 243 void URLBlacklist::AddFilter(const std::string& filter, bool block) { |
| 244 std::string scheme; |
| 245 std::string host; |
| 246 uint16 port; |
| 247 std::string path; |
| 248 SchemeFlag flag; |
| 249 bool match_subdomains = true; |
| 250 |
| 251 if (!FilterToComponents(filter, &scheme, &host, &port, &path)) { |
| 252 LOG(WARNING) << "Invalid filter, ignoring: " << filter; |
| 253 return; |
| 254 } |
| 255 |
| 256 if (!SchemeToFlag(scheme, &flag)) { |
| 257 LOG(WARNING) << "Unsupported scheme in filter, ignoring filter: " << filter; |
| 258 return; |
| 259 } |
| 260 |
| 261 // Special syntax to disable subdomain matching. |
| 262 if (!host.empty() && host[0] == '.') { |
| 263 host.erase(0, 1); |
| 264 match_subdomains = false; |
| 265 } |
| 266 |
| 267 // Try to find an existing PathFilter with the same path prefix, port and |
| 268 // match_subdomains value. |
| 269 PathFilterList* list; |
| 270 HostFilterTable::iterator host_filter = host_filters_.find(host); |
| 271 if (host_filter == host_filters_.end()) { |
| 272 list = new PathFilterList; |
| 273 host_filters_[host] = list; |
| 274 } else { |
| 275 list = host_filter->second; |
| 276 } |
| 277 PathFilterList::iterator it; |
| 278 for (it = list->begin(); it != list->end(); ++it) { |
| 279 if (it->port == port && it->match_subdomains == match_subdomains && |
| 280 it->path_prefix == path) |
| 281 break; |
| 282 } |
| 283 PathFilter* path_filter; |
| 284 if (it == list->end()) { |
| 285 list->push_back(PathFilter(path, port, match_subdomains)); |
| 286 path_filter = &list->back(); |
| 287 } else { |
| 288 path_filter = &(*it); |
| 289 } |
| 290 |
| 291 if (block) |
| 292 path_filter->blocked_schemes |= flag; |
| 293 else |
| 294 path_filter->allowed_schemes |= flag; |
| 295 } |
| 296 |
298 URLBlacklistManager::URLBlacklistManager(PrefService* pref_service) | 297 URLBlacklistManager::URLBlacklistManager(PrefService* pref_service) |
299 : ALLOW_THIS_IN_INITIALIZER_LIST(ui_method_factory_(this)), | 298 : ALLOW_THIS_IN_INITIALIZER_LIST(ui_method_factory_(this)), |
300 pref_service_(pref_service), | 299 pref_service_(pref_service), |
301 ALLOW_THIS_IN_INITIALIZER_LIST(io_weak_ptr_factory_(this)), | 300 ALLOW_THIS_IN_INITIALIZER_LIST(io_weak_ptr_factory_(this)), |
302 blacklist_(new URLBlacklist) { | 301 blacklist_(new URLBlacklist) { |
303 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 302 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
304 | 303 |
305 pref_change_registrar_.Init(pref_service_); | 304 pref_change_registrar_.Init(pref_service_); |
306 pref_change_registrar_.Add(prefs::kUrlBlacklist, this); | 305 pref_change_registrar_.Add(prefs::kUrlBlacklist, this); |
307 pref_change_registrar_.Add(prefs::kUrlWhitelist, this); | 306 pref_change_registrar_.Add(prefs::kUrlWhitelist, this); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 | 392 |
394 // static | 393 // static |
395 void URLBlacklistManager::RegisterPrefs(PrefService* pref_service) { | 394 void URLBlacklistManager::RegisterPrefs(PrefService* pref_service) { |
396 pref_service->RegisterListPref(prefs::kUrlBlacklist, | 395 pref_service->RegisterListPref(prefs::kUrlBlacklist, |
397 PrefService::UNSYNCABLE_PREF); | 396 PrefService::UNSYNCABLE_PREF); |
398 pref_service->RegisterListPref(prefs::kUrlWhitelist, | 397 pref_service->RegisterListPref(prefs::kUrlWhitelist, |
399 PrefService::UNSYNCABLE_PREF); | 398 PrefService::UNSYNCABLE_PREF); |
400 } | 399 } |
401 | 400 |
402 } // namespace policy | 401 } // namespace policy |
OLD | NEW |