Remove obsolete entries from .gitignore.
[chromium-blink-merge.git] / components / cronet / android / cronet_upload_data_stream.h
blobb2ff6df2c1c79508b2bf6dd98b51b2c9b5e76a76
1 // Copyright 2015 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 COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_H_
6 #define COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_H_
8 #include "base/basictypes.h"
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "net/base/upload_data_stream.h"
14 namespace net {
15 class IOBuffer;
16 } // namespace net
18 namespace cronet {
20 // The CronetUploadDataStream is created on a Java thread, but afterwards, lives
21 // and is deleted on the network thread. It's responsible for ensuring only one
22 // read/rewind request sent to Java is outstanding at a time. The main
23 // complexity is around Reset/Initialize calls while there's a pending read or
24 // rewind.
25 class CronetUploadDataStream : public net::UploadDataStream {
26 public:
27 class Delegate {
28 public:
29 // Called once during initial setup on the network thread, called before
30 // all other methods.
31 virtual void InitializeOnNetworkThread(
32 base::WeakPtr<CronetUploadDataStream> upload_data_stream) = 0;
34 // Called for each read request. Delegate must respond by calling
35 // OnReadSuccess on the network thread asynchronous, or failing the request.
36 // Only called when there's no other pending read or rewind operation.
37 virtual void Read(net::IOBuffer* buffer, int buf_len) = 0;
39 // Called to rewind the stream. Not called when already at the start of the
40 // stream. The delegate must respond by calling OnRewindSuccess
41 // asynchronously on the network thread, or failing the request. Only called
42 // when there's no other pending read or rewind operation.
43 virtual void Rewind() = 0;
45 // Called when the CronetUploadDataStream is destroyed. The Delegate is then
46 // responsible for destroying itself. May be called when there's a pending
47 // read or rewind operation.
48 virtual void OnUploadDataStreamDestroyed() = 0;
50 protected:
51 Delegate() {}
52 virtual ~Delegate() {}
54 private:
55 DISALLOW_COPY_AND_ASSIGN(Delegate);
58 CronetUploadDataStream(Delegate* delegate, int64 size);
59 ~CronetUploadDataStream() override;
61 // Failure is handled at the Java layer. These two success callbacks are
62 // invoked by Java UploadDataSink upon completion of the operation.
63 void OnReadSuccess(int bytes_read, bool final_chunk);
64 void OnRewindSuccess();
66 private:
67 // net::UploadDataStream implementation:
68 int InitInternal() override;
69 int ReadInternal(net::IOBuffer* buf, int buf_len) override;
70 void ResetInternal() override;
72 // Starts rewinding the stream. Only called when not already at the front of
73 // the stream, and no operation is pending. Completes asynchronously.
74 void StartRewind();
76 // Size of the upload. -1 if chunked.
77 const int64 size_;
79 // True if ReadInternal has been called, the read hasn't completed, and there
80 // hasn't been a ResetInternal call yet.
81 bool waiting_on_read_;
82 // True if there's a read operation in progress. This will always be true
83 // when |waiting_on_read_| is true. This will only be set to false once it
84 // completes, even though ResetInternal may have been called since the read
85 // started.
86 bool read_in_progress_;
88 // True if InitInternal has been called, the rewind hasn't completed, and
89 // there hasn't been a ResetInternal call yet. Note that this may be true
90 // even when the rewind hasn't yet started, if there's a read in progress.
91 bool waiting_on_rewind_;
92 // True if there's a rewind operation in progress. Rewinding will only start
93 // when |waiting_on_rewind_| is true, and |read_in_progress_| is false. This
94 // will only be set to false once it completes, even though ResetInternal may
95 // have been called since the rewind started.
96 bool rewind_in_progress_;
98 // Set to false when a read starts, true when a rewind completes.
99 bool at_front_of_stream_;
101 Delegate* const delegate_;
103 // Vends pointers on the network thread, though created on a Java thread.
104 base::WeakPtrFactory<CronetUploadDataStream> weak_factory_;
106 DISALLOW_COPY_AND_ASSIGN(CronetUploadDataStream);
109 } // namespace cronet
111 #endif // COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_H_