146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// found in the LICENSE file.
446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_TRANSACTION_H_
646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_TRANSACTION_H_
746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
9f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "base/memory/weak_ptr.h"
1046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "net/base/completion_callback.h"
1146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "net/base/load_states.h"
1246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "net/base/request_priority.h"
1346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "net/http/http_transaction.h"
1446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "net/websockets/websocket_handshake_stream_base.h"
1546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
1646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class DevToolsNetworkController;
17f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class DevToolsNetworkInterceptor;
1846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class GURL;
1946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
2046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)namespace net {
2146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class AuthCredentials;
2246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class BoundNetLog;
2346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class HttpRequestHeaders;
2446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)struct HttpRequestInfo;
2546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class HttpResponseInfo;
2646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class HttpNetworkSession;
2746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class IOBuffer;
2846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)struct LoadTimingInfo;
2946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class UploadProgress;
3046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class X509Certificate;
31f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}  // namespace net
32f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
33f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)namespace test {
34f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class DevToolsNetworkControllerHelper;
3546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
3646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// DevToolsNetworkTransaction is a wrapper for network transaction. All
3846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// HttpTransaction methods are proxied to real transaction, but |callback|
3946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// parameter is saved and replaced with proxy callback. Fail method should be
4046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// used to simulate network outage. It runs saved callback (if any) with
4146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// net::ERR_INTERNET_DISCONNECTED result value.
4246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class DevToolsNetworkTransaction : public net::HttpTransaction {
4346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) public:
4446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DevToolsNetworkTransaction(
4546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      DevToolsNetworkController* controller,
4646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      scoped_ptr<net::HttpTransaction> network_transaction);
4746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
4846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual ~DevToolsNetworkTransaction();
4946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
5046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const net::HttpRequestInfo* request() const { return request_; }
51f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
52f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Checks if request contains DevTools specific headers. Found values are
53f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // remembered and corresponding keys are removed from headers.
54f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void ProcessRequest();
55f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
5646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool failed() const { return failed_; }
5746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
5846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Runs callback (if any) with net::ERR_INTERNET_DISCONNECTED result value.
5946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void Fail();
6046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
61f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  int64_t throttled_byte_count() const { return throttled_byte_count_; }
62f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void DecreaseThrottledByteCount(int64_t delta) {
63f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    throttled_byte_count_ -= delta;
64f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
65f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
66f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  const std::string& request_initiator() const { return request_initiator_; }
67f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
68f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  const std::string& client_id() const {
69f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return client_id_;
70f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
71f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
72f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void FireThrottledCallback();
73f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
7446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // HttpTransaction methods:
7546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual int Start(
7646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const net::HttpRequestInfo* request,
7746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const net::CompletionCallback& callback,
7846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const net::BoundNetLog& net_log) OVERRIDE;
7946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual int RestartIgnoringLastError(
8046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const net::CompletionCallback& callback) OVERRIDE;
8146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual int RestartWithCertificate(
8246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      net::X509Certificate* client_cert,
8346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const net::CompletionCallback& callback) OVERRIDE;
8446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual int RestartWithAuth(
8546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const net::AuthCredentials& credentials,
8646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const net::CompletionCallback& callback) OVERRIDE;
8746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual bool IsReadyToRestartForAuth() OVERRIDE;
8846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
8946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual int Read(
9046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      net::IOBuffer* buf,
9146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      int buf_len,
9246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const net::CompletionCallback& callback) OVERRIDE;
9346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void StopCaching() OVERRIDE;
9446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual bool GetFullRequestHeaders(
9546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      net::HttpRequestHeaders* headers) const OVERRIDE;
9646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual int64 GetTotalReceivedBytes() const OVERRIDE;
9746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void DoneReading() OVERRIDE;
9846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual const net::HttpResponseInfo* GetResponseInfo() const OVERRIDE;
9946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual net::LoadState GetLoadState() const OVERRIDE;
10046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual net::UploadProgress GetUploadProgress() const OVERRIDE;
10146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void SetQuicServerInfo(
10246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      net::QuicServerInfo* quic_server_info) OVERRIDE;
10346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual bool GetLoadTimingInfo(
10446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      net::LoadTimingInfo* load_timing_info) const OVERRIDE;
10546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void SetPriority(net::RequestPriority priority) OVERRIDE;
10646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void SetWebSocketHandshakeStreamCreateHelper(
10746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      net::WebSocketHandshakeStreamBase::CreateHelper* create_helper) OVERRIDE;
10846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void SetBeforeNetworkStartCallback(
10946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const BeforeNetworkStartCallback& callback) OVERRIDE;
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual void SetBeforeProxyHeadersSentCallback(
111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const BeforeProxyHeadersSentCallback& callback) OVERRIDE;
11246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual int ResumeNetworkStart() OVERRIDE;
11346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
114f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) protected:
115f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  friend class test::DevToolsNetworkControllerHelper;
116f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
11746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) private:
11846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Proxy callback handler. Runs saved callback.
11946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void OnCallback(int result);
12046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
12146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DevToolsNetworkController* controller_;
122f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  base::WeakPtr<DevToolsNetworkInterceptor> interceptor_;
12346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Modified request. Should be destructed after |network_transaction_|
125116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<net::HttpRequestInfo> custom_request_;
126116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
12746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Real network transaction.
12846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_ptr<net::HttpTransaction> network_transaction_;
12946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
13046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const net::HttpRequestInfo* request_;
13146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
13246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // True if Start was already invoked.
13346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool started_;
13446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
13546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // True if Fail was already invoked.
13646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool failed_;
13746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
138f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Value of "X-DevTools-Request-Initiator" request header.
139f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string request_initiator_;
140f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
141f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Value of "X-DevTools-Emulate-Network-Conditions-Client-Id" request header.
142f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string client_id_;
143f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
144f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  enum CallbackType {
145f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      NONE,
146f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      READ,
147f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      RESTART_IGNORING_LAST_ERROR,
148f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      RESTART_WITH_AUTH,
149f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      RESTART_WITH_CERTIFICATE,
150f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      START
151f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  };
152f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
153f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  int SetupCallback(
154f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      net::CompletionCallback callback,
155f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      int result,
156f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      CallbackType callback_type);
157f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
158f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void Throttle(int result);
159f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
160f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  int throttled_result_;
161f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  int64_t throttled_byte_count_;
162f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  CallbackType callback_type_;
16346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  net::CompletionCallback proxy_callback_;
16446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  net::CompletionCallback callback_;
16546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
16646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkTransaction);
16746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)};
16846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
16946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#endif  // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_TRANSACTION_H_
170