Home | History | Annotate | Download | only in fileapi
      1 // Copyright (c) 2012 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 WEBKIT_BROWSER_FILEAPI_FILE_WRITER_DELEGATE_H_
      6 #define WEBKIT_BROWSER_FILEAPI_FILE_WRITER_DELEGATE_H_
      7 
      8 #include "base/files/file_path.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/memory/weak_ptr.h"
     11 #include "base/platform_file.h"
     12 #include "base/time/time.h"
     13 #include "net/base/file_stream.h"
     14 #include "net/base/io_buffer.h"
     15 #include "net/url_request/url_request.h"
     16 #include "webkit/browser/webkit_storage_browser_export.h"
     17 
     18 namespace fileapi {
     19 
     20 class FileStreamWriter;
     21 
     22 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE FileWriterDelegate
     23     : public net::URLRequest::Delegate {
     24  public:
     25   enum WriteProgressStatus {
     26     SUCCESS_IO_PENDING,
     27     SUCCESS_COMPLETED,
     28     ERROR_WRITE_STARTED,
     29     ERROR_WRITE_NOT_STARTED,
     30   };
     31 
     32   typedef base::Callback<void(base::PlatformFileError result,
     33                               int64 bytes,
     34                               WriteProgressStatus write_status)>
     35       DelegateWriteCallback;
     36 
     37   FileWriterDelegate(scoped_ptr<FileStreamWriter> file_writer);
     38   virtual ~FileWriterDelegate();
     39 
     40   void Start(scoped_ptr<net::URLRequest> request,
     41              const DelegateWriteCallback& write_callback);
     42 
     43   // Cancels the current write operation.  This will synchronously or
     44   // asynchronously call the given write callback (which may result in
     45   // deleting this).
     46   void Cancel();
     47 
     48   virtual void OnReceivedRedirect(net::URLRequest* request,
     49                                   const GURL& new_url,
     50                                   bool* defer_redirect) OVERRIDE;
     51   virtual void OnAuthRequired(net::URLRequest* request,
     52                               net::AuthChallengeInfo* auth_info) OVERRIDE;
     53   virtual void OnCertificateRequested(
     54       net::URLRequest* request,
     55       net::SSLCertRequestInfo* cert_request_info) OVERRIDE;
     56   virtual void OnSSLCertificateError(net::URLRequest* request,
     57                                      const net::SSLInfo& ssl_info,
     58                                      bool fatal) OVERRIDE;
     59   virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
     60   virtual void OnReadCompleted(net::URLRequest* request,
     61                                int bytes_read) OVERRIDE;
     62 
     63  private:
     64   void OnGetFileInfoAndStartRequest(
     65       scoped_ptr<net::URLRequest> request,
     66       base::PlatformFileError error,
     67       const base::PlatformFileInfo& file_info);
     68   void Read();
     69   void OnDataReceived(int bytes_read);
     70   void Write();
     71   void OnDataWritten(int write_response);
     72   void OnError(base::PlatformFileError error);
     73   void OnProgress(int bytes_read, bool done);
     74   void OnWriteCancelled(int status);
     75   void FlushForCompletion(base::PlatformFileError error,
     76                           int bytes_written,
     77                           WriteProgressStatus progress_status);
     78   void OnFlushed(base::PlatformFileError error,
     79                  int bytes_written,
     80                  WriteProgressStatus progress_status,
     81                  int flush_error);
     82 
     83   WriteProgressStatus GetCompletionStatusOnError() const;
     84 
     85   DelegateWriteCallback write_callback_;
     86   scoped_ptr<FileStreamWriter> file_stream_writer_;
     87   base::Time last_progress_event_time_;
     88   bool writing_started_;
     89   int bytes_written_backlog_;
     90   int bytes_written_;
     91   int bytes_read_;
     92   scoped_refptr<net::IOBufferWithSize> io_buffer_;
     93   scoped_refptr<net::DrainableIOBuffer> cursor_;
     94   scoped_ptr<net::URLRequest> request_;
     95 
     96   base::WeakPtrFactory<FileWriterDelegate> weak_factory_;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(FileWriterDelegate);
     99 };
    100 
    101 }  // namespace fileapi
    102 
    103 #endif  // WEBKIT_BROWSER_FILEAPI_FILE_WRITER_DELEGATE_H_
    104