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

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: Remove wallpaper URL from ServicesCustomizationDocumentTest. 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 <math.h>
8 #include <algorithm>
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_fetcher.h"
15 #include "net/url_request/url_request_context_getter.h"
16 #include "url/gurl.h"
17
18 namespace chromeos {
19 namespace {
20 // This is temporary file suffix (for downloading or resizing).
21 const char kTemporarySuffix[] = ".tmp";
22
23 // Sleep between wallpaper retries (used multiplied by squared retry number).
24 const unsigned kRetrySleepSeconds = 10;
25
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
29
30 void CreateWallpaperDirectory(const base::FilePath& wallpaper_dir,
31 bool* success) {
32 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
33 DCHECK(success);
34
35 *success = CreateDirectoryAndGetError(wallpaper_dir, NULL);
36 if (!*success) {
37 NOTREACHED() << "Failed to create directory '" << wallpaper_dir.value()
38 << "'";
39 }
40 }
41
42 void RenameTemporaryFile(const base::FilePath& from,
43 const base::FilePath& to,
44 bool* success) {
45 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
46 DCHECK(success);
47
48 base::File::Error error;
49 if (base::ReplaceFile(from, to, &error)) {
50 *success = true;
51 } else {
52 LOG(WARNING)
53 << "Failed to rename temporary file of Customized Wallpaper. error="
54 << error;
55 *success = false;
56 }
57 }
58
59 } // namespace
60
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() +
73 kTemporarySuffix),
74 retries_(0),
75 on_wallpaper_fetch_completed_(on_wallpaper_fetch_completed),
76 weak_factory_(this) {
77 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
78 }
79
80 CustomizationWallpaperDownloader::~CustomizationWallpaperDownloader() {
81 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
82 }
83
84 void CustomizationWallpaperDownloader::StartRequest() {
85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
86 DCHECK(wallpaper_url_.is_valid());
87
88 url_fetcher_.reset(
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();
102 }
103
104 void CustomizationWallpaperDownloader::Retry() {
105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
106 ++retries_;
107
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));
113
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);
118 }
119
120 void CustomizationWallpaperDownloader::Start() {
121 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
122 scoped_ptr<bool> success(new bool(false));
123
124 base::Closure mkdir_closure = base::Bind(&CreateWallpaperDirectory,
125 wallpaper_dir_,
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.";
134 }
135 }
136
137 void CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated(
138 scoped_ptr<bool> success) {
139 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
140 if (*success)
141 StartRequest();
142 }
143
144 void CustomizationWallpaperDownloader::OnURLFetchComplete(
145 const net::URLFetcher* source) {
146 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
147 DCHECK_EQ(url_fetcher_.get(), source);
148
149 const net::URLRequestStatus status = source->GetStatus();
150 const int response_code = source->GetResponseCode();
151
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));
156
157 VLOG(1) << "CustomizationWallpaperDownloader::OnURLFetchComplete(): status="
158 << status.status();
159
160 if (server_error) {
161 url_fetcher_.reset();
162 Retry();
163 return;
164 }
165
166 base::FilePath response_path;
167 url_fetcher_->GetResponseAsFilePath(true, &response_path);
168 url_fetcher_.reset();
169
170 scoped_ptr<bool> success(new bool(false));
171
172 base::Closure rename_closure = base::Bind(&RenameTemporaryFile,
173 response_path,
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)) {
182 LOG(WARNING)
183 << "Failed to start Customized Wallpaper Rename DownloadedFile.";
184 on_wallpaper_fetch_completed_.Run(false, wallpaper_url_);
185 }
186 }
187
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_);
192 }
193
194 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698