Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/downloader.js

Issue 29336735: Issue 394 - hit statistics tool data collection (core) (Closed)
Patch Set: Use Downloader to send the data to server Created April 6, 2016, 2:55 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | lib/filterListener.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 */ 108 */
109 onDownloadSuccess: null, 109 onDownloadSuccess: null,
110 110
111 /** 111 /**
112 * Callback to be triggered whenever a download fails. 112 * Callback to be triggered whenever a download fails.
113 * @type Function 113 * @type Function
114 */ 114 */
115 onDownloadError: null, 115 onDownloadError: null,
116 116
117 /** 117 /**
118 * Callback to be triggered for POST request data generation.
119 * @type Function
120 */
121 generateRequestData: null,
122
123 /**
124 * List of valid responses.
125 * @type Array
126 */
127 validResponses: [200],
128
129 /**
118 * Checks whether anything needs downloading. 130 * Checks whether anything needs downloading.
119 */ 131 */
120 _doCheck: function() 132 _doCheck: function()
121 { 133 {
122 let now = Date.now(); 134 let now = Date.now();
123 for (let downloadable of this.dataSource()) 135 for (let downloadable of this.dataSource())
124 { 136 {
125 if (downloadable.lastCheck && now - downloadable.lastCheck > this.maxAbsen ceInterval) 137 if (downloadable.lastCheck && now - downloadable.lastCheck > this.maxAbsen ceInterval)
126 { 138 {
127 // No checks for a long time interval - user must have been offline, e.g . 139 // No checks for a long time interval - user must have been offline, e.g .
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 } 255 }
244 256
245 this.onDownloadError(downloadable, downloadUrl, error, channelStatus, re sponseStatus, redirectCallback); 257 this.onDownloadError(downloadable, downloadUrl, error, channelStatus, re sponseStatus, redirectCallback);
246 } 258 }
247 }.bind(this); 259 }.bind(this);
248 260
249 try 261 try
250 { 262 {
251 request = new XMLHttpRequest(); 263 request = new XMLHttpRequest();
252 request.mozBackgroundRequest = true; 264 request.mozBackgroundRequest = true;
253 request.open("GET", downloadUrl); 265 request.open(this.generateRequestData ? "POST" : "GET", downloadUrl);
254 } 266 }
255 catch (e) 267 catch (e)
256 { 268 {
257 errorCallback("synchronize_invalid_url"); 269 errorCallback("synchronize_invalid_url");
258 return; 270 return;
259 } 271 }
260 272
261 try { 273 try {
262 request.overrideMimeType("text/plain"); 274 request.overrideMimeType("text/plain");
263 request.channel.loadFlags = request.channel.loadFlags | 275 request.channel.loadFlags = request.channel.loadFlags |
(...skipping 19 matching lines...) Expand all
283 }.bind(this), false); 295 }.bind(this), false);
284 296
285 request.addEventListener("load", function(event) 297 request.addEventListener("load", function(event)
286 { 298 {
287 if (onShutdown.done) 299 if (onShutdown.done)
288 return; 300 return;
289 301
290 delete this._downloading[downloadable.url]; 302 delete this._downloading[downloadable.url];
291 303
292 // Status will be 0 for non-HTTP requests 304 // Status will be 0 for non-HTTP requests
293 if (request.status && request.status != 200) 305 if (request.status && this.validResponses.indexOf(request.status) == -1)
294 { 306 {
295 errorCallback("synchronize_connection_error"); 307 errorCallback("synchronize_connection_error");
296 return; 308 return;
297 } 309 }
298 310
299 downloadable.downloadCount++; 311 downloadable.downloadCount++;
300 312
301 this.onDownloadSuccess(downloadable, request.responseText, errorCallback, function redirectCallback(url) 313 this.onDownloadSuccess(downloadable, request.responseText, errorCallback, function redirectCallback(url)
302 { 314 {
303 if (redirects >= this.maxRedirects) 315 if (redirects >= this.maxRedirects)
304 errorCallback("synchronize_connection_error"); 316 errorCallback("synchronize_connection_error");
305 else 317 else
306 { 318 {
307 downloadable.redirectURL = url; 319 downloadable.redirectURL = url;
308 this._download(downloadable, redirects + 1); 320 this._download(downloadable, redirects + 1);
309 } 321 }
310 }.bind(this)); 322 }.bind(this));
311 }.bind(this), false); 323 }.bind(this), false);
312 324
313 request.send(null); 325 if (this.generateRequestData)
326 {
327 Promise.resolve(this.generateRequestData(downloadable))
328 .then(data => {
329 request.setRequestHeader("Content-Type", "application/json");
330 request.send(JSON.stringify(data));
331 });
332 }
333 else
334 request.send(null);
314 335
315 this._downloading[downloadable.url] = true; 336 this._downloading[downloadable.url] = true;
316 if (this.onDownloadStarted) 337 if (this.onDownloadStarted)
317 this.onDownloadStarted(downloadable); 338 this.onDownloadStarted(downloadable);
318 }, 339 },
319 340
320 /** 341 /**
321 * Produces a soft and a hard expiration interval for a given supplied 342 * Produces a soft and a hard expiration interval for a given supplied
322 * expiration interval. 343 * expiration interval.
323 * @return {Array} soft and hard expiration interval 344 * @return {Array} soft and hard expiration interval
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 * @type Integer 406 * @type Integer
386 */ 407 */
387 hardExpiration: 0, 408 hardExpiration: 0,
388 409
389 /** 410 /**
390 * Number indicating how often the object was downloaded. 411 * Number indicating how often the object was downloaded.
391 * @type Integer 412 * @type Integer
392 */ 413 */
393 downloadCount: 0, 414 downloadCount: 0,
394 }; 415 };
OLDNEW
« no previous file with comments | « no previous file | lib/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld