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

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

Issue 2938503002: DevTools: unify Network & CPU throttling (Closed)
Patch Set: update test Created 3 years, 5 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/ThrottlingManager.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/ThrottlingManager.js
similarity index 40%
rename from third_party/WebKit/Source/devtools/front_end/mobile_throttling/NetworkConditionsSelector.js
rename to third_party/WebKit/Source/devtools/front_end/mobile_throttling/ThrottlingManager.js
index 3c25698f2f5b31d4725727810b52dbf167745678..5381321968828276e5e349b5eff2b2e647e0cd0a 100644
--- a/third_party/WebKit/Source/devtools/front_end/mobile_throttling/NetworkConditionsSelector.js
+++ b/third_party/WebKit/Source/devtools/front_end/mobile_throttling/ThrottlingManager.js
@@ -1,52 +1,47 @@
-// Copyright 2015 The Chromium Authors. All rights reserved.
+// 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.
+
/**
- * @unrestricted
+ * @implements {SDK.SDKModelObserver<!SDK.EmulationModel>}
*/
-MobileThrottling.NetworkConditionsSelector = class {
- /**
- * @param {function(!Array<!MobileThrottling.NetworkConditionsGroup>):!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);
- this._populateOptions();
- }
+MobileThrottling.ThrottlingManager = class extends Common.Object {
+ constructor() {
+ super();
+ /** @type {!MobileThrottling.CPUThrottlingRates} */
+ this._cpuThrottlingRate = MobileThrottling.CPUThrottlingRates.NoThrottling;
+ /** @type {!Set<!UI.ToolbarComboBox>} */
+ this._cpuThrottlingControls = new Set();
+ this._cpuThrottlingRates = MobileThrottling.cpuThrottlingPresets;
+ /** @type {!Common.Setting<!Array<!SDK.NetworkManager.Conditions>>} */
+ this._customNetworkConditionsSetting = Common.moduleSetting('customNetworkConditions');
+ /** @type {!SDK.NetworkManager.Conditions} */
+ this._currentNetworkThrottlingConditions = SDK.NetworkManager.NoThrottlingConditions;
+ /** @type {!SDK.NetworkManager.Conditions} */
+ this._lastNetworkThrottlingConditions;
- /**
- * @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);
+ SDK.multitargetNetworkManager.addEventListener(SDK.MultitargetNetworkManager.Events.ConditionsChanged, () => {
+ this._lastNetworkThrottlingConditions = this._currentNetworkThrottlingConditions;
+ this._currentNetworkThrottlingConditions = SDK.multitargetNetworkManager.networkConditions();
+ });
+
+ SDK.targetManager.observeModels(SDK.EmulationModel, this);
}
+
/**
* @param {!HTMLSelectElement} selectElement
+ * @return {!MobileThrottling.NetworkThrottlingSelector}
*/
- static decorateSelect(selectElement) {
+ decorateSelectWithNetworkThrottling(selectElement) {
var options = [];
- var selector = new MobileThrottling.NetworkConditionsSelector(populate, select);
+ var selector =
+ new MobileThrottling.NetworkThrottlingSelector(populate, select, this._customNetworkConditionsSetting);
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 +52,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);
@@ -86,18 +81,47 @@ MobileThrottling.NetworkConditionsSelector = class {
}
}
+ /**
+ * @return {!UI.ToolbarCheckbox}
+ */
+ createOfflineToolbarCheckbox() {
+ var checkbox = new UI.ToolbarCheckbox(
+ Common.UIString('Offline'), Common.UIString('Force disconnected from network'), forceOffline.bind(this));
+ SDK.multitargetNetworkManager.addEventListener(
+ SDK.MultitargetNetworkManager.Events.ConditionsChanged, networkConditionsChanged);
+ checkbox.setChecked(SDK.multitargetNetworkManager.networkConditions() === SDK.NetworkManager.OfflineConditions);
+
+ /**
+ * @this {!MobileThrottling.ThrottlingManager}
+ */
+ function forceOffline() {
+ if (checkbox.checked())
+ SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.OfflineConditions);
+ else
+ SDK.multitargetNetworkManager.setNetworkConditions(this._lastNetworkThrottlingConditions);
+ }
+
+ function networkConditionsChanged() {
+ checkbox.setChecked(SDK.multitargetNetworkManager.networkConditions() === SDK.NetworkManager.OfflineConditions);
+ }
+
+ return checkbox;
+ }
+
+
/**
* @return {!UI.ToolbarMenuButton}
*/
- static createToolbarMenuButton() {
+ createMobileThrottlingButton() {
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 +132,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,97 +164,83 @@ MobileThrottling.NetworkConditionsSelector = class {
function select(index) {
selectedIndex = index;
button.setText(options[index].title);
+ button.setTitle(options[index].description);
}
}
/**
- * @return {!UI.ToolbarCheckbox}
+ * @return {number}
*/
- 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;
+ cpuThrottlingRate() {
+ return this._cpuThrottlingRate;
}
- _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;
- }
- }
+ /**
+ * @param {!MobileThrottling.CPUThrottlingRates} rate
+ */
+ setCPUThrottlingRate(rate) {
+ this._cpuThrottlingRate = rate;
+ for (var emulationModel of SDK.targetManager.models(SDK.EmulationModel))
+ emulationModel.setCPUThrottlingRate(this._cpuThrottlingRate);
+ var icon = null;
+ if (this._cpuThrottlingRate !== MobileThrottling.CPUThrottlingRates.NoThrottling) {
+ Host.userMetrics.actionTaken(Host.UserMetrics.Action.CpuThrottlingEnabled);
+ icon = UI.Icon.create('smallicon-warning');
+ icon.title = Common.UIString('CPU throttling is enabled');
}
+ var index = this._cpuThrottlingRates.indexOf(this._cpuThrottlingRate);
+ for (var control of this._cpuThrottlingControls)
+ control.setSelectedIndex(index);
+ UI.inspectorView.setPanelIcon('timeline', icon);
+ this.dispatchEventToListeners(MobileThrottling.ThrottlingManager.Events.RateChanged, this._cpuThrottlingRate);
}
- revealAndUpdate() {
- Common.Revealer.reveal(this._customSetting);
- this._conditionsChanged();
+ /**
+ * @override
+ * @param {!SDK.EmulationModel} emulationModel
+ */
+ modelAdded(emulationModel) {
+ if (this._cpuThrottlingRate !== MobileThrottling.CPUThrottlingRates.NoThrottling)
+ emulationModel.setCPUThrottlingRate(this._cpuThrottlingRate);
}
/**
- * @param {!SDK.NetworkManager.Conditions} conditions
+ * @override
+ * @param {!SDK.EmulationModel} emulationModel
*/
- optionSelected(conditions) {
- this._manager.setNetworkConditions(conditions);
+ modelRemoved(emulationModel) {
}
/**
- * @return {boolean}
+ * @return {!UI.ToolbarComboBox}
*/
- _conditionsChanged() {
- var value = this._manager.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 && option.title === value.title) {
- this._selectCallback(index);
- return true;
- }
+ createCPUThrottlingSelector() {
+ var control = new UI.ToolbarComboBox(
+ event => this.setCPUThrottlingRate(this._cpuThrottlingRates[event.target.selectedIndex]));
+ this._cpuThrottlingControls.add(control);
+ var currentRate = this._cpuThrottlingRate;
+
+ for (var i = 0; i < this._cpuThrottlingRates.length; ++i) {
+ var rate = this._cpuThrottlingRates[i];
+ var title = rate === 1 ? Common.UIString('No throttling') : Common.UIString('%d\xD7 slowdown', rate);
+ var option = control.createOption(title);
+ control.addOption(option);
+ if (currentRate === rate)
+ control.setSelectedIndex(i);
}
- return false;
+ return control;
}
};
-/** @typedef {!{title: string, items: !Array<!SDK.NetworkManager.Conditions>}} */
-MobileThrottling.NetworkConditionsGroup;
-
-
-/** @type {!Array.<!SDK.NetworkManager.Conditions>} */
-MobileThrottling.NetworkConditionsSelector.presets = [
- 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}
-];
+/** @enum {symbol} */
+MobileThrottling.ThrottlingManager.Events = {
+ RateChanged: Symbol('RateChanged')
+};
/**
* @implements {UI.ActionDelegate}
- * @unrestricted
*/
-MobileThrottling.NetworkConditionsActionDelegate = class {
+MobileThrottling.ThrottlingManager.ActionDelegate = class {
/**
* @override
* @param {!UI.Context} context
@@ -246,3 +259,10 @@ MobileThrottling.NetworkConditionsActionDelegate = class {
return false;
}
};
+
+/**
+ * @return {!MobileThrottling.ThrottlingManager}
+ */
+MobileThrottling.throttlingManager = function() {
+ return self.singleton(MobileThrottling.ThrottlingManager);
+};

Powered by Google App Engine
This is Rietveld 408576698