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

Unified Diff: third_party/WebKit/Source/devtools/front_end/mobile_throttling/ThrottlingSelector.js

Issue 2938503002: DevTools: unify Network & CPU throttling (Closed)
Patch Set: rebaseline Created 3 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/devtools/front_end/mobile_throttling/ThrottlingSelector.js
diff --git a/third_party/WebKit/Source/devtools/front_end/mobile_throttling/NetworkConditionsSelector.js b/third_party/WebKit/Source/devtools/front_end/mobile_throttling/ThrottlingSelector.js
similarity index 34%
rename from third_party/WebKit/Source/devtools/front_end/mobile_throttling/NetworkConditionsSelector.js
rename to third_party/WebKit/Source/devtools/front_end/mobile_throttling/ThrottlingSelector.js
index 3c25698f2f5b31d4725727810b52dbf167745678..3534a5eea9be3cb085b729b3b13dd2a34ad3dba3 100644
--- a/third_party/WebKit/Source/devtools/front_end/mobile_throttling/NetworkConditionsSelector.js
+++ b/third_party/WebKit/Source/devtools/front_end/mobile_throttling/ThrottlingSelector.js
@@ -1,52 +1,35 @@
// Copyright 2015 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.
-/**
- * @unrestricted
- */
-MobileThrottling.NetworkConditionsSelector = class {
+
+MobileThrottling.NetworkThrottlingSelector = class {
/**
- * @param {function(!Array<!MobileThrottling.NetworkConditionsGroup>):!Array<?SDK.NetworkManager.Conditions>} populateCallback
+ * @param {function(!Array<!MobileThrottling.NetworkThrottlingConditionsGroup>):!Array<?SDK.NetworkManager.Conditions>} populateCallback
* @param {function(number)} selectCallback
*/
constructor(populateCallback, selectCallback) {
this._populateCallback = populateCallback;
this._selectCallback = selectCallback;
- this._customSetting = Common.moduleSetting('customNetworkConditions');
- this._customSetting.addChangeListener(this._populateOptions, this);
- this._manager = SDK.multitargetNetworkManager;
- this._manager.addEventListener(
- SDK.MultitargetNetworkManager.Events.ConditionsChanged, this._conditionsChanged, this);
+ MobileThrottling.customNetworkConditionsSetting().addChangeListener(this._populateOptions, this);
+ SDK.multitargetNetworkManager.addEventListener(
+ SDK.MultitargetNetworkManager.Events.ConditionsChanged, this._networkConditionsChanged, this);
+ /** @type {!Array<?SDK.NetworkManager.Conditions>} */
+ this._options;
this._populateOptions();
}
- /**
- * @param {number} throughput
- * @param {boolean=} plainText
- * @return {string}
- */
- static throughputText(throughput, plainText) {
- if (throughput < 0)
- return '';
- var throughputInKbps = throughput / (1024 / 8);
- var delimiter = plainText ? '' : ' ';
- if (throughputInKbps < 1024)
- return Common.UIString('%d%skb/s', throughputInKbps, delimiter);
- if (throughputInKbps < 1024 * 10)
- return Common.UIString('%.1f%sMb/s', throughputInKbps / 1024, delimiter);
- return Common.UIString('%d%sMb/s', (throughputInKbps / 1024) | 0, delimiter);
- }
-
/**
* @param {!HTMLSelectElement} selectElement
+ * @return {!MobileThrottling.NetworkThrottlingSelector}
*/
static decorateSelect(selectElement) {
var options = [];
- var selector = new MobileThrottling.NetworkConditionsSelector(populate, select);
+ var selector = new MobileThrottling.NetworkThrottlingSelector(populate, select);
selectElement.addEventListener('change', optionSelected, false);
+ return selector;
/**
- * @param {!Array.<!MobileThrottling.NetworkConditionsGroup>} groups
+ * @param {!Array.<!MobileThrottling.NetworkThrottlingConditionsGroup>} groups
* @return {!Array<?SDK.NetworkManager.Conditions>}
*/
function populate(groups) {
@@ -57,7 +40,7 @@ MobileThrottling.NetworkConditionsSelector = class {
var groupElement = selectElement.createChild('optgroup');
groupElement.label = group.title;
for (var conditions of group.items) {
- var title = Common.UIString(conditions.title);
+ var title = conditions.title;
var option = new Option(title, title);
groupElement.appendChild(option);
options.push(conditions);
@@ -72,9 +55,9 @@ MobileThrottling.NetworkConditionsSelector = class {
function optionSelected() {
if (selectElement.selectedIndex === selectElement.options.length - 1)
- selector.revealAndUpdate();
+ selector._revealAndUpdate();
else
- selector.optionSelected(options[selectElement.selectedIndex]);
+ selector._optionSelected(options[selectElement.selectedIndex]);
}
/**
@@ -86,18 +69,116 @@ MobileThrottling.NetworkConditionsSelector = class {
}
}
+ /**
+ * @return {!UI.ToolbarCheckbox}
+ */
+ static createOfflineToolbarCheckbox() {
+ var checkbox = new UI.ToolbarCheckbox(
+ Common.UIString('Offline'), Common.UIString('Force disconnected from network'), forceOffline);
+ SDK.multitargetNetworkManager.addEventListener(
+ SDK.MultitargetNetworkManager.Events.ConditionsChanged, networkConditionsChanged);
+ checkbox.setChecked(isOfflineConditions(SDK.multitargetNetworkManager.networkConditions()));
+
+ function forceOffline() {
+ if (checkbox.checked()) {
+ MobileThrottling.NetworkThrottlingSelector._lastNetworkThrottlingConditions =
+ SDK.multitargetNetworkManager.networkConditions();
+ SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.OfflineConditions);
+ } else {
+ SDK.multitargetNetworkManager.setNetworkConditions(
+ MobileThrottling.NetworkThrottlingSelector._lastNetworkThrottlingConditions);
+ }
+ }
+
+ function networkConditionsChanged() {
+ checkbox.setChecked(isOfflineConditions(SDK.multitargetNetworkManager.networkConditions()));
+ }
+
+ function isOfflineConditions(conditions) {
+ return conditions.latency === SDK.NetworkManager.OfflineConditions.latency &&
+ conditions.upload === SDK.NetworkManager.OfflineConditions.upload &&
+ conditions.download === SDK.NetworkManager.OfflineConditions.download;
+ }
+ return checkbox;
+ }
+
+ _populateOptions() {
+ var disabledGroup = {title: Common.UIString('Disabled'), items: [MobileThrottling.NoThrottlingConditions]};
+ var presetsGroup = {title: Common.UIString('Common'), items: MobileThrottling.networkPresets};
+ var advancedGroup = {title: Common.UIString('Advanced'), items: MobileThrottling.advancedNetworkPresets};
+ var customGroup = {
+ title: Common.UIString('Custom'),
+ items: MobileThrottling.customNetworkConditionsSetting().get()
+ };
+ this._options = this._populateCallback([disabledGroup, presetsGroup, advancedGroup, customGroup]);
+ if (!this._networkConditionsChanged()) {
+ for (var i = this._options.length - 1; i >= 0; i--) {
+ if (this._options[i]) {
+ this._optionSelected(/** @type {!MobileThrottling.Conditions} */ (this._options[i]));
+ break;
+ }
+ }
+ }
+ }
+
+ _revealAndUpdate() {
+ Common.Revealer.reveal(MobileThrottling.customNetworkConditionsSetting());
+ this._networkConditionsChanged();
+ }
+
+ /**
+ * @param {!SDK.NetworkManager.Conditions} conditions
+ */
+ _optionSelected(conditions) {
+ SDK.multitargetNetworkManager.setNetworkConditions(conditions);
+ }
+
+ /**
+ * @return {boolean} returns false if selected condition no longer exists
+ */
+ _networkConditionsChanged() {
+ var value = SDK.multitargetNetworkManager.networkConditions();
+ for (var index = 0; index < this._options.length; ++index) {
+ var option = this._options[index];
+ if (option && option.download === value.download && option.upload === value.upload &&
+ option.latency === value.latency) {
+ this._selectCallback(index);
+ return true;
+ }
+ }
+ return false;
+ }
+};
+
+MobileThrottling.MobileThrottlingSelector = class {
+ /**
+ * @param {function(!Array<!MobileThrottling.MobileThrottlingConditionsGroup>):!MobileThrottling.ConditionsList} populateCallback
+ * @param {function(number)} selectCallback
+ */
+ constructor(populateCallback, selectCallback) {
+ this._populateCallback = populateCallback;
+ this._selectCallback = selectCallback;
+ MobileThrottling.cpuThrottlingRateSetting().addChangeListener(this._conditionsChanged, this);
+ SDK.multitargetNetworkManager.addEventListener(
+ SDK.MultitargetNetworkManager.Events.ConditionsChanged, this._conditionsChanged, this);
+ /** @type {!MobileThrottling.ConditionsList} */
+ this._options = this._populateOptions();
+ this._conditionsChanged();
+ }
+
/**
* @return {!UI.ToolbarMenuButton}
*/
static createToolbarMenuButton() {
var button = new UI.ToolbarMenuButton(appendItems);
+ button.setTitle(Common.UIString('Throttling'));
button.setGlyph('');
button.turnIntoSelect();
- /** @type {!Array<?SDK.NetworkManager.Conditions>} */
+ /** @type {!MobileThrottling.ConditionsList} */
var options = [];
var selectedIndex = -1;
- var selector = new MobileThrottling.NetworkConditionsSelector(populate, select);
+ var selector = new MobileThrottling.MobileThrottlingSelector(populate, select);
return button;
/**
@@ -108,18 +189,21 @@ MobileThrottling.NetworkConditionsSelector = class {
var conditions = options[index];
if (!conditions) {
contextMenu.appendSeparator();
- } else {
- contextMenu.appendCheckboxItem(
- Common.UIString(conditions.title), selector.optionSelected.bind(selector, conditions),
- selectedIndex === index);
+ continue;
}
+ if (conditions.title === MobileThrottling.CustomConditions.title &&
+ conditions.description === MobileThrottling.CustomConditions.description)
+ continue;
+ contextMenu.appendCheckboxItem(
+ Common.UIString(conditions.title),
+ selector._optionSelected.bind(selector, /** @type {!MobileThrottling.Conditions} */ (conditions)),
+ selectedIndex === index);
}
- contextMenu.appendItem(Common.UIString('Edit\u2026'), selector.revealAndUpdate.bind(selector));
}
/**
- * @param {!Array.<!MobileThrottling.NetworkConditionsGroup>} groups
- * @return {!Array<?SDK.NetworkManager.Conditions>}
+ * @param {!Array.<!MobileThrottling.MobileThrottlingConditionsGroup>} groups
+ * @return {!MobileThrottling.ConditionsList}
*/
function populate(groups) {
options = [];
@@ -137,90 +221,157 @@ MobileThrottling.NetworkConditionsSelector = class {
function select(index) {
selectedIndex = index;
button.setText(options[index].title);
+ button.setTitle(options[index].description || 'Throttling');
}
}
/**
- * @return {!UI.ToolbarCheckbox}
+ * @return {!MobileThrottling.ConditionsList}
*/
- static createOfflineToolbarCheckbox() {
- var checkbox = new UI.ToolbarCheckbox(
- Common.UIString('Offline'), Common.UIString('Force disconnected from network'), forceOffline);
- SDK.multitargetNetworkManager.addEventListener(
- SDK.MultitargetNetworkManager.Events.ConditionsChanged, networkConditionsChanged);
- checkbox.setChecked(SDK.multitargetNetworkManager.networkConditions() === SDK.NetworkManager.OfflineConditions);
-
- function forceOffline() {
- if (checkbox.checked()) {
- MobileThrottling.NetworkConditionsSelector._lastNetworkConditions =
- SDK.multitargetNetworkManager.networkConditions();
- SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.OfflineConditions);
- } else {
- SDK.multitargetNetworkManager.setNetworkConditions(
- MobileThrottling.NetworkConditionsSelector._lastNetworkConditions);
- }
- }
-
- function networkConditionsChanged() {
- var conditions = SDK.multitargetNetworkManager.networkConditions();
- checkbox.setChecked(conditions === SDK.NetworkManager.OfflineConditions);
- }
- return checkbox;
- }
-
_populateOptions() {
- var customGroup = {title: Common.UIString('Custom'), items: this._customSetting.get()};
- var presetsGroup = {title: Common.UIString('Presets'), items: MobileThrottling.NetworkConditionsSelector.presets};
- var disabledGroup = {title: Common.UIString('Disabled'), items: [SDK.NetworkManager.NoThrottlingConditions]};
- this._options = this._populateCallback([disabledGroup, presetsGroup, customGroup]);
- if (!this._conditionsChanged()) {
- for (var i = this._options.length - 1; i >= 0; i--) {
- if (this._options[i]) {
- this.optionSelected(/** @type {!SDK.NetworkManager.Conditions} */ (this._options[i]));
- break;
- }
- }
- }
- }
-
- revealAndUpdate() {
- Common.Revealer.reveal(this._customSetting);
- this._conditionsChanged();
+ var disabledGroup = {title: Common.UIString('Disabled'), items: [MobileThrottling.NoThrottlingConditions]};
+ var presetsGroup = {title: Common.UIString('Presets'), items: MobileThrottling.mobilePresets};
+ var advancedGroup = {title: Common.UIString('Advanced'), items: MobileThrottling.advancedMobilePresets};
+ return this._populateCallback([disabledGroup, presetsGroup, advancedGroup]);
}
/**
- * @param {!SDK.NetworkManager.Conditions} conditions
+ * @param {!MobileThrottling.Conditions} conditions
*/
- optionSelected(conditions) {
- this._manager.setNetworkConditions(conditions);
+ _optionSelected(conditions) {
+ SDK.multitargetNetworkManager.setNetworkConditions(conditions);
+ MobileThrottling.cpuThrottlingRateSetting().set(conditions.cpuThrottlingRate);
}
- /**
- * @return {boolean}
- */
_conditionsChanged() {
- var value = this._manager.networkConditions();
+ var value = Object.assign(
+ {}, SDK.multitargetNetworkManager.networkConditions(),
+ {cpuThrottlingRate: MobileThrottling.cpuThrottlingRateSetting().get()});
for (var index = 0; index < this._options.length; ++index) {
var option = this._options[index];
if (option && option.download === value.download && option.upload === value.upload &&
- option.latency === value.latency && option.title === value.title) {
+ option.latency === value.latency && option.cpuThrottlingRate === value.cpuThrottlingRate) {
this._selectCallback(index);
- return true;
+ return;
}
}
- return false;
+ this._selectCallback(this._options.indexOf(MobileThrottling.CustomConditions));
}
};
+/**
+ * @return {!Common.Setting<!Array<!SDK.NetworkManager.Conditions>>}
+ */
+MobileThrottling.customNetworkConditionsSetting = function() {
+ if (!MobileThrottling._customNetworkConditionsSetting)
+ MobileThrottling._customNetworkConditionsSetting = Common.moduleSetting('customNetworkConditions');
+ return MobileThrottling._customNetworkConditionsSetting;
+};
+
+/**
+ * @return {!Common.Setting<number>}
+ */
+MobileThrottling.cpuThrottlingRateSetting = function() {
+ if (!MobileThrottling._cpuThrottlingRateSetting)
+ MobileThrottling._cpuThrottlingRateSetting = Common.moduleSetting('cpuThrottlingRate');
+ return MobileThrottling._cpuThrottlingRateSetting;
+};
+
+/**
+ * Superset of SDK.NetworkManager.Conditions
+ * @typedef {{
dgozman 2017/06/28 22:33:09 Should we have network:SDK.NetworkManager.Conditio
chenwilliam 2017/06/29 20:34:02 I agree - done.
+ * download: number,
+ * upload: number,
+ * latency: number,
+ * title: string,
+ * description: (string|undefined),
+ * cpuThrottlingRate: number
+ * }}
+ **/
+MobileThrottling.Conditions;
+
+/** @enum {number} */
+MobileThrottling.CPUThrottlingRates = {
+ NoThrottling: 1,
+ MidTierMobile: 4,
+ LowEndMobile: 6,
+};
+
+MobileThrottling.NoThrottlingConditions = /** @type {!MobileThrottling.Conditions} */ (
+ Object.assign(/** @type {!Object} */ ({}), SDK.NetworkManager.NoThrottlingConditions, {
+ cpuThrottlingRate: MobileThrottling.CPUThrottlingRates.NoThrottling,
+ }));
+
+MobileThrottling.OfflineConditions = /** @type {!MobileThrottling.Conditions} */ (
+ Object.assign(/** @type {!Object} */ ({}), SDK.NetworkManager.OfflineConditions, {
+ cpuThrottlingRate: MobileThrottling.CPUThrottlingRates.MidTierMobile,
+ }));
+
+MobileThrottling.LieFiMobileConditions = /** @type {!MobileThrottling.Conditions} */ (
+ Object.assign(/** @type {!Object} */ ({}), SDK.NetworkManager.LieFiMobileConditions, {
+ cpuThrottlingRate: MobileThrottling.CPUThrottlingRates.MidTierMobile,
+ }));
+
+/**
+ * @typedef {{
+ * title: string,
+ * description: string
+ * }}
+ **/
+MobileThrottling.PlaceholderConditions;
+
+/** @type {!MobileThrottling.PlaceholderConditions} */
+MobileThrottling.CustomConditions = {
+ title: Common.UIString('Custom'),
+ description: Common.UIString('Check Network and Performance panels'),
+};
+
/** @typedef {!{title: string, items: !Array<!SDK.NetworkManager.Conditions>}} */
-MobileThrottling.NetworkConditionsGroup;
+MobileThrottling.NetworkThrottlingConditionsGroup;
+
+/** @typedef {!{title: string, items: !Array<!MobileThrottling.Conditions|!MobileThrottling.PlaceholderConditions>}} */
+MobileThrottling.MobileThrottlingConditionsGroup;
+/** @typedef {!Array<?MobileThrottling.Conditions|!MobileThrottling.PlaceholderConditions>} */
+MobileThrottling.ConditionsList;
+
+/** @type {!MobileThrottling.Conditions} */
+MobileThrottling.LowEndMobileConditions = /** @type {!MobileThrottling.Conditions} */ (
+ Object.assign(/** @type {!Object} */ ({}), SDK.NetworkManager.Slow3GConditions, {
+ title: Common.UIString('Low-end mobile'),
+ cpuThrottlingRate: MobileThrottling.CPUThrottlingRates.LowEndMobile,
+ description: Common.UIString('Slow 3G & 6x CPU slowdown')
+ }));
+
+/** @type {!MobileThrottling.Conditions} */
+MobileThrottling.MidTierMobileConditions = /** @type {!MobileThrottling.Conditions} */ (
+ Object.assign(/** @type {!Object} */ ({}), SDK.NetworkManager.Fast3GConditions, {
+ title: Common.UIString('Mid-tier mobile'),
+ cpuThrottlingRate: MobileThrottling.CPUThrottlingRates.MidTierMobile,
+ description: Common.UIString('Fast 3G & 4x CPU slowdown')
+ }));
+
+/** @type {!Array.<!MobileThrottling.Conditions>} */
+MobileThrottling.mobilePresets = [
+ MobileThrottling.MidTierMobileConditions, MobileThrottling.LowEndMobileConditions, MobileThrottling.CustomConditions
+];
+
+/** @type {!Array.<!MobileThrottling.Conditions>} */
+MobileThrottling.advancedMobilePresets = [
+ MobileThrottling.OfflineConditions,
+ MobileThrottling.LieFiMobileConditions,
+];
+
+/** @type {!Array<!SDK.NetworkManager.Conditions>} */
+MobileThrottling.networkPresets = [
+ SDK.NetworkManager.Fast3GConditions,
+ SDK.NetworkManager.Slow3GConditions,
+];
-/** @type {!Array.<!SDK.NetworkManager.Conditions>} */
-MobileThrottling.NetworkConditionsSelector.presets = [
+/** @type {!Array<!SDK.NetworkManager.Conditions>} */
+MobileThrottling.advancedNetworkPresets = [
SDK.NetworkManager.OfflineConditions,
- {title: 'Slow 3G', download: 500 * 1024 / 8 * .8, upload: 500 * 1024 / 8 * .8, latency: 400 * 5},
- {title: 'Fast 3G', download: 1.6 * 1024 * 1024 / 8 * .9, upload: 750 * 1024 / 8 * .9, latency: 150 * 3.75}
+ SDK.NetworkManager.LieFiMobileConditions,
];
/**
@@ -236,11 +387,11 @@ MobileThrottling.NetworkConditionsActionDelegate = class {
*/
handleAction(context, actionId) {
if (actionId === 'network-conditions.network-online') {
- SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.NoThrottlingConditions);
+ SDK.multitargetNetworkManager.setNetworkConditions(MobileThrottling.NoThrottlingConditions);
return true;
}
if (actionId === 'network-conditions.network-offline') {
- SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.OfflineConditions);
+ SDK.multitargetNetworkManager.setNetworkConditions(MobileThrottling.OfflineConditions);
return true;
}
return false;

Powered by Google App Engine
This is Rietveld 408576698