| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_WRITE_FROM_URL_OPERAT
ION_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_WRITE_FROM_URL_OPERAT
ION_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_WRITE_FROM_URL_OPERAT
ION_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_WRITE_FROM_URL_OPERAT
ION_H_ |
| 7 | 7 |
| 8 #include "base/scoped_observer.h" | |
| 9 #include "chrome/browser/extensions/api/image_writer_private/operation.h" | 8 #include "chrome/browser/extensions/api/image_writer_private/operation.h" |
| 10 #include "content/public/browser/download_interrupt_reasons.h" | 9 #include "net/url_request/url_fetcher_delegate.h" |
| 11 #include "content/public/browser/download_item.h" | |
| 12 #include "url/gurl.h" | 10 #include "url/gurl.h" |
| 13 | 11 |
| 14 namespace content { | 12 namespace net { |
| 15 | 13 class URLFetcher; |
| 16 class RenderViewHost; | 14 } // namespace net |
| 17 | |
| 18 } // namespace content | |
| 19 | 15 |
| 20 namespace extensions { | 16 namespace extensions { |
| 21 namespace image_writer { | 17 namespace image_writer { |
| 22 | 18 |
| 23 class OperationManager; | 19 class OperationManager; |
| 24 | 20 |
| 25 // Encapsulates a write of an image accessed via URL. | 21 // Encapsulates a write of an image accessed via URL. |
| 26 class WriteFromUrlOperation : public Operation, | 22 class WriteFromUrlOperation : public Operation, public net::URLFetcherDelegate { |
| 27 public content::DownloadItem::Observer { | |
| 28 public: | 23 public: |
| 29 WriteFromUrlOperation(base::WeakPtr<OperationManager> manager, | 24 WriteFromUrlOperation(base::WeakPtr<OperationManager> manager, |
| 30 const ExtensionId& extension_id, | 25 const ExtensionId& extension_id, |
| 31 content::RenderViewHost* rvh, | 26 net::URLRequestContextGetter* request_context, |
| 32 GURL url, | 27 GURL url, |
| 33 const std::string& hash, | 28 const std::string& hash, |
| 34 bool saveImageAsDownload, | |
| 35 const std::string& storage_unit_id); | 29 const std::string& storage_unit_id); |
| 36 virtual void Start() OVERRIDE; | 30 virtual void StartImpl() OVERRIDE; |
| 31 |
| 32 protected: |
| 33 virtual ~WriteFromUrlOperation(); |
| 34 |
| 35 // Sets the image_path to the correct location to download to. |
| 36 void GetDownloadTarget(const base::Closure& continuation); |
| 37 |
| 38 // Downloads the |url| to the currently configured |image_path|. Should not |
| 39 // be called without calling |GetDownloadTarget| first. |
| 40 void Download(const base::Closure& continuation); |
| 41 |
| 42 // Verifies the download matches |hash|. If the hash is empty, this stage is |
| 43 // skipped. |
| 44 void VerifyDownload(const base::Closure& continuation); |
| 45 |
| 37 private: | 46 private: |
| 38 virtual ~WriteFromUrlOperation(); | 47 // Destroys the URLFetcher. The URLFetcher needs to be destroyed on the same |
| 39 void CreateTempFile(); | 48 // thread it was created on. The Operation may be deleted on the UI thread |
| 49 // and so we must first delete the URLFetcher on the FILE thread. |
| 50 void DestroyUrlFetcher(); |
| 40 | 51 |
| 41 void DownloadStart(); | 52 // URLFetcherDelegate implementation. |
| 42 void OnDownloadStarted(content::DownloadItem*, | 53 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 43 content::DownloadInterruptReason interrupt_reason); | 54 virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source, |
| 44 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; | 55 int64 current, |
| 45 void DownloadComplete(); | 56 int64 total) OVERRIDE; |
| 46 void DownloadCleanUp(); | 57 virtual void OnURLFetchUploadProgress(const net::URLFetcher* source, |
| 58 int64 current, |
| 59 int64 total) OVERRIDE; |
| 47 | 60 |
| 48 void VerifyDownloadStart(); | 61 void VerifyDownloadCompare(const base::Closure& continuation, |
| 49 void VerifyDownloadRun(); | 62 const std::string& download_hash); |
| 50 void VerifyDownloadCompare(scoped_ptr<std::string> download_hash); | 63 void VerifyDownloadComplete(const base::Closure& continuation); |
| 51 void VerifyDownloadComplete(); | |
| 52 | 64 |
| 53 // Arguments | 65 // Arguments |
| 54 content::RenderViewHost* rvh_; | 66 net::URLRequestContextGetter* request_context_; |
| 55 GURL url_; | 67 GURL url_; |
| 56 const std::string hash_; | 68 const std::string hash_; |
| 57 const bool saveImageAsDownload_; | |
| 58 | 69 |
| 59 // Local state | 70 // Local state |
| 60 scoped_ptr<base::FilePath> tmp_file_; | 71 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 61 bool download_stopped_; | 72 base::Closure download_continuation_; |
| 62 content::DownloadItem* download_; | |
| 63 base::FilePath download_path_; | |
| 64 }; | 73 }; |
| 65 | 74 |
| 66 } // namespace image_writer | 75 } // namespace image_writer |
| 67 } // namespace extensions | 76 } // namespace extensions |
| 68 | 77 |
| 69 #endif // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_WRITE_FROM_URL_OPE
RATION_H_ | 78 #endif // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_WRITE_FROM_URL_OPE
RATION_H_ |
| OLD | NEW |