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.
5 #ifndef CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_CUSTOMIZATION_WALLPAPER_DOWNLOADER_H_
6 #define CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_CUSTOMIZATION_WALLPAPER_DOWNLOADER_H_
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"
20 class URLRequestContextGetter
;
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
{
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
);
47 ~CustomizationWallpaperDownloader() override
;
52 // net::URLFetcherDelegate
53 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
55 // This is called in tests to modify (lower) retry delay.
56 void set_retry_delay_for_testing(base::TimeDelta value
) {
60 base::TimeDelta
retry_current_delay_for_testing() const {
61 return retry_current_delay_
;
71 // Called on UI thread.
72 void OnWallpaperDirectoryCreated(scoped_ptr
<bool> success
);
74 // Called on UI thread.
75 void OnTemporaryFileRenamed(scoped_ptr
<bool> success
);
77 // This is used to initialize net::URLFetcher object.
78 scoped_refptr
<net::URLRequestContextGetter
> url_context_getter_
;
80 // This fetcher is used to download wallpaper file.
81 scoped_ptr
<net::URLFetcher
> url_fetcher_
;
83 // The wallpaper URL to fetch.
84 const GURL wallpaper_url_
;
86 // Wallpaper directory (to be created).
87 const base::FilePath wallpaper_dir_
;
89 // Full path to local file to save downloaded wallpaper.
90 const base::FilePath wallpaper_downloaded_file_
;
92 // Full path to temporary file to fetch downloaded wallpper.
93 const base::FilePath wallpaper_temporary_file_
;
96 base::OneShotTimer
<CustomizationWallpaperDownloader
> request_scheduled_
;
98 // Number of download retries (first attempt is not counted as retry).
101 // Sleep between retry requests (increasing, see Retry() method for details).
102 // Non-constant value for tests.
103 base::TimeDelta retry_delay_
;
105 // Retry delay of the last attempt. For testing only.
106 base::TimeDelta retry_current_delay_
;
108 // Callback supplied by caller.
109 base::Callback
<void(bool success
, const GURL
&)> on_wallpaper_fetch_completed_
;
111 base::WeakPtrFactory
<CustomizationWallpaperDownloader
> weak_factory_
;
113 DISALLOW_COPY_AND_ASSIGN(CustomizationWallpaperDownloader
);
116 } // namespace chromeos
118 #endif // CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_CUSTOMIZATION_WALLPAPER_DOWNLOADER_H_