Chromium Code Reviews| 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 /** | 4 |
| 5 * @unrestricted | 5 MobileThrottling.NetworkThrottlingSelector = class { |
| 6 */ | 6 /** |
| 7 MobileThrottling.NetworkConditionsSelector = class { | 7 * @param {function(!Array<!MobileThrottling.NetworkThrottlingConditionsGroup> ):!Array<?SDK.NetworkManager.Conditions>} populateCallback |
| 8 /** | |
| 9 * @param {function(!Array<!MobileThrottling.NetworkConditionsGroup>):!Array<? SDK.NetworkManager.Conditions>} populateCallback | |
| 10 * @param {function(number)} selectCallback | 8 * @param {function(number)} selectCallback |
| 11 */ | 9 * @param {!Common.Setting<!Array<!SDK.NetworkManager.Conditions>>} customNetw orkConditionsSetting |
| 12 constructor(populateCallback, selectCallback) { | 10 */ |
| 11 constructor(populateCallback, selectCallback, customNetworkConditionsSetting) { | |
| 13 this._populateCallback = populateCallback; | 12 this._populateCallback = populateCallback; |
| 14 this._selectCallback = selectCallback; | 13 this._selectCallback = selectCallback; |
| 15 this._customSetting = Common.moduleSetting('customNetworkConditions'); | 14 this._customNetworkConditionsSetting = customNetworkConditionsSetting; |
| 16 this._customSetting.addChangeListener(this._populateOptions, this); | 15 this._customNetworkConditionsSetting.addChangeListener(this._populateOptions , this); |
| 17 this._manager = SDK.multitargetNetworkManager; | 16 SDK.multitargetNetworkManager.addEventListener( |
| 18 this._manager.addEventListener( | 17 SDK.MultitargetNetworkManager.Events.ConditionsChanged, this._networkCon ditionsChanged, this); |
| 19 SDK.MultitargetNetworkManager.Events.ConditionsChanged, this._conditions Changed, this); | 18 /** @type {!Array<?SDK.NetworkManager.Conditions>} */ |
| 19 this._options; | |
| 20 this._populateOptions(); | 20 this._populateOptions(); |
| 21 } | 21 } |
| 22 | 22 |
| 23 /** | 23 revealAndUpdate() { |
| 24 * @param {number} throughput | 24 Common.Revealer.reveal(this._customNetworkConditionsSetting); |
| 25 * @param {boolean=} plainText | 25 this._networkConditionsChanged(); |
| 26 * @return {string} | 26 } |
| 27 */ | 27 |
| 28 static throughputText(throughput, plainText) { | 28 /** |
| 29 if (throughput < 0) | 29 * @param {!SDK.NetworkManager.Conditions} conditions |
| 30 return ''; | 30 */ |
| 31 var throughputInKbps = throughput / (1024 / 8); | 31 optionSelected(conditions) { |
| 32 var delimiter = plainText ? '' : ' '; | 32 SDK.multitargetNetworkManager.setNetworkConditions(conditions); |
| 33 if (throughputInKbps < 1024) | |
| 34 return Common.UIString('%d%skb/s', throughputInKbps, delimiter); | |
| 35 if (throughputInKbps < 1024 * 10) | |
| 36 return Common.UIString('%.1f%sMb/s', throughputInKbps / 1024, delimiter); | |
| 37 return Common.UIString('%d%sMb/s', (throughputInKbps / 1024) | 0, delimiter) ; | |
| 38 } | |
| 39 | |
| 40 /** | |
| 41 * @param {!HTMLSelectElement} selectElement | |
| 42 */ | |
| 43 static decorateSelect(selectElement) { | |
| 44 var options = []; | |
| 45 var selector = new MobileThrottling.NetworkConditionsSelector(populate, sele ct); | |
| 46 selectElement.addEventListener('change', optionSelected, false); | |
| 47 | |
| 48 /** | |
| 49 * @param {!Array.<!MobileThrottling.NetworkConditionsGroup>} groups | |
| 50 * @return {!Array<?SDK.NetworkManager.Conditions>} | |
| 51 */ | |
| 52 function populate(groups) { | |
| 53 selectElement.removeChildren(); | |
| 54 options = []; | |
| 55 for (var i = 0; i < groups.length; ++i) { | |
| 56 var group = groups[i]; | |
| 57 var groupElement = selectElement.createChild('optgroup'); | |
| 58 groupElement.label = group.title; | |
| 59 for (var conditions of group.items) { | |
| 60 var title = Common.UIString(conditions.title); | |
| 61 var option = new Option(title, title); | |
| 62 groupElement.appendChild(option); | |
| 63 options.push(conditions); | |
| 64 } | |
| 65 if (i === groups.length - 1) { | |
| 66 groupElement.appendChild(new Option(Common.UIString('Add\u2026'), Comm on.UIString('Add\u2026'))); | |
| 67 options.push(null); | |
| 68 } | |
| 69 } | |
| 70 return options; | |
| 71 } | |
| 72 | |
| 73 function optionSelected() { | |
| 74 if (selectElement.selectedIndex === selectElement.options.length - 1) | |
| 75 selector.revealAndUpdate(); | |
| 76 else | |
| 77 selector.optionSelected(options[selectElement.selectedIndex]); | |
| 78 } | |
| 79 | |
| 80 /** | |
| 81 * @param {number} index | |
| 82 */ | |
| 83 function select(index) { | |
| 84 if (selectElement.selectedIndex !== index) | |
| 85 selectElement.selectedIndex = index; | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 /** | |
| 90 * @return {!UI.ToolbarMenuButton} | |
| 91 */ | |
| 92 static createToolbarMenuButton() { | |
| 93 var button = new UI.ToolbarMenuButton(appendItems); | |
| 94 button.setGlyph(''); | |
| 95 button.turnIntoSelect(); | |
| 96 | |
| 97 /** @type {!Array<?SDK.NetworkManager.Conditions>} */ | |
| 98 var options = []; | |
| 99 var selectedIndex = -1; | |
| 100 var selector = new MobileThrottling.NetworkConditionsSelector(populate, sele ct); | |
| 101 return button; | |
| 102 | |
| 103 /** | |
| 104 * @param {!UI.ContextMenu} contextMenu | |
| 105 */ | |
| 106 function appendItems(contextMenu) { | |
| 107 for (var index = 0; index < options.length; ++index) { | |
| 108 var conditions = options[index]; | |
| 109 if (!conditions) { | |
| 110 contextMenu.appendSeparator(); | |
| 111 } else { | |
| 112 contextMenu.appendCheckboxItem( | |
| 113 Common.UIString(conditions.title), selector.optionSelected.bind(se lector, conditions), | |
| 114 selectedIndex === index); | |
| 115 } | |
| 116 } | |
| 117 contextMenu.appendItem(Common.UIString('Edit\u2026'), selector.revealAndUp date.bind(selector)); | |
| 118 } | |
| 119 | |
| 120 /** | |
| 121 * @param {!Array.<!MobileThrottling.NetworkConditionsGroup>} groups | |
| 122 * @return {!Array<?SDK.NetworkManager.Conditions>} | |
| 123 */ | |
| 124 function populate(groups) { | |
| 125 options = []; | |
| 126 for (var group of groups) { | |
| 127 for (var conditions of group.items) | |
| 128 options.push(conditions); | |
| 129 options.push(null); | |
| 130 } | |
| 131 return options; | |
| 132 } | |
| 133 | |
| 134 /** | |
| 135 * @param {number} index | |
| 136 */ | |
| 137 function select(index) { | |
| 138 selectedIndex = index; | |
| 139 button.setText(options[index].title); | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 /** | |
| 144 * @return {!UI.ToolbarCheckbox} | |
| 145 */ | |
| 146 static createOfflineToolbarCheckbox() { | |
| 147 var checkbox = new UI.ToolbarCheckbox( | |
| 148 Common.UIString('Offline'), Common.UIString('Force disconnected from net work'), forceOffline); | |
| 149 SDK.multitargetNetworkManager.addEventListener( | |
| 150 SDK.MultitargetNetworkManager.Events.ConditionsChanged, networkCondition sChanged); | |
| 151 checkbox.setChecked(SDK.multitargetNetworkManager.networkConditions() === SD K.NetworkManager.OfflineConditions); | |
| 152 | |
| 153 function forceOffline() { | |
| 154 if (checkbox.checked()) { | |
| 155 MobileThrottling.NetworkConditionsSelector._lastNetworkConditions = | |
| 156 SDK.multitargetNetworkManager.networkConditions(); | |
| 157 SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.Of flineConditions); | |
| 158 } else { | |
| 159 SDK.multitargetNetworkManager.setNetworkConditions( | |
| 160 MobileThrottling.NetworkConditionsSelector._lastNetworkConditions); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 function networkConditionsChanged() { | |
| 165 var conditions = SDK.multitargetNetworkManager.networkConditions(); | |
| 166 checkbox.setChecked(conditions === SDK.NetworkManager.OfflineConditions); | |
| 167 } | |
| 168 return checkbox; | |
| 169 } | 33 } |
| 170 | 34 |
| 171 _populateOptions() { | 35 _populateOptions() { |
| 172 var customGroup = {title: Common.UIString('Custom'), items: this._customSett ing.get()}; | |
| 173 var presetsGroup = {title: Common.UIString('Presets'), items: MobileThrottli ng.NetworkConditionsSelector.presets}; | |
| 174 var disabledGroup = {title: Common.UIString('Disabled'), items: [SDK.Network Manager.NoThrottlingConditions]}; | 36 var disabledGroup = {title: Common.UIString('Disabled'), items: [SDK.Network Manager.NoThrottlingConditions]}; |
| 175 this._options = this._populateCallback([disabledGroup, presetsGroup, customG roup]); | 37 var presetsGroup = {title: Common.UIString('Common'), items: MobileThrottlin g.networkPresets}; |
| 176 if (!this._conditionsChanged()) { | 38 var advancedGroup = {title: Common.UIString('Advanced'), items: MobileThrott ling.advancedNetworkPresets}; |
| 39 var customGroup = {title: Common.UIString('Custom'), items: this._customNetw orkConditionsSetting.get()}; | |
| 40 this._options = this._populateCallback([disabledGroup, presetsGroup, advance dGroup, customGroup]); | |
| 41 if (!this._networkConditionsChanged()) { | |
| 177 for (var i = this._options.length - 1; i >= 0; i--) { | 42 for (var i = this._options.length - 1; i >= 0; i--) { |
| 178 if (this._options[i]) { | 43 if (this._options[i]) { |
| 179 this.optionSelected(/** @type {!SDK.NetworkManager.Conditions} */ (thi s._options[i])); | 44 this.optionSelected(/** @type {!SDK.NetworkManager.Conditions} */ (thi s._options[i])); |
| 180 break; | 45 break; |
| 181 } | 46 } |
| 182 } | 47 } |
| 183 } | 48 } |
| 184 } | 49 } |
| 185 | 50 |
| 186 revealAndUpdate() { | 51 /** |
| 187 Common.Revealer.reveal(this._customSetting); | 52 * @return {boolean} returns false if selected condition no longer exists |
| 188 this._conditionsChanged(); | 53 */ |
| 189 } | 54 _networkConditionsChanged() { |
| 190 | 55 var value = SDK.multitargetNetworkManager.networkConditions(); |
| 191 /** | |
| 192 * @param {!SDK.NetworkManager.Conditions} conditions | |
| 193 */ | |
| 194 optionSelected(conditions) { | |
| 195 this._manager.setNetworkConditions(conditions); | |
| 196 } | |
| 197 | |
| 198 /** | |
| 199 * @return {boolean} | |
| 200 */ | |
| 201 _conditionsChanged() { | |
| 202 var value = this._manager.networkConditions(); | |
| 203 for (var index = 0; index < this._options.length; ++index) { | 56 for (var index = 0; index < this._options.length; ++index) { |
| 204 var option = this._options[index]; | 57 var option = this._options[index]; |
| 205 if (option && option.download === value.download && option.upload === valu e.upload && | 58 if (option && option.download === value.download && option.upload === valu e.upload && |
| 206 option.latency === value.latency && option.title === value.title) { | 59 option.latency === value.latency && option.title === value.title) { |
| 207 this._selectCallback(index); | 60 this._selectCallback(index); |
| 208 return true; | 61 return true; |
| 209 } | 62 } |
| 210 } | 63 } |
| 211 return false; | 64 return false; |
| 212 } | 65 } |
| 213 }; | 66 }; |
| 214 | 67 |
| 68 MobileThrottling.MobileThrottlingSelector = class { | |
| 69 /** | |
| 70 * @param {function(!Array<!MobileThrottling.MobileThrottlingConditionsGroup>) :!MobileThrottling.ConditionsList} populateCallback | |
| 71 * @param {function(number)} selectCallback | |
| 72 */ | |
| 73 constructor(populateCallback, selectCallback) { | |
| 74 this._populateCallback = populateCallback; | |
| 75 this._selectCallback = selectCallback; | |
| 76 MobileThrottling.throttlingManager().addEventListener( | |
| 77 MobileThrottling.ThrottlingManager.Events.RateChanged, this._conditionsC hanged, this); | |
| 78 SDK.multitargetNetworkManager.addEventListener( | |
| 79 SDK.MultitargetNetworkManager.Events.ConditionsChanged, this._conditions Changed, this); | |
| 80 /** @type {!MobileThrottling.ConditionsList} */ | |
| 81 this._options = this._populateOptions(); | |
| 82 this._conditionsChanged(); | |
| 83 } | |
| 84 | |
| 85 /** | |
| 86 * @param {!MobileThrottling.Conditions} conditions | |
| 87 */ | |
| 88 optionSelected(conditions) { | |
| 89 SDK.multitargetNetworkManager.setNetworkConditions(conditions.network); | |
| 90 MobileThrottling.throttlingManager().setCPUThrottlingRate(conditions.cpuThro ttlingRate); | |
| 91 } | |
| 92 | |
| 93 /** | |
| 94 * @return {!MobileThrottling.ConditionsList} | |
| 95 */ | |
| 96 _populateOptions() { | |
| 97 var disabledGroup = {title: Common.UIString('Disabled'), items: [MobileThrot tling.NoThrottlingConditions]}; | |
| 98 var presetsGroup = {title: Common.UIString('Presets'), items: MobileThrottli ng.mobilePresets}; | |
| 99 var advancedGroup = {title: Common.UIString('Advanced'), items: MobileThrott ling.advancedMobilePresets}; | |
| 100 return this._populateCallback([disabledGroup, presetsGroup, advancedGroup]); | |
| 101 } | |
| 102 | |
| 103 _conditionsChanged() { | |
| 104 var networkConditions = SDK.multitargetNetworkManager.networkConditions(); | |
| 105 var cpuThrottlingRate = MobileThrottling.throttlingManager().cpuThrottlingRa te(); | |
| 106 for (var index = 0; index < this._options.length; ++index) { | |
| 107 var option = this._options[index]; | |
| 108 if (option && option.network === networkConditions && option.cpuThrottling Rate === cpuThrottlingRate) { | |
| 109 this._selectCallback(index); | |
| 110 return; | |
| 111 } | |
| 112 } | |
| 113 this._selectCallback(this._options.indexOf(MobileThrottling.CustomConditions )); | |
| 114 } | |
| 115 }; | |
| 116 | |
| 117 /** | |
| 118 * @typedef {{ | |
| 119 * title: string, | |
| 120 * description: string, | |
| 121 * network: SDK.NetworkManager.Conditions, | |
| 122 * cpuThrottlingRate: number | |
| 123 * }} | |
| 124 **/ | |
| 125 MobileThrottling.Conditions; | |
| 126 | |
| 127 /** @enum {number} */ | |
| 128 MobileThrottling.CPUThrottlingRates = { | |
| 129 NoThrottling: 1, | |
| 130 MidTierMobile: 4, | |
| 131 LowEndMobile: 6, | |
| 132 }; | |
| 133 | |
| 134 /** @type {!MobileThrottling.Conditions} */ | |
| 135 MobileThrottling.NoThrottlingConditions = { | |
| 136 title: SDK.NetworkManager.NoThrottlingConditions.title, | |
| 137 description: Common.UIString('No throttling'), | |
| 138 network: SDK.NetworkManager.NoThrottlingConditions, | |
| 139 cpuThrottlingRate: MobileThrottling.CPUThrottlingRates.NoThrottling, | |
| 140 }; | |
| 141 | |
| 142 /** @type {!MobileThrottling.Conditions} */ | |
| 143 MobileThrottling.OfflineConditions = { | |
| 144 title: SDK.NetworkManager.OfflineConditions.title, | |
| 145 description: Common.UIString('No internet connectivity'), | |
| 146 network: SDK.NetworkManager.OfflineConditions, | |
| 147 cpuThrottlingRate: MobileThrottling.CPUThrottlingRates.NoThrottling, | |
| 148 }; | |
| 149 | |
| 150 /** @type {!MobileThrottling.Conditions} */ | |
| 151 MobileThrottling.LowEndMobileConditions = { | |
| 152 title: Common.UIString('Low-end mobile'), | |
| 153 description: Common.UIString('Slow 3G & 6x CPU slowdown'), | |
| 154 network: SDK.NetworkManager.Slow3GConditions, | |
| 155 cpuThrottlingRate: MobileThrottling.CPUThrottlingRates.LowEndMobile, | |
| 156 }; | |
| 157 | |
| 158 /** @type {!MobileThrottling.Conditions} */ | |
| 159 MobileThrottling.MidTierMobileConditions = { | |
| 160 title: Common.UIString('Mid-tier mobile'), | |
| 161 description: Common.UIString('Fast 3G & 4x CPU slowdown'), | |
| 162 network: SDK.NetworkManager.Fast3GConditions, | |
| 163 cpuThrottlingRate: MobileThrottling.CPUThrottlingRates.MidTierMobile, | |
| 164 }; | |
| 165 | |
| 166 /** | |
| 167 * @typedef {{ | |
| 168 * title: string, | |
| 169 * description: string | |
| 170 * }} | |
| 171 **/ | |
| 172 MobileThrottling.PlaceholderConditions; | |
| 173 | |
| 174 /** @type {!MobileThrottling.PlaceholderConditions} */ | |
| 175 MobileThrottling.CustomConditions = { | |
| 176 title: Common.UIString('Custom'), | |
| 177 description: Common.UIString('Check Network and Performance panels'), | |
| 178 }; | |
| 179 | |
| 215 /** @typedef {!{title: string, items: !Array<!SDK.NetworkManager.Conditions>}} * / | 180 /** @typedef {!{title: string, items: !Array<!SDK.NetworkManager.Conditions>}} * / |
| 216 MobileThrottling.NetworkConditionsGroup; | 181 MobileThrottling.NetworkThrottlingConditionsGroup; |
| 217 | 182 |
| 218 | 183 /** @typedef {!{title: string, items: !Array<!MobileThrottling.Conditions|!Mobil eThrottling.PlaceholderConditions>}} */ |
| 219 /** @type {!Array.<!SDK.NetworkManager.Conditions>} */ | 184 MobileThrottling.MobileThrottlingConditionsGroup; |
| 220 MobileThrottling.NetworkConditionsSelector.presets = [ | 185 |
| 186 /** @typedef {!Array<?MobileThrottling.Conditions|!MobileThrottling.PlaceholderC onditions>} */ | |
| 187 MobileThrottling.ConditionsList; | |
| 188 | |
| 189 /** @type {!Array.<!MobileThrottling.Conditions>} */ | |
| 190 MobileThrottling.mobilePresets = [ | |
| 191 MobileThrottling.MidTierMobileConditions, MobileThrottling.LowEndMobileConditi ons, MobileThrottling.CustomConditions | |
| 192 ]; | |
| 193 | |
| 194 /** @type {!Array.<!MobileThrottling.Conditions>} */ | |
| 195 MobileThrottling.advancedMobilePresets = [ | |
| 196 MobileThrottling.OfflineConditions, | |
| 197 ]; | |
| 198 | |
| 199 /** @type {!Array<!SDK.NetworkManager.Conditions>} */ | |
| 200 MobileThrottling.networkPresets = [ | |
| 201 SDK.NetworkManager.Fast3GConditions, | |
| 202 SDK.NetworkManager.Slow3GConditions, | |
| 203 ]; | |
| 204 | |
| 205 /** @type {!Array<!SDK.NetworkManager.Conditions>} */ | |
| 206 MobileThrottling.advancedNetworkPresets = [ | |
| 221 SDK.NetworkManager.OfflineConditions, | 207 SDK.NetworkManager.OfflineConditions, |
| 222 {title: 'Slow 3G', download: 500 * 1024 / 8 * .8, upload: 500 * 1024 / 8 * .8, latency: 400 * 5}, | |
| 223 {title: 'Fast 3G', download: 1.6 * 1024 * 1024 / 8 * .9, upload: 750 * 1024 / 8 * .9, latency: 150 * 3.75} | |
| 224 ]; | 208 ]; |
| 225 | 209 |
| 226 /** | 210 /** |
| 227 * @implements {UI.ActionDelegate} | 211 * @implements {UI.ActionDelegate} |
| 228 * @unrestricted | 212 * @unrestricted |
| 229 */ | 213 */ |
| 230 MobileThrottling.NetworkConditionsActionDelegate = class { | 214 MobileThrottling.NetworkConditionsActionDelegate = class { |
|
dgozman
2017/06/30 23:21:49
ThrottlingManager.ActionDelegate
chenwilliam
2017/07/05 22:35:32
Done.
| |
| 231 /** | 215 /** |
| 232 * @override | 216 * @override |
| 233 * @param {!UI.Context} context | 217 * @param {!UI.Context} context |
| 234 * @param {string} actionId | 218 * @param {string} actionId |
| 235 * @return {boolean} | 219 * @return {boolean} |
| 236 */ | 220 */ |
| 237 handleAction(context, actionId) { | 221 handleAction(context, actionId) { |
| 238 if (actionId === 'network-conditions.network-online') { | 222 if (actionId === 'network-conditions.network-online') { |
| 239 SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.NoTh rottlingConditions); | 223 SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.NoTh rottlingConditions); |
| 240 return true; | 224 return true; |
| 241 } | 225 } |
| 242 if (actionId === 'network-conditions.network-offline') { | 226 if (actionId === 'network-conditions.network-offline') { |
| 243 SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.Offl ineConditions); | 227 SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.Offl ineConditions); |
| 244 return true; | 228 return true; |
| 245 } | 229 } |
| 246 return false; | 230 return false; |
| 247 } | 231 } |
| 248 }; | 232 }; |
| OLD | NEW |