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_PUBLIC_BROWSER_DEVTOOLS_AGENT_HOST_H_
6#define CONTENT_PUBLIC_BROWSER_DEVTOOLS_AGENT_HOST_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/callback.h"
13#include "base/memory/ref_counted.h"
14#include "content/common/content_export.h"
15#include "content/public/browser/devtools_agent_host_client.h"
16#include "url/gurl.h"
17
18namespace content {
19
20class DevToolsExternalAgentProxyDelegate;
21class WebContents;
22
23// Describes interface for managing devtools agents from browser process.
24class CONTENT_EXPORT DevToolsAgentHost
25    : public base::RefCounted<DevToolsAgentHost> {
26 public:
27  enum Type {
28    // Agent host associated with WebContents.
29    TYPE_WEB_CONTENTS,
30
31    // Agent host associated with shared worker.
32    TYPE_SHARED_WORKER,
33
34    // Agent host associated with service worker.
35    TYPE_SERVICE_WORKER,
36
37    // Agent host associated with DevToolsExternalAgentProxyDelegate.
38    TYPE_EXTERNAL,
39  };
40
41  // Returns DevToolsAgentHost with a given |id| or NULL of it does not exist.
42  static scoped_refptr<DevToolsAgentHost> GetForId(const std::string& id);
43
44  // Returns DevToolsAgentHost that can be used for inspecting |web_contents|.
45  // New DevToolsAgentHost will be created if it does not exist.
46  static scoped_refptr<DevToolsAgentHost> GetOrCreateFor(
47      WebContents* web_contents);
48
49  // Returns true iff an instance of DevToolsAgentHost for the |web_contents|
50  // does exist.
51  static bool HasFor(WebContents* web_contents);
52
53  // Returns DevToolsAgentHost that can be used for inspecting shared worker
54  // with given worker process host id and routing id.
55  static scoped_refptr<DevToolsAgentHost> GetForWorker(int worker_process_id,
56                                                       int worker_route_id);
57
58  // Creates DevToolsAgentHost that communicates to the target by means of
59  // provided |delegate|. |delegate| ownership is passed to the created agent
60  // host.
61  static scoped_refptr<DevToolsAgentHost> Create(
62      DevToolsExternalAgentProxyDelegate* delegate);
63
64  static bool IsDebuggerAttached(WebContents* web_contents);
65
66  typedef std::vector<scoped_refptr<DevToolsAgentHost> > List;
67
68  // Returns all possible DevToolsAgentHosts.
69  static List GetOrCreateAll();
70
71  // Client attaches to this agent host to start debugging it.
72  virtual void AttachClient(DevToolsAgentHostClient* client) = 0;
73
74  // Already attached client detaches from this agent host to stop debugging it.
75  virtual void DetachClient() = 0;
76
77  // Returns true if there is a client attached.
78  virtual bool IsAttached() = 0;
79
80  // Sends a message to the agent.
81  virtual void DispatchProtocolMessage(const std::string& message) = 0;
82
83  // Starts inspecting element at position (|x|, |y|) in the specified page.
84  virtual void InspectElement(int x, int y) = 0;
85
86  // Returns the unique id of the agent.
87  virtual std::string GetId() = 0;
88
89  // Returns web contents instance for this host if any.
90  virtual WebContents* GetWebContents() = 0;
91
92  // Temporarily detaches render view host from this host. Must be followed by
93  // a call to ConnectWebContents (may leak the host instance otherwise).
94  virtual void DisconnectWebContents() = 0;
95
96  // Attaches render view host to this host.
97  virtual void ConnectWebContents(WebContents* web_contents) = 0;
98
99  // Returns true if DevToolsAgentHost is for worker.
100  virtual bool IsWorker() const = 0;
101
102  // Returns agent host type.
103  virtual Type GetType() = 0;
104
105  // Returns agent host title.
106  virtual std::string GetTitle() = 0;
107
108  // Returns url associated with agent host.
109  virtual GURL GetURL() = 0;
110
111  // Activates agent host. Returns false if the operation failed.
112  virtual bool Activate() = 0;
113
114  // Closes agent host. Returns false if the operation failed.
115  virtual bool Close() = 0;
116
117  // Terminates all debugging sessions and detaches all clients.
118  static void DetachAllClients();
119
120  typedef base::Callback<void(DevToolsAgentHost*, bool attached)>
121      AgentStateCallback;
122
123  static void AddAgentStateCallback(const AgentStateCallback& callback);
124  static void RemoveAgentStateCallback(const AgentStateCallback& callback);
125
126 protected:
127  friend class base::RefCounted<DevToolsAgentHost>;
128  virtual ~DevToolsAgentHost() {}
129};
130
131}  // namespace content
132
133#endif  // CONTENT_PUBLIC_BROWSER_DEVTOOLS_AGENT_HOST_H_
134