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_AGENT_HOST_IMPL_H_
6 #define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_AGENT_HOST_IMPL_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 
12 #include "base/compiler_specific.h"
13 #include "base/containers/flat_map.h"
14 #include "base/containers/flat_set.h"
15 #include "base/macros.h"
16 #include "base/process/kill.h"
17 #include "content/browser/devtools/devtools_io_context.h"
18 #include "content/browser/devtools/devtools_renderer_channel.h"
19 #include "content/browser/devtools/devtools_session.h"
20 #include "content/common/content_export.h"
21 #include "content/public/browser/certificate_request_result_type.h"
22 #include "content/public/browser/devtools_agent_host.h"
23 
24 namespace content {
25 
26 class BrowserContext;
27 
28 // Describes interface for managing devtools agents from the browser process.
29 class CONTENT_EXPORT DevToolsAgentHostImpl : public DevToolsAgentHost {
30  public:
31   // DevToolsAgentHost implementation.
32   bool AttachClient(DevToolsAgentHostClient* client) override;
33   bool DetachClient(DevToolsAgentHostClient* client) override;
34   void DispatchProtocolMessage(DevToolsAgentHostClient* client,
35                                base::span<const uint8_t> message) override;
36   bool IsAttached() override;
37   void InspectElement(RenderFrameHost* frame_host, int x, int y) override;
38   std::string GetId() override;
39   std::string CreateIOStreamFromData(
40       scoped_refptr<base::RefCountedMemory> data) override;
41   std::string GetParentId() override;
42   std::string GetOpenerId() override;
43   std::string GetDescription() override;
44   GURL GetFaviconURL() override;
45   std::string GetFrontendURL() override;
46   base::TimeTicks GetLastActivityTime() override;
47   BrowserContext* GetBrowserContext() override;
48   WebContents* GetWebContents() override;
49   void DisconnectWebContents() override;
50   void ConnectWebContents(WebContents* wc) override;
51 
52   bool Inspect();
53 
54   template <typename Handler>
HandlersByName(const std::string & name)55   std::vector<Handler*> HandlersByName(const std::string& name) {
56     std::vector<Handler*> result;
57     if (sessions_.empty())
58       return result;
59     for (DevToolsSession* session : sessions_) {
60       auto it = session->handlers().find(name);
61       if (it != session->handlers().end())
62         result.push_back(static_cast<Handler*>(it->second.get()));
63     }
64     return result;
65   }
66 
67  protected:
68   DevToolsAgentHostImpl(const std::string& id);
69   ~DevToolsAgentHostImpl() override;
70 
71   static bool ShouldForceCreation();
72 
73   // Returning |false| will block the attach.
74   virtual bool AttachSession(DevToolsSession* session);
75   virtual void DetachSession(DevToolsSession* session);
76   virtual void UpdateRendererChannel(bool force);
77 
78   void NotifyCreated();
79   void NotifyNavigated();
80   void NotifyCrashed(base::TerminationStatus status);
81   void ForceDetachAllSessions();
82   void ForceDetachRestrictedSessions(
83       const std::vector<DevToolsSession*>& restricted_sessions);
GetIOContext()84   DevToolsIOContext* GetIOContext() { return &io_context_; }
GetRendererChannel()85   DevToolsRendererChannel* GetRendererChannel() { return &renderer_channel_; }
86 
sessions()87   const std::vector<DevToolsSession*>& sessions() const { return sessions_; }
88 
89  private:
90   friend class DevToolsAgentHost;  // for static methods
91   friend class DevToolsSession;
92   friend class DevToolsRendererChannel;
93 
94   bool AttachInternal(std::unique_ptr<DevToolsSession> session);
95   void DetachInternal(DevToolsSession* session);
96   void NotifyAttached();
97   void NotifyDetached();
98   void NotifyDestroyed();
99   DevToolsSession* SessionByClient(DevToolsAgentHostClient* client);
100 
101   const std::string id_;
102   std::vector<DevToolsSession*> sessions_;
103   base::flat_map<DevToolsAgentHostClient*, std::unique_ptr<DevToolsSession>>
104       session_by_client_;
105   DevToolsIOContext io_context_;
106   DevToolsRendererChannel renderer_channel_;
107   static int s_force_creation_count_;
108 
109   DISALLOW_COPY_AND_ASSIGN(DevToolsAgentHostImpl);
110 };
111 
112 }  // namespace content
113 
114 #endif  // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_AGENT_HOST_IMPL_H_
115