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

Side by Side Diff: chrome/browser/chromeos/customization_wallpaper_downloader.cc

Issue 236013002: Apply default wallpaper from customization manifest. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments updated. Created 6 years, 8 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
(Empty)
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.
4
5 #include "chrome/browser/chromeos/customization_wallpaper_downloader.h"
6
7 #include <algorithm>
8 #include <math.h>
9
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_request_context_getter.h"
15 #include "url/gurl.h"
16
17 namespace chromeos {
18 namespace {
19 // This is temporary file suffix (for downloading or resizing).
20 const char kTemporarySuffix[] = ".tmp";
21
22 // Sleep between wallpaper retries (used multiplied by squared retry number).
23 const unsigned kRetrySleepSeconds = 10;
24
25 // Retry is infinite with increasing intervals. When calculated delay becomes
26 // longer than maximum (kMaxRetrySleepSeconds) it is set to the maximum.
27 const double kMaxRetrySleepSeconds = 6 * 3600; // 6 hours
28
29 void CreateWallpaperDirectory(const base::FilePath& wallpaper_dir,
30 bool* success) {
31 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
32 DCHECK(success);
33
34 *success = CreateDirectoryAndGetError(wallpaper_dir, NULL);
35 if (!*success) {
36 NOTREACHED() << "Failed to create directory '" << wallpaper_dir.value()
37 << "'";
38 }
39 }
40
41 void RenameTemporaryFile(const base::FilePath& from,
42 const base::FilePath& to,
43 bool* success) {
44 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
45 DCHECK(success);
46
47 base::File::Error error;
48 if (base::ReplaceFile(from, to, &error)) {
49 *success = true;
50 } else {
51 LOG(WARNING)
52 << "Failed to rename temporary file of Customized Wallpaper. error="
53 << error;
54 *success = false;
55 }
56 }
57
58 } // namespace
59
60 CustomizationWallpaperDownloader::CustomizationWallpaperDownloader(
61 net::URLRequestContextGetter* url_context_getter,
62 const GURL& wallpaper_url,
63 const base::FilePath& wallpaper_dir,
64 const base::FilePath& wallpaper_downloaded_file,
65 base::Callback<void(bool success, const GURL&)>
66 on_wallpaper_fetch_completed)
67 : url_context_getter_(url_context_getter),
68 wallpaper_url_(wallpaper_url),
69 wallpaper_dir_(wallpaper_dir),
70 wallpaper_downloaded_file_(wallpaper_downloaded_file),
71 wallpaper_temporary_file_(wallpaper_downloaded_file.value() +
72 kTemporarySuffix),
73 retries_(0),
74 on_wallpaper_fetch_completed_(on_wallpaper_fetch_completed),
75 weak_factory_(this) {
76 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
77 }
78
79 CustomizationWallpaperDownloader::~CustomizationWallpaperDownloader() {
80 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
81 }
82
83 void CustomizationWallpaperDownloader::StartRequest() {
84 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
85 DCHECK(wallpaper_url_.is_valid());
86
87 url_fetcher_.reset(
88 net::URLFetcher::Create(wallpaper_url_, net::URLFetcher::GET, this));
89 url_fetcher_->SetRequestContext(url_context_getter_);
90 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE |
91 net::LOAD_DISABLE_CACHE |
92 net::LOAD_DO_NOT_SAVE_COOKIES |
93 net::LOAD_DO_NOT_SEND_COOKIES |
94 net::LOAD_DO_NOT_SEND_AUTH_DATA);
95 base::SequencedWorkerPool* blocking_pool =
96 content::BrowserThread::GetBlockingPool();
97 url_fetcher_->SaveResponseToFileAtPath(
98 wallpaper_temporary_file_,
99 blocking_pool->GetSequencedTaskRunner(blocking_pool->GetSequenceToken()));
100 url_fetcher_->Start();
101 }
102
103 void CustomizationWallpaperDownloader::Retry() {
104 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
105 ++retries_;
106
107 const double delay_seconds =
108 std::min(kMaxRetrySleepSeconds,
109 static_cast<double>(retries_) * retries_ * kRetrySleepSeconds);
110 const base::TimeDelta delay =
111 base::TimeDelta::FromSeconds(lround(delay_seconds));
112
113 VLOG(1) << "Schedule Customized Wallpaper download in " << delay.InSecondsF()
114 << " seconds (retry = " << retries_ << ").";
115 request_scheduled_.Start(
116 FROM_HERE, delay, this, &CustomizationWallpaperDownloader::StartRequest);
117 }
118
119 void CustomizationWallpaperDownloader::Start() {
120 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
121 scoped_ptr<bool> success(new bool(false));
122
123 base::Closure mkdir_closure = base::Bind(&CreateWallpaperDirectory,
124 wallpaper_dir_,
125 base::Unretained(success.get()));
126 base::Closure on_created_closure =
127 base::Bind(&CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated,
128 weak_factory_.GetWeakPtr(),
129 base::Passed(success.Pass()));
130 if (!content::BrowserThread::PostBlockingPoolTaskAndReply(
131 FROM_HERE, mkdir_closure, on_created_closure)) {
132 LOG(WARNING) << "Failed to start Customized Wallpaper download.";
133 }
134 }
135
136 void CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated(
137 scoped_ptr<bool> success) {
138 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
139 if (*success)
140 StartRequest();
141 }
142
143 void CustomizationWallpaperDownloader::OnURLFetchComplete(
144 const net::URLFetcher* source) {
145 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
146 DCHECK_EQ(url_fetcher_.get(), source);
147
148 const net::URLRequestStatus status = source->GetStatus();
149 const int response_code = source->GetResponseCode();
150
151 const bool server_error =
152 !status.is_success() ||
153 (response_code >= net::HTTP_INTERNAL_SERVER_ERROR &&
154 response_code < (net::HTTP_INTERNAL_SERVER_ERROR + 100));
155
156 VLOG(1) << "CustomizationWallpaperDownloader::OnURLFetchComplete(): status="
157 << status.status();
158
159 if (server_error) {
160 url_fetcher_.reset();
161 Retry();
162 return;
163 }
164
165 base::FilePath response_path;
166 url_fetcher_->GetResponseAsFilePath(true, &response_path);
167 url_fetcher_.reset();
168
169 scoped_ptr<bool> success(new bool(false));
170
171 base::Closure rename_closure = base::Bind(&RenameTemporaryFile,
172 response_path,
173 wallpaper_downloaded_file_,
174 base::Unretained(success.get()));
175 base::Closure on_rename_closure =
176 base::Bind(&CustomizationWallpaperDownloader::OnTemporaryFileRenamed,
177 weak_factory_.GetWeakPtr(),
178 base::Passed(success.Pass()));
179 if (!content::BrowserThread::PostBlockingPoolTaskAndReply(
180 FROM_HERE, rename_closure, on_rename_closure)) {
181 LOG(WARNING)
182 << "Failed to start Customized Wallpaper Rename DownloadedFile.";
183 on_wallpaper_fetch_completed_.Run(false, wallpaper_url_);
184 }
185 }
186
187 void CustomizationWallpaperDownloader::OnTemporaryFileRenamed(
188 scoped_ptr<bool> success) {
189 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
190 on_wallpaper_fetch_completed_.Run(*success, wallpaper_url_);
191 }
192
193 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698