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_BROWSER_TARGET_H_
6#define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_BROWSER_TARGET_H_
7
8#include <map>
9#include <set>
10#include <string>
11
12#include "base/basictypes.h"
13#include "base/compiler_specific.h"
14#include "base/memory/ref_counted.h"
15#include "base/memory/weak_ptr.h"
16#include "base/stl_util.h"
17#include "content/browser/devtools/devtools_protocol.h"
18
19namespace base {
20class DictionaryValue;
21class MessageLoopProxy;
22class Value;
23}  // namespace base
24
25namespace net {
26class HttpServer;
27}  // namespace net
28
29namespace content {
30
31// This class handles the "Browser" target for remote debugging.
32class DevToolsBrowserTarget
33    : public base::RefCountedThreadSafe<DevToolsBrowserTarget> {
34 public:
35  DevToolsBrowserTarget(base::MessageLoopProxy* message_loop_proxy,
36                        net::HttpServer* server,
37                        int connection_id);
38
39  int connection_id() const { return connection_id_; }
40
41  // Takes ownership of |handler|.
42  void RegisterDomainHandler(const std::string& domain,
43                             DevToolsProtocol::Handler* handler,
44                             bool handle_on_ui_thread);
45
46  void HandleMessage(const std::string& data);
47
48  void Detach();
49
50 private:
51  friend class base::RefCountedThreadSafe<DevToolsBrowserTarget>;
52  ~DevToolsBrowserTarget();
53
54  void HandleCommandOnUIThread(
55      DevToolsProtocol::Handler*,
56      scoped_refptr<DevToolsProtocol::Command> command);
57
58  void DeleteHandlersOnUIThread(
59      std::vector<DevToolsProtocol::Handler*> handlers);
60
61  void Respond(const std::string& message);
62  void RespondFromUIThread(const std::string& message);
63
64  base::MessageLoopProxy* message_loop_proxy_;
65  net::HttpServer* http_server_;
66  const int connection_id_;
67
68  typedef std::map<std::string, DevToolsProtocol::Handler*> DomainHandlerMap;
69  DomainHandlerMap handlers_;
70  STLValueDeleter<DomainHandlerMap> handlers_deleter_;
71  std::set<std::string> handle_on_ui_thread_;
72  base::WeakPtrFactory<DevToolsBrowserTarget> weak_factory_;
73
74  DISALLOW_COPY_AND_ASSIGN(DevToolsBrowserTarget);
75};
76
77}  // namespace content
78
79#endif  // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_BROWSER_TARGET_H_
80