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