| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/customization/customization_wallpaper_download
er.h" | 5 #include "chrome/browser/chromeos/customization/customization_wallpaper_download
er.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 | 9 |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 : url_context_getter_(url_context_getter), | 68 : url_context_getter_(url_context_getter), |
| 69 wallpaper_url_(wallpaper_url), | 69 wallpaper_url_(wallpaper_url), |
| 70 wallpaper_dir_(wallpaper_dir), | 70 wallpaper_dir_(wallpaper_dir), |
| 71 wallpaper_downloaded_file_(wallpaper_downloaded_file), | 71 wallpaper_downloaded_file_(wallpaper_downloaded_file), |
| 72 wallpaper_temporary_file_(wallpaper_downloaded_file.value() + | 72 wallpaper_temporary_file_(wallpaper_downloaded_file.value() + |
| 73 kTemporarySuffix), | 73 kTemporarySuffix), |
| 74 retries_(0), | 74 retries_(0), |
| 75 retry_delay_(base::TimeDelta::FromSeconds(kRetrySleepSeconds)), | 75 retry_delay_(base::TimeDelta::FromSeconds(kRetrySleepSeconds)), |
| 76 on_wallpaper_fetch_completed_(on_wallpaper_fetch_completed), | 76 on_wallpaper_fetch_completed_(on_wallpaper_fetch_completed), |
| 77 weak_factory_(this) { | 77 weak_factory_(this) { |
| 78 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 78 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 79 } | 79 } |
| 80 | 80 |
| 81 CustomizationWallpaperDownloader::~CustomizationWallpaperDownloader() { | 81 CustomizationWallpaperDownloader::~CustomizationWallpaperDownloader() { |
| 82 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void CustomizationWallpaperDownloader::StartRequest() { | 85 void CustomizationWallpaperDownloader::StartRequest() { |
| 86 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 86 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 87 DCHECK(wallpaper_url_.is_valid()); | 87 DCHECK(wallpaper_url_.is_valid()); |
| 88 | 88 |
| 89 url_fetcher_.reset( | 89 url_fetcher_.reset( |
| 90 net::URLFetcher::Create(wallpaper_url_, net::URLFetcher::GET, this)); | 90 net::URLFetcher::Create(wallpaper_url_, net::URLFetcher::GET, this)); |
| 91 url_fetcher_->SetRequestContext(url_context_getter_.get()); | 91 url_fetcher_->SetRequestContext(url_context_getter_.get()); |
| 92 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | | 92 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | |
| 93 net::LOAD_DISABLE_CACHE | | 93 net::LOAD_DISABLE_CACHE | |
| 94 net::LOAD_DO_NOT_SAVE_COOKIES | | 94 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 95 net::LOAD_DO_NOT_SEND_COOKIES | | 95 net::LOAD_DO_NOT_SEND_COOKIES | |
| 96 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 96 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| 97 base::SequencedWorkerPool* blocking_pool = | 97 base::SequencedWorkerPool* blocking_pool = |
| 98 content::BrowserThread::GetBlockingPool(); | 98 content::BrowserThread::GetBlockingPool(); |
| 99 url_fetcher_->SaveResponseToFileAtPath( | 99 url_fetcher_->SaveResponseToFileAtPath( |
| 100 wallpaper_temporary_file_, | 100 wallpaper_temporary_file_, |
| 101 blocking_pool->GetSequencedTaskRunner(blocking_pool->GetSequenceToken())); | 101 blocking_pool->GetSequencedTaskRunner(blocking_pool->GetSequenceToken())); |
| 102 url_fetcher_->Start(); | 102 url_fetcher_->Start(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void CustomizationWallpaperDownloader::Retry() { | 105 void CustomizationWallpaperDownloader::Retry() { |
| 106 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 107 ++retries_; | 107 ++retries_; |
| 108 | 108 |
| 109 const double delay_seconds = std::min( | 109 const double delay_seconds = std::min( |
| 110 kMaxRetrySleepSeconds, | 110 kMaxRetrySleepSeconds, |
| 111 static_cast<double>(retries_) * retries_ * retry_delay_.InSecondsF()); | 111 static_cast<double>(retries_) * retries_ * retry_delay_.InSecondsF()); |
| 112 const base::TimeDelta delay = base::TimeDelta::FromSecondsD(delay_seconds); | 112 const base::TimeDelta delay = base::TimeDelta::FromSecondsD(delay_seconds); |
| 113 | 113 |
| 114 VLOG(1) << "Schedule Customized Wallpaper download in " << delay.InSecondsF() | 114 VLOG(1) << "Schedule Customized Wallpaper download in " << delay.InSecondsF() |
| 115 << " seconds (retry = " << retries_ << ")."; | 115 << " seconds (retry = " << retries_ << ")."; |
| 116 retry_current_delay_ = delay; | 116 retry_current_delay_ = delay; |
| 117 request_scheduled_.Start( | 117 request_scheduled_.Start( |
| 118 FROM_HERE, delay, this, &CustomizationWallpaperDownloader::StartRequest); | 118 FROM_HERE, delay, this, &CustomizationWallpaperDownloader::StartRequest); |
| 119 } | 119 } |
| 120 | 120 |
| 121 void CustomizationWallpaperDownloader::Start() { | 121 void CustomizationWallpaperDownloader::Start() { |
| 122 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 122 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 123 scoped_ptr<bool> success(new bool(false)); | 123 scoped_ptr<bool> success(new bool(false)); |
| 124 | 124 |
| 125 base::Closure mkdir_closure = base::Bind(&CreateWallpaperDirectory, | 125 base::Closure mkdir_closure = base::Bind(&CreateWallpaperDirectory, |
| 126 wallpaper_dir_, | 126 wallpaper_dir_, |
| 127 base::Unretained(success.get())); | 127 base::Unretained(success.get())); |
| 128 base::Closure on_created_closure = | 128 base::Closure on_created_closure = |
| 129 base::Bind(&CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated, | 129 base::Bind(&CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated, |
| 130 weak_factory_.GetWeakPtr(), | 130 weak_factory_.GetWeakPtr(), |
| 131 base::Passed(success.Pass())); | 131 base::Passed(success.Pass())); |
| 132 if (!content::BrowserThread::PostBlockingPoolTaskAndReply( | 132 if (!content::BrowserThread::PostBlockingPoolTaskAndReply( |
| 133 FROM_HERE, mkdir_closure, on_created_closure)) { | 133 FROM_HERE, mkdir_closure, on_created_closure)) { |
| 134 LOG(WARNING) << "Failed to start Customized Wallpaper download."; | 134 LOG(WARNING) << "Failed to start Customized Wallpaper download."; |
| 135 } | 135 } |
| 136 } | 136 } |
| 137 | 137 |
| 138 void CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated( | 138 void CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated( |
| 139 scoped_ptr<bool> success) { | 139 scoped_ptr<bool> success) { |
| 140 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 140 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 141 if (*success) | 141 if (*success) |
| 142 StartRequest(); | 142 StartRequest(); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void CustomizationWallpaperDownloader::OnURLFetchComplete( | 145 void CustomizationWallpaperDownloader::OnURLFetchComplete( |
| 146 const net::URLFetcher* source) { | 146 const net::URLFetcher* source) { |
| 147 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 148 DCHECK_EQ(url_fetcher_.get(), source); | 148 DCHECK_EQ(url_fetcher_.get(), source); |
| 149 | 149 |
| 150 const net::URLRequestStatus status = source->GetStatus(); | 150 const net::URLRequestStatus status = source->GetStatus(); |
| 151 const int response_code = source->GetResponseCode(); | 151 const int response_code = source->GetResponseCode(); |
| 152 | 152 |
| 153 const bool server_error = | 153 const bool server_error = |
| 154 !status.is_success() || | 154 !status.is_success() || |
| 155 (response_code >= net::HTTP_INTERNAL_SERVER_ERROR && | 155 (response_code >= net::HTTP_INTERNAL_SERVER_ERROR && |
| 156 response_code < (net::HTTP_INTERNAL_SERVER_ERROR + 100)); | 156 response_code < (net::HTTP_INTERNAL_SERVER_ERROR + 100)); |
| 157 | 157 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 181 if (!content::BrowserThread::PostBlockingPoolTaskAndReply( | 181 if (!content::BrowserThread::PostBlockingPoolTaskAndReply( |
| 182 FROM_HERE, rename_closure, on_rename_closure)) { | 182 FROM_HERE, rename_closure, on_rename_closure)) { |
| 183 LOG(WARNING) | 183 LOG(WARNING) |
| 184 << "Failed to start Customized Wallpaper Rename DownloadedFile."; | 184 << "Failed to start Customized Wallpaper Rename DownloadedFile."; |
| 185 on_wallpaper_fetch_completed_.Run(false, wallpaper_url_); | 185 on_wallpaper_fetch_completed_.Run(false, wallpaper_url_); |
| 186 } | 186 } |
| 187 } | 187 } |
| 188 | 188 |
| 189 void CustomizationWallpaperDownloader::OnTemporaryFileRenamed( | 189 void CustomizationWallpaperDownloader::OnTemporaryFileRenamed( |
| 190 scoped_ptr<bool> success) { | 190 scoped_ptr<bool> success) { |
| 191 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 191 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 192 on_wallpaper_fetch_completed_.Run(*success, wallpaper_url_); | 192 on_wallpaper_fetch_completed_.Run(*success, wallpaper_url_); |
| 193 } | 193 } |
| 194 | 194 |
| 195 } // namespace chromeos | 195 } // namespace chromeos |
| OLD | NEW |