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

Side by Side Diff: chrome/content/ui/sendReport.js

Issue 29333297: Issue 3375 - Issue reporter: move screenshot creation into the content process (Closed)
Patch Set: Made sure screenshotDataCollector is always initialized Created Jan. 7, 2016, 6:57 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/child/dataCollector.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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 } 314 }
315 } 315 }
316 callback(); 316 callback();
317 } 317 }
318 }; 318 };
319 319
320 var screenshotDataSource = 320 var screenshotDataSource =
321 { 321 {
322 imageOffset: 10, 322 imageOffset: 10,
323 323
324 // Fields used for color reduction
325 _mapping: [0x00, 0x55, 0xAA, 0xFF],
326 _i: null,
327 _max: null,
328 _pixelData: null,
329 _callback: null,
330
331 // Fields used for user interaction 324 // Fields used for user interaction
332 _enabled: true, 325 _enabled: true,
333 _canvas: null, 326 _canvas: null,
334 _context: null, 327 _context: null,
335 _selectionType: "mark", 328 _selectionType: "mark",
336 _currentData: null, 329 _currentData: null,
337 _undoQueue: [], 330 _undoQueue: [],
338 331
339 collectData: function(wnd, windowURI, callback) 332 collectData: function(wnd, windowURI, callback)
340 { 333 {
341 this._callback = callback; 334 let outerWindowID = wnd.QueryInterface(Ci.nsIInterfaceRequestor)
342 this._canvas = E("screenshotCanvas"); 335 .getInterface(Ci.nsIDOMWindowUtils)
343 this._canvas.width = this._canvas.offsetWidth; 336 .outerWindowID;
337 let dataCollector = require("dataCollector");
338 let canvas = E("screenshotCanvas");
339 let screenshotWidth = canvas.offsetWidth - this.imageOffset * 2;
344 340
345 // Do not resize canvas any more (no idea why Gecko requires both to be set) 341 // Do not resize canvas any more (no idea why Gecko requires both to be set)
346 this._canvas.parentNode.style.MozBoxAlign = "center"; 342 canvas.parentNode.style.MozBoxAlign = "center";
347 this._canvas.parentNode.align = "center"; 343 canvas.parentNode.align = "center";
348
349 this._context = this._canvas.getContext("2d");
350 let wndWidth = wnd.document.documentElement.scrollWidth;
351 let wndHeight = wnd.document.documentElement.scrollHeight;
352
353 // Copy scaled screenshot of the webpage. We scale the webpage by width
354 // but leave 10px on each side for easier selecting.
355
356 // Gecko doesn't like sizes more than 64k, restrict to 30k to be on the safe side.
357 // Also, make sure height is at most five times the width to keep image size down.
358 let copyWidth = Math.min(wndWidth, 30000);
359 let copyHeight = Math.min(wndHeight, 30000, copyWidth * 5);
360 let copyX = Math.max(Math.min(wnd.scrollX - copyWidth / 2, wndWidth - copyWi dth), 0);
361 let copyY = Math.max(Math.min(wnd.scrollY - copyHeight / 2, wndHeight - copy Height), 0);
362
363 let scalingFactor = (this._canvas.width - this.imageOffset * 2) / copyWidth;
364 this._canvas.height = copyHeight * scalingFactor + this.imageOffset * 2;
365
366 this._context.save();
367 this._context.translate(this.imageOffset, this.imageOffset);
368 this._context.scale(scalingFactor, scalingFactor);
369 try
370 {
371 this._context.drawWindow(wnd, copyX, copyY, copyWidth, copyHeight, "rgb(25 5,255,255)");
372 }
373 catch (e)
374 {
375 Cu.reportError(e);
376 }
377 this._context.restore();
378 344
379 // Init canvas settings 345 // Init canvas settings
380 this._context.fillStyle = "rgb(0, 0, 0)"; 346 let context = canvas.getContext("2d");
381 this._context.strokeStyle = "rgba(255, 0, 0, 0.7)"; 347 context.fillStyle = "rgb(0, 0, 0)";
382 this._context.lineWidth = 3; 348 context.strokeStyle = "rgba(255, 0, 0, 0.7)";
383 this._context.lineJoin = "round"; 349 context.lineWidth = 3;
350 context.lineJoin = "round";
384 351
385 // Reduce colors asynchronously 352 this._canvas = canvas;
386 this._pixelData = this._context.getImageData(this.imageOffset, this.imageOff set, 353 this._context = context;
387 this._canvas.width - this.imageOffset * 2,
388 this._canvas.height - this.imageOffset * 2 );
389 this._max = this._pixelData.width * this._pixelData.height * 4;
390 this._i = 0;
391 Utils.runAsync(this.run.bind(this));
392 },
393 354
394 run: function() 355 dataCollector.collectData(outerWindowID, screenshotWidth, data => {
395 { 356 if (data && data.screenshot)
396 // Process only 5000 bytes at a time to prevent browser hangs 357 {
397 let endIndex = Math.min(this._i + 5000, this._max); 358 let image = new Image();
398 let i = this._i; 359 image.src = data.screenshot;
399 for (; i < endIndex; i++) 360 image.addEventListener("load", () => {
400 this._pixelData.data[i] = this._mapping[this._pixelData.data[i] >> 6]; 361 canvas.width = image.width + this.imageOffset * 2;
401 362 canvas.height = image.height + this.imageOffset * 2;
402 if (i >= this._max) 363 context.drawImage(image, this.imageOffset, this.imageOffset);
403 { 364 });
404 // Save data back and we are done 365 }
405 this._context.putImageData(this._pixelData, this.imageOffset, this.imageOf fset); 366 callback();
406 this._callback(); 367 });
407 }
408 else
409 {
410 this._i = i;
411 Utils.runAsync(this.run.bind(this));
412 }
413 }, 368 },
414 369
415 get enabled() this._enabled, 370 get enabled() this._enabled,
416 set enabled(enabled) 371 set enabled(enabled)
417 { 372 {
418 if (this._enabled == enabled) 373 if (this._enabled == enabled)
419 return; 374 return;
420 375
421 this._enabled = enabled; 376 this._enabled = enabled;
422 this._canvas.style.opacity = this._enabled ? "" : "0.3" 377 this._canvas.style.opacity = this._enabled ? "" : "0.3"
(...skipping 1167 matching lines...) Expand 10 before | Expand all | Expand 10 after
1590 1545
1591 function censorURL(url) 1546 function censorURL(url)
1592 { 1547 {
1593 return url.replace(/([?;&\/#][^?;&\/#]+?=)[^?;&\/#]+/g, "$1*"); 1548 return url.replace(/([?;&\/#][^?;&\/#]+?=)[^?;&\/#]+/g, "$1*");
1594 } 1549 }
1595 1550
1596 function encodeHTML(str) 1551 function encodeHTML(str)
1597 { 1552 {
1598 return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;"). replace(/"/g, "&quot;"); 1553 return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;"). replace(/"/g, "&quot;");
1599 } 1554 }
OLDNEW
« no previous file with comments | « no previous file | lib/child/dataCollector.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld