| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/devtools/chrome_devtools_manager_delegate.h" | 5 #include "chrome/browser/devtools/chrome_devtools_manager_delegate.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/devtools/devtools_network_conditions.h" | |
| 9 #include "chrome/browser/devtools/devtools_network_controller.h" | |
| 10 #include "chrome/browser/devtools/devtools_protocol.h" | |
| 11 #include "chrome/browser/devtools/devtools_protocol_constants.h" | |
| 12 #include "chrome/browser/devtools/devtools_window.h" | 8 #include "chrome/browser/devtools/devtools_window.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "content/public/browser/devtools_agent_host.h" | 10 #include "content/public/browser/devtools_agent_host.h" |
| 16 #include "content/public/browser/render_process_host.h" | |
| 17 #include "content/public/browser/render_view_host.h" | |
| 18 #include "content/public/browser/site_instance.h" | |
| 19 #include "content/public/browser/web_contents.h" | |
| 20 | 11 |
| 21 ChromeDevToolsManagerDelegate::ChromeDevToolsManagerDelegate() { | 12 ChromeDevToolsManagerDelegate::ChromeDevToolsManagerDelegate() |
| 13 : network_protocol_handler_(new DevToolsNetworkProtocolHandler()) { |
| 22 } | 14 } |
| 23 | 15 |
| 24 ChromeDevToolsManagerDelegate::~ChromeDevToolsManagerDelegate() { | 16 ChromeDevToolsManagerDelegate::~ChromeDevToolsManagerDelegate() { |
| 25 } | 17 } |
| 26 | 18 |
| 27 void ChromeDevToolsManagerDelegate::Inspect( | 19 void ChromeDevToolsManagerDelegate::Inspect( |
| 28 content::BrowserContext* browser_context, | 20 content::BrowserContext* browser_context, |
| 29 content::DevToolsAgentHost* agent_host) { | 21 content::DevToolsAgentHost* agent_host) { |
| 30 if (!agent_host->IsWorker()) { | 22 if (!agent_host->IsWorker()) { |
| 31 // TODO(horo): Support other types of DevToolsAgentHost when necessary. | 23 // TODO(horo): Support other types of DevToolsAgentHost when necessary. |
| 32 NOTREACHED() << "Inspect() only supports workers."; | 24 NOTREACHED() << "Inspect() only supports workers."; |
| 33 } | 25 } |
| 34 #if !defined(OS_ANDROID) | 26 #if !defined(OS_ANDROID) |
| 35 if (Profile* profile = Profile::FromBrowserContext(browser_context)) | 27 if (Profile* profile = Profile::FromBrowserContext(browser_context)) |
| 36 DevToolsWindow::OpenDevToolsWindowForWorker(profile, agent_host); | 28 DevToolsWindow::OpenDevToolsWindowForWorker(profile, agent_host); |
| 37 #endif | 29 #endif |
| 38 } | 30 } |
| 39 | 31 |
| 40 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( | 32 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( |
| 41 content::DevToolsAgentHost* agent_host, | 33 content::DevToolsAgentHost* agent_host, |
| 42 base::DictionaryValue* command_dict) { | 34 base::DictionaryValue* command_dict) { |
| 43 scoped_ptr<DevToolsProtocol::Command> command( | 35 return network_protocol_handler_->HandleCommand(agent_host, command_dict); |
| 44 DevToolsProtocol::ParseCommand(command_dict)); | |
| 45 if (!command) | |
| 46 return NULL; | |
| 47 | |
| 48 namespace network = ::chrome::devtools::Network; | |
| 49 const std::string method = command->method(); | |
| 50 scoped_ptr<DevToolsProtocol::Response> response; | |
| 51 | |
| 52 if (method == network::emulateNetworkConditions::kName) { | |
| 53 response = EmulateNetworkConditions(agent_host, command.get()); | |
| 54 } else if (method == network::canEmulateNetworkConditions::kName) { | |
| 55 response = CanEmulateNetworkConditions(agent_host, command.get()); | |
| 56 } | |
| 57 | |
| 58 if (response) | |
| 59 return response->Serialize(); | |
| 60 return NULL; | |
| 61 } | |
| 62 | |
| 63 Profile* ChromeDevToolsManagerDelegate::GetProfile( | |
| 64 content::DevToolsAgentHost* agent_host) { | |
| 65 content::WebContents* web_contents = agent_host->GetWebContents(); | |
| 66 if (!web_contents) | |
| 67 return NULL; | |
| 68 return Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 69 } | |
| 70 | |
| 71 scoped_ptr<DevToolsProtocol::Response> | |
| 72 ChromeDevToolsManagerDelegate::CanEmulateNetworkConditions( | |
| 73 content::DevToolsAgentHost* agent_host, | |
| 74 DevToolsProtocol::Command* command) { | |
| 75 base::DictionaryValue* result = new base::DictionaryValue(); | |
| 76 result->SetBoolean(chrome::devtools::kResult, true); | |
| 77 return command->SuccessResponse(result); | |
| 78 } | |
| 79 | |
| 80 scoped_ptr<DevToolsProtocol::Response> | |
| 81 ChromeDevToolsManagerDelegate::EmulateNetworkConditions( | |
| 82 content::DevToolsAgentHost* agent_host, | |
| 83 DevToolsProtocol::Command* command) { | |
| 84 base::DictionaryValue* params = command->params(); | |
| 85 namespace names = ::chrome::devtools::Network::emulateNetworkConditions; | |
| 86 | |
| 87 bool offline = false; | |
| 88 if (!params || !params->GetBoolean(names::kParamOffline, &offline)) | |
| 89 return command->InvalidParamResponse(names::kParamOffline); | |
| 90 | |
| 91 double latency = 0.0; | |
| 92 if (!params->GetDouble(names::kParamLatency, &latency)) | |
| 93 return command->InvalidParamResponse(names::kParamLatency); | |
| 94 if (latency < 0.0) | |
| 95 latency = 0.0; | |
| 96 | |
| 97 double download_throughput = 0.0; | |
| 98 if (!params->GetDouble(names::kParamDownloadThroughput, &download_throughput)) | |
| 99 return command->InvalidParamResponse(names::kParamDownloadThroughput); | |
| 100 if (download_throughput < 0.0) | |
| 101 download_throughput = 0.0; | |
| 102 | |
| 103 double upload_throughput = 0.0; | |
| 104 if (!params->GetDouble(names::kParamUploadThroughput, &upload_throughput)) | |
| 105 return command->InvalidParamResponse(names::kParamUploadThroughput); | |
| 106 if (upload_throughput < 0.0) | |
| 107 upload_throughput = 0.0; | |
| 108 | |
| 109 scoped_ptr<DevToolsNetworkConditions> conditions( | |
| 110 new DevToolsNetworkConditions( | |
| 111 offline, latency, download_throughput, upload_throughput)); | |
| 112 | |
| 113 UpdateNetworkState(agent_host, conditions.Pass()); | |
| 114 return command->SuccessResponse(NULL); | |
| 115 } | |
| 116 | |
| 117 void ChromeDevToolsManagerDelegate::UpdateNetworkState( | |
| 118 content::DevToolsAgentHost* agent_host, | |
| 119 scoped_ptr<DevToolsNetworkConditions> conditions) { | |
| 120 Profile* profile = GetProfile(agent_host); | |
| 121 if (!profile) | |
| 122 return; | |
| 123 profile->GetDevToolsNetworkController()->SetNetworkState( | |
| 124 agent_host->GetId(), conditions.Pass()); | |
| 125 } | 36 } |
| 126 | 37 |
| 127 void ChromeDevToolsManagerDelegate::DevToolsAgentStateChanged( | 38 void ChromeDevToolsManagerDelegate::DevToolsAgentStateChanged( |
| 128 content::DevToolsAgentHost* agent_host, | 39 content::DevToolsAgentHost* agent_host, |
| 129 bool attached) { | 40 bool attached) { |
| 130 scoped_ptr<DevToolsNetworkConditions> conditions; | 41 network_protocol_handler_->DevToolsAgentStateChanged(agent_host, attached); |
| 131 if (attached) | |
| 132 conditions.reset(new DevToolsNetworkConditions()); | |
| 133 UpdateNetworkState(agent_host, conditions.Pass()); | |
| 134 } | 42 } |
| OLD | NEW |