devtools_http_handler_impl.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 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_DEVTOOLS_DEVTOOLS_HTTP_HANDLER_IMPL_H_
6#define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_HTTP_HANDLER_IMPL_H_
7
8#include <map>
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 "content/common/content_export.h"
16#include "content/public/browser/devtools_http_handler.h"
17#include "content/public/browser/devtools_http_handler_delegate.h"
18#include "content/public/browser/worker_service.h"
19#include "net/server/http_server.h"
20
21namespace base {
22class DictionaryValue;
23class ListValue;
24class Thread;
25class Value;
26}
27
28namespace net {
29class StreamListenSocketFactory;
30class URLRequestContextGetter;
31}
32
33namespace content {
34
35class DevToolsBrowserTarget;
36class DevToolsClientHost;
37
38class DevToolsHttpHandlerImpl
39    : public DevToolsHttpHandler,
40      public base::RefCountedThreadSafe<DevToolsHttpHandlerImpl>,
41      public net::HttpServer::Delegate {
42 private:
43  friend class base::RefCountedThreadSafe<DevToolsHttpHandlerImpl>;
44  friend class DevToolsHttpHandler;
45
46  // Takes ownership over |socket_factory|.
47  DevToolsHttpHandlerImpl(const net::StreamListenSocketFactory* socket_factory,
48                          const std::string& frontend_url,
49                          DevToolsHttpHandlerDelegate* delegate);
50  virtual ~DevToolsHttpHandlerImpl();
51  void Start();
52
53  // DevToolsHttpHandler implementation.
54  virtual void Stop() OVERRIDE;
55  virtual void SetDevToolsAgentHostBinding(
56      DevToolsAgentHostBinding* binding) OVERRIDE;
57  virtual GURL GetFrontendURL(DevToolsAgentHost* agent_host) OVERRIDE;
58
59  // net::HttpServer::Delegate implementation.
60  virtual void OnHttpRequest(int connection_id,
61                             const net::HttpServerRequestInfo& info) OVERRIDE;
62  virtual void OnWebSocketRequest(
63      int connection_id,
64      const net::HttpServerRequestInfo& info) OVERRIDE;
65  virtual void OnWebSocketMessage(int connection_id,
66                                  const std::string& data) OVERRIDE;
67  virtual void OnClose(int connection_id) OVERRIDE;
68
69  void OnJsonRequestUI(int connection_id,
70                       const net::HttpServerRequestInfo& info);
71  void OnThumbnailRequestUI(int connection_id, const GURL& page_url);
72  void OnDiscoveryPageRequestUI(int connection_id);
73
74  void OnWebSocketRequestUI(int connection_id,
75                            const net::HttpServerRequestInfo& info);
76  void OnWebSocketMessageUI(int connection_id, const std::string& data);
77  void OnCloseUI(int connection_id);
78
79  void ResetHandlerThread();
80  void ResetHandlerThreadAndRelease();
81
82  void CollectWorkerInfo(base::ListValue* target_list, std::string host);
83  void SendTargetList(int connection_id, base::ListValue* target_list);
84
85  void Init();
86  void Teardown();
87
88  void StartHandlerThread();
89  void StopHandlerThread();
90
91  void SendJson(int connection_id,
92                net::HttpStatusCode status_code,
93                base::Value* value,
94                const std::string& message);
95  void Send200(int connection_id,
96               const std::string& data,
97               const std::string& mime_type);
98  void Send404(int connection_id);
99  void Send500(int connection_id,
100               const std::string& message);
101  void AcceptWebSocket(int connection_id,
102                       const net::HttpServerRequestInfo& request);
103
104  // Returns the front end url without the host at the beginning.
105  std::string GetFrontendURLInternal(const std::string rvh_id,
106                                     const std::string& host);
107
108  base::DictionaryValue* SerializePageInfo(RenderViewHost* rvh,
109                                           const std::string& host);
110
111  base::DictionaryValue* SerializeWorkerInfo(
112      const WorkerService::WorkerInfo& worker,
113      const std::string& host);
114
115  void SerializeDebuggerURLs(base::DictionaryValue* dictionary,
116                             const std::string& id,
117                             const std::string& host);
118
119  // The thread used by the devtools handler to run server socket.
120  scoped_ptr<base::Thread> thread_;
121
122  std::string overridden_frontend_url_;
123  scoped_ptr<const net::StreamListenSocketFactory> socket_factory_;
124  scoped_refptr<net::HttpServer> server_;
125  typedef std::map<int, DevToolsClientHost*> ConnectionToClientHostMap;
126  ConnectionToClientHostMap connection_to_client_host_ui_;
127  scoped_ptr<DevToolsHttpHandlerDelegate> delegate_;
128  DevToolsAgentHostBinding* binding_;
129  scoped_ptr<DevToolsAgentHostBinding> default_binding_;
130  scoped_ptr<DevToolsBrowserTarget> browser_target_;
131  DISALLOW_COPY_AND_ASSIGN(DevToolsHttpHandlerImpl);
132};
133
134}  // namespace content
135
136#endif  // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_HTTP_HANDLER_IMPL_H_
137