| 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/devtools_frontend_host.h" | 5 #include "content/browser/devtools/devtools_frontend_host.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" |
| 8 #include "base/memory/weak_ptr.h" |
| 7 #include "content/browser/devtools/devtools_manager_impl.h" | 9 #include "content/browser/devtools/devtools_manager_impl.h" |
| 10 #include "content/browser/power_profiler/power_profiler_service.h" |
| 8 #include "content/browser/renderer_host/render_view_host_impl.h" | 11 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 9 #include "content/browser/web_contents/web_contents_impl.h" | 12 #include "content/browser/web_contents/web_contents_impl.h" |
| 10 #include "content/common/devtools_messages.h" | 13 #include "content/common/devtools_messages.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/devtools_client_host.h" | 15 #include "content/public/browser/devtools_client_host.h" |
| 12 #include "content/public/browser/devtools_frontend_host_delegate.h" | 16 #include "content/public/browser/devtools_frontend_host_delegate.h" |
| 13 | 17 |
| 14 namespace content { | 18 namespace content { |
| 15 | 19 |
| 16 // static | 20 // static |
| 17 DevToolsClientHost* DevToolsClientHost::CreateDevToolsFrontendHost( | 21 DevToolsClientHost* DevToolsClientHost::CreateDevToolsFrontendHost( |
| 18 WebContents* client_web_contents, | 22 WebContents* client_web_contents, |
| 19 DevToolsFrontendHostDelegate* delegate) { | 23 DevToolsFrontendHostDelegate* delegate) { |
| 20 return new DevToolsFrontendHost( | 24 return new DevToolsFrontendHost( |
| 21 static_cast<WebContentsImpl*>(client_web_contents), delegate); | 25 static_cast<WebContentsImpl*>(client_web_contents), delegate); |
| 22 } | 26 } |
| 23 | 27 |
| 24 // static | 28 // static |
| 25 void DevToolsClientHost::SetupDevToolsFrontendClient( | 29 void DevToolsClientHost::SetupDevToolsFrontendClient( |
| 26 RenderViewHost* frontend_rvh) { | 30 RenderViewHost* frontend_rvh) { |
| 27 frontend_rvh->Send(new DevToolsMsg_SetupDevToolsClient( | 31 frontend_rvh->Send(new DevToolsMsg_SetupDevToolsClient( |
| 28 frontend_rvh->GetRoutingID())); | 32 frontend_rvh->GetRoutingID())); |
| 29 } | 33 } |
| 30 | 34 |
| 31 DevToolsFrontendHost::DevToolsFrontendHost( | 35 DevToolsFrontendHost::DevToolsFrontendHost( |
| 32 WebContentsImpl* web_contents, | 36 WebContentsImpl* web_contents, |
| 33 DevToolsFrontendHostDelegate* delegate) | 37 DevToolsFrontendHostDelegate* delegate) |
| 34 : WebContentsObserver(web_contents), | 38 : WebContentsObserver(web_contents), |
| 35 delegate_(delegate) { | 39 delegate_(delegate), |
| 40 weak_factory_(this), |
| 41 power_profiler_host_(new DevToolsPowerProfilerHost( |
| 42 weak_factory_.GetWeakPtr())) { |
| 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 36 } | 44 } |
| 37 | 45 |
| 38 DevToolsFrontendHost::~DevToolsFrontendHost() { | 46 DevToolsFrontendHost::~DevToolsFrontendHost() { |
| 47 power_profiler_host_->UnRegister(); |
| 39 DevToolsManager::GetInstance()->ClientHostClosing(this); | 48 DevToolsManager::GetInstance()->ClientHostClosing(this); |
| 40 } | 49 } |
| 41 | 50 |
| 42 void DevToolsFrontendHost::DispatchOnInspectorFrontend( | 51 void DevToolsFrontendHost::DispatchOnInspectorFrontend( |
| 43 const std::string& message) { | 52 const std::string& message) { |
| 44 if (!web_contents()) | 53 if (!web_contents()) |
| 45 return; | 54 return; |
| 46 RenderViewHostImpl* target_host = | 55 RenderViewHostImpl* target_host = |
| 47 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost()); | 56 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost()); |
| 48 target_host->Send(new DevToolsClientMsg_DispatchOnInspectorFrontend( | 57 target_host->Send(new DevToolsClientMsg_DispatchOnInspectorFrontend( |
| 49 target_host->GetRoutingID(), | 58 target_host->GetRoutingID(), |
| 50 message)); | 59 message)); |
| 51 } | 60 } |
| 52 | 61 |
| 53 void DevToolsFrontendHost::InspectedContentsClosing() { | 62 void DevToolsFrontendHost::InspectedContentsClosing() { |
| 54 delegate_->InspectedContentsClosing(); | 63 delegate_->InspectedContentsClosing(); |
| 55 } | 64 } |
| 56 | 65 |
| 57 void DevToolsFrontendHost::ReplacedWithAnotherClient() { | 66 void DevToolsFrontendHost::ReplacedWithAnotherClient() { |
| 58 } | 67 } |
| 59 | 68 |
| 60 bool DevToolsFrontendHost::OnMessageReceived( | 69 bool DevToolsFrontendHost::OnMessageReceived( |
| 61 const IPC::Message& message) { | 70 const IPC::Message& message) { |
| 62 bool handled = true; | 71 bool handled = true; |
| 63 IPC_BEGIN_MESSAGE_MAP(DevToolsFrontendHost, message) | 72 IPC_BEGIN_MESSAGE_MAP(DevToolsFrontendHost, message) |
| 64 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_DispatchOnInspectorBackend, | 73 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_DispatchOnInspectorBackend, |
| 65 OnDispatchOnInspectorBackend) | 74 OnDispatchOnInspectorBackend) |
| 66 IPC_MESSAGE_HANDLER(DevToolsHostMsg_DispatchOnEmbedder, | 75 IPC_MESSAGE_HANDLER(DevToolsHostMsg_DispatchOnEmbedder, |
| 67 OnDispatchOnEmbedder) | 76 OnDispatchOnEmbedder) |
| 77 IPC_MESSAGE_HANDLER(DevToolsHostMsg_PowerProfileSupported, |
| 78 OnPowerProfileStatus) |
| 79 IPC_MESSAGE_HANDLER(DevToolsHostMsg_StartPowerProfile, OnStartProfile) |
| 80 IPC_MESSAGE_HANDLER(DevToolsHostMsg_StopPowerProfile, OnStopProfile) |
| 81 IPC_MESSAGE_HANDLER(DevToolsHostMsg_SetPowerProfileResolution, |
| 82 OnSetResolution) |
| 68 IPC_MESSAGE_UNHANDLED(handled = false) | 83 IPC_MESSAGE_UNHANDLED(handled = false) |
| 69 IPC_END_MESSAGE_MAP() | 84 IPC_END_MESSAGE_MAP() |
| 70 return handled; | 85 return handled; |
| 71 } | 86 } |
| 72 | 87 |
| 73 void DevToolsFrontendHost::RenderProcessGone( | 88 void DevToolsFrontendHost::RenderProcessGone( |
| 74 base::TerminationStatus status) { | 89 base::TerminationStatus status) { |
| 75 switch(status) { | 90 switch(status) { |
| 76 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: | 91 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: |
| 77 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: | 92 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: |
| 78 case base::TERMINATION_STATUS_PROCESS_CRASHED: | 93 case base::TERMINATION_STATUS_PROCESS_CRASHED: |
| 79 DevToolsManager::GetInstance()->ClientHostClosing(this); | 94 DevToolsManager::GetInstance()->ClientHostClosing(this); |
| 80 break; | 95 break; |
| 81 default: | 96 default: |
| 82 break; | 97 break; |
| 83 } | 98 } |
| 84 } | 99 } |
| 85 | 100 |
| 86 void DevToolsFrontendHost::OnDispatchOnInspectorBackend( | 101 void DevToolsFrontendHost::OnDispatchOnInspectorBackend( |
| 87 const std::string& message) { | 102 const std::string& message) { |
| 88 DevToolsManagerImpl::GetInstance()->DispatchOnInspectorBackend(this, message); | 103 DevToolsManagerImpl::GetInstance()->DispatchOnInspectorBackend(this, message); |
| 89 } | 104 } |
| 90 | 105 |
| 91 void DevToolsFrontendHost::OnDispatchOnEmbedder( | 106 void DevToolsFrontendHost::OnDispatchOnEmbedder( |
| 92 const std::string& message) { | 107 const std::string& message) { |
| 93 delegate_->DispatchOnEmbedder(message); | 108 delegate_->DispatchOnEmbedder(message); |
| 94 } | 109 } |
| 95 | 110 |
| 111 void DevToolsFrontendHost::OnPowerProfileStatus() { |
| 112 bool is_supported = true; |
| 113 if (!PowerProfilerService::Get() || PowerProfilerService::UNINITIALIZED == |
| 114 PowerProfilerService::Get()->status()) |
| 115 is_supported = false; |
| 116 std::string json_string; |
| 117 base::DictionaryValue message_object; |
| 118 base::DictionaryValue* params = new base::DictionaryValue(); |
| 119 params->Set("isSupported", base::Value::CreateBooleanValue(is_supported)); |
| 120 message_object.Set("params", params); |
| 121 message_object.SetString("method", "Power.powerProfileSupported"); |
| 122 base::JSONWriter::Write(&message_object, &json_string); |
| 123 DispatchOnInspectorFrontend(json_string); |
| 124 } |
| 125 |
| 126 void DevToolsFrontendHost::OnStartProfile() { |
| 127 power_profiler_host_->Register(); |
| 128 } |
| 129 |
| 130 void DevToolsFrontendHost::OnStopProfile() { |
| 131 power_profiler_host_->UnRegister(); |
| 132 } |
| 133 |
| 134 void DevToolsFrontendHost::OnSetResolution(unsigned resolution) { |
| 135 assert (resolution >= 0 && |
| 136 resolution < static_cast<unsigned>(PowerProfilerHost::TYPE_COUNT)); |
| 137 power_profiler_host_->SetResolution( |
| 138 static_cast<PowerProfilerHost::Resolution>(resolution)); |
| 139 } |
| 140 |
| 96 } // namespace content | 141 } // namespace content |
| OLD | NEW |