OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @constructor |
| 7 * @extends {WebInspector.VBox} |
| 8 */ |
| 9 WebInspector.BlockedURLsPane = function() |
| 10 { |
| 11 WebInspector.VBox.call(this, true); |
| 12 this.registerRequiredCSS("network/blockedURLsPane.css"); |
| 13 |
| 14 this._blockedURLsSetting = WebInspector.moduleSetting("blockedURLs"); |
| 15 this._blockedURLsSetting.addChangeListener(this._update, this); |
| 16 |
| 17 this._toolbar = new WebInspector.Toolbar(this.contentElement); |
| 18 this._toolbar.element.addEventListener("click", consumeEvent); |
| 19 this._toolbar.appendToolbarItem(new WebInspector.ToolbarText(WebInspector.UI
String("Requests containing following URLs will be blocked"))); |
| 20 this._toolbar.appendToolbarItem(new WebInspector.ToolbarItem(createElementWi
thClass("div", "flex-auto-important"))); |
| 21 var addButton = new WebInspector.ToolbarButton(WebInspector.UIString("Add UR
L"), "add-toolbar-item"); |
| 22 addButton.addEventListener("click", this._addButtonClicked.bind(this)); |
| 23 this._toolbar.appendToolbarItem(addButton); |
| 24 var clearButton = new WebInspector.ToolbarButton(WebInspector.UIString("Remo
ve all"), "clear-toolbar-item"); |
| 25 clearButton.addEventListener("click", this._removeAll.bind(this)); |
| 26 this._toolbar.appendToolbarItem(clearButton); |
| 27 |
| 28 this._emptyElement = this.contentElement.createChild("div", "no-blocked-urls
"); |
| 29 this._emptyElement.textContent = WebInspector.UIString("No blocked URLs"); |
| 30 this._emptyElement.addEventListener("contextmenu", this._emptyElementContext
Menu.bind(this), true); |
| 31 |
| 32 this._listElement = this.contentElement.createChild("div", "blocked-urls-lis
t"); |
| 33 this._filterUI = new WebInspector.BlockedURLsPane.FilterUI(); |
| 34 |
| 35 /** @type {!Map<string, number>} */ |
| 36 this._blockedCountForUrl = new Map(); |
| 37 WebInspector.targetManager.addModelListener(WebInspector.NetworkManager, Web
Inspector.NetworkManager.EventTypes.RequestFinished, this._onRequestFinished, th
is); |
| 38 |
| 39 this._updateThrottler = new WebInspector.Throttler(200); |
| 40 |
| 41 this._update(); |
| 42 } |
| 43 |
| 44 WebInspector.BlockedURLsPane.prototype = { |
| 45 /** |
| 46 * @return {!WebInspector.FilterUI} |
| 47 */ |
| 48 filterUI: function() |
| 49 { |
| 50 return this._filterUI; |
| 51 }, |
| 52 |
| 53 /** |
| 54 * @param {!Event} event |
| 55 */ |
| 56 _emptyElementContextMenu: function(event) |
| 57 { |
| 58 var contextMenu = new WebInspector.ContextMenu(event); |
| 59 contextMenu.appendItem(WebInspector.UIString.capitalize("Add ^URL"), thi
s._addButtonClicked.bind(this)); |
| 60 contextMenu.show(); |
| 61 }, |
| 62 |
| 63 _addButtonClicked: function() |
| 64 { |
| 65 this._editing = true; |
| 66 var element = this._createElement("", this._blockedURLsSetting.get().len
gth); |
| 67 this._listElement.appendChild(element); |
| 68 element.scrollIntoView(); |
| 69 |
| 70 /** |
| 71 * @param {boolean} accept |
| 72 * @param {!Element} e |
| 73 * @param {string} text |
| 74 * @this {WebInspector.BlockedURLsPane} |
| 75 */ |
| 76 function finishEditing(accept, e, text) |
| 77 { |
| 78 this._listElement.removeChild(element); |
| 79 this._editing = false; |
| 80 if (accept && text) |
| 81 this._addBlockedURL(text); |
| 82 else |
| 83 this._update(); |
| 84 } |
| 85 |
| 86 WebInspector.InplaceEditor.startEditing(element._label, new WebInspector
.InplaceEditor.Config(finishEditing.bind(this, true), finishEditing.bind(this, f
alse))); |
| 87 }, |
| 88 |
| 89 /** |
| 90 * @param {string} url |
| 91 */ |
| 92 _addBlockedURL: function(url) |
| 93 { |
| 94 var blocked = this._blockedURLsSetting.get(); |
| 95 blocked.push(url); |
| 96 this._blockedURLsSetting.set(blocked); |
| 97 }, |
| 98 |
| 99 /** |
| 100 * @param {number} index |
| 101 */ |
| 102 _removeBlockedURL: function(index) |
| 103 { |
| 104 var blocked = this._blockedURLsSetting.get(); |
| 105 blocked.splice(index, 1); |
| 106 this._blockedURLsSetting.set(blocked); |
| 107 }, |
| 108 |
| 109 /** |
| 110 * @param {number} index |
| 111 * @param {string} url |
| 112 */ |
| 113 _changeBlockedURL: function(index, url) |
| 114 { |
| 115 var blocked = this._blockedURLsSetting.get(); |
| 116 blocked.splice(index, 1, url); |
| 117 this._blockedURLsSetting.set(blocked); |
| 118 }, |
| 119 |
| 120 _removeAll: function() |
| 121 { |
| 122 this._blockedURLsSetting.set([]); |
| 123 }, |
| 124 |
| 125 /** |
| 126 * @param {number} index |
| 127 * @param {!Event} event |
| 128 */ |
| 129 _contextMenu: function(index, event) |
| 130 { |
| 131 var contextMenu = new WebInspector.ContextMenu(event); |
| 132 contextMenu.appendItem(WebInspector.UIString.capitalize("Add ^URL"), thi
s._addButtonClicked.bind(this)); |
| 133 contextMenu.appendItem(WebInspector.UIString.capitalize("Remove ^URL"),
this._removeBlockedURL.bind(this, index)); |
| 134 contextMenu.appendItem(WebInspector.UIString.capitalize("Remove ^all"),
this._removeAll.bind(this)); |
| 135 contextMenu.show(); |
| 136 }, |
| 137 |
| 138 /** |
| 139 * @param {!Element} element |
| 140 * @param {number} index |
| 141 */ |
| 142 _labelClicked: function(element, index) |
| 143 { |
| 144 this._editing = true; |
| 145 |
| 146 /** |
| 147 * @param {boolean} accept |
| 148 * @param {!Element} e |
| 149 * @param {string} text |
| 150 * @this {WebInspector.BlockedURLsPane} |
| 151 */ |
| 152 function finishEditing(accept, e, text) |
| 153 { |
| 154 this._editing = false; |
| 155 if (accept) |
| 156 this._changeBlockedURL(index, text); |
| 157 else |
| 158 this._update(); |
| 159 } |
| 160 |
| 161 WebInspector.InplaceEditor.startEditing(element, new WebInspector.Inplac
eEditor.Config(finishEditing.bind(this, true), finishEditing.bind(this, false)))
; |
| 162 }, |
| 163 |
| 164 /** |
| 165 * @return {!Promise<?>} |
| 166 */ |
| 167 _update: function() |
| 168 { |
| 169 if (this._editing) |
| 170 return Promise.resolve(); |
| 171 |
| 172 this._listElement.removeChildren(); |
| 173 var blocked = this._blockedURLsSetting.get(); |
| 174 for (var index = 0; index < blocked.length; index++) |
| 175 this._listElement.appendChild(this._createElement(blocked[index], in
dex)); |
| 176 |
| 177 this._emptyElement.classList.toggle("hidden", !!blocked.length); |
| 178 this._filterUI.setActive(!!blocked.length); |
| 179 this._filterUI.dispatchEventToListeners(WebInspector.FilterUI.Events.Fil
terChanged); |
| 180 |
| 181 return Promise.resolve(); |
| 182 }, |
| 183 |
| 184 /** |
| 185 * @param {string} url |
| 186 * @param {number} index |
| 187 * @return {!Element} |
| 188 */ |
| 189 _createElement: function(url, index) |
| 190 { |
| 191 var element = createElementWithClass("div", "blocked-url"); |
| 192 |
| 193 var label = element.createChild("div", "blocked-url-text"); |
| 194 element._label = label; |
| 195 label.textContent = url; |
| 196 |
| 197 var count = this._blockedRequestsCount(url); |
| 198 var countElement = element.createChild("div", "blocked-count monospace")
; |
| 199 countElement.textContent = String.sprintf("[%d]", count); |
| 200 countElement.title = WebInspector.UIString(count === 1 ? "%d request blo
cked by this URL" : "%d requests blocked by this URL", count); |
| 201 |
| 202 var removeButton = element.createChild("div", "remove-button"); |
| 203 removeButton.title = WebInspector.UIString("Remove URL"); |
| 204 removeButton.addEventListener("click", this._removeBlockedURL.bind(this,
index), false); |
| 205 |
| 206 element.addEventListener("contextmenu", this._contextMenu.bind(this, ind
ex), true); |
| 207 element.addEventListener("dblclick", this._labelClicked.bind(this, label
, index), false); |
| 208 return element; |
| 209 }, |
| 210 |
| 211 /** |
| 212 * @param {string} url |
| 213 * @return {number} |
| 214 */ |
| 215 _blockedRequestsCount: function(url) |
| 216 { |
| 217 if (!url) |
| 218 return 0; |
| 219 |
| 220 var result = 0; |
| 221 for (var blockedUrl of this._blockedCountForUrl.keys()) { |
| 222 if (blockedUrl.indexOf(url) !== -1) |
| 223 result += this._blockedCountForUrl.get(blockedUrl); |
| 224 } |
| 225 return result; |
| 226 }, |
| 227 |
| 228 reset: function() |
| 229 { |
| 230 this._blockedCountForUrl.clear(); |
| 231 }, |
| 232 |
| 233 /** |
| 234 * @param {!WebInspector.Event} event |
| 235 */ |
| 236 _onRequestFinished: function(event) |
| 237 { |
| 238 var request = /** @type {!WebInspector.NetworkRequest} */ (event.data); |
| 239 if (request.blocked) { |
| 240 var count = this._blockedCountForUrl.get(request.url) || 0; |
| 241 this._blockedCountForUrl.set(request.url, count + 1); |
| 242 this._updateThrottler.schedule(this._update.bind(this)); |
| 243 } |
| 244 }, |
| 245 |
| 246 __proto__: WebInspector.VBox.prototype |
| 247 } |
| 248 |
| 249 |
| 250 /** |
| 251 * @constructor |
| 252 * @extends {WebInspector.Object} |
| 253 * @implements {WebInspector.FilterUI} |
| 254 */ |
| 255 WebInspector.BlockedURLsPane.FilterUI = function() |
| 256 { |
| 257 WebInspector.Object.call(this); |
| 258 this._active = false; |
| 259 this._element = createElement("span"); |
| 260 } |
| 261 |
| 262 WebInspector.BlockedURLsPane.FilterUI.prototype = { |
| 263 /** |
| 264 * @override |
| 265 * @return {boolean} |
| 266 */ |
| 267 isActive: function() |
| 268 { |
| 269 return this._active; |
| 270 }, |
| 271 |
| 272 /** |
| 273 * @param {boolean} active |
| 274 */ |
| 275 setActive: function(active) |
| 276 { |
| 277 this._active = active; |
| 278 }, |
| 279 |
| 280 /** |
| 281 * @override |
| 282 * @return {!Element} |
| 283 */ |
| 284 element: function() |
| 285 { |
| 286 return this._element; |
| 287 }, |
| 288 |
| 289 __proto__: WebInspector.Object.prototype |
| 290 } |
OLD | NEW |