OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_ADAPTER_H_ | 5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_H_ |
6 #define COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_ADAPTER_H_ | 6 #define COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "net/base/upload_data_stream.h" | 12 #include "net/base/upload_data_stream.h" |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 class IOBuffer; | 15 class IOBuffer; |
16 } // namespace net | 16 } // namespace net |
17 | 17 |
18 namespace cronet { | 18 namespace cronet { |
19 | 19 |
20 // The CronetUploadDataStreamAdapter is created on a Java thread, but | 20 // The CronetUploadDataStream is created on a Java thread, but afterwards, lives |
21 // afterwards, lives and is deleted on the network thread. It's responsible for | 21 // and is deleted on the network thread. It's responsible for ensuring only one |
22 // invoking UploadDataStream's callbacks, and ensuring only one read/rewind | 22 // read/rewind request sent to Java is outstanding at a time. The main |
23 // request sent to Java is outstanding at a time. The main complexity is around | 23 // complexity is around Reset/Initialize calls while there's a pending read or |
24 // Reset/Initialize calls while there's a pending read or rewind. | 24 // rewind. |
25 class CronetUploadDataStreamAdapter : public net::UploadDataStream { | 25 class CronetUploadDataStream : public net::UploadDataStream { |
26 public: | 26 public: |
27 class Delegate { | 27 class Delegate { |
28 public: | 28 public: |
29 // Called once during initial setup on the network thread, called before | 29 // Called once during initial setup on the network thread, called before |
30 // all other methods. | 30 // all other methods. |
31 virtual void InitializeOnNetworkThread( | 31 virtual void InitializeOnNetworkThread( |
32 base::WeakPtr<CronetUploadDataStreamAdapter> adapter) = 0; | 32 base::WeakPtr<CronetUploadDataStream> upload_data_stream) = 0; |
33 | 33 |
34 // Called for each read request. Delegate must respond by calling | 34 // Called for each read request. Delegate must respond by calling |
35 // OnReadSuccess on the network thread asynchronous, or failing the request. | 35 // OnReadSuccess on the network thread asynchronous, or failing the request. |
36 // Only called when there's no other pending read or rewind operation. | 36 // Only called when there's no other pending read or rewind operation. |
37 virtual void Read(net::IOBuffer* buffer, int buf_len) = 0; | 37 virtual void Read(net::IOBuffer* buffer, int buf_len) = 0; |
38 | 38 |
39 // Called to rewind the stream. Not called when already at the start of the | 39 // Called to rewind the stream. Not called when already at the start of the |
40 // stream. The delegate must respond by calling OnRewindSuccess | 40 // stream. The delegate must respond by calling OnRewindSuccess |
41 // asynchronously on the network thread, or failing the request. Only called | 41 // asynchronously on the network thread, or failing the request. Only called |
42 // when there's no other pending read or rewind operation. | 42 // when there's no other pending read or rewind operation. |
43 virtual void Rewind() = 0; | 43 virtual void Rewind() = 0; |
44 | 44 |
45 // Called when the adapter is destroyed. May be called when there's a | 45 // Called when the CronetUploadDataStream is destroyed. The Delegate is then |
46 // pending read or rewind operation. The Delegate is then responsible for | 46 // responsible for destroying itself. May be called when there's a pending |
47 // destroying itself. | 47 // read or rewind operation. |
48 virtual void OnAdapterDestroyed() = 0; | 48 virtual void OnUploadDataStreamDestroyed() = 0; |
49 | 49 |
50 protected: | 50 protected: |
51 Delegate() {} | 51 Delegate() {} |
52 virtual ~Delegate() {} | 52 virtual ~Delegate() {} |
53 | 53 |
54 private: | 54 private: |
55 DISALLOW_COPY_AND_ASSIGN(Delegate); | 55 DISALLOW_COPY_AND_ASSIGN(Delegate); |
56 }; | 56 }; |
57 | 57 |
58 CronetUploadDataStreamAdapter(Delegate* delegate, int64 size); | 58 CronetUploadDataStream(Delegate* delegate, int64 size); |
59 ~CronetUploadDataStreamAdapter() override; | 59 ~CronetUploadDataStream() override; |
60 | 60 |
61 // Failure is handled at the Java layer. These two success callbacks are | 61 // Failure is handled at the Java layer. These two success callbacks are |
62 // invoked by Java UploadDataSink upon completion of the operation. | 62 // invoked by Java UploadDataSink upon completion of the operation. |
63 void OnReadSuccess(int bytes_read, bool final_chunk); | 63 void OnReadSuccess(int bytes_read, bool final_chunk); |
64 void OnRewindSuccess(); | 64 void OnRewindSuccess(); |
65 | 65 |
66 private: | 66 private: |
67 // net::UploadDataStream implementation: | 67 // net::UploadDataStream implementation: |
68 int InitInternal() override; | 68 int InitInternal() override; |
69 int ReadInternal(net::IOBuffer* buf, int buf_len) override; | 69 int ReadInternal(net::IOBuffer* buf, int buf_len) override; |
(...skipping 24 matching lines...) Expand all Loading... |
94 // will only be set to false once it completes, even though ResetInternal may | 94 // will only be set to false once it completes, even though ResetInternal may |
95 // have been called since the rewind started. | 95 // have been called since the rewind started. |
96 bool rewind_in_progress_; | 96 bool rewind_in_progress_; |
97 | 97 |
98 // Set to false when a read starts, true when a rewind completes. | 98 // Set to false when a read starts, true when a rewind completes. |
99 bool at_front_of_stream_; | 99 bool at_front_of_stream_; |
100 | 100 |
101 Delegate* const delegate_; | 101 Delegate* const delegate_; |
102 | 102 |
103 // Vends pointers on the network thread, though created on a Java thread. | 103 // Vends pointers on the network thread, though created on a Java thread. |
104 base::WeakPtrFactory<CronetUploadDataStreamAdapter> weak_factory_; | 104 base::WeakPtrFactory<CronetUploadDataStream> weak_factory_; |
105 | 105 |
106 DISALLOW_COPY_AND_ASSIGN(CronetUploadDataStreamAdapter); | 106 DISALLOW_COPY_AND_ASSIGN(CronetUploadDataStream); |
107 }; | 107 }; |
108 | 108 |
109 } // namespace cronet | 109 } // namespace cronet |
110 | 110 |
111 #endif // COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_ADAPTER_H_ | 111 #endif // COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_H_ |
OLD | NEW |