blob: a4573eae6d53746ccf373946dbcc8e0ea6e156fd [file] [log] [blame]
[email protected]6db833d12012-01-21 00:45:191// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#ifndef NET_BASE_UPLOAD_DATA_STREAM_H_
6#define NET_BASE_UPLOAD_DATA_STREAM_H_
7
mmenkecbc2b712014-10-09 20:29:078#include "base/basictypes.h"
9#include "base/macros.h"
[email protected]d98961652012-09-11 20:27:2110#include "base/memory/scoped_vector.h"
[email protected]df7adc62012-09-18 14:01:5311#include "net/base/completion_callback.h"
[email protected]172da1b2011-08-12 15:52:2612#include "net/base/net_export.h"
initial.commit586acc5fe2008-07-26 22:42:5213
14namespace net {
15
[email protected]5b768142012-10-17 10:15:1716class DrainableIOBuffer;
[email protected]597cf6e2009-05-29 09:43:2617class IOBuffer;
[email protected]d98961652012-09-11 20:27:2118class UploadElementReader;
[email protected]597cf6e2009-05-29 09:43:2619
mmenkecbc2b712014-10-09 20:29:0720// A class for retrieving all data to be sent as a request body. Supports both
21// chunked and non-chunked uploads.
[email protected]172da1b2011-08-12 15:52:2622class NET_EXPORT UploadDataStream {
initial.commit586acc5fe2008-07-26 22:42:5223 public:
mmenkecbc2b712014-10-09 20:29:0724 // |identifier| identifies a particular upload instance, which is used by the
25 // cache to formulate a cache key. This value should be unique across browser
26 // sessions. A value of 0 is used to indicate an unspecified identifier.
wtc69f8ea82015-06-04 00:08:1327 UploadDataStream(bool is_chunked, int64_t identifier);
[email protected]b2d26cfd2012-12-11 10:36:0628
mmenkecbc2b712014-10-09 20:29:0729 virtual ~UploadDataStream();
[email protected]f288ef02012-12-15 20:28:2830
[email protected]e5d477342012-11-02 15:18:4631 // Initializes the stream. This function must be called before calling any
32 // other method. It is not valid to call any method (other than the
mmenkecbc2b712014-10-09 20:29:0733 // destructor) if Init() fails. This method can be called multiple times.
34 // Calling this method after an Init() success results in resetting the
35 // state (i.e. the stream is rewound).
[email protected]49ed6cc2012-02-02 08:59:1636 //
[email protected]df7adc62012-09-18 14:01:5337 // Does the initialization synchronously and returns the result if possible,
38 // otherwise returns ERR_IO_PENDING and runs the callback with the result.
39 //
[email protected]49ed6cc2012-02-02 08:59:1640 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected
41 // file modification time is set (usually not set, but set for sliced
42 // files) and the target file is changed.
[email protected]df7adc62012-09-18 14:01:5343 int Init(const CompletionCallback& callback);
44
[email protected]5b768142012-10-17 10:15:1745 // When possible, reads up to |buf_len| bytes synchronously from the upload
46 // data stream to |buf| and returns the number of bytes read; otherwise,
47 // returns ERR_IO_PENDING and calls |callback| with the number of bytes read.
48 // Partial reads are allowed. Zero is returned on a call to Read when there
49 // are no remaining bytes in the stream, and IsEof() will return true
50 // hereafter.
[email protected]6db833d12012-01-21 00:45:1951 //
[email protected]0ab2e242012-02-08 06:07:0052 // If there's less data to read than we initially observed (i.e. the actual
53 // upload data is smaller than size()), zeros are padded to ensure that
54 // size() bytes can be read, which can happen for TYPE_FILE payloads.
mmenkecbc2b712014-10-09 20:29:0755 //
56 // Reads are currently not allowed to fail - they must either return
57 // a value >= 0 or ERR_IO_PENDING, and call OnReadCompleted with a
58 // value >= 0.
59 // TODO(mmenke): Investigate letting reads fail.
[email protected]5b768142012-10-17 10:15:1760 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
61
initial.commit586acc5fe2008-07-26 22:42:5262 // Returns the total size of the data stream and the current position.
mmenkecbc2b712014-10-09 20:29:0763 // When the data is chunked, always returns zero. Must always return the same
64 // value after each call to Initialize().
wtc69f8ea82015-06-04 00:08:1365 uint64_t size() const { return total_size_; }
66 uint64_t position() const { return current_position_; }
initial.commit586acc5fe2008-07-26 22:42:5267
mmenkecbc2b712014-10-09 20:29:0768 // See constructor for description.
wtc69f8ea82015-06-04 00:08:1369 int64_t identifier() const { return identifier_; }
[email protected]699efe602011-01-25 07:17:1170
mmenkecbc2b712014-10-09 20:29:0771 bool is_chunked() const { return is_chunked_; }
[email protected]0736d9e2012-11-28 19:50:4072
[email protected]0ab2e242012-02-08 06:07:0073 // Returns true if all data has been consumed from this upload data
mmenkecbc2b712014-10-09 20:29:0774 // stream. For chunked uploads, returns false until the first read attempt.
75 // This makes some state machines a little simpler.
[email protected]0ab2e242012-02-08 06:07:0076 bool IsEOF() const;
[email protected]0c9bf872011-03-04 17:53:2277
mmenkecbc2b712014-10-09 20:29:0778 // Cancels all pending callbacks, and resets state. Any IOBuffer currently
79 // being read to is not safe for future use, as it may be in use on another
80 // thread.
[email protected]e5d477342012-11-02 15:18:4681 void Reset();
82
mmenkecbc2b712014-10-09 20:29:0783 // Returns true if the upload data in the stream is entirely in memory, and
84 // all read requests will succeed synchronously. Expected to return false for
85 // chunked requests.
86 virtual bool IsInMemory() const;
87
88 // Returns a list of element readers owned by |this|, if it has any.
89 virtual const ScopedVector<UploadElementReader>*
90 GetElementReaders() const;
91
92 protected:
93 // Must be called by subclasses when InitInternal and ReadInternal complete
94 // asynchronously.
95 void OnInitCompleted(int result);
96 void OnReadCompleted(int result);
97
98 // Must be called before InitInternal completes, for non-chunked uploads.
99 // Must not be called for chunked uploads.
wtc69f8ea82015-06-04 00:08:13100 void SetSize(uint64_t size);
mmenkecbc2b712014-10-09 20:29:07101
102 // Must be called for chunked uploads before the final ReadInternal call
103 // completes. Must not be called for non-chunked uploads.
104 void SetIsFinalChunk();
105
[email protected]02cad5d2013-10-02 08:14:03106 private:
mmenkecbc2b712014-10-09 20:29:07107 // See Init(). If it returns ERR_IO_PENDING, OnInitCompleted must be called
108 // once it completes. If the upload is not chunked, SetSize must be called
109 // before it completes.
110 virtual int InitInternal() = 0;
[email protected]e5d477342012-11-02 15:18:46111
mmenkecbc2b712014-10-09 20:29:07112 // See Read(). For chunked uploads, must call SetIsFinalChunk if this is the
113 // final chunk. For non-chunked uploads, the UploadDataStream determins which
114 // read is the last based on size. Must read 1 or more bytes on every call,
115 // though the final chunk may be 0 bytes, for chunked requests. If it returns
116 // ERR_IO_PENDING, OnInitCompleted must be called once it completes. Must not
117 // return any error, other than ERR_IO_PENDING.
118 virtual int ReadInternal(IOBuffer* buf, int buf_len) = 0;
[email protected]df7adc62012-09-18 14:01:53119
mmenkecbc2b712014-10-09 20:29:07120 // Resets state and cancels any pending callbacks. Guaranteed to be called
121 // before all but the first call to InitInternal.
122 virtual void ResetInternal() = 0;
[email protected]9fa94db2012-10-22 03:34:07123
wtc69f8ea82015-06-04 00:08:13124 uint64_t total_size_;
125 uint64_t current_position_;
initial.commit586acc5fe2008-07-26 22:42:52126
wtc69f8ea82015-06-04 00:08:13127 const int64_t identifier_;
[email protected]b2d26cfd2012-12-11 10:36:06128
129 const bool is_chunked_;
[email protected]fac16e22012-12-29 18:46:31130
[email protected]49ed6cc2012-02-02 08:59:16131 // True if the initialization was successful.
132 bool initialized_successfully_;
133
mmenkecbc2b712014-10-09 20:29:07134 bool is_eof_;
[email protected]9fa94db2012-10-22 03:34:07135
mmenkecbc2b712014-10-09 20:29:07136 CompletionCallback callback_;
[email protected]df7adc62012-09-18 14:01:53137
[email protected]73a797fb2010-06-07 02:10:18138 DISALLOW_COPY_AND_ASSIGN(UploadDataStream);
initial.commit586acc5fe2008-07-26 22:42:52139};
140
141} // namespace net
142
143#endif // NET_BASE_UPLOAD_DATA_STREAM_H_