Home | History | Annotate | Download | only in image_writer_private
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_WRITE_FROM_URL_OPERATION_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_WRITE_FROM_URL_OPERATION_H_
      7 
      8 #include "chrome/browser/extensions/api/image_writer_private/operation.h"
      9 #include "net/url_request/url_fetcher_delegate.h"
     10 #include "url/gurl.h"
     11 
     12 namespace net {
     13 class URLFetcher;
     14 class URLRequestContextGetter;
     15 }  // namespace net
     16 
     17 namespace extensions {
     18 namespace image_writer {
     19 
     20 class OperationManager;
     21 
     22 // Encapsulates a write of an image accessed via URL.
     23 class WriteFromUrlOperation : public Operation, public net::URLFetcherDelegate {
     24  public:
     25   WriteFromUrlOperation(base::WeakPtr<OperationManager> manager,
     26                         const ExtensionId& extension_id,
     27                         net::URLRequestContextGetter* request_context,
     28                         GURL url,
     29                         const std::string& hash,
     30                         const std::string& storage_unit_id);
     31   virtual void StartImpl() OVERRIDE;
     32 
     33  protected:
     34   virtual ~WriteFromUrlOperation();
     35 
     36   // Sets the image_path to the correct location to download to.
     37   void GetDownloadTarget(const base::Closure& continuation);
     38 
     39   // Downloads the |url| to the currently configured |image_path|.  Should not
     40   // be called without calling |GetDownloadTarget| first.
     41   void Download(const base::Closure& continuation);
     42 
     43   // Verifies the download matches |hash|.  If the hash is empty, this stage is
     44   // skipped.
     45   void VerifyDownload(const base::Closure& continuation);
     46 
     47  private:
     48   // Destroys the URLFetcher.  The URLFetcher needs to be destroyed on the same
     49   // thread it was created on.  The Operation may be deleted on the UI thread
     50   // and so we must first delete the URLFetcher on the FILE thread.
     51   void DestroyUrlFetcher();
     52 
     53   // URLFetcherDelegate implementation.
     54   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
     55   virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source,
     56                                           int64 current,
     57                                           int64 total) OVERRIDE;
     58   virtual void OnURLFetchUploadProgress(const net::URLFetcher* source,
     59                                         int64 current,
     60                                         int64 total) OVERRIDE;
     61 
     62   void VerifyDownloadCompare(const base::Closure& continuation,
     63                              const std::string& download_hash);
     64   void VerifyDownloadComplete(const base::Closure& continuation);
     65 
     66   // Arguments
     67   net::URLRequestContextGetter* request_context_;
     68   GURL url_;
     69   const std::string hash_;
     70 
     71   // Local state
     72   scoped_ptr<net::URLFetcher> url_fetcher_;
     73   base::Closure download_continuation_;
     74 };
     75 
     76 } // namespace image_writer
     77 } // namespace extensions
     78 
     79 #endif  // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_WRITE_FROM_URL_OPERATION_H_
     80