Chromium Code Reviews| 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/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 retry beyond this number of attempts. | |
| 21 const unsigned kMaxRetries = 200; | |
|
Daniel Erat
2014/04/14 15:26:15
where did these numbers come from? why is it meani
Alexander Alekseev
2014/04/15 01:57:16
The original idea was to "never give up". 200 retr
| |
| 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 base::Callback<void(const GURL&)> on_url_fetched) | |
| 32 : url_context_getter_(url_context_getter), | |
| 33 wallpaper_url_(wallpaper_url), | |
| 34 wallpaper_dir_(wallpaper_dir), | |
| 35 wallpaper_downloaded_file_(wallpaper_downloaded_file), | |
| 36 wallpaper_temporary_file_(wallpaper_downloaded_file.value() + | |
| 37 kTemporarySuffix), | |
| 38 retries_(0), | |
| 39 on_url_fetched_(on_url_fetched), | |
| 40 weak_factory_(this) { | |
| 41 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 42 } | |
| 43 | |
| 44 CustomizationWallpaperDownloader::~CustomizationWallpaperDownloader() { | |
| 45 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 46 } | |
| 47 | |
| 48 void CustomizationWallpaperDownloader::StartRequest() { | |
| 49 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 50 DCHECK(wallpaper_url_.is_valid()); | |
| 51 ++retries_; | |
|
Daniel Erat
2014/04/14 15:26:15
should |retries_| actually be named |attempts_|? "
Alexander Alekseev
2014/04/15 01:57:16
Done.
| |
| 52 | |
| 53 url_fetcher_.reset( | |
| 54 net::URLFetcher::Create(wallpaper_url_, net::URLFetcher::GET, this)); | |
| 55 url_fetcher_->SetRequestContext(url_context_getter_); | |
| 56 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | | |
| 57 net::LOAD_DO_NOT_SAVE_COOKIES | | |
| 58 net::LOAD_DO_NOT_SEND_COOKIES | | |
| 59 net::LOAD_DO_NOT_SEND_AUTH_DATA); | |
| 60 base::SequencedWorkerPool* blocking_pool = | |
| 61 content::BrowserThread::GetBlockingPool(); | |
| 62 url_fetcher_->SaveResponseToFileAtPath( | |
| 63 wallpaper_temporary_file_, | |
| 64 blocking_pool->GetSequencedTaskRunner(blocking_pool->GetSequenceToken())); | |
| 65 url_fetcher_->Start(); | |
| 66 } | |
| 67 | |
| 68 void CustomizationWallpaperDownloader::Retry() { | |
| 69 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 70 if (retries_ > kMaxRetries) { | |
| 71 LOG(WARNING) | |
| 72 << "Failed to fetch customized wallpaper. Retry limit reached."; | |
| 73 // Destroy self. | |
| 74 ServicesCustomizationDocument::GetInstance()->DestroyWallpaperDownloader(); | |
|
Daniel Erat
2014/04/14 15:26:15
having CustomizationWallpaperDownloader call into
Dmitry Polukhin
2014/04/14 20:26:37
Good suggestion how to eliminate DestroyWallpaperD
Alexander Alekseev
2014/04/15 01:57:16
Done.
| |
| 75 } | |
| 76 | |
| 77 const base::TimeDelta delay = | |
| 78 base::TimeDelta::FromSeconds(kRetrySleepSeconds) * retries_; | |
| 79 VLOG(1) << "Schedule Customized Wallpaper download in " << delay.InSecondsF() | |
| 80 << " seconds."; | |
| 81 request_scheduled_.Start( | |
| 82 FROM_HERE, delay, this, &CustomizationWallpaperDownloader::StartRequest); | |
| 83 } | |
| 84 | |
| 85 void CustomizationWallpaperDownloader::Start() { | |
| 86 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 87 scoped_ptr<bool> success(new bool(false)); | |
| 88 | |
| 89 base::Closure mkdir_closure = | |
| 90 base::Bind(&CustomizationWallpaperDownloader::CreateWallpaperDirectory, | |
| 91 wallpaper_dir_, | |
| 92 base::Unretained(success.get())); | |
| 93 base::Closure on_created_closure = | |
| 94 base::Bind(&CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated, | |
| 95 weak_factory_.GetWeakPtr(), | |
| 96 base::Passed(success.Pass())); | |
| 97 if (!content::BrowserThread::PostBlockingPoolTaskAndReply( | |
| 98 FROM_HERE, mkdir_closure, on_created_closure)) { | |
| 99 LOG(WARNING) << "Failed to start Customized Wallpaper download."; | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 // static | |
| 104 void CustomizationWallpaperDownloader::CreateWallpaperDirectory( | |
|
Daniel Erat
2014/04/14 15:26:15
private static method -> should probably be functi
Alexander Alekseev
2014/04/15 01:57:16
Done.
| |
| 105 const base::FilePath& wallpaper_dir, | |
| 106 bool* success) { | |
| 107 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 108 DCHECK(success); | |
| 109 | |
| 110 *success = CreateDirectoryAndGetError(wallpaper_dir, NULL); | |
| 111 } | |
| 112 | |
| 113 void CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated( | |
| 114 scoped_ptr<bool> success) { | |
| 115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 116 if (*success) | |
| 117 StartRequest(); | |
| 118 } | |
| 119 | |
| 120 void CustomizationWallpaperDownloader::OnURLFetchComplete( | |
| 121 const net::URLFetcher* source) { | |
| 122 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 123 DCHECK_EQ(url_fetcher_.get(), source); | |
| 124 | |
| 125 const net::URLRequestStatus status = source->GetStatus(); | |
| 126 const int response_code = source->GetResponseCode(); | |
| 127 | |
| 128 const bool server_error = | |
| 129 !status.is_success() || (response_code >= 500 && response_code < 600); | |
|
Daniel Erat
2014/04/14 15:26:15
are there constants anywhere for HTTP response cod
Alexander Alekseev
2014/04/15 01:57:16
I've replaced this with constants, but I don't kno
| |
| 130 | |
| 131 VLOG(1) << "CustomizationWallpaperDownloader::OnURLFetchComplete(): status=" | |
| 132 << status.status(); | |
| 133 | |
| 134 if (server_error) { | |
| 135 url_fetcher_.reset(); | |
| 136 Retry(); | |
| 137 return; | |
| 138 } | |
| 139 | |
| 140 base::FilePath response_path; | |
| 141 url_fetcher_->GetResponseAsFilePath(true, &response_path); | |
| 142 url_fetcher_.reset(); | |
| 143 | |
| 144 scoped_ptr<bool> success(new bool(false)); | |
| 145 | |
| 146 base::Closure rename_closure = | |
| 147 base::Bind(&CustomizationWallpaperDownloader::RenameTemporaryFile, | |
| 148 response_path, | |
| 149 wallpaper_downloaded_file_, | |
| 150 base::Unretained(success.get())); | |
| 151 base::Closure on_rename_closure = | |
| 152 base::Bind(&CustomizationWallpaperDownloader::OnTemporaryFileRenamed, | |
| 153 weak_factory_.GetWeakPtr(), | |
| 154 base::Passed(success.Pass())); | |
| 155 if (!content::BrowserThread::PostBlockingPoolTaskAndReply( | |
| 156 FROM_HERE, rename_closure, on_rename_closure)) { | |
| 157 LOG(WARNING) | |
| 158 << "Failed to start Customized Wallpaper Rename DownloadedFile."; | |
| 159 // Destroy self. | |
| 160 ServicesCustomizationDocument::GetInstance()->DestroyWallpaperDownloader(); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 void CustomizationWallpaperDownloader::OnTemporaryFileRenamed( | |
| 165 scoped_ptr<bool> success) { | |
| 166 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 167 if (!success) { | |
| 168 // Destroy self. | |
| 169 ServicesCustomizationDocument::GetInstance()->DestroyWallpaperDownloader(); | |
| 170 return; | |
| 171 } | |
| 172 on_url_fetched_.Run(wallpaper_url_); | |
| 173 } | |
| 174 | |
| 175 // static | |
| 176 void CustomizationWallpaperDownloader::RenameTemporaryFile( | |
|
Daniel Erat
2014/04/14 15:26:15
private static method -> should probably be functi
Alexander Alekseev
2014/04/15 01:57:16
Done.
| |
| 177 const base::FilePath& from, | |
| 178 const base::FilePath& to, | |
| 179 bool* success) { | |
| 180 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 181 DCHECK(success); | |
| 182 | |
| 183 base::File::Error error; | |
| 184 if (ReplaceFile(from, to, &error)) { | |
| 185 *success = true; | |
| 186 } else { | |
| 187 LOG(WARNING) | |
| 188 << "Failed to rename temporary file of Customized Wallpaper. error=" | |
| 189 << error; | |
| 190 *success = false; | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 } // namespace chromeos | |
| OLD | NEW |