Chromium Code Reviews| Index: chrome/browser/managed_mode_url_filter.cc |
| diff --git a/chrome/browser/managed_mode_url_filter.cc b/chrome/browser/managed_mode_url_filter.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b40fc4b4694f8d26d44c86c0b887ca9a5f30b562 |
| --- /dev/null |
| +++ b/chrome/browser/managed_mode_url_filter.cc |
| @@ -0,0 +1,102 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/managed_mode_url_filter.h" |
| + |
| +#include "base/task_runner_util.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "chrome/browser/policy/url_blacklist_manager.h" |
| +#include "chrome/common/extensions/matcher/url_matcher.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "googleurl/src/gurl.h" |
| + |
| +using content::BrowserThread; |
| +using extensions::URLMatcher; |
| +using extensions::URLMatcherCondition; |
| +using extensions::URLMatcherConditionFactory; |
|
Joao da Silva
2012/07/13 12:32:11
Are these 2 used?
|
| +using extensions::URLMatcherConditionSet; |
| +using extensions::URLMatcherPortFilter; |
| +using extensions::URLMatcherSchemeFilter; |
|
Joao da Silva
2012/07/13 12:32:11
Are these 2 used?
Bernhard Bauer
2012/07/13 14:21:07
Not anymore; removed.
|
| + |
| +ManagedModeURLFilter::ManagedModeURLFilter() |
| + : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
| + active_(false), |
| + url_matcher_(new URLMatcher()) { |
| +} |
| + |
| +ManagedModeURLFilter::~ManagedModeURLFilter() { |
| +} |
| + |
| +bool ManagedModeURLFilter::IsURLWhitelisted(const GURL& url) const { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + if (!active_) |
| + return true; |
| + |
| + if (!policy::URLBlacklist::HasStandardScheme(url)) |
| + return true; |
| + |
| + std::set<URLMatcherConditionSet::ID> matching_ids = |
| + url_matcher_->MatchURL(url); |
| + return !matching_ids.empty(); |
| +} |
| + |
| +void ManagedModeURLFilter::SetActive(bool active) { |
| + DCHECK(CalledOnValidThread()); |
| + active_ = active; |
| +} |
| + |
| +void ManagedModeURLFilter::SetWhitelist(scoped_ptr<base::ListValue> whitelist, |
| + const base::Closure& continuation) { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + // Create the URL matcher from the list on a blocking pool thread, because it |
| + // might be CPU intensive, and we don't want to block the thread we're on. |
| + base::PostTaskAndReplyWithResult( |
| + BrowserThread::GetBlockingPool(), |
| + FROM_HERE, |
| + base::Bind(&CreateURLMatcherForWhitelist, |
| + base::Passed(&whitelist)), |
| + base::Bind(&ManagedModeURLFilter::SetURLMatcher, |
| + weak_ptr_factory_.GetWeakPtr(), continuation)); |
| +} |
| + |
| +// static |
| +scoped_ptr<URLMatcher> ManagedModeURLFilter::CreateURLMatcherForWhitelist( |
| + scoped_ptr<base::ListValue> whitelist) { |
| + scoped_ptr<URLMatcher> url_matcher(new URLMatcher()); |
|
Joao da Silva
2012/07/13 12:32:11
DCHECK(BrowserThread::GetBlockingPool()->RunsTasks
Bernhard Bauer
2012/07/13 14:21:07
Done.
|
| + URLMatcherConditionSet::Vector all_conditions; |
| + int id = 0; |
| + for (base::ListValue::iterator it = whitelist->begin(); |
| + it != whitelist->end(); ++it) { |
| + std::string pattern; |
| + bool success = (*it)->GetAsString(&pattern); |
| + DCHECK(success); |
| + std::string scheme; |
| + std::string host; |
| + uint16 port; |
| + std::string path; |
| + bool match_subdomains = true; |
| + if (!policy::URLBlacklist::FilterToComponents( |
| + pattern, &scheme, &host, &match_subdomains, &port, &path)) { |
| + LOG(ERROR) << "Invalid pattern " << pattern; |
| + continue; |
| + } |
| + |
| + all_conditions.push_back( |
| + policy::URLBlacklist::CreateConditionSet( |
| + url_matcher.get(), ++id, |
| + scheme, host, match_subdomains, port, path)); |
| + } |
| + url_matcher->AddConditionSets(all_conditions); |
| + return url_matcher.Pass(); |
| +} |
| + |
| +void ManagedModeURLFilter::SetURLMatcher(const base::Closure& continuation, |
| + scoped_ptr<URLMatcher> url_matcher) { |
| + DCHECK(CalledOnValidThread()); |
| + url_matcher_ = url_matcher.Pass(); |
| + if (!continuation.is_null()) |
| + continuation.Run(); |
| +} |