OLD | NEW |
| (Empty) |
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 #include "content/browser/debugger/render_view_devtools_agent_host.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/lazy_instance.h" | |
9 #include "content/browser/debugger/devtools_manager_impl.h" | |
10 #include "content/browser/debugger/render_view_devtools_agent_host.h" | |
11 #include "content/browser/renderer_host/render_process_host_impl.h" | |
12 #include "content/browser/renderer_host/render_view_host_impl.h" | |
13 #include "content/browser/site_instance_impl.h" | |
14 #include "content/browser/web_contents/web_contents_impl.h" | |
15 #include "content/common/devtools_messages.h" | |
16 #include "content/public/browser/content_browser_client.h" | |
17 #include "content/public/browser/devtools_agent_host_registry.h" | |
18 #include "content/public/browser/notification_service.h" | |
19 #include "content/public/browser/notification_types.h" | |
20 | |
21 namespace content { | |
22 | |
23 typedef std::map<RenderViewHost*, RenderViewDevToolsAgentHost*> Instances; | |
24 | |
25 namespace { | |
26 base::LazyInstance<Instances>::Leaky g_instances = LAZY_INSTANCE_INITIALIZER; | |
27 } // namespace | |
28 | |
29 // static | |
30 DevToolsAgentHost* DevToolsAgentHostRegistry::GetDevToolsAgentHost( | |
31 RenderViewHost* rvh) { | |
32 Instances::iterator it = g_instances.Get().find(rvh); | |
33 if (it != g_instances.Get().end()) | |
34 return it->second; | |
35 return new RenderViewDevToolsAgentHost(rvh); | |
36 } | |
37 | |
38 // static | |
39 RenderViewHost* DevToolsAgentHostRegistry::GetRenderViewHost( | |
40 DevToolsAgentHost* agent_host) { | |
41 for (Instances::iterator it = g_instances.Get().begin(); | |
42 it != g_instances.Get().end(); ++it) { | |
43 if (agent_host == it->second) | |
44 return it->first; | |
45 } | |
46 return NULL; | |
47 } | |
48 | |
49 // static | |
50 bool DevToolsAgentHostRegistry::HasDevToolsAgentHost(RenderViewHost* rvh) { | |
51 if (g_instances == NULL) | |
52 return false; | |
53 Instances::iterator it = g_instances.Get().find(rvh); | |
54 return it != g_instances.Get().end(); | |
55 } | |
56 | |
57 bool DevToolsAgentHostRegistry::IsDebuggerAttached(WebContents* web_contents) { | |
58 if (g_instances == NULL) | |
59 return false; | |
60 DevToolsManager* devtools_manager = DevToolsManager::GetInstance(); | |
61 if (!devtools_manager) | |
62 return false; | |
63 RenderViewHostDelegate* delegate = | |
64 static_cast<WebContentsImpl*>(web_contents); | |
65 for (Instances::iterator it = g_instances.Get().begin(); | |
66 it != g_instances.Get().end(); ++it) { | |
67 if (it->first->GetDelegate() != delegate) | |
68 continue; | |
69 if (devtools_manager->GetDevToolsClientHostFor(it->second)) | |
70 return true; | |
71 } | |
72 return false; | |
73 } | |
74 | |
75 RenderViewDevToolsAgentHost::RenderViewDevToolsAgentHost( | |
76 RenderViewHost* rvh) | |
77 : RenderViewHostObserver(rvh), | |
78 render_view_host_(rvh) { | |
79 g_instances.Get()[rvh] = this; | |
80 } | |
81 | |
82 void RenderViewDevToolsAgentHost::SendMessageToAgent(IPC::Message* msg) { | |
83 msg->set_routing_id(render_view_host_->GetRoutingID()); | |
84 render_view_host_->Send(msg); | |
85 } | |
86 | |
87 void RenderViewDevToolsAgentHost::NotifyClientAttaching() { | |
88 NotificationService::current()->Notify( | |
89 NOTIFICATION_DEVTOOLS_AGENT_ATTACHED, | |
90 Source<BrowserContext>( | |
91 render_view_host_->GetSiteInstance()->GetProcess()-> | |
92 GetBrowserContext()), | |
93 Details<RenderViewHost>(render_view_host_)); | |
94 } | |
95 | |
96 void RenderViewDevToolsAgentHost::NotifyClientDetaching() { | |
97 NotificationService::current()->Notify( | |
98 NOTIFICATION_DEVTOOLS_AGENT_DETACHED, | |
99 Source<BrowserContext>( | |
100 render_view_host_->GetSiteInstance()->GetProcess()-> | |
101 GetBrowserContext()), | |
102 Details<RenderViewHost>(render_view_host_)); | |
103 } | |
104 | |
105 int RenderViewDevToolsAgentHost::GetRenderProcessId() { | |
106 return render_view_host_->GetProcess()->GetID(); | |
107 } | |
108 | |
109 RenderViewDevToolsAgentHost::~RenderViewDevToolsAgentHost() { | |
110 g_instances.Get().erase(render_view_host_); | |
111 } | |
112 | |
113 void RenderViewDevToolsAgentHost::RenderViewHostDestroyed( | |
114 RenderViewHost* rvh) { | |
115 NotifyCloseListener(); | |
116 delete this; | |
117 } | |
118 | |
119 bool RenderViewDevToolsAgentHost::OnMessageReceived( | |
120 const IPC::Message& message) { | |
121 bool handled = true; | |
122 IPC_BEGIN_MESSAGE_MAP(RenderViewDevToolsAgentHost, message) | |
123 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, | |
124 OnDispatchOnInspectorFrontend) | |
125 IPC_MESSAGE_HANDLER(DevToolsHostMsg_SaveAgentRuntimeState, | |
126 OnSaveAgentRuntimeState) | |
127 IPC_MESSAGE_HANDLER(DevToolsHostMsg_ClearBrowserCache, OnClearBrowserCache) | |
128 IPC_MESSAGE_HANDLER(DevToolsHostMsg_ClearBrowserCookies, | |
129 OnClearBrowserCookies) | |
130 IPC_MESSAGE_UNHANDLED(handled = false) | |
131 IPC_END_MESSAGE_MAP() | |
132 return handled; | |
133 } | |
134 | |
135 void RenderViewDevToolsAgentHost::OnSaveAgentRuntimeState( | |
136 const std::string& state) { | |
137 DevToolsManagerImpl::GetInstance()->SaveAgentRuntimeState(this, state); | |
138 } | |
139 | |
140 void RenderViewDevToolsAgentHost::OnDispatchOnInspectorFrontend( | |
141 const std::string& message) { | |
142 DevToolsManagerImpl::GetInstance()->DispatchOnInspectorFrontend( | |
143 this, message); | |
144 } | |
145 | |
146 void RenderViewDevToolsAgentHost::OnClearBrowserCache() { | |
147 GetContentClient()->browser()->ClearCache(render_view_host_); | |
148 } | |
149 | |
150 void RenderViewDevToolsAgentHost::OnClearBrowserCookies() { | |
151 GetContentClient()->browser()->ClearCookies(render_view_host_); | |
152 } | |
153 | |
154 } // namespace content | |
OLD | NEW |