1// Copyright 2014 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 CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_TRANSACTION_H_
6#define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_TRANSACTION_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "base/memory/weak_ptr.h"
10#include "net/base/completion_callback.h"
11#include "net/base/load_states.h"
12#include "net/base/request_priority.h"
13#include "net/http/http_transaction.h"
14#include "net/websockets/websocket_handshake_stream_base.h"
15
16class DevToolsNetworkController;
17class DevToolsNetworkInterceptor;
18class GURL;
19
20namespace net {
21class AuthCredentials;
22class BoundNetLog;
23class HttpRequestHeaders;
24struct HttpRequestInfo;
25class HttpResponseInfo;
26class HttpNetworkSession;
27class IOBuffer;
28struct LoadTimingInfo;
29class UploadProgress;
30class X509Certificate;
31}  // namespace net
32
33namespace test {
34class DevToolsNetworkControllerHelper;
35}
36
37// DevToolsNetworkTransaction is a wrapper for network transaction. All
38// HttpTransaction methods are proxied to real transaction, but |callback|
39// parameter is saved and replaced with proxy callback. Fail method should be
40// used to simulate network outage. It runs saved callback (if any) with
41// net::ERR_INTERNET_DISCONNECTED result value.
42class DevToolsNetworkTransaction : public net::HttpTransaction {
43 public:
44  DevToolsNetworkTransaction(
45      DevToolsNetworkController* controller,
46      scoped_ptr<net::HttpTransaction> network_transaction);
47
48  virtual ~DevToolsNetworkTransaction();
49
50  const net::HttpRequestInfo* request() const { return request_; }
51
52  // Checks if request contains DevTools specific headers. Found values are
53  // remembered and corresponding keys are removed from headers.
54  void ProcessRequest();
55
56  bool failed() const { return failed_; }
57
58  // Runs callback (if any) with net::ERR_INTERNET_DISCONNECTED result value.
59  void Fail();
60
61  int64_t throttled_byte_count() const { return throttled_byte_count_; }
62  void DecreaseThrottledByteCount(int64_t delta) {
63    throttled_byte_count_ -= delta;
64  }
65
66  const std::string& request_initiator() const { return request_initiator_; }
67
68  const std::string& client_id() const {
69    return client_id_;
70  }
71
72  void FireThrottledCallback();
73
74  // HttpTransaction methods:
75  virtual int Start(
76      const net::HttpRequestInfo* request,
77      const net::CompletionCallback& callback,
78      const net::BoundNetLog& net_log) OVERRIDE;
79  virtual int RestartIgnoringLastError(
80      const net::CompletionCallback& callback) OVERRIDE;
81  virtual int RestartWithCertificate(
82      net::X509Certificate* client_cert,
83      const net::CompletionCallback& callback) OVERRIDE;
84  virtual int RestartWithAuth(
85      const net::AuthCredentials& credentials,
86      const net::CompletionCallback& callback) OVERRIDE;
87  virtual bool IsReadyToRestartForAuth() OVERRIDE;
88
89  virtual int Read(
90      net::IOBuffer* buf,
91      int buf_len,
92      const net::CompletionCallback& callback) OVERRIDE;
93  virtual void StopCaching() OVERRIDE;
94  virtual bool GetFullRequestHeaders(
95      net::HttpRequestHeaders* headers) const OVERRIDE;
96  virtual int64 GetTotalReceivedBytes() const OVERRIDE;
97  virtual void DoneReading() OVERRIDE;
98  virtual const net::HttpResponseInfo* GetResponseInfo() const OVERRIDE;
99  virtual net::LoadState GetLoadState() const OVERRIDE;
100  virtual net::UploadProgress GetUploadProgress() const OVERRIDE;
101  virtual void SetQuicServerInfo(
102      net::QuicServerInfo* quic_server_info) OVERRIDE;
103  virtual bool GetLoadTimingInfo(
104      net::LoadTimingInfo* load_timing_info) const OVERRIDE;
105  virtual void SetPriority(net::RequestPriority priority) OVERRIDE;
106  virtual void SetWebSocketHandshakeStreamCreateHelper(
107      net::WebSocketHandshakeStreamBase::CreateHelper* create_helper) OVERRIDE;
108  virtual void SetBeforeNetworkStartCallback(
109      const BeforeNetworkStartCallback& callback) OVERRIDE;
110  virtual void SetBeforeProxyHeadersSentCallback(
111      const BeforeProxyHeadersSentCallback& callback) OVERRIDE;
112  virtual int ResumeNetworkStart() OVERRIDE;
113
114 protected:
115  friend class test::DevToolsNetworkControllerHelper;
116
117 private:
118  // Proxy callback handler. Runs saved callback.
119  void OnCallback(int result);
120
121  DevToolsNetworkController* controller_;
122  base::WeakPtr<DevToolsNetworkInterceptor> interceptor_;
123
124  // Modified request. Should be destructed after |network_transaction_|
125  scoped_ptr<net::HttpRequestInfo> custom_request_;
126
127  // Real network transaction.
128  scoped_ptr<net::HttpTransaction> network_transaction_;
129
130  const net::HttpRequestInfo* request_;
131
132  // True if Start was already invoked.
133  bool started_;
134
135  // True if Fail was already invoked.
136  bool failed_;
137
138  // Value of "X-DevTools-Request-Initiator" request header.
139  std::string request_initiator_;
140
141  // Value of "X-DevTools-Emulate-Network-Conditions-Client-Id" request header.
142  std::string client_id_;
143
144  enum CallbackType {
145      NONE,
146      READ,
147      RESTART_IGNORING_LAST_ERROR,
148      RESTART_WITH_AUTH,
149      RESTART_WITH_CERTIFICATE,
150      START
151  };
152
153  int SetupCallback(
154      net::CompletionCallback callback,
155      int result,
156      CallbackType callback_type);
157
158  void Throttle(int result);
159
160  int throttled_result_;
161  int64_t throttled_byte_count_;
162  CallbackType callback_type_;
163  net::CompletionCallback proxy_callback_;
164  net::CompletionCallback callback_;
165
166  DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkTransaction);
167};
168
169#endif  // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_TRANSACTION_H_
170