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

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

Powered by Google App Engine
This is Rietveld 408576698