| OLD | NEW | 
| (Empty) |  | 
 |    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. | 
 |    4  | 
 |    5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_ADAPTER_H_ | 
 |    6 #define COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_ADAPTER_H_ | 
 |    7  | 
 |    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" | 
 |   13  | 
 |   14 namespace net { | 
 |   15 class IOBuffer; | 
 |   16 }  // namespace net | 
 |   17  | 
 |   18 namespace cronet { | 
 |   19  | 
 |   20 // The CronetUploadDataStreamAdapter is created on a Java thread, but | 
 |   21 // afterwards, lives and is deleted on the network thread. It's responsible for | 
 |   22 // invoking UploadDataStream's callbacks, and ensuring only one read/rewind | 
 |   23 // request sent to Java is outstanding at a time. The main complexity is around | 
 |   24 // Reset/Initialize calls while there's a pending read or rewind. | 
 |   25 class CronetUploadDataStreamAdapter : 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<CronetUploadDataStreamAdapter> adapter) = 0; | 
 |   33  | 
 |   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; | 
 |   38  | 
 |   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; | 
 |   44  | 
 |   45     // Called when the adapter is destroyed. May be called when there's a | 
 |   46     // pending read or rewind operation. The Delegate is then responsible for | 
 |   47     // destroying itself. | 
 |   48     virtual void OnAdapterDestroyed() = 0; | 
 |   49  | 
 |   50    protected: | 
 |   51     Delegate() {} | 
 |   52     virtual ~Delegate() {} | 
 |   53  | 
 |   54    private: | 
 |   55     DISALLOW_COPY_AND_ASSIGN(Delegate); | 
 |   56   }; | 
 |   57  | 
 |   58   CronetUploadDataStreamAdapter(Delegate* delegate, int64 size); | 
 |   59   ~CronetUploadDataStreamAdapter() override; | 
 |   60  | 
 |   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(); | 
 |   65  | 
 |   66  private: | 
 |   67   // net::UploadDataStream implementation: | 
 |   68   int InitInternal() override; | 
 |   69   int ReadInternal(net::IOBuffer* buf, int buf_len) override; | 
 |   70   void ResetInternal() override; | 
 |   71  | 
 |   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(); | 
 |   75  | 
 |   76   // Size of the upload. -1 if chunked. | 
 |   77   const int64 size_; | 
 |   78  | 
 |   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_; | 
 |   87  | 
 |   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_; | 
 |   97  | 
 |   98   // Set to false when a read starts, true when a rewind completes. | 
 |   99   bool at_front_of_stream_; | 
 |  100  | 
 |  101   Delegate* const delegate_; | 
 |  102  | 
 |  103   // Vends pointers on the network thread, though created on a Java thread. | 
 |  104   base::WeakPtrFactory<CronetUploadDataStreamAdapter> weak_factory_; | 
 |  105  | 
 |  106   DISALLOW_COPY_AND_ASSIGN(CronetUploadDataStreamAdapter); | 
 |  107 }; | 
 |  108  | 
 |  109 }  // namespace cronet | 
 |  110  | 
 |  111 #endif  // COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_ADAPTER_H_ | 
| OLD | NEW |