customization_wallpaper_downloader.h revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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 19namespace net { 20class URLRequestContextGetter; 21} // namespace net 22 23namespace 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. 28class 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 private: 56 // Start new request. 57 void StartRequest(); 58 59 // Schedules retry. 60 void Retry(); 61 62 // Called on UI thread. 63 void OnWallpaperDirectoryCreated(scoped_ptr<bool> success); 64 65 // Called on UI thread. 66 void OnTemporaryFileRenamed(scoped_ptr<bool> success); 67 68 // This is used to initialize net::URLFetcher object. 69 scoped_refptr<net::URLRequestContextGetter> url_context_getter_; 70 71 // This fetcher is used to download wallpaper file. 72 scoped_ptr<net::URLFetcher> url_fetcher_; 73 74 // The wallpaper URL to fetch. 75 const GURL wallpaper_url_; 76 77 // Wallpaper directory (to be created). 78 const base::FilePath wallpaper_dir_; 79 80 // Full path to local file to save downloaded wallpaper. 81 const base::FilePath wallpaper_downloaded_file_; 82 83 // Full path to temporary file to fetch downloaded wallpper. 84 const base::FilePath wallpaper_temporary_file_; 85 86 // Pending retry. 87 base::OneShotTimer<CustomizationWallpaperDownloader> request_scheduled_; 88 89 // Number of download retries (first attempt is not counted as retry). 90 size_t retries_; 91 92 // Callback supplied by caller. 93 base::Callback<void(bool success, const GURL&)> on_wallpaper_fetch_completed_; 94 95 base::WeakPtrFactory<CustomizationWallpaperDownloader> weak_factory_; 96 97 DISALLOW_COPY_AND_ASSIGN(CustomizationWallpaperDownloader); 98}; 99 100} // namespace chromeos 101 102#endif // CHROME_BROWSER_CHROMEOS_CUSTOMIZATION_WALLPAPER_DOWNLOADER_H_ 103