Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1918)

Side by Side Diff: chrome/browser/devtools/chrome_devtools_manager_delegate.cc

Issue 342473004: DevTools: make network conditions emulation scoped (browser) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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" 8 #include "chrome/browser/devtools/devtools_network_conditions.h"
9 #include "chrome/browser/devtools/devtools_network_controller.h" 9 #include "chrome/browser/devtools/devtools_network_controller.h"
10 #include "chrome/browser/devtools/devtools_protocol.h" 10 #include "chrome/browser/devtools/devtools_protocol.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 #endif 54 #endif
55 } 55 }
56 56
57 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( 57 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand(
58 content::DevToolsAgentHost* agent_host, 58 content::DevToolsAgentHost* agent_host,
59 base::DictionaryValue* command_dict) { 59 base::DictionaryValue* command_dict) {
60 scoped_ptr<DevToolsProtocol::Command> command( 60 scoped_ptr<DevToolsProtocol::Command> command(
61 DevToolsProtocol::ParseCommand(command_dict)); 61 DevToolsProtocol::ParseCommand(command_dict));
62 if (!command) 62 if (!command)
63 return NULL; 63 return NULL;
64
64 const std::string method = command->method(); 65 const std::string method = command->method();
66 scoped_ptr<DevToolsProtocol::Response> response;
67
65 if (method == chrome::devtools::Network::emulateNetworkConditions::kName) 68 if (method == chrome::devtools::Network::emulateNetworkConditions::kName)
66 return EmulateNetworkConditions(agent_host, command.get())->Serialize(); 69 response = EmulateNetworkConditions(agent_host, command.get());
70
71 if (response)
72 return response->Serialize();
67 return NULL; 73 return NULL;
68 } 74 }
69 75
70 Profile* ChromeDevToolsManagerDelegate::GetProfile( 76 Profile* ChromeDevToolsManagerDelegate::GetProfile(
71 content::DevToolsAgentHost* agent_host) { 77 content::DevToolsAgentHost* agent_host) {
72 content::RenderViewHost* host = agent_host->GetRenderViewHost(); 78 content::RenderViewHost* host = agent_host->GetRenderViewHost();
73 if (!host) 79 if (!host)
74 return NULL; 80 return NULL;
75 return Profile::FromBrowserContext(host->GetSiteInstance()->GetProcess()-> 81 return Profile::FromBrowserContext(host->GetSiteInstance()->GetProcess()->
76 GetBrowserContext()); 82 GetBrowserContext());
77 } 83 }
78 84
79 scoped_ptr<DevToolsProtocol::Response> 85 scoped_ptr<DevToolsProtocol::Response>
80 ChromeDevToolsManagerDelegate::EmulateNetworkConditions( 86 ChromeDevToolsManagerDelegate::EmulateNetworkConditions(
81 content::DevToolsAgentHost* agent_host, 87 content::DevToolsAgentHost* agent_host,
82 DevToolsProtocol::Command* command) { 88 DevToolsProtocol::Command* command) {
83 base::DictionaryValue* params = command->params(); 89 base::DictionaryValue* params = command->params();
84 90
85 std::vector<std::string> domains;
86 base::ListValue* domain_list = NULL;
87 const char* domains_param =
88 chrome::devtools::Network::emulateNetworkConditions::kParamDomains;
89 if (!params || !params->GetList(domains_param, &domain_list))
90 return command->InvalidParamResponse(domains_param);
91 size_t size = domain_list->GetSize();
92 for (size_t i = 0; i < size; ++i) {
93 std::string domain;
94 if (!domain_list->GetString(i, &domain))
95 return command->InvalidParamResponse(domains_param);
96 domains.push_back(domain);
97 }
98
99 bool offline = false; 91 bool offline = false;
100 const char* offline_param = 92 const char* offline_param =
101 chrome::devtools::Network::emulateNetworkConditions::kParamOffline; 93 chrome::devtools::Network::emulateNetworkConditions::kParamOffline;
102 if (!params->GetBoolean(offline_param, &offline)) 94 if (!params || !params->GetBoolean(offline_param, &offline))
103 return command->InvalidParamResponse(offline_param); 95 return command->InvalidParamResponse(offline_param);
104 96
105 double maximal_throughput = 0.0; 97 double latency = 0.0;
106 const char* maximal_throughput_param = 98 const char* latency_param =
107 chrome::devtools::Network::emulateNetworkConditions::kParamMaximalThroughput; 99 chrome::devtools::Network::emulateNetworkConditions::kParamLatency;
108 if (!params->GetDouble(maximal_throughput_param, &maximal_throughput)) 100 if (!params->GetDouble(latency_param, &latency))
109 return command->InvalidParamResponse(maximal_throughput_param); 101 return command->InvalidParamResponse(latency_param);
110 if (maximal_throughput < 0.0) 102 if (latency < 0.0)
111 maximal_throughput = 0.0; 103 latency = 0.0;
104
105 double download_throughput = 0.0;
106 const char* download_throughput_param =
107 chrome::devtools::Network::emulateNetworkConditions::kParamDownloadThroughput;
108 if (!params->GetDouble(download_throughput_param, &download_throughput))
109 return command->InvalidParamResponse(download_throughput_param);
110 if (download_throughput < 0.0)
111 download_throughput = 0.0;
112
113 double upload_throughput = 0.0;
114 const char* upload_throughput_param =
115 chrome::devtools::Network::emulateNetworkConditions::kParamUploadThroughput;
116 if (!params->GetDouble(upload_throughput_param, &upload_throughput))
117 return command->InvalidParamResponse(upload_throughput_param);
118 if (upload_throughput < 0.0)
119 upload_throughput = 0.0;
112 120
113 EnsureDevtoolsCallbackRegistered(); 121 EnsureDevtoolsCallbackRegistered();
114 scoped_refptr<DevToolsNetworkConditions> conditions; 122 scoped_refptr<DevToolsNetworkConditions> conditions(
115 if (offline || maximal_throughput) 123 new DevToolsNetworkConditions(
116 conditions = new DevToolsNetworkConditions(domains, maximal_throughput); 124 offline, latency, download_throughput, upload_throughput));
117 125
118 emulate_network_conditions_client_id_ = agent_host->GetId();
119 UpdateNetworkState(agent_host, conditions); 126 UpdateNetworkState(agent_host, conditions);
120 return command->SuccessResponse(NULL); 127 return command->SuccessResponse(NULL);
121 } 128 }
122 129
123 void ChromeDevToolsManagerDelegate::UpdateNetworkState( 130 void ChromeDevToolsManagerDelegate::UpdateNetworkState(
124 content::DevToolsAgentHost* agent_host, 131 content::DevToolsAgentHost* agent_host,
125 scoped_refptr<DevToolsNetworkConditions> conditions) { 132 scoped_refptr<DevToolsNetworkConditions> conditions) {
126 Profile* profile = GetProfile(agent_host); 133 Profile* profile = GetProfile(agent_host);
127 if (!profile) 134 if (!profile)
128 return; 135 return;
129 profile->GetDevToolsNetworkController()->SetNetworkState(conditions); 136 profile->GetDevToolsNetworkController()->SetNetworkState(
137 agent_host->GetId(), conditions);
130 } 138 }
131 139
132 void ChromeDevToolsManagerDelegate::OnDevToolsStateChanged( 140 void ChromeDevToolsManagerDelegate::OnDevToolsStateChanged(
133 content::DevToolsAgentHost* agent_host, 141 content::DevToolsAgentHost* agent_host,
134 bool attached) { 142 bool attached) {
135 if (agent_host->GetId() == emulate_network_conditions_client_id_) { 143 scoped_refptr<DevToolsNetworkConditions> conditions;
136 emulate_network_conditions_client_id_ = std::string(); 144 if (attached)
137 UpdateNetworkState(agent_host, scoped_refptr<DevToolsNetworkConditions>()); 145 conditions = new DevToolsNetworkConditions();
138 } 146 UpdateNetworkState(agent_host, conditions);
139 } 147 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698