Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / supervised_user / experimental / supervised_user_blacklist_downloader.cc
blob1d96d8b64c92595bf4b59366082b6dc9bd79b391
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/supervised_user/experimental/supervised_user_blacklist_downloader.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/logging.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "net/base/load_flags.h"
12 #include "net/http/http_status_code.h"
13 #include "net/url_request/url_fetcher.h"
14 #include "url/gurl.h"
16 using content::BrowserThread;
17 using net::URLFetcher;
19 const int kNumRetries = 1;
21 SupervisedUserBlacklistDownloader::SupervisedUserBlacklistDownloader(
22 const GURL& url,
23 const base::FilePath& path,
24 net::URLRequestContextGetter* request_context,
25 const DownloadFinishedCallback& callback)
26 : callback_(callback),
27 fetcher_(URLFetcher::Create(url, URLFetcher::GET, this)),
28 weak_ptr_factory_(this) {
29 fetcher_->SetRequestContext(request_context);
30 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
31 net::LOAD_DO_NOT_SAVE_COOKIES |
32 net::LOAD_IS_DOWNLOAD);
33 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries);
34 fetcher_->SaveResponseToFileAtPath(
35 path,
36 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
38 base::PostTaskAndReplyWithResult(
39 BrowserThread::GetBlockingPool(),
40 FROM_HERE,
41 base::Bind(&base::PathExists, path),
42 base::Bind(&SupervisedUserBlacklistDownloader::OnFileExistsCheckDone,
43 weak_ptr_factory_.GetWeakPtr()));
46 SupervisedUserBlacklistDownloader::~SupervisedUserBlacklistDownloader() {}
48 void SupervisedUserBlacklistDownloader::OnURLFetchComplete(
49 const net::URLFetcher* source) {
50 DCHECK_EQ(fetcher_.get(), source);
52 const net::URLRequestStatus& status = source->GetStatus();
53 if (!status.is_success()) {
54 DLOG(WARNING) << "URLRequestStatus error " << status.error();
55 callback_.Run(false);
56 return;
59 int response_code = source->GetResponseCode();
60 if (response_code != net::HTTP_OK) {
61 DLOG(WARNING) << "HTTP error " << response_code;
62 callback_.Run(false);
63 return;
66 // Take ownership of the new file.
67 base::FilePath response_path;
68 bool success = source->GetResponseAsFilePath(true, &response_path);
69 callback_.Run(success);
72 void SupervisedUserBlacklistDownloader::OnFileExistsCheckDone(bool exists) {
73 if (exists) {
74 // TODO(treib): Figure out a strategy for updating the file.
75 callback_.Run(true);
76 } else {
77 fetcher_->Start();