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