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.
5 #ifndef STORAGE_BROWSER_FILEAPI_FILE_WRITER_DELEGATE_H_
6 #define STORAGE_BROWSER_FILEAPI_FILE_WRITER_DELEGATE_H_
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"
20 class FileStreamWriter
;
22 class STORAGE_EXPORT_PRIVATE FileWriterDelegate
23 : public net::URLRequest::Delegate
{
27 NO_FLUSH_ON_COMPLETION
,
30 enum WriteProgressStatus
{
34 ERROR_WRITE_NOT_STARTED
,
37 typedef base::Callback
<void(base::File::Error result
,
39 WriteProgressStatus write_status
)>
40 DelegateWriteCallback
;
42 FileWriterDelegate(scoped_ptr
<FileStreamWriter
> file_writer
,
43 FlushPolicy flush_policy
);
44 virtual ~FileWriterDelegate();
46 void Start(scoped_ptr
<net::URLRequest
> request
,
47 const DelegateWriteCallback
& write_callback
);
49 // Cancels the current write operation. This will synchronously or
50 // asynchronously call the given write callback (which may result in
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
,
65 virtual void OnResponseStarted(net::URLRequest
* request
) OVERRIDE
;
66 virtual void OnReadCompleted(net::URLRequest
* request
,
67 int bytes_read
) OVERRIDE
;
70 void OnGetFileInfoAndStartRequest(
71 scoped_ptr
<net::URLRequest
> request
,
72 base::File::Error error
,
73 const base::File::Info
& file_info
);
75 void OnDataReceived(int bytes_read
);
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
,
83 WriteProgressStatus progress_status
);
84 void OnFlushed(base::File::Error error
,
86 WriteProgressStatus progress_status
,
89 WriteProgressStatus
GetCompletionStatusOnError() const;
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_
;
99 scoped_refptr
<net::IOBufferWithSize
> io_buffer_
;
100 scoped_refptr
<net::DrainableIOBuffer
> cursor_
;
101 scoped_ptr
<net::URLRequest
> request_
;
103 base::WeakPtrFactory
<FileWriterDelegate
> weak_factory_
;
105 DISALLOW_COPY_AND_ASSIGN(FileWriterDelegate
);
108 } // namespace storage
110 #endif // STORAGE_BROWSER_FILEAPI_FILE_WRITER_DELEGATE_H_