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.
5 #ifndef CONTENT_PUBLIC_BROWSER_DEVTOOLS_AGENT_HOST_H_
6 #define CONTENT_PUBLIC_BROWSER_DEVTOOLS_AGENT_HOST_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop_proxy.h"
16 #include "content/common/content_export.h"
17 #include "content/public/browser/devtools_agent_host_client.h"
27 class DevToolsExternalAgentProxyDelegate
;
30 // Describes interface for managing devtools agents from browser process.
31 class CONTENT_EXPORT DevToolsAgentHost
32 : public base::RefCounted
<DevToolsAgentHost
> {
35 // Agent host associated with WebContents.
38 // Agent host associated with RenderFrameHost.
41 // Agent host associated with shared worker.
44 // Agent host associated with service worker.
47 // Agent host associated with DevToolsExternalAgentProxyDelegate.
50 // Agent host associated with browser.
54 // Latest DevTools protocol version supported.
55 static std::string
GetProtocolVersion();
57 // Returns whether particular version of DevTools protocol is supported.
58 static bool IsSupportedProtocolVersion(const std::string
& version
);
60 // Returns DevToolsAgentHost with a given |id| or nullptr of it doesn't exist.
61 static scoped_refptr
<DevToolsAgentHost
> GetForId(const std::string
& id
);
63 // Returns DevToolsAgentHost that can be used for inspecting |web_contents|.
64 // New DevToolsAgentHost will be created if it does not exist.
65 static scoped_refptr
<DevToolsAgentHost
> GetOrCreateFor(
66 WebContents
* web_contents
);
68 // Returns true iff an instance of DevToolsAgentHost for the |web_contents|
70 static bool HasFor(WebContents
* web_contents
);
72 // Returns DevToolsAgentHost that can be used for inspecting shared worker
73 // with given worker process host id and routing id.
74 static scoped_refptr
<DevToolsAgentHost
> GetForWorker(int worker_process_id
,
77 // Creates DevToolsAgentHost that communicates to the target by means of
78 // provided |delegate|. |delegate| ownership is passed to the created agent
80 static scoped_refptr
<DevToolsAgentHost
> Create(
81 DevToolsExternalAgentProxyDelegate
* delegate
);
83 using CreateServerSocketCallback
=
84 base::Callback
<scoped_ptr
<net::ServerSocket
>(std::string
*)>;
86 // Creates DevToolsAgentHost for the browser, which works with browser-wide
87 // debugging protocol.
88 static scoped_refptr
<DevToolsAgentHost
> CreateForBrowser(
89 scoped_refptr
<base::MessageLoopProxy
> tethering_message_loop
,
90 const CreateServerSocketCallback
& socket_callback
);
92 static bool IsDebuggerAttached(WebContents
* web_contents
);
94 typedef std::vector
<scoped_refptr
<DevToolsAgentHost
> > List
;
96 // Returns all possible DevToolsAgentHosts.
97 static List
GetOrCreateAll();
99 // Client attaches to this agent host to start debugging it.
100 virtual void AttachClient(DevToolsAgentHostClient
* client
) = 0;
102 // Already attached client detaches from this agent host to stop debugging it.
103 virtual void DetachClient() = 0;
105 // Returns true if there is a client attached.
106 virtual bool IsAttached() = 0;
108 // Sends a message to the agent. Returns true if the message is handled.
109 virtual bool DispatchProtocolMessage(const std::string
& message
) = 0;
111 // Starts inspecting element at position (|x|, |y|) in the specified page.
112 virtual void InspectElement(int x
, int y
) = 0;
114 // Returns the unique id of the agent.
115 virtual std::string
GetId() = 0;
117 // Returns web contents instance for this host if any.
118 virtual WebContents
* GetWebContents() = 0;
120 // Returns related browser context instance if available.
121 virtual BrowserContext
* GetBrowserContext() = 0;
123 // Temporarily detaches render view host from this host. Must be followed by
124 // a call to ConnectWebContents (may leak the host instance otherwise).
125 virtual void DisconnectWebContents() = 0;
127 // Attaches render view host to this host.
128 virtual void ConnectWebContents(WebContents
* web_contents
) = 0;
130 // Returns agent host type.
131 virtual Type
GetType() = 0;
133 // Returns agent host title.
134 virtual std::string
GetTitle() = 0;
136 // Returns url associated with agent host.
137 virtual GURL
GetURL() = 0;
139 // Activates agent host. Returns false if the operation failed.
140 virtual bool Activate() = 0;
142 // Closes agent host. Returns false if the operation failed.
143 virtual bool Close() = 0;
145 // Terminates all debugging sessions and detaches all clients.
146 static void DetachAllClients();
148 typedef base::Callback
<void(DevToolsAgentHost
*, bool attached
)>
151 static void AddAgentStateCallback(const AgentStateCallback
& callback
);
152 static void RemoveAgentStateCallback(const AgentStateCallback
& callback
);
155 friend class base::RefCounted
<DevToolsAgentHost
>;
156 virtual ~DevToolsAgentHost() {}
159 } // namespace content
161 #endif // CONTENT_PUBLIC_BROWSER_DEVTOOLS_AGENT_HOST_H_