Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / browser / devtools_agent_host.h
blob49feaf1d163989d917614b764b88ecfe8302e239
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_
8 #include <string>
9 #include <vector>
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"
18 namespace content {
20 class BrowserContext;
21 class DevToolsExternalAgentProxyDelegate;
22 class WebContents;
24 // Describes interface for managing devtools agents from browser process.
25 class CONTENT_EXPORT DevToolsAgentHost
26 : public base::RefCounted<DevToolsAgentHost> {
27 public:
28 enum Type {
29 // Agent host associated with WebContents.
30 TYPE_WEB_CONTENTS,
32 // Agent host associated with RenderFrameHost.
33 TYPE_FRAME,
35 // Agent host associated with shared worker.
36 TYPE_SHARED_WORKER,
38 // Agent host associated with service worker.
39 TYPE_SERVICE_WORKER,
41 // Agent host associated with DevToolsExternalAgentProxyDelegate.
42 TYPE_EXTERNAL,
45 // Returns DevToolsAgentHost with a given |id| or nullptr of it doesn't exist.
46 static scoped_refptr<DevToolsAgentHost> GetForId(const std::string& id);
48 // Returns DevToolsAgentHost that can be used for inspecting |web_contents|.
49 // New DevToolsAgentHost will be created if it does not exist.
50 static scoped_refptr<DevToolsAgentHost> GetOrCreateFor(
51 WebContents* web_contents);
53 // Returns true iff an instance of DevToolsAgentHost for the |web_contents|
54 // does exist.
55 static bool HasFor(WebContents* web_contents);
57 // Returns DevToolsAgentHost that can be used for inspecting shared worker
58 // with given worker process host id and routing id.
59 static scoped_refptr<DevToolsAgentHost> GetForWorker(int worker_process_id,
60 int worker_route_id);
62 // Creates DevToolsAgentHost that communicates to the target by means of
63 // provided |delegate|. |delegate| ownership is passed to the created agent
64 // host.
65 static scoped_refptr<DevToolsAgentHost> Create(
66 DevToolsExternalAgentProxyDelegate* delegate);
68 static bool IsDebuggerAttached(WebContents* web_contents);
70 typedef std::vector<scoped_refptr<DevToolsAgentHost> > List;
72 // Returns all possible DevToolsAgentHosts.
73 static List GetOrCreateAll();
75 // Client attaches to this agent host to start debugging it.
76 virtual void AttachClient(DevToolsAgentHostClient* client) = 0;
78 // Already attached client detaches from this agent host to stop debugging it.
79 virtual void DetachClient() = 0;
81 // Returns true if there is a client attached.
82 virtual bool IsAttached() = 0;
84 // Sends a message to the agent. Returns true if the message is handled.
85 virtual bool DispatchProtocolMessage(const std::string& message) = 0;
87 // Starts inspecting element at position (|x|, |y|) in the specified page.
88 virtual void InspectElement(int x, int y) = 0;
90 // Returns the unique id of the agent.
91 virtual std::string GetId() = 0;
93 // Returns web contents instance for this host if any.
94 virtual WebContents* GetWebContents() = 0;
96 // Returns related browser context instance if available.
97 virtual BrowserContext* GetBrowserContext() = 0;
99 // Temporarily detaches render view host from this host. Must be followed by
100 // a call to ConnectWebContents (may leak the host instance otherwise).
101 virtual void DisconnectWebContents() = 0;
103 // Attaches render view host to this host.
104 virtual void ConnectWebContents(WebContents* web_contents) = 0;
106 // Returns true if DevToolsAgentHost is for worker.
107 virtual bool IsWorker() const = 0;
109 // Returns agent host type.
110 virtual Type GetType() = 0;
112 // Returns agent host title.
113 virtual std::string GetTitle() = 0;
115 // Returns url associated with agent host.
116 virtual GURL GetURL() = 0;
118 // Activates agent host. Returns false if the operation failed.
119 virtual bool Activate() = 0;
121 // Closes agent host. Returns false if the operation failed.
122 virtual bool Close() = 0;
124 // Terminates all debugging sessions and detaches all clients.
125 static void DetachAllClients();
127 typedef base::Callback<void(DevToolsAgentHost*, bool attached)>
128 AgentStateCallback;
130 static void AddAgentStateCallback(const AgentStateCallback& callback);
131 static void RemoveAgentStateCallback(const AgentStateCallback& callback);
133 protected:
134 friend class base::RefCounted<DevToolsAgentHost>;
135 virtual ~DevToolsAgentHost() {}
138 } // namespace content
140 #endif // CONTENT_PUBLIC_BROWSER_DEVTOOLS_AGENT_HOST_H_