Chromium Code Reviews| 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 36% |
| copy from third_party/WebKit/Source/devtools/front_end/mobile_throttling/NetworkConditionsSelector.js |
| copy to third_party/WebKit/Source/devtools/front_end/mobile_throttling/ThrottlingManager.js |
| index 3c25698f2f5b31d4725727810b52dbf167745678..e9833c895939bd2f742b2a3b6e92eae7997eade8 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,45 @@ |
| -// 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(); |
| + this._cpuThrottlingRate = MobileThrottling.CPUThrottlingRates.NoThrottling; |
| + /** @type {!Set<!UI.ToolbarComboBox>} */ |
| + this._cpuThrottlingControls = new Set(); |
| + this._cpuThrottlingRates = [ |
|
dgozman
2017/06/30 23:21:49
Place next to other presets.
chenwilliam
2017/07/05 22:35:31
Done.
|
| + MobileThrottling.CPUThrottlingRates.NoThrottling, |
| + MobileThrottling.CPUThrottlingRates.MidTierMobile, |
| + MobileThrottling.CPUThrottlingRates.LowEndMobile, |
| + ]; |
| + SDK.targetManager.observeModels(SDK.EmulationModel, this); |
|
dgozman
2017/06/30 23:21:49
Do this in a last line.
chenwilliam
2017/07/05 22:35:32
Done.
|
| + /** @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; |
| + this._trackNetworkConditions(); |
| } |
| - /** |
| - * @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) { |
| + 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 +50,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 +79,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 +130,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,112 +162,99 @@ 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 {number} 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; |
| - } |
| + createControl() { |
|
dgozman
2017/06/30 23:21:49
createCPUThrottlingSelector
chenwilliam
2017/07/05 22:35:31
Done.
|
| + 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; |
| + _trackNetworkConditions() { |
|
dgozman
2017/06/30 23:21:49
Let's inline it.
chenwilliam
2017/07/05 22:35:32
Done.
|
| + SDK.multitargetNetworkManager.addEventListener( |
| + SDK.MultitargetNetworkManager.Events.ConditionsChanged, networkConditionsChanged.bind(this)); |
| + /** |
| + * @this {!MobileThrottling.ThrottlingManager} |
| + */ |
| + function networkConditionsChanged() { |
|
dgozman
2017/06/30 23:21:49
arrow function ftw!
chenwilliam
2017/07/05 22:35:31
Done.
|
| + this._lastNetworkThrottlingConditions = this._currentNetworkThrottlingConditions; |
| + this._currentNetworkThrottlingConditions = SDK.multitargetNetworkManager.networkConditions(); |
| + } |
| + } |
| +}; |
| -/** @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 |
| + * @return {!MobileThrottling.ThrottlingManager} |
| */ |
| -MobileThrottling.NetworkConditionsActionDelegate = class { |
| - /** |
| - * @override |
| - * @param {!UI.Context} context |
| - * @param {string} actionId |
| - * @return {boolean} |
| - */ |
| - handleAction(context, actionId) { |
| - if (actionId === 'network-conditions.network-online') { |
| - SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.NoThrottlingConditions); |
| - return true; |
| - } |
| - if (actionId === 'network-conditions.network-offline') { |
| - SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.OfflineConditions); |
| - return true; |
| - } |
| - return false; |
| +MobileThrottling.throttlingManager = function() { |
|
dgozman
2017/06/30 23:21:49
return self.singleton(MobileThrottling.ThrottlingM
chenwilliam
2017/07/05 22:35:31
Done.
|
| + if (!MobileThrottling._throttlingManager) { |
| + /** @type {!MobileThrottling.ThrottlingManager} */ |
| + MobileThrottling._throttlingManager = new MobileThrottling.ThrottlingManager(); |
| } |
| + return MobileThrottling._throttlingManager; |
| }; |