1 // Copyright (c) 2009 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 NET_FLIP_NETWORK_TRANSACTION_H_ 6 #define NET_FLIP_NETWORK_TRANSACTION_H_ 7 8 #include <string> 9 #include <deque> 10 11 #include "base/basictypes.h" 12 #include "base/ref_counted.h" 13 #include "base/scoped_ptr.h" 14 #include "base/time.h" 15 #include "net/base/completion_callback.h" 16 #include "net/base/load_states.h" 17 #include "net/flip/flip_session.h" 18 #include "net/http/http_response_info.h" 19 #include "net/http/http_transaction.h" 20 21 namespace net { 22 23 class FlipSession; 24 class FlipStream; 25 class HttpNetworkSession; 26 class HttpResponseInfo; 27 class IOBuffer; 28 class UploadDataStream; 29 30 // A FlipNetworkTransaction can be used to fetch HTTP conent. 31 // The FlipDelegate is the consumer of events from the FlipSession. 32 class FlipNetworkTransaction : public HttpTransaction { 33 public: 34 explicit FlipNetworkTransaction(HttpNetworkSession* session); 35 virtual ~FlipNetworkTransaction(); 36 37 // HttpTransaction methods: 38 virtual int Start(const HttpRequestInfo* request_info, 39 CompletionCallback* callback, 40 LoadLog* load_log); 41 virtual int RestartIgnoringLastError(CompletionCallback* callback); 42 virtual int RestartWithCertificate(X509Certificate* client_cert, 43 CompletionCallback* callback); 44 virtual int RestartWithAuth(const std::wstring& username, 45 const std::wstring& password, 46 CompletionCallback* callback); IsReadyToRestartForAuth()47 virtual bool IsReadyToRestartForAuth() { return false; } 48 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); 49 virtual const HttpResponseInfo* GetResponseInfo() const; 50 virtual LoadState GetLoadState() const; 51 virtual uint64 GetUploadProgress() const; 52 53 protected: 54 friend class FlipNetworkTransactionTest; 55 56 // Provide access to the session for testing. GetFlipSession()57 FlipSession* GetFlipSession() { return flip_.get(); } 58 59 private: 60 enum State { 61 STATE_INIT_CONNECTION, 62 STATE_INIT_CONNECTION_COMPLETE, 63 STATE_SEND_REQUEST, 64 STATE_SEND_REQUEST_COMPLETE, 65 STATE_READ_HEADERS, 66 STATE_READ_HEADERS_COMPLETE, 67 STATE_READ_BODY, 68 STATE_READ_BODY_COMPLETE, 69 STATE_NONE 70 }; 71 72 void DoCallback(int result); 73 void OnIOComplete(int result); 74 75 // Runs the state transition loop. 76 int DoLoop(int result); 77 78 // Each of these methods corresponds to a State value. Those with an input 79 // argument receive the result from the previous state. If a method returns 80 // ERR_IO_PENDING, then the result from OnIOComplete will be passed to the 81 // next state method as the result arg. 82 int DoInitConnection(); 83 int DoInitConnectionComplete(int result); 84 int DoSendRequest(); 85 int DoSendRequestComplete(int result); 86 int DoReadHeaders(); 87 int DoReadHeadersComplete(int result); 88 int DoReadBody(); 89 int DoReadBodyComplete(int result); 90 91 scoped_refptr<LoadLog> load_log_; 92 93 scoped_refptr<FlipSession> flip_; 94 95 CompletionCallbackImpl<FlipNetworkTransaction> io_callback_; 96 CompletionCallback* user_callback_; 97 98 // Used to pass onto the FlipStream 99 scoped_refptr<IOBuffer> user_buffer_; 100 int user_buffer_len_; 101 102 scoped_refptr<HttpNetworkSession> session_; 103 104 const HttpRequestInfo* request_; 105 HttpResponseInfo response_; 106 107 // The time the Start method was called. 108 base::TimeTicks start_time_; 109 110 // The next state in the state machine. 111 State next_state_; 112 113 scoped_refptr<FlipStream> stream_; 114 115 DISALLOW_COPY_AND_ASSIGN(FlipNetworkTransaction); 116 }; 117 118 } // namespace net 119 120 #endif // NET_HTTP_NETWORK_TRANSACTION_H_ 121