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 | |
5 /** | 4 /** |
6 * @constructor | 5 * @unrestricted |
7 * @param {function(!Array<!WebInspector.NetworkConditionsGroup>):!Array<?WebIns
pector.NetworkManager.Conditions>} populateCallback | |
8 * @param {function(number)} selectCallback | |
9 */ | 6 */ |
10 WebInspector.NetworkConditionsSelector = function(populateCallback, selectCallba
ck) | 7 WebInspector.NetworkConditionsSelector = class { |
11 { | 8 /** |
| 9 * @param {function(!Array<!WebInspector.NetworkConditionsGroup>):!Array<?WebI
nspector.NetworkManager.Conditions>} populateCallback |
| 10 * @param {function(number)} selectCallback |
| 11 */ |
| 12 constructor(populateCallback, selectCallback) { |
12 this._populateCallback = populateCallback; | 13 this._populateCallback = populateCallback; |
13 this._selectCallback = selectCallback; | 14 this._selectCallback = selectCallback; |
14 this._customSetting = WebInspector.moduleSetting("customNetworkConditions"); | 15 this._customSetting = WebInspector.moduleSetting('customNetworkConditions'); |
15 this._customSetting.addChangeListener(this._populateOptions, this); | 16 this._customSetting.addChangeListener(this._populateOptions, this); |
16 this._manager = WebInspector.multitargetNetworkManager; | 17 this._manager = WebInspector.multitargetNetworkManager; |
17 this._manager.addEventListener(WebInspector.MultitargetNetworkManager.Events
.ConditionsChanged, this._conditionsChanged, this); | 18 this._manager.addEventListener( |
| 19 WebInspector.MultitargetNetworkManager.Events.ConditionsChanged, this._c
onditionsChanged, this); |
18 this._populateOptions(); | 20 this._populateOptions(); |
19 }; | 21 } |
20 | 22 |
21 /** @typedef {!{title: string, items: !Array<!WebInspector.NetworkManager.Condit
ions>}} */ | 23 /** |
22 WebInspector.NetworkConditionsGroup; | 24 * @param {number} throughput |
| 25 * @param {boolean=} plainText |
| 26 * @return {string} |
| 27 */ |
| 28 static _throughputText(throughput, plainText) { |
| 29 if (throughput < 0) |
| 30 return ''; |
| 31 var throughputInKbps = throughput / (1024 / 8); |
| 32 var delimiter = plainText ? '' : ' '; |
| 33 if (throughputInKbps < 1024) |
| 34 return WebInspector.UIString('%d%skb/s', throughputInKbps, delimiter); |
| 35 if (throughputInKbps < 1024 * 10) |
| 36 return WebInspector.UIString('%.1f%sMb/s', throughputInKbps / 1024, delimi
ter); |
| 37 return WebInspector.UIString('%d%sMb/s', (throughputInKbps / 1024) | 0, deli
miter); |
| 38 } |
23 | 39 |
24 /** | 40 /** |
25 * @param {number} throughput | 41 * @param {!WebInspector.NetworkManager.Conditions} conditions |
26 * @param {boolean=} plainText | 42 * @param {boolean=} plainText |
27 * @return {string} | 43 * @return {!{text: string, title: string}} |
28 */ | 44 */ |
29 WebInspector.NetworkConditionsSelector._throughputText = function(throughput, pl
ainText) | 45 static _conditionsTitle(conditions, plainText) { |
30 { | |
31 if (throughput < 0) | |
32 return ""; | |
33 var throughputInKbps = throughput / (1024 / 8); | |
34 var delimiter = plainText ? "" : " "; | |
35 if (throughputInKbps < 1024) | |
36 return WebInspector.UIString("%d%skb/s", throughputInKbps, delimiter); | |
37 if (throughputInKbps < 1024 * 10) | |
38 return WebInspector.UIString("%.1f%sMb/s", throughputInKbps / 1024, deli
miter); | |
39 return WebInspector.UIString("%d%sMb/s", (throughputInKbps / 1024) | 0, deli
miter); | |
40 }; | |
41 | |
42 /** @type {!Array.<!WebInspector.NetworkManager.Conditions>} */ | |
43 WebInspector.NetworkConditionsSelector._presets = [ | |
44 WebInspector.NetworkManager.OfflineConditions, | |
45 {title: "GPRS", download: 50 * 1024 / 8, upload: 20 * 1024 / 8, latency: 500
}, | |
46 {title: "Regular 2G", download: 250 * 1024 / 8, upload: 50 * 1024 / 8, laten
cy: 300}, | |
47 {title: "Good 2G", download: 450 * 1024 / 8, upload: 150 * 1024 / 8, latency
: 150}, | |
48 {title: "Regular 3G", download: 750 * 1024 / 8, upload: 250 * 1024 / 8, late
ncy: 100}, | |
49 {title: "Good 3G", download: 1.5 * 1024 * 1024 / 8, upload: 750 * 1024 / 8,
latency: 40}, | |
50 {title: "Regular 4G", download: 4 * 1024 * 1024 / 8, upload: 3 * 1024 * 1024
/ 8, latency: 20}, | |
51 {title: "DSL", download: 2 * 1024 * 1024 / 8, upload: 1 * 1024 * 1024 / 8, l
atency: 5}, | |
52 {title: "WiFi", download: 30 * 1024 * 1024 / 8, upload: 15 * 1024 * 1024 / 8
, latency: 2} | |
53 ]; | |
54 | |
55 /** | |
56 * @param {!WebInspector.NetworkManager.Conditions} conditions | |
57 * @param {boolean=} plainText | |
58 * @return {!{text: string, title: string}} | |
59 */ | |
60 WebInspector.NetworkConditionsSelector._conditionsTitle = function(conditions, p
lainText) | |
61 { | |
62 var downloadInKbps = conditions.download / (1024 / 8); | 46 var downloadInKbps = conditions.download / (1024 / 8); |
63 var uploadInKbps = conditions.upload / (1024 / 8); | 47 var uploadInKbps = conditions.upload / (1024 / 8); |
64 var isThrottling = (downloadInKbps >= 0) || (uploadInKbps >= 0) || (conditio
ns.latency > 0); | 48 var isThrottling = (downloadInKbps >= 0) || (uploadInKbps >= 0) || (conditio
ns.latency > 0); |
65 var conditionTitle = WebInspector.UIString(conditions.title); | 49 var conditionTitle = WebInspector.UIString(conditions.title); |
66 if (!isThrottling) | 50 if (!isThrottling) |
67 return {text: conditionTitle, title: conditionTitle}; | 51 return {text: conditionTitle, title: conditionTitle}; |
68 | 52 |
69 var downloadText = WebInspector.NetworkConditionsSelector._throughputText(co
nditions.download, plainText); | 53 var downloadText = WebInspector.NetworkConditionsSelector._throughputText(co
nditions.download, plainText); |
70 var uploadText = WebInspector.NetworkConditionsSelector._throughputText(cond
itions.upload, plainText); | 54 var uploadText = WebInspector.NetworkConditionsSelector._throughputText(cond
itions.upload, plainText); |
71 var pattern = plainText ? "%s (%dms, %s, %s)" : "%s (%dms RTT, %s\u2b07, %s\
u2b06)"; | 55 var pattern = plainText ? '%s (%dms, %s, %s)' : '%s (%dms RTT, %s\u2b07, %s\
u2b06)'; |
72 var title = WebInspector.UIString(pattern, conditionTitle, conditions.latenc
y, downloadText, uploadText); | 56 var title = WebInspector.UIString(pattern, conditionTitle, conditions.latenc
y, downloadText, uploadText); |
73 return {text: title, title: WebInspector.UIString("Maximum download throughp
ut: %s.\r\nMaximum upload throughput: %s.\r\nMinimum round-trip time: %dms.", do
wnloadText, uploadText, conditions.latency)}; | 57 return { |
74 }; | 58 text: title, |
| 59 title: WebInspector.UIString( |
| 60 'Maximum download throughput: %s.\r\nMaximum upload throughput: %s.\r\
nMinimum round-trip time: %dms.', |
| 61 downloadText, uploadText, conditions.latency) |
| 62 }; |
| 63 } |
75 | 64 |
76 WebInspector.NetworkConditionsSelector.prototype = { | 65 /** |
77 _populateOptions: function() | 66 * @param {!HTMLSelectElement} selectElement |
78 { | 67 */ |
79 var customGroup = {title: WebInspector.UIString("Custom"), items: this._
customSetting.get()}; | 68 static decorateSelect(selectElement) { |
80 var presetsGroup = {title: WebInspector.UIString("Presets"), items: WebI
nspector.NetworkConditionsSelector._presets}; | |
81 var disabledGroup = {title: WebInspector.UIString("Disabled"), items: [W
ebInspector.NetworkManager.NoThrottlingConditions]}; | |
82 this._options = this._populateCallback([customGroup, presetsGroup, disab
ledGroup]); | |
83 if (!this._conditionsChanged()) { | |
84 for (var i = this._options.length - 1; i >= 0; i--) { | |
85 if (this._options[i]) { | |
86 this.optionSelected(/** @type {!WebInspector.NetworkManager.
Conditions} */ (this._options[i])); | |
87 break; | |
88 } | |
89 } | |
90 } | |
91 }, | |
92 | |
93 revealAndUpdate: function() | |
94 { | |
95 WebInspector.Revealer.reveal(this._customSetting); | |
96 this._conditionsChanged(); | |
97 }, | |
98 | |
99 /** | |
100 * @param {!WebInspector.NetworkManager.Conditions} conditions | |
101 */ | |
102 optionSelected: function(conditions) | |
103 { | |
104 this._manager.setNetworkConditions(conditions); | |
105 }, | |
106 | |
107 /** | |
108 * @return {boolean} | |
109 */ | |
110 _conditionsChanged: function() | |
111 { | |
112 var value = this._manager.networkConditions(); | |
113 for (var index = 0; index < this._options.length; ++index) { | |
114 var option = this._options[index]; | |
115 if (option && option.download === value.download && option.upload ==
= value.upload && option.latency === value.latency && option.title === value.tit
le) { | |
116 this._selectCallback(index); | |
117 return true; | |
118 } | |
119 } | |
120 return false; | |
121 } | |
122 }; | |
123 | |
124 /** | |
125 * @param {!HTMLSelectElement} selectElement | |
126 */ | |
127 WebInspector.NetworkConditionsSelector.decorateSelect = function(selectElement) | |
128 { | |
129 var options = []; | 69 var options = []; |
130 var selector = new WebInspector.NetworkConditionsSelector(populate, select); | 70 var selector = new WebInspector.NetworkConditionsSelector(populate, select); |
131 selectElement.addEventListener("change", optionSelected, false); | 71 selectElement.addEventListener('change', optionSelected, false); |
132 | 72 |
133 /** | 73 /** |
134 * @param {!Array.<!WebInspector.NetworkConditionsGroup>} groups | 74 * @param {!Array.<!WebInspector.NetworkConditionsGroup>} groups |
135 * @return {!Array<?WebInspector.NetworkManager.Conditions>} | 75 * @return {!Array<?WebInspector.NetworkManager.Conditions>} |
136 */ | 76 */ |
137 function populate(groups) | 77 function populate(groups) { |
138 { | 78 selectElement.removeChildren(); |
139 selectElement.removeChildren(); | 79 options = []; |
140 options = []; | 80 for (var i = 0; i < groups.length; ++i) { |
141 for (var i = 0; i < groups.length; ++i) { | 81 var group = groups[i]; |
142 var group = groups[i]; | 82 var groupElement = selectElement.createChild('optgroup'); |
143 var groupElement = selectElement.createChild("optgroup"); | 83 groupElement.label = group.title; |
144 groupElement.label = group.title; | 84 if (!i) { |
145 if (!i) { | 85 groupElement.appendChild(new Option(WebInspector.UIString('Add\u2026')
, WebInspector.UIString('Add\u2026'))); |
146 groupElement.appendChild(new Option(WebInspector.UIString("Add\u
2026"), WebInspector.UIString("Add\u2026"))); | 86 options.push(null); |
147 options.push(null); | |
148 } | |
149 for (var conditions of group.items) { | |
150 var title = WebInspector.NetworkConditionsSelector._conditionsTi
tle(conditions, true); | |
151 var option = new Option(title.text, title.text); | |
152 option.title = title.title; | |
153 groupElement.appendChild(option); | |
154 options.push(conditions); | |
155 } | |
156 } | 87 } |
157 return options; | 88 for (var conditions of group.items) { |
| 89 var title = WebInspector.NetworkConditionsSelector._conditionsTitle(co
nditions, true); |
| 90 var option = new Option(title.text, title.text); |
| 91 option.title = title.title; |
| 92 groupElement.appendChild(option); |
| 93 options.push(conditions); |
| 94 } |
| 95 } |
| 96 return options; |
158 } | 97 } |
159 | 98 |
160 function optionSelected() | 99 function optionSelected() { |
161 { | 100 if (selectElement.selectedIndex === 0) |
162 if (selectElement.selectedIndex === 0) | 101 selector.revealAndUpdate(); |
163 selector.revealAndUpdate(); | 102 else |
164 else | 103 selector.optionSelected(options[selectElement.selectedIndex]); |
165 selector.optionSelected(options[selectElement.selectedIndex]); | |
166 } | 104 } |
167 | 105 |
168 /** | 106 /** |
169 * @param {number} index | 107 * @param {number} index |
170 */ | 108 */ |
171 function select(index) | 109 function select(index) { |
172 { | 110 if (selectElement.selectedIndex !== index) |
173 if (selectElement.selectedIndex !== index) | 111 selectElement.selectedIndex = index; |
174 selectElement.selectedIndex = index; | |
175 } | 112 } |
176 }; | 113 } |
177 | 114 |
178 /** | 115 /** |
179 * @return {!WebInspector.ToolbarMenuButton} | 116 * @return {!WebInspector.ToolbarMenuButton} |
180 */ | 117 */ |
181 WebInspector.NetworkConditionsSelector.createToolbarMenuButton = function() | 118 static createToolbarMenuButton() { |
182 { | |
183 var button = new WebInspector.ToolbarMenuButton(appendItems); | 119 var button = new WebInspector.ToolbarMenuButton(appendItems); |
184 button.setGlyph(""); | 120 button.setGlyph(''); |
185 button.turnIntoSelect(); | 121 button.turnIntoSelect(); |
186 | 122 |
187 /** @type {!Array<?WebInspector.NetworkManager.Conditions>} */ | 123 /** @type {!Array<?WebInspector.NetworkManager.Conditions>} */ |
188 var options = []; | 124 var options = []; |
189 var selectedIndex = -1; | 125 var selectedIndex = -1; |
190 var selector = new WebInspector.NetworkConditionsSelector(populate, select); | 126 var selector = new WebInspector.NetworkConditionsSelector(populate, select); |
191 return button; | 127 return button; |
192 | 128 |
193 /** | 129 /** |
194 * @param {!WebInspector.ContextMenu} contextMenu | 130 * @param {!WebInspector.ContextMenu} contextMenu |
195 */ | 131 */ |
196 function appendItems(contextMenu) | 132 function appendItems(contextMenu) { |
197 { | 133 for (var index = 0; index < options.length; ++index) { |
198 for (var index = 0; index < options.length; ++index) { | 134 var conditions = options[index]; |
199 var conditions = options[index]; | 135 if (!conditions) |
200 if (!conditions) | 136 contextMenu.appendSeparator(); |
201 contextMenu.appendSeparator(); | 137 else |
202 else | 138 contextMenu.appendCheckboxItem( |
203 contextMenu.appendCheckboxItem(WebInspector.NetworkConditionsSel
ector._conditionsTitle(conditions, true).text, selector.optionSelected.bind(sele
ctor, conditions), selectedIndex === index); | 139 WebInspector.NetworkConditionsSelector._conditionsTitle(conditions
, true).text, |
204 } | 140 selector.optionSelected.bind(selector, conditions), selectedIndex
=== index); |
205 contextMenu.appendItem(WebInspector.UIString("Edit\u2026"), selector.rev
ealAndUpdate.bind(selector)); | 141 } |
| 142 contextMenu.appendItem(WebInspector.UIString('Edit\u2026'), selector.revea
lAndUpdate.bind(selector)); |
206 } | 143 } |
207 | 144 |
208 /** | 145 /** |
209 * @param {!Array.<!WebInspector.NetworkConditionsGroup>} groups | 146 * @param {!Array.<!WebInspector.NetworkConditionsGroup>} groups |
210 * @return {!Array<?WebInspector.NetworkManager.Conditions>} | 147 * @return {!Array<?WebInspector.NetworkManager.Conditions>} |
211 */ | 148 */ |
212 function populate(groups) | 149 function populate(groups) { |
213 { | 150 options = []; |
214 options = []; | 151 for (var group of groups) { |
215 for (var group of groups) { | 152 for (var conditions of group.items) |
216 for (var conditions of group.items) | 153 options.push(conditions); |
217 options.push(conditions); | 154 options.push(null); |
218 options.push(null); | 155 } |
| 156 return options; |
| 157 } |
| 158 |
| 159 /** |
| 160 * @param {number} index |
| 161 */ |
| 162 function select(index) { |
| 163 selectedIndex = index; |
| 164 button.setText(options[index].title); |
| 165 } |
| 166 } |
| 167 |
| 168 /** |
| 169 * @return {!WebInspector.ToolbarCheckbox} |
| 170 */ |
| 171 static createOfflineToolbarCheckbox() { |
| 172 var checkbox = new WebInspector.ToolbarCheckbox( |
| 173 WebInspector.UIString('Offline'), WebInspector.UIString('Force disconnec
ted from network'), undefined, |
| 174 forceOffline); |
| 175 WebInspector.multitargetNetworkManager.addEventListener( |
| 176 WebInspector.MultitargetNetworkManager.Events.ConditionsChanged, network
ConditionsChanged); |
| 177 checkbox.setChecked( |
| 178 WebInspector.multitargetNetworkManager.networkConditions() === WebInspec
tor.NetworkManager.OfflineConditions); |
| 179 |
| 180 var lastNetworkConditions; |
| 181 |
| 182 function forceOffline() { |
| 183 if (checkbox.checked()) { |
| 184 lastNetworkConditions = WebInspector.multitargetNetworkManager.networkCo
nditions(); |
| 185 WebInspector.multitargetNetworkManager.setNetworkConditions(WebInspector
.NetworkManager.OfflineConditions); |
| 186 } else { |
| 187 WebInspector.multitargetNetworkManager.setNetworkConditions(lastNetworkC
onditions); |
| 188 } |
| 189 } |
| 190 |
| 191 function networkConditionsChanged() { |
| 192 var conditions = WebInspector.multitargetNetworkManager.networkConditions(
); |
| 193 if (conditions !== WebInspector.NetworkManager.OfflineConditions) |
| 194 lastNetworkConditions = conditions; |
| 195 checkbox.setChecked(conditions === WebInspector.NetworkManager.OfflineCond
itions); |
| 196 } |
| 197 return checkbox; |
| 198 } |
| 199 |
| 200 _populateOptions() { |
| 201 var customGroup = {title: WebInspector.UIString('Custom'), items: this._cust
omSetting.get()}; |
| 202 var presetsGroup = { |
| 203 title: WebInspector.UIString('Presets'), |
| 204 items: WebInspector.NetworkConditionsSelector._presets |
| 205 }; |
| 206 var disabledGroup = { |
| 207 title: WebInspector.UIString('Disabled'), |
| 208 items: [WebInspector.NetworkManager.NoThrottlingConditions] |
| 209 }; |
| 210 this._options = this._populateCallback([customGroup, presetsGroup, disabledG
roup]); |
| 211 if (!this._conditionsChanged()) { |
| 212 for (var i = this._options.length - 1; i >= 0; i--) { |
| 213 if (this._options[i]) { |
| 214 this.optionSelected(/** @type {!WebInspector.NetworkManager.Conditions
} */ (this._options[i])); |
| 215 break; |
219 } | 216 } |
220 return options; | 217 } |
221 } | 218 } |
222 | 219 } |
223 /** | 220 |
224 * @param {number} index | 221 revealAndUpdate() { |
225 */ | 222 WebInspector.Revealer.reveal(this._customSetting); |
226 function select(index) | 223 this._conditionsChanged(); |
227 { | 224 } |
228 selectedIndex = index; | 225 |
229 button.setText(options[index].title); | 226 /** |
230 } | 227 * @param {!WebInspector.NetworkManager.Conditions} conditions |
| 228 */ |
| 229 optionSelected(conditions) { |
| 230 this._manager.setNetworkConditions(conditions); |
| 231 } |
| 232 |
| 233 /** |
| 234 * @return {boolean} |
| 235 */ |
| 236 _conditionsChanged() { |
| 237 var value = this._manager.networkConditions(); |
| 238 for (var index = 0; index < this._options.length; ++index) { |
| 239 var option = this._options[index]; |
| 240 if (option && option.download === value.download && option.upload === valu
e.upload && |
| 241 option.latency === value.latency && option.title === value.title) { |
| 242 this._selectCallback(index); |
| 243 return true; |
| 244 } |
| 245 } |
| 246 return false; |
| 247 } |
231 }; | 248 }; |
232 | 249 |
| 250 /** @typedef {!{title: string, items: !Array<!WebInspector.NetworkManager.Condit
ions>}} */ |
| 251 WebInspector.NetworkConditionsGroup; |
| 252 |
| 253 |
| 254 /** @type {!Array.<!WebInspector.NetworkManager.Conditions>} */ |
| 255 WebInspector.NetworkConditionsSelector._presets = [ |
| 256 WebInspector.NetworkManager.OfflineConditions, |
| 257 {title: 'GPRS', download: 50 * 1024 / 8, upload: 20 * 1024 / 8, latency: 500}, |
| 258 {title: 'Regular 2G', download: 250 * 1024 / 8, upload: 50 * 1024 / 8, latency
: 300}, |
| 259 {title: 'Good 2G', download: 450 * 1024 / 8, upload: 150 * 1024 / 8, latency:
150}, |
| 260 {title: 'Regular 3G', download: 750 * 1024 / 8, upload: 250 * 1024 / 8, latenc
y: 100}, |
| 261 {title: 'Good 3G', download: 1.5 * 1024 * 1024 / 8, upload: 750 * 1024 / 8, la
tency: 40}, |
| 262 {title: 'Regular 4G', download: 4 * 1024 * 1024 / 8, upload: 3 * 1024 * 1024 /
8, latency: 20}, |
| 263 {title: 'DSL', download: 2 * 1024 * 1024 / 8, upload: 1 * 1024 * 1024 / 8, lat
ency: 5}, |
| 264 {title: 'WiFi', download: 30 * 1024 * 1024 / 8, upload: 15 * 1024 * 1024 / 8,
latency: 2} |
| 265 ]; |
| 266 |
| 267 |
233 /** | 268 /** |
234 * @return {!WebInspector.ToolbarCheckbox} | 269 * @implements {WebInspector.ListWidget.Delegate} |
| 270 * @unrestricted |
235 */ | 271 */ |
236 WebInspector.NetworkConditionsSelector.createOfflineToolbarCheckbox = function() | 272 WebInspector.NetworkConditionsSettingsTab = class extends WebInspector.VBox { |
237 { | 273 constructor() { |
238 var checkbox = new WebInspector.ToolbarCheckbox(WebInspector.UIString("Offli
ne"), WebInspector.UIString("Force disconnected from network"), undefined, force
Offline); | 274 super(true); |
239 WebInspector.multitargetNetworkManager.addEventListener(WebInspector.Multita
rgetNetworkManager.Events.ConditionsChanged, networkConditionsChanged); | 275 this.registerRequiredCSS('components/networkConditionsSettingsTab.css'); |
240 checkbox.setChecked(WebInspector.multitargetNetworkManager.networkConditions
() === WebInspector.NetworkManager.OfflineConditions); | 276 |
241 | 277 this.contentElement.createChild('div', 'header').textContent = WebInspector.
UIString('Network Throttling Profiles'); |
242 var lastNetworkConditions; | 278 |
243 | 279 var addButton = createTextButton( |
244 function forceOffline() | 280 WebInspector.UIString('Add custom profile...'), this._addButtonClicked.b
ind(this), 'add-conditions-button'); |
245 { | |
246 if (checkbox.checked()) { | |
247 lastNetworkConditions = WebInspector.multitargetNetworkManager.netwo
rkConditions(); | |
248 WebInspector.multitargetNetworkManager.setNetworkConditions(WebInspe
ctor.NetworkManager.OfflineConditions); | |
249 } else { | |
250 WebInspector.multitargetNetworkManager.setNetworkConditions(lastNetw
orkConditions); | |
251 } | |
252 } | |
253 | |
254 function networkConditionsChanged() | |
255 { | |
256 var conditions = WebInspector.multitargetNetworkManager.networkCondition
s(); | |
257 if (conditions !== WebInspector.NetworkManager.OfflineConditions) | |
258 lastNetworkConditions = conditions; | |
259 checkbox.setChecked(conditions === WebInspector.NetworkManager.OfflineCo
nditions); | |
260 } | |
261 return checkbox; | |
262 }; | |
263 | |
264 /** | |
265 * @constructor | |
266 * @extends {WebInspector.VBox} | |
267 * @implements {WebInspector.ListWidget.Delegate} | |
268 */ | |
269 WebInspector.NetworkConditionsSettingsTab = function() | |
270 { | |
271 WebInspector.VBox.call(this, true); | |
272 this.registerRequiredCSS("components/networkConditionsSettingsTab.css"); | |
273 | |
274 this.contentElement.createChild("div", "header").textContent = WebInspector.
UIString("Network Throttling Profiles"); | |
275 | |
276 var addButton = createTextButton(WebInspector.UIString("Add custom profile..
."), this._addButtonClicked.bind(this), "add-conditions-button"); | |
277 this.contentElement.appendChild(addButton); | 281 this.contentElement.appendChild(addButton); |
278 | 282 |
279 this._list = new WebInspector.ListWidget(this); | 283 this._list = new WebInspector.ListWidget(this); |
280 this._list.element.classList.add("conditions-list"); | 284 this._list.element.classList.add('conditions-list'); |
281 this._list.registerRequiredCSS("components/networkConditionsSettingsTab.css"
); | 285 this._list.registerRequiredCSS('components/networkConditionsSettingsTab.css'
); |
282 this._list.show(this.contentElement); | 286 this._list.show(this.contentElement); |
283 | 287 |
284 this._customSetting = WebInspector.moduleSetting("customNetworkConditions"); | 288 this._customSetting = WebInspector.moduleSetting('customNetworkConditions'); |
285 this._customSetting.addChangeListener(this._conditionsUpdated, this); | 289 this._customSetting.addChangeListener(this._conditionsUpdated, this); |
286 | 290 |
287 this.setDefaultFocusedElement(addButton); | 291 this.setDefaultFocusedElement(addButton); |
288 this.contentElement.tabIndex = 0; | 292 this.contentElement.tabIndex = 0; |
289 }; | 293 } |
290 | 294 |
291 WebInspector.NetworkConditionsSettingsTab.prototype = { | 295 /** |
292 wasShown: function() | 296 * @override |
293 { | 297 */ |
294 WebInspector.VBox.prototype.wasShown.call(this); | 298 wasShown() { |
295 this._conditionsUpdated(); | 299 super.wasShown(); |
296 }, | 300 this._conditionsUpdated(); |
297 | 301 } |
298 _conditionsUpdated: function() | 302 |
299 { | 303 _conditionsUpdated() { |
300 this._list.clear(); | 304 this._list.clear(); |
301 | 305 |
302 var conditions = this._customSetting.get(); | 306 var conditions = this._customSetting.get(); |
303 for (var i = 0; i < conditions.length; ++i) | 307 for (var i = 0; i < conditions.length; ++i) |
304 this._list.appendItem(conditions[i], true); | 308 this._list.appendItem(conditions[i], true); |
305 | 309 |
306 this._list.appendSeparator(); | 310 this._list.appendSeparator(); |
307 | 311 |
308 conditions = WebInspector.NetworkConditionsSelector._presets; | 312 conditions = WebInspector.NetworkConditionsSelector._presets; |
309 for (var i = 0; i < conditions.length; ++i) | 313 for (var i = 0; i < conditions.length; ++i) |
310 this._list.appendItem(conditions[i], false); | 314 this._list.appendItem(conditions[i], false); |
311 }, | 315 } |
312 | 316 |
313 _addButtonClicked: function() | 317 _addButtonClicked() { |
314 { | 318 this._list.addNewItem(this._customSetting.get().length, {title: '', download
: -1, upload: -1, latency: 0}); |
315 this._list.addNewItem(this._customSetting.get().length, {title: "", down
load: -1, upload: -1, latency: 0}); | 319 } |
316 }, | 320 |
317 | 321 /** |
318 /** | 322 * @override |
319 * @override | 323 * @param {*} item |
320 * @param {*} item | 324 * @param {boolean} editable |
321 * @param {boolean} editable | 325 * @return {!Element} |
322 * @return {!Element} | 326 */ |
323 */ | 327 renderItem(item, editable) { |
324 renderItem: function(item, editable) | 328 var conditions = /** @type {!WebInspector.NetworkManager.Conditions} */ (ite
m); |
325 { | 329 var element = createElementWithClass('div', 'conditions-list-item'); |
326 var conditions = /** @type {!WebInspector.NetworkManager.Conditions} */
(item); | 330 var title = element.createChild('div', 'conditions-list-text conditions-list
-title'); |
327 var element = createElementWithClass("div", "conditions-list-item"); | 331 var titleText = title.createChild('div', 'conditions-list-title-text'); |
328 var title = element.createChild("div", "conditions-list-text conditions-
list-title"); | 332 titleText.textContent = conditions.title; |
329 var titleText = title.createChild("div", "conditions-list-title-text"); | 333 titleText.title = conditions.title; |
330 titleText.textContent = conditions.title; | 334 element.createChild('div', 'conditions-list-separator'); |
331 titleText.title = conditions.title; | 335 element.createChild('div', 'conditions-list-text').textContent = |
332 element.createChild("div", "conditions-list-separator"); | 336 WebInspector.NetworkConditionsSelector._throughputText(conditions.downlo
ad); |
333 element.createChild("div", "conditions-list-text").textContent = WebInsp
ector.NetworkConditionsSelector._throughputText(conditions.download); | 337 element.createChild('div', 'conditions-list-separator'); |
334 element.createChild("div", "conditions-list-separator"); | 338 element.createChild('div', 'conditions-list-text').textContent = |
335 element.createChild("div", "conditions-list-text").textContent = WebInsp
ector.NetworkConditionsSelector._throughputText(conditions.upload); | 339 WebInspector.NetworkConditionsSelector._throughputText(conditions.upload
); |
336 element.createChild("div", "conditions-list-separator"); | 340 element.createChild('div', 'conditions-list-separator'); |
337 element.createChild("div", "conditions-list-text").textContent = WebInsp
ector.UIString("%dms", conditions.latency); | 341 element.createChild('div', 'conditions-list-text').textContent = WebInspecto
r.UIString('%dms', conditions.latency); |
338 return element; | 342 return element; |
339 }, | 343 } |
340 | 344 |
341 /** | 345 /** |
342 * @override | 346 * @override |
| 347 * @param {*} item |
| 348 * @param {number} index |
| 349 */ |
| 350 removeItemRequested(item, index) { |
| 351 var list = this._customSetting.get(); |
| 352 list.splice(index, 1); |
| 353 this._customSetting.set(list); |
| 354 } |
| 355 |
| 356 /** |
| 357 * @override |
| 358 * @param {*} item |
| 359 * @param {!WebInspector.ListWidget.Editor} editor |
| 360 * @param {boolean} isNew |
| 361 */ |
| 362 commitEdit(item, editor, isNew) { |
| 363 var conditions = /** @type {?WebInspector.NetworkManager.Conditions} */ (ite
m); |
| 364 conditions.title = editor.control('title').value.trim(); |
| 365 var download = editor.control('download').value.trim(); |
| 366 conditions.download = download ? parseInt(download, 10) * (1024 / 8) : -1; |
| 367 var upload = editor.control('upload').value.trim(); |
| 368 conditions.upload = upload ? parseInt(upload, 10) * (1024 / 8) : -1; |
| 369 var latency = editor.control('latency').value.trim(); |
| 370 conditions.latency = latency ? parseInt(latency, 10) : 0; |
| 371 |
| 372 var list = this._customSetting.get(); |
| 373 if (isNew) |
| 374 list.push(conditions); |
| 375 this._customSetting.set(list); |
| 376 } |
| 377 |
| 378 /** |
| 379 * @override |
| 380 * @param {*} item |
| 381 * @return {!WebInspector.ListWidget.Editor} |
| 382 */ |
| 383 beginEdit(item) { |
| 384 var conditions = /** @type {?WebInspector.NetworkManager.Conditions} */ (ite
m); |
| 385 var editor = this._createEditor(); |
| 386 editor.control('title').value = conditions.title; |
| 387 editor.control('download').value = conditions.download <= 0 ? '' : String(co
nditions.download / (1024 / 8)); |
| 388 editor.control('upload').value = conditions.upload <= 0 ? '' : String(condit
ions.upload / (1024 / 8)); |
| 389 editor.control('latency').value = conditions.latency ? String(conditions.lat
ency) : ''; |
| 390 return editor; |
| 391 } |
| 392 |
| 393 /** |
| 394 * @return {!WebInspector.ListWidget.Editor} |
| 395 */ |
| 396 _createEditor() { |
| 397 if (this._editor) |
| 398 return this._editor; |
| 399 |
| 400 var editor = new WebInspector.ListWidget.Editor(); |
| 401 this._editor = editor; |
| 402 var content = editor.contentElement(); |
| 403 |
| 404 var titles = content.createChild('div', 'conditions-edit-row'); |
| 405 titles.createChild('div', 'conditions-list-text conditions-list-title').text
Content = |
| 406 WebInspector.UIString('Profile Name'); |
| 407 titles.createChild('div', 'conditions-list-separator conditions-list-separat
or-invisible'); |
| 408 titles.createChild('div', 'conditions-list-text').textContent = WebInspector
.UIString('Download'); |
| 409 titles.createChild('div', 'conditions-list-separator conditions-list-separat
or-invisible'); |
| 410 titles.createChild('div', 'conditions-list-text').textContent = WebInspector
.UIString('Upload'); |
| 411 titles.createChild('div', 'conditions-list-separator conditions-list-separat
or-invisible'); |
| 412 titles.createChild('div', 'conditions-list-text').textContent = WebInspector
.UIString('Latency'); |
| 413 |
| 414 var fields = content.createChild('div', 'conditions-edit-row'); |
| 415 fields.createChild('div', 'conditions-list-text conditions-list-title') |
| 416 .appendChild(editor.createInput('title', 'text', '', titleValidator)); |
| 417 fields.createChild('div', 'conditions-list-separator conditions-list-separat
or-invisible'); |
| 418 |
| 419 var cell = fields.createChild('div', 'conditions-list-text'); |
| 420 cell.appendChild(editor.createInput('download', 'text', WebInspector.UIStrin
g('kb/s'), throughputValidator)); |
| 421 cell.createChild('div', 'conditions-edit-optional').textContent = WebInspect
or.UIString('optional'); |
| 422 fields.createChild('div', 'conditions-list-separator conditions-list-separat
or-invisible'); |
| 423 |
| 424 cell = fields.createChild('div', 'conditions-list-text'); |
| 425 cell.appendChild(editor.createInput('upload', 'text', WebInspector.UIString(
'kb/s'), throughputValidator)); |
| 426 cell.createChild('div', 'conditions-edit-optional').textContent = WebInspect
or.UIString('optional'); |
| 427 fields.createChild('div', 'conditions-list-separator conditions-list-separat
or-invisible'); |
| 428 |
| 429 cell = fields.createChild('div', 'conditions-list-text'); |
| 430 cell.appendChild(editor.createInput('latency', 'text', WebInspector.UIString
('ms'), latencyValidator)); |
| 431 cell.createChild('div', 'conditions-edit-optional').textContent = WebInspect
or.UIString('optional'); |
| 432 |
| 433 return editor; |
| 434 |
| 435 /** |
343 * @param {*} item | 436 * @param {*} item |
344 * @param {number} index | 437 * @param {number} index |
345 */ | 438 * @param {!HTMLInputElement|!HTMLSelectElement} input |
346 removeItemRequested: function(item, index) | 439 * @return {boolean} |
347 { | 440 */ |
348 var list = this._customSetting.get(); | 441 function titleValidator(item, index, input) { |
349 list.splice(index, 1); | 442 var value = input.value.trim(); |
350 this._customSetting.set(list); | 443 return value.length > 0 && value.length < 50; |
351 }, | 444 } |
352 | 445 |
353 /** | 446 /** |
354 * @override | |
355 * @param {*} item | 447 * @param {*} item |
356 * @param {!WebInspector.ListWidget.Editor} editor | 448 * @param {number} index |
357 * @param {boolean} isNew | 449 * @param {!HTMLInputElement|!HTMLSelectElement} input |
358 */ | 450 * @return {boolean} |
359 commitEdit: function(item, editor, isNew) | 451 */ |
360 { | 452 function throughputValidator(item, index, input) { |
361 var conditions = /** @type {?WebInspector.NetworkManager.Conditions} */
(item); | 453 var value = input.value.trim(); |
362 conditions.title = editor.control("title").value.trim(); | 454 return !value || (/^[\d]+(\.\d+)?|\.\d+$/.test(value) && value >= 0 && val
ue <= 10000000); |
363 var download = editor.control("download").value.trim(); | 455 } |
364 conditions.download = download ? parseInt(download, 10) * (1024 / 8) : -
1; | 456 |
365 var upload = editor.control("upload").value.trim(); | 457 /** |
366 conditions.upload = upload ? parseInt(upload, 10) * (1024 / 8) : -1; | |
367 var latency = editor.control("latency").value.trim(); | |
368 conditions.latency = latency ? parseInt(latency, 10) : 0; | |
369 | |
370 var list = this._customSetting.get(); | |
371 if (isNew) | |
372 list.push(conditions); | |
373 this._customSetting.set(list); | |
374 }, | |
375 | |
376 /** | |
377 * @override | |
378 * @param {*} item | 458 * @param {*} item |
379 * @return {!WebInspector.ListWidget.Editor} | 459 * @param {number} index |
380 */ | 460 * @param {!HTMLInputElement|!HTMLSelectElement} input |
381 beginEdit: function(item) | 461 * @return {boolean} |
382 { | 462 */ |
383 var conditions = /** @type {?WebInspector.NetworkManager.Conditions} */
(item); | 463 function latencyValidator(item, index, input) { |
384 var editor = this._createEditor(); | 464 var value = input.value.trim(); |
385 editor.control("title").value = conditions.title; | 465 return !value || (/^[\d]+$/.test(value) && value >= 0 && value <= 1000000)
; |
386 editor.control("download").value = conditions.download <= 0 ? "" : Strin
g(conditions.download / (1024 / 8)); | 466 } |
387 editor.control("upload").value = conditions.upload <= 0 ? "" : String(co
nditions.upload / (1024 / 8)); | 467 } |
388 editor.control("latency").value = conditions.latency ? String(conditions
.latency) : ""; | |
389 return editor; | |
390 }, | |
391 | |
392 /** | |
393 * @return {!WebInspector.ListWidget.Editor} | |
394 */ | |
395 _createEditor: function() | |
396 { | |
397 if (this._editor) | |
398 return this._editor; | |
399 | |
400 var editor = new WebInspector.ListWidget.Editor(); | |
401 this._editor = editor; | |
402 var content = editor.contentElement(); | |
403 | |
404 var titles = content.createChild("div", "conditions-edit-row"); | |
405 titles.createChild("div", "conditions-list-text conditions-list-title").
textContent = WebInspector.UIString("Profile Name"); | |
406 titles.createChild("div", "conditions-list-separator conditions-list-sep
arator-invisible"); | |
407 titles.createChild("div", "conditions-list-text").textContent = WebInspe
ctor.UIString("Download"); | |
408 titles.createChild("div", "conditions-list-separator conditions-list-sep
arator-invisible"); | |
409 titles.createChild("div", "conditions-list-text").textContent = WebInspe
ctor.UIString("Upload"); | |
410 titles.createChild("div", "conditions-list-separator conditions-list-sep
arator-invisible"); | |
411 titles.createChild("div", "conditions-list-text").textContent = WebInspe
ctor.UIString("Latency"); | |
412 | |
413 var fields = content.createChild("div", "conditions-edit-row"); | |
414 fields.createChild("div", "conditions-list-text conditions-list-title").
appendChild(editor.createInput("title", "text", "", titleValidator)); | |
415 fields.createChild("div", "conditions-list-separator conditions-list-sep
arator-invisible"); | |
416 | |
417 var cell = fields.createChild("div", "conditions-list-text"); | |
418 cell.appendChild(editor.createInput("download", "text", WebInspector.UIS
tring("kb/s"), throughputValidator)); | |
419 cell.createChild("div", "conditions-edit-optional").textContent = WebIns
pector.UIString("optional"); | |
420 fields.createChild("div", "conditions-list-separator conditions-list-sep
arator-invisible"); | |
421 | |
422 cell = fields.createChild("div", "conditions-list-text"); | |
423 cell.appendChild(editor.createInput("upload", "text", WebInspector.UIStr
ing("kb/s"), throughputValidator)); | |
424 cell.createChild("div", "conditions-edit-optional").textContent = WebIns
pector.UIString("optional"); | |
425 fields.createChild("div", "conditions-list-separator conditions-list-sep
arator-invisible"); | |
426 | |
427 cell = fields.createChild("div", "conditions-list-text"); | |
428 cell.appendChild(editor.createInput("latency", "text", WebInspector.UISt
ring("ms"), latencyValidator)); | |
429 cell.createChild("div", "conditions-edit-optional").textContent = WebIns
pector.UIString("optional"); | |
430 | |
431 return editor; | |
432 | |
433 /** | |
434 * @param {*} item | |
435 * @param {number} index | |
436 * @param {!HTMLInputElement|!HTMLSelectElement} input | |
437 * @return {boolean} | |
438 */ | |
439 function titleValidator(item, index, input) | |
440 { | |
441 var value = input.value.trim(); | |
442 return value.length > 0 && value.length < 50; | |
443 } | |
444 | |
445 /** | |
446 * @param {*} item | |
447 * @param {number} index | |
448 * @param {!HTMLInputElement|!HTMLSelectElement} input | |
449 * @return {boolean} | |
450 */ | |
451 function throughputValidator(item, index, input) | |
452 { | |
453 var value = input.value.trim(); | |
454 return !value || (/^[\d]+(\.\d+)?|\.\d+$/.test(value) && value >= 0
&& value <= 10000000); | |
455 } | |
456 | |
457 /** | |
458 * @param {*} item | |
459 * @param {number} index | |
460 * @param {!HTMLInputElement|!HTMLSelectElement} input | |
461 * @return {boolean} | |
462 */ | |
463 function latencyValidator(item, index, input) | |
464 { | |
465 var value = input.value.trim(); | |
466 return !value || (/^[\d]+$/.test(value) && value >= 0 && value <= 10
00000); | |
467 } | |
468 }, | |
469 | |
470 __proto__: WebInspector.VBox.prototype | |
471 }; | 468 }; |
472 | 469 |
473 /** | 470 /** |
474 * @constructor | |
475 * @implements {WebInspector.ActionDelegate} | 471 * @implements {WebInspector.ActionDelegate} |
| 472 * @unrestricted |
476 */ | 473 */ |
477 WebInspector.NetworkConditionsActionDelegate = function() | 474 WebInspector.NetworkConditionsActionDelegate = class { |
478 { | 475 /** |
479 }; | 476 * @override |
480 | 477 * @param {!WebInspector.Context} context |
481 WebInspector.NetworkConditionsActionDelegate.prototype = { | 478 * @param {string} actionId |
482 /** | 479 * @return {boolean} |
483 * @override | 480 */ |
484 * @param {!WebInspector.Context} context | 481 handleAction(context, actionId) { |
485 * @param {string} actionId | 482 if (actionId === 'components.network-online') { |
486 * @return {boolean} | 483 WebInspector.multitargetNetworkManager.setNetworkConditions(WebInspector.N
etworkManager.NoThrottlingConditions); |
487 */ | 484 return true; |
488 handleAction: function(context, actionId) | 485 } |
489 { | 486 if (actionId === 'components.network-offline') { |
490 if (actionId === "components.network-online") { | 487 WebInspector.multitargetNetworkManager.setNetworkConditions(WebInspector.N
etworkManager.OfflineConditions); |
491 WebInspector.multitargetNetworkManager.setNetworkConditions(WebInspe
ctor.NetworkManager.NoThrottlingConditions); | 488 return true; |
492 return true; | 489 } |
493 } | 490 return false; |
494 if (actionId === "components.network-offline") { | 491 } |
495 WebInspector.multitargetNetworkManager.setNetworkConditions(WebInspe
ctor.NetworkManager.OfflineConditions); | |
496 return true; | |
497 } | |
498 return false; | |
499 } | |
500 }; | 492 }; |
501 | 493 |
502 /** | 494 /** |
503 * @param {?NetworkAgent.ResourcePriority} priority | 495 * @param {?NetworkAgent.ResourcePriority} priority |
504 * @return {string} | 496 * @return {string} |
505 */ | 497 */ |
506 WebInspector.uiLabelForPriority = function(priority) | 498 WebInspector.uiLabelForPriority = function(priority) { |
507 { | 499 var labelMap = WebInspector.uiLabelForPriority._priorityToUILabel; |
508 var labelMap = WebInspector.uiLabelForPriority._priorityToUILabel; | 500 if (!labelMap) { |
509 if (!labelMap) { | 501 labelMap = new Map([ |
510 labelMap = new Map([ | 502 [NetworkAgent.ResourcePriority.VeryLow, WebInspector.UIString('Lowest')], |
511 [NetworkAgent.ResourcePriority.VeryLow, WebInspector.UIString("Lowes
t")], | 503 [NetworkAgent.ResourcePriority.Low, WebInspector.UIString('Low')], |
512 [NetworkAgent.ResourcePriority.Low, WebInspector.UIString("Low")], | 504 [NetworkAgent.ResourcePriority.Medium, WebInspector.UIString('Medium')], |
513 [NetworkAgent.ResourcePriority.Medium, WebInspector.UIString("Medium
")], | 505 [NetworkAgent.ResourcePriority.High, WebInspector.UIString('High')], |
514 [NetworkAgent.ResourcePriority.High, WebInspector.UIString("High")], | 506 [NetworkAgent.ResourcePriority.VeryHigh, WebInspector.UIString('Highest')] |
515 [NetworkAgent.ResourcePriority.VeryHigh, WebInspector.UIString("High
est")] | 507 ]); |
516 ]); | 508 WebInspector.uiLabelForPriority._priorityToUILabel = labelMap; |
517 WebInspector.uiLabelForPriority._priorityToUILabel = labelMap; | 509 } |
518 } | 510 return labelMap.get(priority) || WebInspector.UIString('Unknown'); |
519 return labelMap.get(priority) || WebInspector.UIString("Unknown"); | |
520 }; | 511 }; |
OLD | NEW |