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_MANAGER_IMPL_H_ 6 #define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_MANAGER_IMPL_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 12 #include "base/compiler_specific.h" 13 #include "base/memory/singleton.h" 14 #include "content/browser/devtools/devtools_agent_host_impl.h" 15 #include "content/common/content_export.h" 16 #include "content/public/browser/devtools_client_host.h" 17 #include "content/public/browser/devtools_manager.h" 18 19 class GURL; 20 21 namespace IPC { 22 class Message; 23 } 24 25 namespace content { 26 27 class BrowserContext; 28 class DevToolsManagerDelegate; 29 class RenderViewHost; 30 31 // This class is a singleton that manages DevToolsClientHost instances and 32 // routes messages between developer tools clients and agents. 33 // 34 // Methods below that accept inspected RenderViewHost as a parameter are 35 // just convenience methods that call corresponding methods accepting 36 // DevToolAgentHost. 37 class CONTENT_EXPORT DevToolsManagerImpl 38 : public DevToolsAgentHostImpl::CloseListener, 39 public DevToolsManager { 40 public: 41 // Returns single instance of this class. The instance is destroyed on the 42 // browser main loop exit so this method MUST NOT be called after that point. 43 static DevToolsManagerImpl* GetInstance(); 44 45 DevToolsManagerImpl(); 46 virtual ~DevToolsManagerImpl(); 47 48 // Opens the inspector for |agent_host|. 49 void Inspect(BrowserContext* browser_context, DevToolsAgentHost* agent_host); 50 51 void DispatchOnInspectorFrontend(DevToolsAgentHost* agent_host, 52 const std::string& message); 53 54 DevToolsManagerDelegate* delegate() const { return delegate_.get(); } 55 56 // DevToolsManager implementation 57 virtual bool DispatchOnInspectorBackend(DevToolsClientHost* from, 58 const std::string& message) OVERRIDE; 59 virtual void CloseAllClientHosts() OVERRIDE; 60 virtual DevToolsAgentHost* GetDevToolsAgentHostFor( 61 DevToolsClientHost* client_host) OVERRIDE; 62 virtual void RegisterDevToolsClientHostFor( 63 DevToolsAgentHost* agent_host, 64 DevToolsClientHost* client_host) OVERRIDE; 65 virtual void ClientHostClosing(DevToolsClientHost* host) OVERRIDE; 66 virtual void AddAgentStateCallback(const Callback& callback) OVERRIDE; 67 virtual void RemoveAgentStateCallback(const Callback& callback) OVERRIDE; 68 69 private: 70 friend class DevToolsAgentHostImpl; 71 friend class RenderViewDevToolsAgentHost; 72 friend struct DefaultSingletonTraits<DevToolsManagerImpl>; 73 74 // DevToolsAgentHost::CloseListener implementation. 75 virtual void AgentHostClosing(DevToolsAgentHostImpl* host) OVERRIDE; 76 77 void BindClientHost(DevToolsAgentHostImpl* agent_host, 78 DevToolsClientHost* client_host); 79 void UnbindClientHost(DevToolsAgentHostImpl* agent_host, 80 DevToolsClientHost* client_host); 81 82 DevToolsClientHost* GetDevToolsClientHostFor( 83 DevToolsAgentHostImpl* agent_host); 84 85 void UnregisterDevToolsClientHostFor(DevToolsAgentHostImpl* agent_host); 86 87 void NotifyObservers(DevToolsAgentHost* agent_host, bool attached); 88 89 // These two maps are for tracking dependencies between inspected contents and 90 // their DevToolsClientHosts. They are useful for routing devtools messages 91 // and allow us to have at most one devtools client host per contents. 92 // 93 // DevToolsManagerImpl starts listening to DevToolsClientHosts when they are 94 // put into these maps and removes them when they are closing. 95 typedef std::map<DevToolsAgentHostImpl*, DevToolsClientHost*> 96 AgentToClientHostMap; 97 AgentToClientHostMap agent_to_client_host_; 98 99 typedef std::map<DevToolsClientHost*, scoped_refptr<DevToolsAgentHostImpl> > 100 ClientToAgentHostMap; 101 ClientToAgentHostMap client_to_agent_host_; 102 103 typedef std::vector<const Callback*> CallbackContainer; 104 CallbackContainer callbacks_; 105 106 scoped_ptr<DevToolsManagerDelegate> delegate_; 107 108 DISALLOW_COPY_AND_ASSIGN(DevToolsManagerImpl); 109 }; 110 111 } // namespace content 112 113 #endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_MANAGER_IMPL_H_ 114