| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/devtools/render_view_devtools_agent_host.h" | 5 #include "content/browser/devtools/render_view_devtools_agent_host.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/utf_string_conversions.h" |
| 10 #include "content/browser/child_process_security_policy_impl.h" | 11 #include "content/browser/child_process_security_policy_impl.h" |
| 11 #include "content/browser/devtools/devtools_manager_impl.h" | 12 #include "content/browser/devtools/devtools_manager_impl.h" |
| 12 #include "content/browser/devtools/devtools_protocol.h" | 13 #include "content/browser/devtools/devtools_protocol.h" |
| 13 #include "content/browser/devtools/renderer_overrides_handler.h" | 14 #include "content/browser/devtools/renderer_overrides_handler.h" |
| 14 #include "content/browser/renderer_host/render_process_host_impl.h" | 15 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 15 #include "content/browser/renderer_host/render_view_host_impl.h" | 16 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 16 #include "content/browser/site_instance_impl.h" | 17 #include "content/browser/site_instance_impl.h" |
| 17 #include "content/browser/web_contents/web_contents_impl.h" | 18 #include "content/browser/web_contents/web_contents_impl.h" |
| 18 #include "content/common/devtools_messages.h" | 19 #include "content/common/devtools_messages.h" |
| 19 #include "content/public/browser/content_browser_client.h" | 20 #include "content/public/browser/content_browser_client.h" |
| 21 #include "content/public/browser/favicon_status.h" |
| 22 #include "content/public/browser/navigation_entry.h" |
| 20 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/notification_types.h" | 24 #include "content/public/browser/notification_types.h" |
| 22 #include "content/public/browser/render_widget_host_view.h" | 25 #include "content/public/browser/render_widget_host_view.h" |
| 26 #include "net/base/escape.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDevToolsAgent.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDevToolsAgent.h" |
| 24 #include "ui/gfx/image/image_skia.h" | 28 #include "ui/gfx/image/image_skia.h" |
| 25 #include "ui/gfx/native_widget_types.h" | 29 #include "ui/gfx/native_widget_types.h" |
| 26 #include "ui/snapshot/snapshot.h" | 30 #include "ui/snapshot/snapshot.h" |
| 27 | 31 |
| 28 | 32 |
| 29 namespace content { | 33 namespace content { |
| 30 | 34 |
| 31 typedef std::vector<RenderViewDevToolsAgentHost*> Instances; | 35 typedef std::vector<RenderViewDevToolsAgentHost*> Instances; |
| 32 | 36 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 RenderViewHost* rvh = (*it)->render_view_host_; | 101 RenderViewHost* rvh = (*it)->render_view_host_; |
| 98 if (rvh && rvh->GetDelegate() != delegate) | 102 if (rvh && rvh->GetDelegate() != delegate) |
| 99 continue; | 103 continue; |
| 100 if (devtools_manager->GetDevToolsClientHostFor(*it)) | 104 if (devtools_manager->GetDevToolsClientHostFor(*it)) |
| 101 return true; | 105 return true; |
| 102 } | 106 } |
| 103 return false; | 107 return false; |
| 104 } | 108 } |
| 105 | 109 |
| 106 // static | 110 // static |
| 107 int DevToolsAgentHost::DisconnectRenderViewHost(RenderViewHost* rvh) { | 111 const std::string DevToolsAgentHost::EmptyCookie; |
| 112 |
| 113 // static |
| 114 std::string DevToolsAgentHost::DisconnectRenderViewHost(RenderViewHost* rvh) { |
| 108 RenderViewDevToolsAgentHost* agent_host = FindAgentHost(rvh); | 115 RenderViewDevToolsAgentHost* agent_host = FindAgentHost(rvh); |
| 109 if (!agent_host) | 116 if (!agent_host) |
| 110 return -1; | 117 return EmptyCookie; |
| 111 agent_host->DisconnectRenderViewHost(); | 118 agent_host->DisconnectRenderViewHost(); |
| 112 return agent_host->id(); | 119 return agent_host->id(); |
| 113 } | 120 } |
| 114 | 121 |
| 115 // static | 122 // static |
| 116 void DevToolsAgentHost::ConnectRenderViewHost(int cookie, | 123 void DevToolsAgentHost::ConnectRenderViewHost(const std::string& cookie, |
| 117 RenderViewHost* rvh) { | 124 RenderViewHost* rvh) { |
| 118 for (Instances::iterator it = g_instances.Get().begin(); | 125 for (Instances::iterator it = g_instances.Get().begin(); |
| 119 it != g_instances.Get().end(); ++it) { | 126 it != g_instances.Get().end(); ++it) { |
| 120 if (cookie == (*it)->id()) { | 127 if (cookie == (*it)->id()) { |
| 121 (*it)->ConnectRenderViewHost(rvh, true); | 128 (*it)->ConnectRenderViewHost(rvh, true); |
| 122 break; | 129 break; |
| 123 } | 130 } |
| 124 } | 131 } |
| 125 } | 132 } |
| 126 | 133 |
| 127 // static | 134 // static |
| 128 void RenderViewDevToolsAgentHost::OnCancelPendingNavigation( | 135 void RenderViewDevToolsAgentHost::OnCancelPendingNavigation( |
| 129 RenderViewHost* pending, | 136 RenderViewHost* pending, |
| 130 RenderViewHost* current) { | 137 RenderViewHost* current) { |
| 131 int cookie = DevToolsAgentHost::DisconnectRenderViewHost(pending); | 138 std::string cookie = DevToolsAgentHost::DisconnectRenderViewHost(pending); |
| 132 if (cookie != -1) | 139 if (cookie != EmptyCookie) |
| 133 DevToolsAgentHost::ConnectRenderViewHost(cookie, current); | 140 DevToolsAgentHost::ConnectRenderViewHost(cookie, current); |
| 134 } | 141 } |
| 135 | 142 |
| 136 RenderViewDevToolsAgentHost::RenderViewDevToolsAgentHost( | 143 RenderViewDevToolsAgentHost::RenderViewDevToolsAgentHost( |
| 137 RenderViewHost* rvh) | 144 RenderViewHost* rvh) |
| 138 : overrides_handler_(new RendererOverridesHandler(this)) { | 145 : overrides_handler_(new RendererOverridesHandler(this)) { |
| 139 ConnectRenderViewHost(rvh, false); | 146 ConnectRenderViewHost(rvh, false); |
| 140 g_instances.Get().push_back(this); | 147 g_instances.Get().push_back(this); |
| 141 RenderViewHostDelegate* delegate = render_view_host_->GetDelegate(); | 148 RenderViewHostDelegate* delegate = render_view_host_->GetDelegate(); |
| 142 if (delegate && delegate->GetAsWebContents()) | 149 if (delegate && delegate->GetAsWebContents()) |
| 143 Observe(delegate->GetAsWebContents()); | 150 Observe(delegate->GetAsWebContents()); |
| 144 } | 151 } |
| 145 | 152 |
| 146 RenderViewHost* RenderViewDevToolsAgentHost::GetRenderViewHost() { | 153 RenderViewHost* RenderViewDevToolsAgentHost::GetRenderViewHost() { |
| 147 return render_view_host_; | 154 return render_view_host_; |
| 148 } | 155 } |
| 149 | 156 |
| 157 std::string RenderViewDevToolsAgentHost::title() { |
| 158 if (!render_view_host_) |
| 159 return ""; |
| 160 |
| 161 WebContents* web_contents = |
| 162 render_view_host_->GetDelegate()->GetAsWebContents(); |
| 163 if (!web_contents) |
| 164 return ""; |
| 165 |
| 166 return UTF16ToUTF8(net::EscapeForHTML(web_contents->GetTitle())); |
| 167 } |
| 168 |
| 169 GURL RenderViewDevToolsAgentHost::url() { |
| 170 if (!render_view_host_) |
| 171 return GURL(); |
| 172 |
| 173 return render_view_host_->GetDelegate()->GetURL(); |
| 174 } |
| 175 |
| 176 GURL RenderViewDevToolsAgentHost::thumbnail_url() { |
| 177 NavigationEntry* entry = GetNavigationEntry(); |
| 178 if (!entry) |
| 179 return GURL(); |
| 180 |
| 181 return GURL("/thumb/" + entry->GetURL().spec()); |
| 182 } |
| 183 |
| 184 GURL RenderViewDevToolsAgentHost::favicon_url() { |
| 185 NavigationEntry* entry = GetNavigationEntry(); |
| 186 if (!entry) |
| 187 return GURL(); |
| 188 |
| 189 return entry->GetFavicon().url; |
| 190 } |
| 191 |
| 150 void RenderViewDevToolsAgentHost::DispatchOnInspectorBackend( | 192 void RenderViewDevToolsAgentHost::DispatchOnInspectorBackend( |
| 151 const std::string& message) { | 193 const std::string& message) { |
| 152 std::string error_message; | 194 std::string error_message; |
| 153 scoped_ptr<DevToolsProtocol::Command> command( | 195 scoped_ptr<DevToolsProtocol::Command> command( |
| 154 DevToolsProtocol::ParseCommand(message, &error_message)); | 196 DevToolsProtocol::ParseCommand(message, &error_message)); |
| 155 if (!command) { | 197 if (!command) { |
| 156 OnDispatchOnInspectorFrontend(error_message); | 198 OnDispatchOnInspectorFrontend(error_message); |
| 157 return; | 199 return; |
| 158 } | 200 } |
| 159 | 201 |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 &png, | 381 &png, |
| 340 snapshot_bounds)) | 382 snapshot_bounds)) |
| 341 return false; | 383 return false; |
| 342 | 384 |
| 343 return base::Base64Encode(base::StringPiece( | 385 return base::Base64Encode(base::StringPiece( |
| 344 reinterpret_cast<char*>(&*png.begin()), | 386 reinterpret_cast<char*>(&*png.begin()), |
| 345 png.size()), | 387 png.size()), |
| 346 base_64_data); | 388 base_64_data); |
| 347 } | 389 } |
| 348 | 390 |
| 391 NavigationEntry* RenderViewDevToolsAgentHost::GetNavigationEntry() { |
| 392 if (!render_view_host_) |
| 393 return NULL; |
| 394 |
| 395 WebContents* web_contents = |
| 396 render_view_host_->GetDelegate()->GetAsWebContents(); |
| 397 if (!web_contents) |
| 398 return NULL; |
| 399 |
| 400 NavigationController& controller = web_contents->GetController(); |
| 401 NavigationEntry* entry = controller.GetActiveEntry(); |
| 402 if (!entry || !entry->GetURL().is_valid()) |
| 403 return NULL; |
| 404 |
| 405 return entry; |
| 406 } |
| 407 |
| 349 } // namespace content | 408 } // namespace content |
| OLD | NEW |