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