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/chromeos/customization_wallpaper_downloader.h"
10 #include "base/file_util.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "net/base/load_flags.h"
13 #include "net/http/http_status_code.h"
14 #include "net/url_request/url_fetcher.h"
15 #include "net/url_request/url_request_context_getter.h"
20 // This is temporary file suffix (for downloading or resizing).
21 const char kTemporarySuffix[] = ".tmp";
23 // Sleep between wallpaper retries (used multiplied by squared retry number).
24 const unsigned kRetrySleepSeconds = 10;
26 // Retry is infinite with increasing intervals. When calculated delay becomes
27 // longer than maximum (kMaxRetrySleepSeconds) it is set to the maximum.
28 const double kMaxRetrySleepSeconds = 6 * 3600; // 6 hours
30 void CreateWallpaperDirectory(const base::FilePath& wallpaper_dir,
32 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
35 *success = CreateDirectoryAndGetError(wallpaper_dir, NULL);
37 NOTREACHED() << "Failed to create directory '" << wallpaper_dir.value()
42 void RenameTemporaryFile(const base::FilePath& from,
43 const base::FilePath& to,
45 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
48 base::File::Error error;
49 if (base::ReplaceFile(from, to, &error)) {
53 << "Failed to rename temporary file of Customized Wallpaper. error="
61 CustomizationWallpaperDownloader::CustomizationWallpaperDownloader(
62 net::URLRequestContextGetter* url_context_getter,
63 const GURL& wallpaper_url,
64 const base::FilePath& wallpaper_dir,
65 const base::FilePath& wallpaper_downloaded_file,
66 base::Callback<void(bool success, const GURL&)>
67 on_wallpaper_fetch_completed)
68 : url_context_getter_(url_context_getter),
69 wallpaper_url_(wallpaper_url),
70 wallpaper_dir_(wallpaper_dir),
71 wallpaper_downloaded_file_(wallpaper_downloaded_file),
72 wallpaper_temporary_file_(wallpaper_downloaded_file.value() +
75 on_wallpaper_fetch_completed_(on_wallpaper_fetch_completed),
77 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
80 CustomizationWallpaperDownloader::~CustomizationWallpaperDownloader() {
81 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
84 void CustomizationWallpaperDownloader::StartRequest() {
85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
86 DCHECK(wallpaper_url_.is_valid());
89 net::URLFetcher::Create(wallpaper_url_, net::URLFetcher::GET, this));
90 url_fetcher_->SetRequestContext(url_context_getter_);
91 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE |
92 net::LOAD_DISABLE_CACHE |
93 net::LOAD_DO_NOT_SAVE_COOKIES |
94 net::LOAD_DO_NOT_SEND_COOKIES |
95 net::LOAD_DO_NOT_SEND_AUTH_DATA);
96 base::SequencedWorkerPool* blocking_pool =
97 content::BrowserThread::GetBlockingPool();
98 url_fetcher_->SaveResponseToFileAtPath(
99 wallpaper_temporary_file_,
100 blocking_pool->GetSequencedTaskRunner(blocking_pool->GetSequenceToken()));
101 url_fetcher_->Start();
104 void CustomizationWallpaperDownloader::Retry() {
105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
108 const double delay_seconds =
109 std::min(kMaxRetrySleepSeconds,
110 static_cast<double>(retries_) * retries_ * kRetrySleepSeconds);
111 const base::TimeDelta delay =
112 base::TimeDelta::FromSeconds(lround(delay_seconds));
114 VLOG(1) << "Schedule Customized Wallpaper download in " << delay.InSecondsF()
115 << " seconds (retry = " << retries_ << ").";
116 request_scheduled_.Start(
117 FROM_HERE, delay, this, &CustomizationWallpaperDownloader::StartRequest);
120 void CustomizationWallpaperDownloader::Start() {
121 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
122 scoped_ptr<bool> success(new bool(false));
124 base::Closure mkdir_closure = base::Bind(&CreateWallpaperDirectory,
126 base::Unretained(success.get()));
127 base::Closure on_created_closure =
128 base::Bind(&CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated,
129 weak_factory_.GetWeakPtr(),
130 base::Passed(success.Pass()));
131 if (!content::BrowserThread::PostBlockingPoolTaskAndReply(
132 FROM_HERE, mkdir_closure, on_created_closure)) {
133 LOG(WARNING) << "Failed to start Customized Wallpaper download.";
137 void CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated(
138 scoped_ptr<bool> success) {
139 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
144 void CustomizationWallpaperDownloader::OnURLFetchComplete(
145 const net::URLFetcher* source) {
146 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
147 DCHECK_EQ(url_fetcher_.get(), source);
149 const net::URLRequestStatus status = source->GetStatus();
150 const int response_code = source->GetResponseCode();
152 const bool server_error =
153 !status.is_success() ||
154 (response_code >= net::HTTP_INTERNAL_SERVER_ERROR &&
155 response_code < (net::HTTP_INTERNAL_SERVER_ERROR + 100));
157 VLOG(1) << "CustomizationWallpaperDownloader::OnURLFetchComplete(): status="
161 url_fetcher_.reset();
166 base::FilePath response_path;
167 url_fetcher_->GetResponseAsFilePath(true, &response_path);
168 url_fetcher_.reset();
170 scoped_ptr<bool> success(new bool(false));
172 base::Closure rename_closure = base::Bind(&RenameTemporaryFile,
174 wallpaper_downloaded_file_,
175 base::Unretained(success.get()));
176 base::Closure on_rename_closure =
177 base::Bind(&CustomizationWallpaperDownloader::OnTemporaryFileRenamed,
178 weak_factory_.GetWeakPtr(),
179 base::Passed(success.Pass()));
180 if (!content::BrowserThread::PostBlockingPoolTaskAndReply(
181 FROM_HERE, rename_closure, on_rename_closure)) {
183 << "Failed to start Customized Wallpaper Rename DownloadedFile.";
184 on_wallpaper_fetch_completed_.Run(false, wallpaper_url_);
188 void CustomizationWallpaperDownloader::OnTemporaryFileRenamed(
189 scoped_ptr<bool> success) {
190 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
191 on_wallpaper_fetch_completed_.Run(*success, wallpaper_url_);
194 } // namespace chromeos