OLD | NEW |
| (Empty) |
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 CONTENT_BROWSER_DEBUGGER_DEVTOOLS_HTTP_PROTOCOL_HANDLER_H_ | |
6 #define CONTENT_BROWSER_DEBUGGER_DEVTOOLS_HTTP_PROTOCOL_HANDLER_H_ | |
7 #pragma once | |
8 | |
9 #include <map> | |
10 #include <set> | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "content/common/content_export.h" | |
17 #include "net/server/http_server.h" | |
18 #include "net/url_request/url_request.h" | |
19 | |
20 class TabContents; | |
21 | |
22 namespace content { | |
23 class DevToolsClientHost; | |
24 } | |
25 | |
26 namespace net { | |
27 class URLRequestContext; | |
28 } | |
29 | |
30 class DevToolsHttpProtocolHandler | |
31 : public net::HttpServer::Delegate, | |
32 public net::URLRequest::Delegate, | |
33 public base::RefCountedThreadSafe<DevToolsHttpProtocolHandler> { | |
34 public: | |
35 typedef std::vector<TabContents*> InspectableTabs; | |
36 class Delegate { | |
37 public: | |
38 Delegate() {} | |
39 virtual ~Delegate() {} | |
40 | |
41 // Should return the list of inspectable tabs. Called on the UI thread. | |
42 virtual InspectableTabs GetInspectableTabs() = 0; | |
43 | |
44 // Should return discovery page HTML that should list available tabs | |
45 // and provide attach links. Called on the IO thread. | |
46 virtual std::string GetDiscoveryPageHTML() = 0; | |
47 | |
48 // Should return URL request context for issuing requests against devtools | |
49 // webui or NULL if no context is available. Called on the IO thread. | |
50 virtual net::URLRequestContext* GetURLRequestContext() = 0; | |
51 private: | |
52 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
53 }; | |
54 | |
55 // Takes ownership over |delegate|. | |
56 CONTENT_EXPORT static scoped_refptr<DevToolsHttpProtocolHandler> Start( | |
57 const std::string& ip, | |
58 int port, | |
59 const std::string& frontend_url, | |
60 Delegate* delegate); | |
61 | |
62 // Called from the main thread in order to stop protocol handler. | |
63 // Will schedule tear down task on IO thread. | |
64 CONTENT_EXPORT void Stop(); | |
65 | |
66 private: | |
67 friend class base::RefCountedThreadSafe<DevToolsHttpProtocolHandler>; | |
68 | |
69 DevToolsHttpProtocolHandler(const std::string& ip, | |
70 int port, | |
71 const std::string& frontend_url, | |
72 Delegate* delegate); | |
73 virtual ~DevToolsHttpProtocolHandler(); | |
74 void Start(); | |
75 | |
76 // net::HttpServer::Delegate implementation. | |
77 virtual void OnHttpRequest(int connection_id, | |
78 const net::HttpServerRequestInfo& info) OVERRIDE; | |
79 virtual void OnWebSocketRequest( | |
80 int connection_id, | |
81 const net::HttpServerRequestInfo& info) OVERRIDE; | |
82 virtual void OnWebSocketMessage(int connection_id, | |
83 const std::string& data) OVERRIDE; | |
84 virtual void OnClose(int connection_id) OVERRIDE; | |
85 | |
86 virtual void OnJsonRequestUI(int connection_id, | |
87 const net::HttpServerRequestInfo& info); | |
88 virtual void OnWebSocketRequestUI(int connection_id, | |
89 const net::HttpServerRequestInfo& info); | |
90 virtual void OnWebSocketMessageUI(int connection_id, | |
91 const std::string& data); | |
92 virtual void OnCloseUI(int connection_id); | |
93 | |
94 // net::URLRequest::Delegate implementation. | |
95 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; | |
96 virtual void OnReadCompleted(net::URLRequest* request, | |
97 int bytes_read) OVERRIDE; | |
98 | |
99 void Init(); | |
100 void Teardown(); | |
101 void Bind(net::URLRequest* request, int connection_id); | |
102 void RequestCompleted(net::URLRequest* request); | |
103 | |
104 void Send200(int connection_id, | |
105 const std::string& data, | |
106 const std::string& mime_type = "text/html"); | |
107 void Send404(int connection_id); | |
108 void Send500(int connection_id, | |
109 const std::string& message); | |
110 void AcceptWebSocket(int connection_id, | |
111 const net::HttpServerRequestInfo& request); | |
112 | |
113 std::string ip_; | |
114 int port_; | |
115 std::string overridden_frontend_url_; | |
116 scoped_refptr<net::HttpServer> server_; | |
117 typedef std::map<net::URLRequest*, int> | |
118 RequestToSocketMap; | |
119 RequestToSocketMap request_to_connection_io_; | |
120 typedef std::map<int, std::set<net::URLRequest*> > | |
121 ConnectionToRequestsMap; | |
122 ConnectionToRequestsMap connection_to_requests_io_; | |
123 typedef std::map<net::URLRequest*, scoped_refptr<net::IOBuffer> > | |
124 BuffersMap; | |
125 BuffersMap request_to_buffer_io_; | |
126 typedef std::map<int, content::DevToolsClientHost*> | |
127 ConnectionToClientHostMap; | |
128 ConnectionToClientHostMap connection_to_client_host_ui_; | |
129 scoped_ptr<Delegate> delegate_; | |
130 DISALLOW_COPY_AND_ASSIGN(DevToolsHttpProtocolHandler); | |
131 }; | |
132 | |
133 #endif // CONTENT_BROWSER_DEBUGGER_DEVTOOLS_HTTP_PROTOCOL_HANDLER_H_ | |
OLD | NEW |