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_agent_host.h"
17#include "content/public/browser/devtools_http_handler.h"
18#include "content/public/browser/devtools_http_handler_delegate.h"
19#include "content/public/browser/devtools_manager_delegate.h"
20#include "net/http/http_status_code.h"
21#include "net/server/http_server.h"
22
23namespace base {
24class DictionaryValue;
25class ListValue;
26class Thread;
27class Value;
28}
29
30namespace net {
31class ServerSocketFactory;
32class URLRequestContextGetter;
33}
34
35namespace content {
36
37class DevToolsBrowserTarget;
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  DevToolsHttpHandlerImpl(scoped_ptr<ServerSocketFactory> server_socket_factory,
48                          const std::string& frontend_url,
49                          DevToolsHttpHandlerDelegate* delegate,
50                          const base::FilePath& active_port_output_directory);
51  virtual ~DevToolsHttpHandlerImpl();
52  void Start();
53
54  // DevToolsHttpHandler implementation.
55  virtual void Stop() OVERRIDE;
56  virtual GURL GetFrontendURL() OVERRIDE;
57
58  // net::HttpServer::Delegate implementation.
59  virtual void OnConnect(int connection_id) OVERRIDE {}
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 OnTargetListReceived(
83      int connection_id,
84      const std::string& host,
85      const DevToolsManagerDelegate::TargetList& targets);
86
87  DevToolsTarget* GetTarget(const std::string& id);
88
89  void Init();
90  void Teardown();
91
92  void StartHandlerThread();
93  void StopHandlerThread();
94  void StopWithoutRelease();
95
96  void WriteActivePortToUserProfile();
97
98  void SendJson(int connection_id,
99                net::HttpStatusCode status_code,
100                base::Value* value,
101                const std::string& message);
102  void Send200(int connection_id,
103               const std::string& data,
104               const std::string& mime_type);
105  void Send404(int connection_id);
106  void Send500(int connection_id,
107               const std::string& message);
108  void AcceptWebSocket(int connection_id,
109                       const net::HttpServerRequestInfo& request);
110
111  // Returns the front end url without the host at the beginning.
112  std::string GetFrontendURLInternal(const std::string target_id,
113                                     const std::string& host);
114
115  base::DictionaryValue* SerializeTarget(const DevToolsTarget& target,
116                                         const std::string& host);
117
118  // The thread used by the devtools handler to run server socket.
119  scoped_ptr<base::Thread> thread_;
120
121  std::string frontend_url_;
122  const scoped_ptr<ServerSocketFactory> server_socket_factory_;
123  scoped_ptr<net::HttpServer> server_;
124  typedef std::map<int, DevToolsAgentHostClient*> ConnectionToClientMap;
125  ConnectionToClientMap connection_to_client_ui_;
126  const scoped_ptr<DevToolsHttpHandlerDelegate> delegate_;
127  const base::FilePath active_port_output_directory_;
128  typedef std::map<std::string, DevToolsTarget*> TargetMap;
129  TargetMap target_map_;
130  typedef std::map<int, scoped_refptr<DevToolsBrowserTarget> > BrowserTargets;
131  BrowserTargets browser_targets_;
132  DISALLOW_COPY_AND_ASSIGN(DevToolsHttpHandlerImpl);
133};
134
135}  // namespace content
136
137#endif  // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_HTTP_HANDLER_IMPL_H_
138