OLD | NEW |
---|---|
(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[] = ".temp"; | |
Nikita (slow)
2014/04/15 12:39:24
nit: .tmp? // To reflect what comments are saying.
Alexander Alekseev
2014/04/15 22:47:31
Done.
| |
18 | |
19 // Sleep between wallpaper retries (used multiplied by retry number). | |
20 const unsigned kRetrySleepSeconds = 10; | |
21 | |
22 // Do not retry beyond this number of attempts. | |
23 const int kMaxRetries = 200; | |
Daniel Erat
2014/04/15 03:06:30
why don't you make it retry forever and add a cons
Alexander Alekseev
2014/04/15 22:47:31
Done.
| |
24 | |
25 void CreateWallpaperDirectory(const base::FilePath& wallpaper_dir, | |
26 bool* success) { | |
27 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
28 DCHECK(success); | |
29 | |
30 *success = CreateDirectoryAndGetError(wallpaper_dir, NULL); | |
Nikita (slow)
2014/04/15 12:39:24
NOTREACHED() if unable to create directory.
Alexander Alekseev
2014/04/15 22:47:31
Done.
| |
31 } | |
32 | |
33 void RenameTemporaryFile(const base::FilePath& from, | |
34 const base::FilePath& to, | |
35 bool* success) { | |
36 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
37 DCHECK(success); | |
38 | |
39 base::File::Error error; | |
40 if (base::ReplaceFile(from, to, &error)) { | |
41 *success = true; | |
42 } else { | |
43 LOG(WARNING) | |
44 << "Failed to rename temporary file of Customized Wallpaper. error=" | |
45 << error; | |
46 *success = false; | |
47 } | |
48 } | |
49 | |
50 } // namespace | |
51 | |
52 CustomizationWallpaperDownloader::CustomizationWallpaperDownloader( | |
53 net::URLRequestContextGetter* url_context_getter, | |
54 const GURL& wallpaper_url, | |
55 const base::FilePath& wallpaper_dir, | |
56 const base::FilePath& wallpaper_downloaded_file, | |
57 base::Callback<void(bool success, const GURL&)> | |
58 on_wallpaper_fetch_completed) | |
59 : url_context_getter_(url_context_getter), | |
60 wallpaper_url_(wallpaper_url), | |
61 wallpaper_dir_(wallpaper_dir), | |
62 wallpaper_downloaded_file_(wallpaper_downloaded_file), | |
63 wallpaper_temporary_file_(wallpaper_downloaded_file.value() + | |
64 kTemporarySuffix), | |
65 attempts_(0), | |
66 on_wallpaper_fetch_completed_(on_wallpaper_fetch_completed), | |
67 weak_factory_(this) { | |
68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
69 } | |
70 | |
71 CustomizationWallpaperDownloader::~CustomizationWallpaperDownloader() { | |
72 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
73 } | |
74 | |
75 void CustomizationWallpaperDownloader::StartRequest() { | |
76 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
77 DCHECK(wallpaper_url_.is_valid()); | |
78 ++attempts_; | |
79 | |
80 url_fetcher_.reset( | |
81 net::URLFetcher::Create(wallpaper_url_, net::URLFetcher::GET, this)); | |
82 url_fetcher_->SetRequestContext(url_context_getter_); | |
83 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | | |
Nikita (slow)
2014/04/15 12:39:24
nit: One flag per line.
Alexander Alekseev
2014/04/15 22:47:31
Done.
| |
84 net::LOAD_DO_NOT_SAVE_COOKIES | | |
85 net::LOAD_DO_NOT_SEND_COOKIES | | |
86 net::LOAD_DO_NOT_SEND_AUTH_DATA); | |
87 base::SequencedWorkerPool* blocking_pool = | |
88 content::BrowserThread::GetBlockingPool(); | |
89 url_fetcher_->SaveResponseToFileAtPath( | |
90 wallpaper_temporary_file_, | |
91 blocking_pool->GetSequencedTaskRunner(blocking_pool->GetSequenceToken())); | |
92 url_fetcher_->Start(); | |
93 } | |
94 | |
95 void CustomizationWallpaperDownloader::Retry() { | |
96 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
97 if (attempts_ > kMaxRetries) { | |
98 LOG(WARNING) | |
99 << "Failed to fetch customized wallpaper. Retry limit reached."; | |
100 on_wallpaper_fetch_completed_.Run(false, wallpaper_url_); | |
101 return; | |
102 } | |
103 | |
104 const base::TimeDelta delay = | |
105 base::TimeDelta::FromSeconds(kRetrySleepSeconds) * attempts_; | |
106 VLOG(1) << "Schedule Customized Wallpaper download in " << delay.InSecondsF() | |
107 << " seconds."; | |
108 request_scheduled_.Start( | |
109 FROM_HERE, delay, this, &CustomizationWallpaperDownloader::StartRequest); | |
110 } | |
111 | |
112 void CustomizationWallpaperDownloader::Start() { | |
113 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
114 scoped_ptr<bool> success(new bool(false)); | |
115 | |
116 base::Closure mkdir_closure = base::Bind(&CreateWallpaperDirectory, | |
117 wallpaper_dir_, | |
118 base::Unretained(success.get())); | |
119 base::Closure on_created_closure = | |
120 base::Bind(&CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated, | |
121 weak_factory_.GetWeakPtr(), | |
122 base::Passed(success.Pass())); | |
123 if (!content::BrowserThread::PostBlockingPoolTaskAndReply( | |
124 FROM_HERE, mkdir_closure, on_created_closure)) { | |
125 LOG(WARNING) << "Failed to start Customized Wallpaper download."; | |
126 } | |
127 } | |
128 | |
129 void CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated( | |
130 scoped_ptr<bool> success) { | |
131 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
132 if (*success) | |
133 StartRequest(); | |
134 } | |
135 | |
136 void CustomizationWallpaperDownloader::OnURLFetchComplete( | |
137 const net::URLFetcher* source) { | |
138 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
139 DCHECK_EQ(url_fetcher_.get(), source); | |
140 | |
141 const net::URLRequestStatus status = source->GetStatus(); | |
142 const int response_code = source->GetResponseCode(); | |
143 | |
144 const bool server_error = | |
145 !status.is_success() || | |
146 (response_code >= net::HTTP_INTERNAL_SERVER_ERROR && | |
147 response_code < (net::HTTP_INTERNAL_SERVER_ERROR + 100)); | |
148 | |
149 VLOG(1) << "CustomizationWallpaperDownloader::OnURLFetchComplete(): status=" | |
150 << status.status(); | |
151 | |
152 if (server_error) { | |
153 url_fetcher_.reset(); | |
154 Retry(); | |
155 return; | |
156 } | |
157 | |
158 base::FilePath response_path; | |
159 url_fetcher_->GetResponseAsFilePath(true, &response_path); | |
160 url_fetcher_.reset(); | |
161 | |
162 scoped_ptr<bool> success(new bool(false)); | |
163 | |
164 base::Closure rename_closure = base::Bind(&RenameTemporaryFile, | |
165 response_path, | |
166 wallpaper_downloaded_file_, | |
167 base::Unretained(success.get())); | |
168 base::Closure on_rename_closure = | |
169 base::Bind(&CustomizationWallpaperDownloader::OnTemporaryFileRenamed, | |
170 weak_factory_.GetWeakPtr(), | |
171 base::Passed(success.Pass())); | |
172 if (!content::BrowserThread::PostBlockingPoolTaskAndReply( | |
173 FROM_HERE, rename_closure, on_rename_closure)) { | |
174 LOG(WARNING) | |
175 << "Failed to start Customized Wallpaper Rename DownloadedFile."; | |
176 on_wallpaper_fetch_completed_.Run(false, wallpaper_url_); | |
177 } | |
178 } | |
179 | |
180 void CustomizationWallpaperDownloader::OnTemporaryFileRenamed( | |
181 scoped_ptr<bool> success) { | |
182 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
183 on_wallpaper_fetch_completed_.Run(*success, wallpaper_url_); | |
184 } | |
185 | |
186 } // namespace chromeos | |
OLD | NEW |