Commit f136dfbf authored by treib's avatar treib Committed by Commit bot

Supervised user blacklist: Add downloader class to download a blacklist if needed.

BUG=410824

Review URL: https://codereview.chromium.org/592423005

Cr-Commit-Position: refs/heads/master@{#296735}
parent d350ea5c
// Copyright 2014 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/supervised_user/experimental/supervised_user_blacklist_downloader.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_fetcher.h"
#include "url/gurl.h"
using content::BrowserThread;
using net::URLFetcher;
const int kNumRetries = 1;
SupervisedUserBlacklistDownloader::SupervisedUserBlacklistDownloader(
const GURL& url,
const base::FilePath& path,
net::URLRequestContextGetter* request_context,
const DownloadFinishedCallback& callback)
: callback_(callback),
fetcher_(URLFetcher::Create(url, URLFetcher::GET, this)),
weak_ptr_factory_(this) {
fetcher_->SetRequestContext(request_context);
fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
net::LOAD_DO_NOT_SAVE_COOKIES |
net::LOAD_IS_DOWNLOAD);
fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries);
fetcher_->SaveResponseToFileAtPath(
path,
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
base::PostTaskAndReplyWithResult(
BrowserThread::GetBlockingPool(),
FROM_HERE,
base::Bind(&base::PathExists, path),
base::Bind(&SupervisedUserBlacklistDownloader::OnFileExistsCheckDone,
weak_ptr_factory_.GetWeakPtr()));
}
SupervisedUserBlacklistDownloader::~SupervisedUserBlacklistDownloader() {}
void SupervisedUserBlacklistDownloader::OnURLFetchComplete(
const net::URLFetcher* source) {
DCHECK_EQ(fetcher_.get(), source);
const net::URLRequestStatus& status = source->GetStatus();
if (!status.is_success()) {
DLOG(WARNING) << "URLRequestStatus error " << status.error();
callback_.Run(false);
return;
}
int response_code = source->GetResponseCode();
if (response_code != net::HTTP_OK) {
DLOG(WARNING) << "HTTP error " << response_code;
callback_.Run(false);
return;
}
// Take ownership of the new file.
base::FilePath response_path;
bool success = source->GetResponseAsFilePath(true, &response_path);
callback_.Run(success);
}
void SupervisedUserBlacklistDownloader::OnFileExistsCheckDone(bool exists) {
if (exists) {
// TODO(treib): Figure out a strategy for updating the file.
callback_.Run(true);
} else {
fetcher_->Start();
}
}
// Copyright 2014 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.
#ifndef CHROME_BROWSER_SUPERVISED_USER_EXPERIMENTAL_SUPERVISED_USER_BLACKLIST_DOWNLOADER_H_
#define CHROME_BROWSER_SUPERVISED_USER_EXPERIMENTAL_SUPERVISED_USER_BLACKLIST_DOWNLOADER_H_
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "net/url_request/url_fetcher_delegate.h"
namespace base {
class FilePath;
} // namespace base
namespace net {
class URLFetcher;
class URLRequestContextGetter;
} // namespace net
class GURL;
// Helper class to download a blacklist file from a given URL and store it in a
// local file. If the local file already exists, reports success without
// downloading anything.
class SupervisedUserBlacklistDownloader : public net::URLFetcherDelegate {
public:
typedef base::Callback<void(bool /* success */)> DownloadFinishedCallback;
// Directly starts the download (if necessary) and runs |callback| when done.
// If the instance is destroyed before it is finished, |callback| is not run.
SupervisedUserBlacklistDownloader(
const GURL& url,
const base::FilePath& path,
net::URLRequestContextGetter* request_context,
const DownloadFinishedCallback& callback);
virtual ~SupervisedUserBlacklistDownloader();
private:
// net::URLFetcherDelegate implementation.
virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
void OnFileExistsCheckDone(bool exists);
DownloadFinishedCallback callback_;
scoped_ptr<net::URLFetcher> fetcher_;
base::WeakPtrFactory<SupervisedUserBlacklistDownloader> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(SupervisedUserBlacklistDownloader);
};
#endif // CHROME_BROWSER_SUPERVISED_USER_EXPERIMENTAL_SUPERVISED_USER_BLACKLIST_DOWNLOADER_H_
......@@ -18,6 +18,7 @@
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/supervised_user/custodian_profile_downloader_service.h"
#include "chrome/browser/supervised_user/custodian_profile_downloader_service_factory.h"
#include "chrome/browser/supervised_user/experimental/supervised_user_blacklist_downloader.h"
#include "chrome/browser/supervised_user/permission_request_creator_apiary.h"
#include "chrome/browser/supervised_user/permission_request_creator_sync.h"
#include "chrome/browser/supervised_user/supervised_user_constants.h"
......@@ -557,13 +558,39 @@ void SupervisedUserService::UpdateSiteLists() {
#endif
}
void SupervisedUserService::LoadBlacklist(const base::FilePath& path) {
void SupervisedUserService::LoadBlacklist(const base::FilePath& path,
const GURL& url) {
if (!url.is_valid()) {
LoadBlacklistFromFile(path);
return;
}
DCHECK(!blacklist_downloader_.get());
blacklist_downloader_.reset(new SupervisedUserBlacklistDownloader(
url,
path,
profile_->GetRequestContext(),
base::Bind(&SupervisedUserService::OnBlacklistDownloadDone,
base::Unretained(this), path)));
}
void SupervisedUserService::LoadBlacklistFromFile(const base::FilePath& path) {
url_filter_context_.LoadBlacklist(path);
FOR_EACH_OBSERVER(
SupervisedUserServiceObserver, observer_list_, OnURLFilterChanged());
}
void SupervisedUserService::OnBlacklistDownloadDone(const base::FilePath& path,
bool success) {
if (success) {
LoadBlacklistFromFile(path);
} else {
LOG(WARNING) << "Blacklist download failed";
}
blacklist_downloader_.reset();
}
bool SupervisedUserService::AccessRequestsEnabled() {
ProfileSyncService* service =
ProfileSyncServiceFactory::GetForProfile(profile_);
......@@ -735,7 +762,7 @@ void SupervisedUserService::SetActive(bool active) {
if (delegate_ && use_blacklist) {
base::FilePath blacklist_path = delegate_->GetBlacklistPath();
if (!blacklist_path.empty())
LoadBlacklist(blacklist_path);
LoadBlacklist(blacklist_path, delegate_->GetBlacklistURL());
}
#if !defined(OS_ANDROID)
......
......@@ -35,6 +35,7 @@ class Browser;
class GoogleServiceAuthError;
class PermissionRequestCreator;
class Profile;
class SupervisedUserBlacklistDownloader;
class SupervisedUserRegistrationUtility;
class SupervisedUserServiceObserver;
class SupervisedUserSettingsService;
......@@ -84,6 +85,9 @@ class SupervisedUserService : public KeyedService,
// Returns the path to a blacklist file to load, or an empty path to
// indicate "none".
virtual base::FilePath GetBlacklistPath() const = 0;
// Returns the URL from which to download a blacklist if no local one exists
// yet. The blacklist file will be stored at |GetBlacklistPath()|.
virtual GURL GetBlacklistURL() const = 0;
};
virtual ~SupervisedUserService();
......@@ -281,9 +285,16 @@ class SupervisedUserService : public KeyedService,
void UpdateSiteLists();
// Asynchronously downloads a static blacklist file from |url|, stores it at
// |path|, loads it, and applies it to the URL filters. If |url| is not valid
// (e.g. empty), directly tries to load from |path|.
void LoadBlacklist(const base::FilePath& path, const GURL& url);
// Asynchronously loads a static blacklist from a binary file at |path| and
// applies it to the URL filters.
void LoadBlacklist(const base::FilePath& path);
void LoadBlacklistFromFile(const base::FilePath& path);
void OnBlacklistDownloadDone(const base::FilePath& path, bool success);
// Updates the manual overrides for hosts in the URL filters when the
// corresponding preference is changed.
......@@ -327,6 +338,7 @@ class SupervisedUserService : public KeyedService,
bool did_shutdown_;
URLFilterContext url_filter_context_;
scoped_ptr<SupervisedUserBlacklistDownloader> blacklist_downloader_;
// Used to create permission requests.
scoped_ptr<PermissionRequestCreator> permissions_creator_;
......
......@@ -2260,6 +2260,8 @@
'browser/supervised_user/custodian_profile_downloader_service_factory.h',
'browser/supervised_user/experimental/supervised_user_blacklist.cc',
'browser/supervised_user/experimental/supervised_user_blacklist.h',
'browser/supervised_user/experimental/supervised_user_blacklist_downloader.cc',
'browser/supervised_user/experimental/supervised_user_blacklist_downloader.h',
'browser/supervised_user/permission_request_creator.h',
'browser/supervised_user/permission_request_creator_apiary.cc',
'browser/supervised_user/permission_request_creator_apiary.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment