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
;
21 enum class FlushPolicy
;
23 class STORAGE_EXPORT_PRIVATE FileWriterDelegate
24 : public net::URLRequest::Delegate
{
26 enum WriteProgressStatus
{
30 ERROR_WRITE_NOT_STARTED
,
33 typedef base::Callback
<void(base::File::Error result
,
35 WriteProgressStatus write_status
)>
36 DelegateWriteCallback
;
38 FileWriterDelegate(scoped_ptr
<FileStreamWriter
> file_writer
,
39 FlushPolicy flush_policy
);
40 ~FileWriterDelegate() override
;
42 void Start(scoped_ptr
<net::URLRequest
> request
,
43 const DelegateWriteCallback
& write_callback
);
45 // Cancels the current write operation. This will synchronously or
46 // asynchronously call the given write callback (which may result in
50 void OnReceivedRedirect(net::URLRequest
* request
,
51 const net::RedirectInfo
& redirect_info
,
52 bool* defer_redirect
) override
;
53 void OnAuthRequired(net::URLRequest
* request
,
54 net::AuthChallengeInfo
* auth_info
) override
;
55 void OnCertificateRequested(
56 net::URLRequest
* request
,
57 net::SSLCertRequestInfo
* cert_request_info
) override
;
58 void OnSSLCertificateError(net::URLRequest
* request
,
59 const net::SSLInfo
& ssl_info
,
61 void OnResponseStarted(net::URLRequest
* request
) override
;
62 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
;
65 void OnGetFileInfoAndStartRequest(
66 scoped_ptr
<net::URLRequest
> request
,
67 base::File::Error error
,
68 const base::File::Info
& file_info
);
70 void OnDataReceived(int bytes_read
);
72 void OnDataWritten(int write_response
);
73 void OnError(base::File::Error error
);
74 void OnProgress(int bytes_read
, bool done
);
75 void OnWriteCancelled(int status
);
76 void MaybeFlushForCompletion(base::File::Error error
,
78 WriteProgressStatus progress_status
);
79 void OnFlushed(base::File::Error error
,
81 WriteProgressStatus progress_status
,
84 WriteProgressStatus
GetCompletionStatusOnError() const;
86 DelegateWriteCallback write_callback_
;
87 scoped_ptr
<FileStreamWriter
> file_stream_writer_
;
88 base::Time last_progress_event_time_
;
89 bool writing_started_
;
90 FlushPolicy flush_policy_
;
91 int bytes_written_backlog_
;
94 scoped_refptr
<net::IOBufferWithSize
> io_buffer_
;
95 scoped_refptr
<net::DrainableIOBuffer
> cursor_
;
96 scoped_ptr
<net::URLRequest
> request_
;
98 base::WeakPtrFactory
<FileWriterDelegate
> weak_factory_
;
100 DISALLOW_COPY_AND_ASSIGN(FileWriterDelegate
);
103 } // namespace storage
105 #endif // STORAGE_BROWSER_FILEAPI_FILE_WRITER_DELEGATE_H_