OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 * @unrestricted | 5 * @unrestricted |
6 */ | 6 */ |
7 NetworkConditions.NetworkConditionsSelector = class { | 7 NetworkConditions.NetworkConditionsSelector = class { |
8 /** | 8 /** |
9 * @param {function(!Array<!NetworkConditions.NetworkConditionsGroup>):!Array< ?SDK.NetworkManager.Conditions>} populateCallback | 9 * @param {function(!Array<!NetworkConditions.NetworkConditionsGroup>):!Array< ?SDK.NetworkManager.Conditions>} populateCallback |
10 * @param {function(number)} selectCallback | 10 * @param {function(number)} selectCallback |
(...skipping 20 matching lines...) Expand all Loading... | |
31 var throughputInKbps = throughput / (1024 / 8); | 31 var throughputInKbps = throughput / (1024 / 8); |
32 var delimiter = plainText ? '' : ' '; | 32 var delimiter = plainText ? '' : ' '; |
33 if (throughputInKbps < 1024) | 33 if (throughputInKbps < 1024) |
34 return Common.UIString('%d%skb/s', throughputInKbps, delimiter); | 34 return Common.UIString('%d%skb/s', throughputInKbps, delimiter); |
35 if (throughputInKbps < 1024 * 10) | 35 if (throughputInKbps < 1024 * 10) |
36 return Common.UIString('%.1f%sMb/s', throughputInKbps / 1024, delimiter); | 36 return Common.UIString('%.1f%sMb/s', throughputInKbps / 1024, delimiter); |
37 return Common.UIString('%d%sMb/s', (throughputInKbps / 1024) | 0, delimiter) ; | 37 return Common.UIString('%d%sMb/s', (throughputInKbps / 1024) | 0, delimiter) ; |
38 } | 38 } |
39 | 39 |
40 /** | 40 /** |
41 * @param {!SDK.NetworkManager.Conditions} conditions | |
42 * @param {boolean=} plainText | |
43 * @return {!{text: string, title: string}} | |
44 */ | |
45 static _conditionsTitle(conditions, plainText) { | |
46 var downloadInKbps = conditions.download / (1024 / 8); | |
47 var uploadInKbps = conditions.upload / (1024 / 8); | |
48 var isThrottling = (downloadInKbps >= 0) || (uploadInKbps >= 0) || (conditio ns.latency > 0); | |
49 var conditionTitle = Common.UIString(conditions.title); | |
50 if (!isThrottling) | |
51 return {text: conditionTitle, title: conditionTitle}; | |
52 | |
53 var downloadText = NetworkConditions.NetworkConditionsSelector.throughputTex t(conditions.download, plainText); | |
54 var uploadText = NetworkConditions.NetworkConditionsSelector.throughputText( conditions.upload, plainText); | |
55 var pattern = plainText ? '%s (%dms, %s, %s)' : '%s (%dms RTT, %s\u2b07, %s\ u2b06)'; | |
56 var title = Common.UIString(pattern, conditionTitle, conditions.latency, dow nloadText, uploadText); | |
57 return { | |
58 text: title, | |
59 title: Common.UIString( | |
60 'Maximum download throughput: %s.\r\nMaximum upload throughput: %s.\r\ nMinimum round-trip time: %dms.', | |
61 downloadText, uploadText, conditions.latency) | |
62 }; | |
63 } | |
64 | |
65 /** | |
66 * @param {!HTMLSelectElement} selectElement | 41 * @param {!HTMLSelectElement} selectElement |
67 */ | 42 */ |
68 static decorateSelect(selectElement) { | 43 static decorateSelect(selectElement) { |
69 var options = []; | 44 var options = []; |
70 var selector = new NetworkConditions.NetworkConditionsSelector(populate, sel ect); | 45 var selector = new NetworkConditions.NetworkConditionsSelector(populate, sel ect); |
71 selectElement.addEventListener('change', optionSelected, false); | 46 selectElement.addEventListener('change', optionSelected, false); |
72 | 47 |
73 /** | 48 /** |
74 * @param {!Array.<!NetworkConditions.NetworkConditionsGroup>} groups | 49 * @param {!Array.<!NetworkConditions.NetworkConditionsGroup>} groups |
75 * @return {!Array<?SDK.NetworkManager.Conditions>} | 50 * @return {!Array<?SDK.NetworkManager.Conditions>} |
76 */ | 51 */ |
77 function populate(groups) { | 52 function populate(groups) { |
78 selectElement.removeChildren(); | 53 selectElement.removeChildren(); |
79 options = []; | 54 options = []; |
80 for (var i = 0; i < groups.length; ++i) { | 55 for (var i = 0; i < groups.length; ++i) { |
81 var group = groups[i]; | 56 var group = groups[i]; |
82 var groupElement = selectElement.createChild('optgroup'); | 57 var groupElement = selectElement.createChild('optgroup'); |
83 groupElement.label = group.title; | 58 groupElement.label = group.title; |
84 for (var conditions of group.items) { | 59 for (var conditions of group.items) { |
85 var title = NetworkConditions.NetworkConditionsSelector._conditionsTit le(conditions, true); | 60 var title = Common.UIString(conditions.title); |
86 var option = new Option(title.text, title.text); | 61 var option = new Option(title, title); |
87 option.title = title.title; | |
88 groupElement.appendChild(option); | 62 groupElement.appendChild(option); |
89 options.push(conditions); | 63 options.push(conditions); |
90 } | 64 } |
91 if (i === groups.length - 1) { | 65 if (i === groups.length - 1) { |
92 groupElement.appendChild(new Option(Common.UIString('Add\u2026'), Comm on.UIString('Add\u2026'))); | 66 groupElement.appendChild(new Option(Common.UIString('Add\u2026'), Comm on.UIString('Add\u2026'))); |
93 options.push(null); | 67 options.push(null); |
94 } | 68 } |
95 } | 69 } |
96 return options; | 70 return options; |
97 } | 71 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 /** | 103 /** |
130 * @param {!UI.ContextMenu} contextMenu | 104 * @param {!UI.ContextMenu} contextMenu |
131 */ | 105 */ |
132 function appendItems(contextMenu) { | 106 function appendItems(contextMenu) { |
133 for (var index = 0; index < options.length; ++index) { | 107 for (var index = 0; index < options.length; ++index) { |
134 var conditions = options[index]; | 108 var conditions = options[index]; |
135 if (!conditions) { | 109 if (!conditions) { |
136 contextMenu.appendSeparator(); | 110 contextMenu.appendSeparator(); |
137 } else { | 111 } else { |
138 contextMenu.appendCheckboxItem( | 112 contextMenu.appendCheckboxItem( |
139 NetworkConditions.NetworkConditionsSelector._conditionsTitle(condi tions, true).text, | 113 Common.UIString(conditions.title), selector.optionSelected.bind(se lector, conditions), |
140 selector.optionSelected.bind(selector, conditions), selectedIndex === index); | 114 selectedIndex === index); |
141 } | 115 } |
142 } | 116 } |
143 contextMenu.appendItem(Common.UIString('Edit\u2026'), selector.revealAndUp date.bind(selector)); | 117 contextMenu.appendItem(Common.UIString('Edit\u2026'), selector.revealAndUp date.bind(selector)); |
144 } | 118 } |
145 | 119 |
146 /** | 120 /** |
147 * @param {!Array.<!NetworkConditions.NetworkConditionsGroup>} groups | 121 * @param {!Array.<!NetworkConditions.NetworkConditionsGroup>} groups |
148 * @return {!Array<?SDK.NetworkManager.Conditions>} | 122 * @return {!Array<?SDK.NetworkManager.Conditions>} |
149 */ | 123 */ |
150 function populate(groups) { | 124 function populate(groups) { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 return false; | 211 return false; |
238 } | 212 } |
239 }; | 213 }; |
240 | 214 |
241 /** @typedef {!{title: string, items: !Array<!SDK.NetworkManager.Conditions>}} * / | 215 /** @typedef {!{title: string, items: !Array<!SDK.NetworkManager.Conditions>}} * / |
242 NetworkConditions.NetworkConditionsGroup; | 216 NetworkConditions.NetworkConditionsGroup; |
243 | 217 |
244 | 218 |
245 /** @type {!Array.<!SDK.NetworkManager.Conditions>} */ | 219 /** @type {!Array.<!SDK.NetworkManager.Conditions>} */ |
246 NetworkConditions.NetworkConditionsSelector.presets = [ | 220 NetworkConditions.NetworkConditionsSelector.presets = [ |
247 SDK.NetworkManager.OfflineConditions, {title: 'GPRS', download: 50 * 1024 / 8, upload: 20 * 1024 / 8, latency: 500}, | 221 SDK.NetworkManager.OfflineConditions, |
248 {title: 'Regular 2G', download: 250 * 1024 / 8, upload: 50 * 1024 / 8, latency : 300}, | 222 {title: 'Slow 3G', download: 500 * 1024 / 8 * .8, upload: 500 * 1024 / 8 * .8, latency: 400 * 5}, |
chenwilliam
2017/05/12 22:43:04
Fixed my slow 3g adjustment numbers. Caught an err
| |
249 {title: 'Good 2G', download: 450 * 1024 / 8, upload: 150 * 1024 / 8, latency: 150}, | 223 {title: 'Fast 3G', download: 1.6 * 1024 * 1024 / 8 * .9, upload: 750 * 1024 / 8 * .9, latency: 150 * 3.75} |
250 {title: 'Regular 3G', download: 750 * 1024 / 8, upload: 250 * 1024 / 8, latenc y: 100}, | |
251 {title: 'Good 3G', download: 1.5 * 1024 * 1024 / 8, upload: 750 * 1024 / 8, la tency: 40}, | |
252 {title: 'Regular 4G', download: 4 * 1024 * 1024 / 8, upload: 3 * 1024 * 1024 / 8, latency: 20}, | |
253 {title: 'DSL', download: 2 * 1024 * 1024 / 8, upload: 1 * 1024 * 1024 / 8, lat ency: 5}, | |
254 {title: 'WiFi', download: 30 * 1024 * 1024 / 8, upload: 15 * 1024 * 1024 / 8, latency: 2} | |
255 ]; | 224 ]; |
256 | 225 |
257 /** | 226 /** |
258 * @implements {UI.ActionDelegate} | 227 * @implements {UI.ActionDelegate} |
259 * @unrestricted | 228 * @unrestricted |
260 */ | 229 */ |
261 NetworkConditions.NetworkConditionsActionDelegate = class { | 230 NetworkConditions.NetworkConditionsActionDelegate = class { |
262 /** | 231 /** |
263 * @override | 232 * @override |
264 * @param {!UI.Context} context | 233 * @param {!UI.Context} context |
265 * @param {string} actionId | 234 * @param {string} actionId |
266 * @return {boolean} | 235 * @return {boolean} |
267 */ | 236 */ |
268 handleAction(context, actionId) { | 237 handleAction(context, actionId) { |
269 if (actionId === 'network-conditions.network-online') { | 238 if (actionId === 'network-conditions.network-online') { |
270 SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.NoTh rottlingConditions); | 239 SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.NoTh rottlingConditions); |
271 return true; | 240 return true; |
272 } | 241 } |
273 if (actionId === 'network-conditions.network-offline') { | 242 if (actionId === 'network-conditions.network-offline') { |
274 SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.Offl ineConditions); | 243 SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.Offl ineConditions); |
275 return true; | 244 return true; |
276 } | 245 } |
277 return false; | 246 return false; |
278 } | 247 } |
279 }; | 248 }; |
OLD | NEW |