• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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_DEBUGGER_DEVTOOLS_HTTP_PROTOCOL_HANDLER_H_
6 #define CHROME_BROWSER_DEBUGGER_DEVTOOLS_HTTP_PROTOCOL_HANDLER_H_
7 #pragma once
8 
9 #include <set>
10 #include <string>
11 #include <vector>
12 
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "net/server/http_server.h"
16 #include "net/url_request/url_request.h"
17 
18 class DevToolsClientHost;
19 class DevToolsHttpServer;
20 class TabContents;
21 class TabContentsWrapper;
22 
23 class DevToolsHttpProtocolHandler
24     : public net::HttpServer::Delegate,
25       public net::URLRequest::Delegate,
26       public base::RefCountedThreadSafe<DevToolsHttpProtocolHandler> {
27  public:
28   typedef std::vector<TabContentsWrapper*> InspectableTabs;
29   class TabContentsProvider {
30    public:
TabContentsProvider()31     TabContentsProvider() {}
~TabContentsProvider()32     virtual ~TabContentsProvider() {}
33     virtual InspectableTabs GetInspectableTabs() = 0;
34    private:
35     DISALLOW_COPY_AND_ASSIGN(TabContentsProvider);
36   };
37 
38   // Takes ownership over |provider|.
39   static scoped_refptr<DevToolsHttpProtocolHandler> Start(
40       const std::string& ip,
41       int port,
42       const std::string& frontend_url,
43       TabContentsProvider* provider);
44 
45   // Called from the main thread in order to stop protocol handler.
46   // Will schedule tear down task on IO thread.
47   void Stop();
48 
49  private:
50   friend class base::RefCountedThreadSafe<DevToolsHttpProtocolHandler>;
51 
52   DevToolsHttpProtocolHandler(const std::string& ip,
53                               int port,
54                               const std::string& frontend_url,
55                               TabContentsProvider* provider);
56   virtual ~DevToolsHttpProtocolHandler();
57   void Start();
58 
59   // net::HttpServer::Delegate implementation.
60   virtual void OnHttpRequest(int connection_id,
61                              const net::HttpServerRequestInfo& info);
62   virtual void OnWebSocketRequest(int connection_id,
63                                   const net::HttpServerRequestInfo& info);
64   virtual void OnWebSocketMessage(int connection_id,
65                                   const std::string& data);
66   virtual void OnClose(int connection_id);
67 
68   virtual void OnRootRequestUI(int connection_id,
69                              const net::HttpServerRequestInfo& info);
70   virtual void OnJsonRequestUI(int connection_id,
71                              const net::HttpServerRequestInfo& info);
72   virtual void OnWebSocketRequestUI(int connection_id,
73                                     const net::HttpServerRequestInfo& info);
74   virtual void OnWebSocketMessageUI(int connection_id,
75                                     const std::string& data);
76   virtual void OnCloseUI(int connection_id);
77 
78   // net::URLRequest::Delegate implementation.
79   virtual void OnResponseStarted(net::URLRequest* request);
80   virtual void OnReadCompleted(net::URLRequest* request, int bytes_read);
81 
82   void Init();
83   void Teardown();
84   void Bind(net::URLRequest* request, int connection_id);
85   void RequestCompleted(net::URLRequest* request);
86 
87   void Send200(int connection_id,
88                const std::string& data,
89                const std::string& mime_type = "text/html");
90   void Send404(int connection_id);
91   void Send500(int connection_id,
92                const std::string& message);
93   void AcceptWebSocket(int connection_id,
94                        const net::HttpServerRequestInfo& request);
95 
96   TabContents* GetTabContents(int session_id);
97 
98   std::string ip_;
99   int port_;
100   std::string overriden_frontend_url_;
101   scoped_refptr<net::HttpServer> server_;
102   typedef std::map<net::URLRequest*, int>
103       RequestToSocketMap;
104   RequestToSocketMap request_to_connection_io_;
105   typedef std::map<int, std::set<net::URLRequest*> >
106       ConnectionToRequestsMap;
107   ConnectionToRequestsMap connection_to_requests_io_;
108   typedef std::map<net::URLRequest*, scoped_refptr<net::IOBuffer> >
109       BuffersMap;
110   BuffersMap request_to_buffer_io_;
111   typedef std::map<int, DevToolsClientHost*>
112       ConnectionToClientHostMap;
113   ConnectionToClientHostMap connection_to_client_host_ui_;
114   scoped_ptr<TabContentsProvider> tab_contents_provider_;
115   DISALLOW_COPY_AND_ASSIGN(DevToolsHttpProtocolHandler);
116 };
117 
118 #endif  // CHROME_BROWSER_DEBUGGER_DEVTOOLS_HTTP_PROTOCOL_HANDLER_H_
119