| 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 "chrome/browser/chromeos/customization_document.h" | 
|  | 9 #include "content/public/browser/browser_thread.h" | 
|  | 10 #include "net/base/load_flags.h" | 
|  | 11 #include "net/url_request/url_request_context_getter.h" | 
|  | 12 #include "url/gurl.h" | 
|  | 13 | 
|  | 14 namespace { | 
|  | 15 // This is temporary file suffix (for downloading or resizing). | 
|  | 16 const char kTemporarySuffix[] = ".temp"; | 
|  | 17 | 
|  | 18 // Sleep between wallpaper retries (used multiplied by retry number). | 
|  | 19 const unsigned kRetrySleepSeconds = 10; | 
|  | 20 | 
|  | 21 // Do not increase retry sleep beyond this number of attempts. | 
|  | 22 const unsigned kRetriesInfinite = 30; | 
|  | 23 } | 
|  | 24 | 
|  | 25 namespace chromeos { | 
|  | 26 | 
|  | 27 CustomizationWallpaperDownloader::CustomizationWallpaperDownloader( | 
|  | 28     net::URLRequestContextGetter* url_context_getter, | 
|  | 29     const GURL& wallpaper_url, | 
|  | 30     const base::FilePath& wallpaper_dir, | 
|  | 31     const base::FilePath& wallpaper_downloaded_file) | 
|  | 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       weak_factory_(this) { | 
|  | 40   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 
|  | 41 } | 
|  | 42 | 
|  | 43 CustomizationWallpaperDownloader::~CustomizationWallpaperDownloader() { | 
|  | 44   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 
|  | 45 } | 
|  | 46 | 
|  | 47 void CustomizationWallpaperDownloader::StartRequest() { | 
|  | 48   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 
|  | 49   DCHECK(wallpaper_url_.is_valid()); | 
|  | 50   ++retries_; | 
|  | 51 | 
|  | 52   url_fetcher_.reset( | 
|  | 53       net::URLFetcher::Create(wallpaper_url_, net::URLFetcher::GET, this)); | 
|  | 54   url_fetcher_->SetRequestContext(url_context_getter_); | 
|  | 55   url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | | 
|  | 56                              net::LOAD_DO_NOT_SAVE_COOKIES | | 
|  | 57                              net::LOAD_DO_NOT_SEND_COOKIES | | 
|  | 58                              net::LOAD_DO_NOT_SEND_AUTH_DATA); | 
|  | 59   base::SequencedWorkerPool* blocking_pool = | 
|  | 60       content::BrowserThread::GetBlockingPool(); | 
|  | 61   url_fetcher_->SaveResponseToFileAtPath( | 
|  | 62       wallpaper_temporary_file_, | 
|  | 63       blocking_pool->GetSequencedTaskRunner(blocking_pool->GetSequenceToken())); | 
|  | 64   url_fetcher_->Start(); | 
|  | 65 } | 
|  | 66 | 
|  | 67 void CustomizationWallpaperDownloader::Retry() { | 
|  | 68   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 
|  | 69   const unsigned delay_multiplier = | 
|  | 70       (retries_ > kRetriesInfinite ? kRetriesInfinite : retries_); | 
|  | 71   const base::TimeDelta delay = | 
|  | 72       base::TimeDelta::FromSeconds(kRetrySleepSeconds) * delay_multiplier; | 
|  | 73   VLOG(1) << "Schedule Customized Wallpaper download in " << delay.InSecondsF() | 
|  | 74           << " seconds."; | 
|  | 75   request_scheduled_.Start( | 
|  | 76       FROM_HERE, delay, this, &CustomizationWallpaperDownloader::StartRequest); | 
|  | 77 } | 
|  | 78 | 
|  | 79 void CustomizationWallpaperDownloader::Start() { | 
|  | 80   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 
|  | 81   scoped_ptr<bool> success(new bool(false)); | 
|  | 82 | 
|  | 83   base::Closure mkdir_closure = | 
|  | 84       base::Bind(&CustomizationWallpaperDownloader::CreateWallpaperDirectory, | 
|  | 85                  wallpaper_dir_, | 
|  | 86                  base::Unretained(success.get())); | 
|  | 87   base::Closure on_created_closure = | 
|  | 88       base::Bind(&CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated, | 
|  | 89                  weak_factory_.GetWeakPtr(), | 
|  | 90                  base::Passed(success.Pass())); | 
|  | 91   if (!content::BrowserThread::PostBlockingPoolTaskAndReply( | 
|  | 92           FROM_HERE, mkdir_closure, on_created_closure)) { | 
|  | 93     LOG(WARNING) << "Failed to start Customized Wallpaper download."; | 
|  | 94   } | 
|  | 95 } | 
|  | 96 | 
|  | 97 // static | 
|  | 98 void CustomizationWallpaperDownloader::CreateWallpaperDirectory( | 
|  | 99     const base::FilePath& wallpaper_dir, | 
|  | 100     bool* success) { | 
|  | 101   DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 
|  | 102   DCHECK(success); | 
|  | 103 | 
|  | 104   *success = CreateDirectoryAndGetError(wallpaper_dir, NULL); | 
|  | 105 } | 
|  | 106 | 
|  | 107 void CustomizationWallpaperDownloader::OnWallpaperDirectoryCreated( | 
|  | 108     scoped_ptr<bool> success) { | 
|  | 109   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 
|  | 110   if (*success) | 
|  | 111     StartRequest(); | 
|  | 112 } | 
|  | 113 | 
|  | 114 void CustomizationWallpaperDownloader::OnURLFetchComplete( | 
|  | 115     const net::URLFetcher* source) { | 
|  | 116   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 
|  | 117   DCHECK_EQ(url_fetcher_.get(), source); | 
|  | 118 | 
|  | 119   const net::URLRequestStatus status = source->GetStatus(); | 
|  | 120   const int response_code = source->GetResponseCode(); | 
|  | 121 | 
|  | 122   const bool server_error = | 
|  | 123       !status.is_success() || (response_code >= 500 && response_code < 600); | 
|  | 124 | 
|  | 125   VLOG(1) << "CustomizationWallpaperDownloader::OnURLFetchComplete(): status=" | 
|  | 126           << status.status(); | 
|  | 127 | 
|  | 128   if (server_error) { | 
|  | 129     url_fetcher_.reset(); | 
|  | 130     Retry(); | 
|  | 131     return; | 
|  | 132   } | 
|  | 133 | 
|  | 134   base::FilePath response_path; | 
|  | 135   url_fetcher_->GetResponseAsFilePath(true, &response_path); | 
|  | 136   url_fetcher_.reset(); | 
|  | 137 | 
|  | 138   scoped_ptr<bool> success(new bool(false)); | 
|  | 139 | 
|  | 140   base::Closure rename_closure = | 
|  | 141       base::Bind(&CustomizationWallpaperDownloader::RenameTemporaryFile, | 
|  | 142                  response_path, | 
|  | 143                  wallpaper_downloaded_file_, | 
|  | 144                  base::Unretained(success.get())); | 
|  | 145   base::Closure on_rename_closure = | 
|  | 146       base::Bind(&CustomizationWallpaperDownloader::OnTemporaryFileRenamed, | 
|  | 147                  weak_factory_.GetWeakPtr(), | 
|  | 148                  base::Passed(success.Pass())); | 
|  | 149   if (!content::BrowserThread::PostBlockingPoolTaskAndReply( | 
|  | 150           FROM_HERE, rename_closure, on_rename_closure)) { | 
|  | 151     LOG(WARNING) | 
|  | 152         << "Failed to start Customized Wallpaper Rename DownloadedFile."; | 
|  | 153     // Destroy self. | 
|  | 154     ServicesCustomizationDocument::GetInstance()->DestroyWallpaperDownloader(); | 
|  | 155   } | 
|  | 156 } | 
|  | 157 | 
|  | 158 void CustomizationWallpaperDownloader::OnTemporaryFileRenamed( | 
|  | 159     scoped_ptr<bool> success) { | 
|  | 160   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 
|  | 161   if (!success) { | 
|  | 162     // Destroy self. | 
|  | 163     ServicesCustomizationDocument::GetInstance()->DestroyWallpaperDownloader(); | 
|  | 164     return; | 
|  | 165   } | 
|  | 166   ServicesCustomizationDocument::OnCustomizedWallpaperDownloaded( | 
|  | 167       wallpaper_url_); | 
|  | 168 } | 
|  | 169 | 
|  | 170 // static | 
|  | 171 void CustomizationWallpaperDownloader::RenameTemporaryFile( | 
|  | 172     const base::FilePath& from, | 
|  | 173     const base::FilePath& to, | 
|  | 174     bool* success) { | 
|  | 175   DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 
|  | 176   DCHECK(success); | 
|  | 177 | 
|  | 178   base::File::Error error; | 
|  | 179   if (ReplaceFile(from, to, &error)) { | 
|  | 180     *success = true; | 
|  | 181   } else { | 
|  | 182     LOG(WARNING) | 
|  | 183         << "Failed to rename temporary file of Customized Wallpaper. error=" | 
|  | 184         << error; | 
|  | 185     *success = false; | 
|  | 186   } | 
|  | 187 } | 
|  | 188 | 
|  | 189 }  //   namespace chromeos | 
| OLD | NEW | 
|---|