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

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

Issue 208273005: If customization includes default wallpaper, download and apply it. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Start customization manifest fetch on EULA accepted. Never re-fetch wallpaper. Created 6 years, 9 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/url_request/url_request_context_getter.h"
11 #include "url/gurl.h"
12
13 namespace {
14 // This is temporary file suffix (for downloading or resizing).
15 const char kTemporarySuffix[] = ".temp";
16
17 // Sleep between wallpaper retries (used multiplied by retry number).
18 const unsigned kRetrySleepSeconds = 10;
19
20 // Do not increase retry sleep beyond this number of attempts.
21 const unsigned kRetriesInfinite = 30;
22 }
23
24 namespace chromeos {
25
26 CustomizationWallpaperDownloader::CustomizationWallpaperDownloader(
27 net::URLRequestContextGetter* url_context_getter,
28 const GURL& wallpaper_url,
29 const base::FilePath& wallpaper_dir,
30 const base::FilePath& wallpaper_downloaded_file)
31 : url_context_getter_(url_context_getter),
32 wallpaper_url_(wallpaper_url),
33 wallpaper_dir_(wallpaper_dir),
34 wallpaper_downloaded_file_(wallpaper_downloaded_file),
35 wallpaper_temporary_file_(wallpaper_downloaded_file.value() +
36 kTemporarySuffix),
37 retries_(0),
38 weak_factory_(this) {
39 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
40 }
41
42 CustomizationWallpaperDownloader::~CustomizationWallpaperDownloader() {
43 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
44 }
45
46 void CustomizationWallpaperDownloader::StartRequest() {
47 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
48 DCHECK(wallpaper_url_.is_valid());
49 ++retries_;
50
51 url_fetcher_.reset(
52 net::URLFetcher::Create(wallpaper_url_, net::URLFetcher::GET, this));
53 url_fetcher_->SetRequestContext(url_context_getter_);
54 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE |
55 net::LOAD_DO_NOT_SAVE_COOKIES |
56 net::LOAD_DO_NOT_SEND_COOKIES |
57 net::LOAD_DO_NOT_SEND_AUTH_DATA);
58 base::SequencedWorkerPool* blocking_pool =
59 content::BrowserThread::GetBlockingPool();
60 url_fetcher_->SaveResponseToFileAtPath(
61 wallpaper_temporary_file_,
62 blocking_pool->GetSequencedTaskRunner(blocking_pool->GetSequenceToken()));
63 url_fetcher_->Start();
64 }
65
66 void CustomizationWallpaperDownloader::Retry() {
67 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
68 const unsigned delay_multiplier =
69 (retries_ > kRetriesInfinite ? kRetriesInfinite : retries_);
70 const base::TimeDelta delay =
71 base::TimeDelta::FromSeconds(kRetrySleepSeconds) * delay_multiplier;
72 VLOG(1) << "Schedule Customized Wallpaper download in " << delay.InSecondsF()
73 << " seconds.";
74 request_scheduled_.Start(
75 FROM_HERE, delay, this, &CustomizationWallpaperDownloader::StartRequest);
76 }
77
78 void CustomizationWallpaperDownloader::Start() {
79 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
80 scoped_ptr<bool> success(new bool(false));
81
82 base::Closure mkdir_closure =
83 base::Bind(&CustomizationWallpaperDownloader::CreateWallpaperDirectory,
84 wallpaper_dir_,
85 base::Unretained(success.get()));
86 base::Closure on_created_closure =
87 base::Bind(&CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated,
88 weak_factory_.GetWeakPtr(),
89 base::Passed(success.Pass()));
90 if (!content::BrowserThread::PostBlockingPoolTaskAndReply(
91 FROM_HERE, mkdir_closure, on_created_closure)) {
92 LOG(WARNING) << "Failed to start Customized Wallpaper download.";
93 }
94 }
95
96 // static
97 void CustomizationWallpaperDownloader::CreateWallpaperDirectory(
98 const base::FilePath& wallpaper_dir,
99 bool* success) {
100 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
101 DCHECK(success);
102
103 base::File::Error error;
104 if (CreateDirectoryAndGetError(wallpaper_dir, &error)) {
105 *success = true;
106 } else {
107 *success = false;
108 }
109 }
110
111 void CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated(
112 scoped_ptr<bool> success) {
113 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
114 if (*success)
115 StartRequest();
116 }
117
118 void CustomizationWallpaperDownloader::OnURLFetchComplete(
119 const net::URLFetcher* source) {
120 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
121 DCHECK_EQ(url_fetcher_.get(), source);
122
123 const net::URLRequestStatus status = source->GetStatus();
124 const int response_code = source->GetResponseCode();
125
126 const bool server_error =
127 !status.is_success() || (response_code >= 500 && response_code < 600);
128
129 VLOG(1) << "CustomizationWallpaperDownloader::OnURLFetchComplete(): status="
130 << status.status();
131
132 if (server_error) {
133 url_fetcher_.reset();
134 Retry();
Mattias Nissler (ping if slow) 2014/03/24 20:38:54 So this keeps retrying forever. Is that intentiona
Alexander Alekseev 2014/03/25 14:38:29 Why? May be we should increase retry sleep...
135 return;
136 }
137
138 base::FilePath response_path;
139 url_fetcher_->GetResponseAsFilePath(true, &response_path);
140 url_fetcher_.reset();
141
142 scoped_ptr<bool> success(new bool(false));
143
144 base::Closure rename_closure =
145 base::Bind(&CustomizationWallpaperDownloader::RenameTemporaryFile,
146 response_path,
147 wallpaper_downloaded_file_,
148 base::Unretained(success.get()));
149 base::Closure on_rename_closure =
150 base::Bind(&CustomizationWallpaperDownloader::OnTemporaryFileRenamed,
151 weak_factory_.GetWeakPtr(),
152 base::Passed(success.Pass()));
153 if (!content::BrowserThread::PostBlockingPoolTaskAndReply(
154 FROM_HERE, rename_closure, on_rename_closure)) {
155 LOG(WARNING)
156 << "Failed to start Customized Wallpaper Rename DownloadedFile.";
157 // Destroy self.
158 ServicesCustomizationDocument::DestroyWallpaperDownloader();
159 }
160 }
161
162 void CustomizationWallpaperDownloader::OnTemporaryFileRenamed(
163 scoped_ptr<bool> success) {
164 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
165 if (!success) {
166 // Destroy self.
167 ServicesCustomizationDocument::DestroyWallpaperDownloader();
168 return;
169 }
170 ServicesCustomizationDocument::SetDefaultWallpaperPath(wallpaper_url_);
Mattias Nissler (ping if slow) 2014/03/24 20:38:54 The use of static functions for reporting success
Alexander Alekseev 2014/03/25 14:38:29 I've recently received comment (in another CL) tha
171 }
172
173 // static
174 void CustomizationWallpaperDownloader::RenameTemporaryFile(
175 const base::FilePath& from,
176 const base::FilePath& to,
177 bool* success) {
178 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
179 DCHECK(success);
180
181 base::File::Error error;
182 if (ReplaceFile(from, to, &error)) {
183 *success = true;
184 } else {
185 LOG(WARNING)
186 << "Failed to rename temporary file of Customized Wallpaper. error="
187 << error;
188 *success = false;
189 }
190 }
191
192 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698