Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(632)

Side by Side Diff: chrome/browser/net/file_downloader.cc

Issue 1308723005: Popular sites on the NTP: re-download popular suggestions once per Chrome run (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@knn_ordering
Patch Set: rebase Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/net/file_downloader.h" 5 #include "chrome/browser/net/file_downloader.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
12 #include "net/base/load_flags.h" 12 #include "net/base/load_flags.h"
13 #include "net/http/http_status_code.h" 13 #include "net/http/http_status_code.h"
14 #include "net/url_request/url_fetcher.h" 14 #include "net/url_request/url_fetcher.h"
15 #include "url/gurl.h" 15 #include "url/gurl.h"
16 16
17 using content::BrowserThread; 17 using content::BrowserThread;
18 using net::URLFetcher; 18 using net::URLFetcher;
19 19
20 const int kNumRetries = 1; 20 const int kNumRetries = 1;
21 21
22 FileDownloader::FileDownloader(const GURL& url, 22 FileDownloader::FileDownloader(const GURL& url,
23 const base::FilePath& path, 23 const base::FilePath& path,
24 bool overwrite,
24 net::URLRequestContextGetter* request_context, 25 net::URLRequestContextGetter* request_context,
25 const DownloadFinishedCallback& callback) 26 const DownloadFinishedCallback& callback)
26 : callback_(callback), 27 : callback_(callback),
27 fetcher_(URLFetcher::Create(url, URLFetcher::GET, this)), 28 fetcher_(URLFetcher::Create(url, URLFetcher::GET, this)),
28 weak_ptr_factory_(this) { 29 weak_ptr_factory_(this) {
29 fetcher_->SetRequestContext(request_context); 30 fetcher_->SetRequestContext(request_context);
30 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 31 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
31 net::LOAD_DO_NOT_SAVE_COOKIES); 32 net::LOAD_DO_NOT_SAVE_COOKIES);
32 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); 33 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries);
33 fetcher_->SaveResponseToFileAtPath( 34 fetcher_->SaveResponseToFileAtPath(
34 path, 35 path,
35 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); 36 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
36 37
37 base::PostTaskAndReplyWithResult( 38 if (overwrite) {
38 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( 39 fetcher_->Start();
39 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), 40 } else {
40 FROM_HERE, 41 base::PostTaskAndReplyWithResult(
41 base::Bind(&base::PathExists, path), 42 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
42 base::Bind(&FileDownloader::OnFileExistsCheckDone, 43 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(),
43 weak_ptr_factory_.GetWeakPtr())); 44 FROM_HERE,
45 base::Bind(&base::PathExists, path),
46 base::Bind(&FileDownloader::OnFileExistsCheckDone,
47 weak_ptr_factory_.GetWeakPtr()));
48 }
44 } 49 }
45 50
46 FileDownloader::~FileDownloader() {} 51 FileDownloader::~FileDownloader() {}
47 52
48 void FileDownloader::OnURLFetchComplete(const net::URLFetcher* source) { 53 void FileDownloader::OnURLFetchComplete(const net::URLFetcher* source) {
49 DCHECK_EQ(fetcher_.get(), source); 54 DCHECK_EQ(fetcher_.get(), source);
50 // Delete |fetcher_| when we leave this method. This is necessary so the 55 // Delete |fetcher_| when we leave this method. This is necessary so the
51 // download file will be deleted if the download failed. 56 // download file will be deleted if the download failed.
52 scoped_ptr<net::URLFetcher> fetcher(fetcher_.Pass()); 57 scoped_ptr<net::URLFetcher> fetcher(fetcher_.Pass());
53 58
54 const net::URLRequestStatus& status = source->GetStatus(); 59 const net::URLRequestStatus& status = source->GetStatus();
55 if (!status.is_success()) { 60 if (!status.is_success()) {
56 DLOG(WARNING) << "URLRequestStatus error " << status.error(); 61 DLOG(WARNING) << "URLRequestStatus error " << status.error()
62 << " while trying to download " << source->GetURL().spec();
57 callback_.Run(false); 63 callback_.Run(false);
58 return; 64 return;
59 } 65 }
60 66
61 int response_code = source->GetResponseCode(); 67 int response_code = source->GetResponseCode();
62 if (response_code != net::HTTP_OK) { 68 if (response_code != net::HTTP_OK) {
63 DLOG(WARNING) << "HTTP error " << response_code; 69 DLOG(WARNING) << "HTTP error " << response_code
70 << " while trying to download " << source->GetURL().spec();
64 callback_.Run(false); 71 callback_.Run(false);
65 return; 72 return;
66 } 73 }
67 74
68 // Take ownership of the new file. 75 // Take ownership of the new file.
69 base::FilePath response_path; 76 base::FilePath response_path;
70 bool success = source->GetResponseAsFilePath(true, &response_path); 77 bool success = source->GetResponseAsFilePath(true, &response_path);
71 callback_.Run(success); 78 callback_.Run(success);
72 } 79 }
73 80
74 void FileDownloader::OnFileExistsCheckDone(bool exists) { 81 void FileDownloader::OnFileExistsCheckDone(bool exists) {
75 if (exists) 82 if (exists)
76 callback_.Run(true); 83 callback_.Run(true);
77 else 84 else
78 fetcher_->Start(); 85 fetcher_->Start();
79 } 86 }
OLDNEW
« no previous file with comments | « chrome/browser/net/file_downloader.h ('k') | chrome/browser/supervised_user/supervised_user_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698