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 #ifndef CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_WALLPAPER_DOWNLOADER_H_ 6 #define CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_WALLPAPER_DOWNLOADER_H_ 7 8 #include <string> 9 10 #include "base/bind.h" 11 #include "base/files/file_path.h" 12 #include "base/macros.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/time/time.h" 15 #include "base/timer/timer.h" 16 #include "net/url_request/url_fetcher_delegate.h" 17 #include "url/gurl.h" 18 19 namespace net { 20 class URLRequestContextGetter; 21 } // namespace net 22 23 namespace chromeos { 24 25 // Download customized wallpaper. 26 // Owner of this class must provide callback, which will be called on 27 // finished (either successful or failed) wallpaper download. 28 class CustomizationWallpaperDownloader : public net::URLFetcherDelegate { 29 public: 30 // - |url_context_getter| - Context to initialize net::URLFetcher. 31 // - |wallpaper_url| - wallpaper URL to download. 32 // - |wallpaper_dir| - directory, where wallpaper will be downloaded 33 // (it will be created). 34 // - |wallpaper_downloaded_file| - full path to local file to store downloaded 35 // wallpaper file. File is downloaded to temporary location 36 // |wallpaper_downloaded_file| + ".tmp", so directory must be writable. 37 // After download is completed, temporary file will be renamed to 38 // |wallpaper_downloaded_file|. 39 CustomizationWallpaperDownloader( 40 net::URLRequestContextGetter* url_context_getter, 41 const GURL& wallpaper_url, 42 const base::FilePath& wallpaper_dir, 43 const base::FilePath& wallpaper_downloaded_file, 44 base::Callback<void(bool success, const GURL&)> 45 on_wallpaper_fetch_completed); 46 47 virtual ~CustomizationWallpaperDownloader(); 48 49 // Start download. 50 void Start(); 51 52 // net::URLFetcherDelegate 53 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 54 55 // This is called in tests to modify (lower) retry delay. set_retry_delay_for_testing(base::TimeDelta value)56 void set_retry_delay_for_testing(base::TimeDelta value) { 57 retry_delay_ = value; 58 } 59 retry_current_delay_for_testing()60 base::TimeDelta retry_current_delay_for_testing() const { 61 return retry_current_delay_; 62 } 63 64 private: 65 // Start new request. 66 void StartRequest(); 67 68 // Schedules retry. 69 void Retry(); 70 71 // Called on UI thread. 72 void OnWallpaperDirectoryCreated(scoped_ptr<bool> success); 73 74 // Called on UI thread. 75 void OnTemporaryFileRenamed(scoped_ptr<bool> success); 76 77 // This is used to initialize net::URLFetcher object. 78 scoped_refptr<net::URLRequestContextGetter> url_context_getter_; 79 80 // This fetcher is used to download wallpaper file. 81 scoped_ptr<net::URLFetcher> url_fetcher_; 82 83 // The wallpaper URL to fetch. 84 const GURL wallpaper_url_; 85 86 // Wallpaper directory (to be created). 87 const base::FilePath wallpaper_dir_; 88 89 // Full path to local file to save downloaded wallpaper. 90 const base::FilePath wallpaper_downloaded_file_; 91 92 // Full path to temporary file to fetch downloaded wallpper. 93 const base::FilePath wallpaper_temporary_file_; 94 95 // Pending retry. 96 base::OneShotTimer<CustomizationWallpaperDownloader> request_scheduled_; 97 98 // Number of download retries (first attempt is not counted as retry). 99 size_t retries_; 100 101 // Sleep between retry requests (increasing, see Retry() method for details). 102 // Non-constant value for tests. 103 base::TimeDelta retry_delay_; 104 105 // Retry delay of the last attempt. For testing only. 106 base::TimeDelta retry_current_delay_; 107 108 // Callback supplied by caller. 109 base::Callback<void(bool success, const GURL&)> on_wallpaper_fetch_completed_; 110 111 base::WeakPtrFactory<CustomizationWallpaperDownloader> weak_factory_; 112 113 DISALLOW_COPY_AND_ASSIGN(CustomizationWallpaperDownloader); 114 }; 115 116 } // namespace chromeos 117 118 #endif // CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_WALLPAPER_DOWNLOADER_H_ 119