-
Notifications
You must be signed in to change notification settings - Fork 450
/
ThrottlingSettingsTab.js
277 lines (247 loc) · 10.4 KB
/
ThrottlingSettingsTab.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @ts-nocheck
// TODO(crbug.com/1011811): Enable TypeScript compiler checks
import * as Common from '../common/common.js';
import * as SDK from '../sdk/sdk.js'; // eslint-disable-line no-unused-vars
import * as UI from '../ui/ui.js';
/**
* @implements {UI.ListWidget.Delegate}
* @unrestricted
*/
export class ThrottlingSettingsTab extends UI.Widget.VBox {
constructor() {
super(true);
this.registerRequiredCSS('mobile_throttling/throttlingSettingsTab.css');
const header = this.contentElement.createChild('div', 'header');
header.textContent = ls`Network Throttling Profiles`;
UI.ARIAUtils.markAsHeading(header, 1);
const addButton = UI.UIUtils.createTextButton(
Common.UIString.UIString('Add custom profile...'), this._addButtonClicked.bind(this), 'add-conditions-button');
this.contentElement.appendChild(addButton);
this._list = new UI.ListWidget.ListWidget(this);
this._list.element.classList.add('conditions-list');
this._list.registerRequiredCSS('mobile_throttling/throttlingSettingsTab.css');
this._list.show(this.contentElement);
this._customSetting = Common.Settings.Settings.instance().moduleSetting('customNetworkConditions');
this._customSetting.addChangeListener(this._conditionsUpdated, this);
this.setDefaultFocusedElement(addButton);
}
/**
* @override
*/
wasShown() {
super.wasShown();
this._conditionsUpdated();
}
_conditionsUpdated() {
this._list.clear();
const conditions = this._customSetting.get();
for (let i = 0; i < conditions.length; ++i) {
this._list.appendItem(conditions[i], true);
}
this._list.appendSeparator();
}
_addButtonClicked() {
this._list.addNewItem(this._customSetting.get().length, {title: '', download: -1, upload: -1, latency: 0});
}
/**
* @override
* @param {*} item
* @param {boolean} editable
* @return {!Element}
*/
renderItem(item, editable) {
const conditions = /** @type {!SDK.NetworkManager.Conditions} */ (item);
const element = document.createElement('div');
element.classList.add('conditions-list-item');
const title = element.createChild('div', 'conditions-list-text conditions-list-title');
const titleText = title.createChild('div', 'conditions-list-title-text');
titleText.textContent = conditions.title;
titleText.title = conditions.title;
element.createChild('div', 'conditions-list-separator');
element.createChild('div', 'conditions-list-text').textContent = throughputText(conditions.download);
element.createChild('div', 'conditions-list-separator');
element.createChild('div', 'conditions-list-text').textContent = throughputText(conditions.upload);
element.createChild('div', 'conditions-list-separator');
element.createChild('div', 'conditions-list-text').textContent =
Common.UIString.UIString('%dms', conditions.latency);
return element;
}
/**
* @override
* @param {*} item
* @param {number} index
*/
removeItemRequested(item, index) {
const list = this._customSetting.get();
list.splice(index, 1);
this._customSetting.set(list);
}
/**
* @override
* @param {*} item
* @param {!UI.ListWidget.Editor} editor
* @param {boolean} isNew
*/
commitEdit(item, editor, isNew) {
const conditions = /** @type {?SDK.NetworkManager.Conditions} */ (item);
conditions.title = editor.control('title').value.trim();
const download = editor.control('download').value.trim();
conditions.download = download ? parseInt(download, 10) * (1024 / 8) : -1;
const upload = editor.control('upload').value.trim();
conditions.upload = upload ? parseInt(upload, 10) * (1024 / 8) : -1;
const latency = editor.control('latency').value.trim();
conditions.latency = latency ? parseInt(latency, 10) : 0;
const list = this._customSetting.get();
if (isNew) {
list.push(conditions);
}
this._customSetting.set(list);
}
/**
* @override
* @param {*} item
* @return {!UI.ListWidget.Editor}
*/
beginEdit(item) {
const conditions = /** @type {?SDK.NetworkManager.Conditions} */ (item);
const editor = this._createEditor();
editor.control('title').value = conditions.title;
editor.control('download').value = conditions.download <= 0 ? '' : String(conditions.download / (1024 / 8));
editor.control('upload').value = conditions.upload <= 0 ? '' : String(conditions.upload / (1024 / 8));
editor.control('latency').value = conditions.latency ? String(conditions.latency) : '';
return editor;
}
/**
* @return {!UI.ListWidget.Editor}
*/
_createEditor() {
if (this._editor) {
return this._editor;
}
const editor = new UI.ListWidget.Editor();
this._editor = editor;
const content = editor.contentElement();
const titles = content.createChild('div', 'conditions-edit-row');
const nameLabel = titles.createChild('div', 'conditions-list-text conditions-list-title');
const nameStr = ls`Profile Name`;
nameLabel.textContent = nameStr;
titles.createChild('div', 'conditions-list-separator conditions-list-separator-invisible');
const downloadLabel = titles.createChild('div', 'conditions-list-text');
const downloadStr = ls`Download`;
downloadLabel.textContent = downloadStr;
titles.createChild('div', 'conditions-list-separator conditions-list-separator-invisible');
const uploadLabel = titles.createChild('div', 'conditions-list-text');
const uploadStr = ls`Upload`;
uploadLabel.textContent = uploadStr;
titles.createChild('div', 'conditions-list-separator conditions-list-separator-invisible');
const latencyLabel = titles.createChild('div', 'conditions-list-text');
const latencyStr = ls`Latency`;
latencyLabel.textContent = latencyStr;
const fields = content.createChild('div', 'conditions-edit-row');
const nameInput = editor.createInput('title', 'text', '', titleValidator);
UI.ARIAUtils.setAccessibleName(nameInput, nameStr);
fields.createChild('div', 'conditions-list-text conditions-list-title').appendChild(nameInput);
fields.createChild('div', 'conditions-list-separator conditions-list-separator-invisible');
let cell = fields.createChild('div', 'conditions-list-text');
const downloadInput = editor.createInput('download', 'text', ls`kb/s`, throughputValidator);
cell.appendChild(downloadInput);
UI.ARIAUtils.setAccessibleName(downloadInput, downloadStr);
const downloadOptional = cell.createChild('div', 'conditions-edit-optional');
const optionalStr = ls`optional`;
downloadOptional.textContent = optionalStr;
UI.ARIAUtils.setDescription(downloadInput, optionalStr);
fields.createChild('div', 'conditions-list-separator conditions-list-separator-invisible');
cell = fields.createChild('div', 'conditions-list-text');
const uploadInput = editor.createInput('upload', 'text', ls`kb/s`, throughputValidator);
UI.ARIAUtils.setAccessibleName(uploadInput, uploadStr);
cell.appendChild(uploadInput);
const uploadOptional = cell.createChild('div', 'conditions-edit-optional');
uploadOptional.textContent = optionalStr;
UI.ARIAUtils.setDescription(uploadInput, optionalStr);
fields.createChild('div', 'conditions-list-separator conditions-list-separator-invisible');
cell = fields.createChild('div', 'conditions-list-text');
const latencyInput = editor.createInput('latency', 'text', ls`ms`, latencyValidator);
UI.ARIAUtils.setAccessibleName(latencyInput, latencyStr);
cell.appendChild(latencyInput);
const latencyOptional = cell.createChild('div', 'conditions-edit-optional');
latencyOptional.textContent = optionalStr;
UI.ARIAUtils.setDescription(latencyInput, optionalStr);
return editor;
/**
* @param {*} item
* @param {number} index
* @param {!HTMLInputElement|!HTMLSelectElement} input
* @return {!UI.ListWidget.ValidatorResult}
*/
function titleValidator(item, index, input) {
const maxLength = 49;
const value = input.value.trim();
const valid = value.length > 0 && value.length <= maxLength;
if (!valid) {
const errorMessage = ls`Profile Name characters length must be between 1 to ${maxLength} inclusive`;
return {valid, errorMessage};
}
return {valid};
}
/**
* @param {*} item
* @param {number} index
* @param {!HTMLInputElement|!HTMLSelectElement} input
* @return {!UI.ListWidget.ValidatorResult}
*/
function throughputValidator(item, index, input) {
const minThroughput = 0;
const maxThroughput = 10000000;
const value = input.value.trim();
const parsedValue = Number(value);
const throughput = input.getAttribute('aria-label');
const valid = !Number.isNaN(parsedValue) && parsedValue >= minThroughput && parsedValue <= maxThroughput;
if (!valid) {
const errorMessage =
ls`${throughput} must be a number between ${minThroughput}kb/s to ${maxThroughput}kb/s inclusive`;
return {valid, errorMessage};
}
return {valid};
}
/**
* @param {*} item
* @param {number} index
* @param {!HTMLInputElement|!HTMLSelectElement} input
* @return {!UI.ListWidget.ValidatorResult}
*/
function latencyValidator(item, index, input) {
const minLatency = 0;
const maxLatency = 1000000;
const value = input.value.trim();
const parsedValue = Number(value);
const valid = Number.isInteger(parsedValue) && parsedValue >= minLatency && parsedValue <= maxLatency;
if (!valid) {
const errorMessage = ls`Latency must be an integer between ${minLatency}ms to ${maxLatency}ms inclusive`;
return {valid, errorMessage};
}
return {valid};
}
}
}
/**
* @param {number} throughput
* @param {boolean=} plainText
* @return {string}
*/
export function throughputText(throughput, plainText) {
if (throughput < 0) {
return '';
}
const throughputInKbps = throughput / (1024 / 8);
const delimiter = plainText ? '' : ' ';
if (throughputInKbps < 1024) {
return Common.UIString.UIString('%d%skb/s', throughputInKbps, delimiter);
}
if (throughputInKbps < 1024 * 10) {
return Common.UIString.UIString('%.1f%sMb/s', throughputInKbps / 1024, delimiter);
}
return Common.UIString.UIString('%d%sMb/s', (throughputInKbps / 1024) | 0, delimiter);
}