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