/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Based on:
// http://hg.mozilla.org/qa/mozmill-tests/file/905bc0ba7a1e/firefox/lib/screenshot.js

Cu.import("resource://gre/modules/Services.jsm");

/**
 * This function creates a screenshot of the window provided in the given
 * controller and highlights elements from the coordinates provided in the
 * given boxes-array.
 *
 * @param {array of array of int} boxes
 * @param {MozmillController} controller
 */
function create(controller, boxes) {
  var doc = controller.window.document;
  var maxWidth = doc.documentElement.boxObject.width;
  var maxHeight = doc.documentElement.boxObject.height;
  var rect = [];
  for (var i = 0, j = boxes.length; i < j; ++i) {
    rect = boxes[i];
    if (rect[0] + rect[2] > maxWidth) maxWidth = rect[0] + rect[2];
    if (rect[1] + rect[3] > maxHeight) maxHeight = rect[1] + rect[3];
  }
  var canvas = doc.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
  var width = doc.documentElement.boxObject.width;
  var height = doc.documentElement.boxObject.height;
  canvas.width = maxWidth;
  canvas.height = maxHeight;
  var ctx = canvas.getContext("2d");
  ctx.clearRect(0,0, canvas.width, canvas.height);
  ctx.save();
  ctx.drawWindow(controller.window, 0, 0, width, height, "rgb(0,0,0)");
  ctx.restore();
  ctx.save();
  ctx.fillStyle = "rgba(255,0,0,0.4)";
  for (var i = 0, j = boxes.length; i < j; ++i) {
    rect = boxes[i];
    ctx.fillRect(rect[0], rect[1], rect[2], rect[3]);
  }
  ctx.restore();

  _saveCanvas(canvas);
}

/**
 * Saves a given Canvas object to a file.
 * The path to save the file under should be given in the
 * MOZMILL_SCREENSHOTS environment variable. If not, it will be saved
 * in the temporary folder of the system.
 *
 * @param {canvas} canvas
 */
function _saveCanvas(canvas) {
  var env = Cc["@mozilla.org/process/environment;1"]
                .getService(Ci.nsIEnvironment);
  var file = null;
  if (env.exists("MOZMILL_SCREENSHOTS")) {
    file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
    file.initWithPath(env.get("MOZMILL_SCREENSHOTS"));
  }
  else {
    file = Services.dirsvc.get("TmpD", Ci.nsIFile);
  }

  file.append("screenshot.png");

  // if a file already exists, don't overwrite it and create a new name
  file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("0666", 8));

  // create a data url from the canvas and then create URIs of the source
  // and targets
  var source = Services.io.newURI(canvas.toDataURL("image/png", ""), "UTF8", null);
  var target = Services.io.newFileURI(file)

  // prepare to save the canvas data
  var wbPersist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].
                  createInstance(Ci.nsIWebBrowserPersist);

  wbPersist.persistFlags = Ci.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
  wbPersist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;

  // save the canvas data to the file
  wbPersist.saveURI(source, null, null, null, null, file, null);
}

// Export of functions
exports.create = create;

@@ -299,16 +299,67 @@
 hot.js%0A%0A
+Cu.import(%22resource://gre/modules/Downloads.jsm%22);%0A
 Cu.impor
@@ -2564,686 +2564,378 @@
 %0A%0A  
-// create a data url from the canvas and then create URIs of the source%0A  // and targets%0A  var source = Services.io.newURI(canvas.toDataURL(%22image/png%22, %22%22), %22UTF8%22, null);%0A  var target = Services.io.newFileURI(file)%0A%0A  // prepare to save the canvas data%0A  var wbPersist = Cc%5B%22@mozilla.org/embedding/browser/nsWebBrowserPersist;1%22%5D.%0A                  createInstance(Ci.nsIWebBrowserPersist);%0A%0A  wbPersist.persistFlags = Ci.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;%0A  wbPersist.persistFlags %7C= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;%0A%0A  // save the canvas data to the file%0A  wbPersist.saveURI(source, null, null, null, null, file, null
+var taskCompleted = false;%0A  Downloads.fetch(canvas.toDataURL(%22image/png%22, %22%22), file).then(() =%3E %7B%0A    taskCompleted = true;%0A  %7D, (aReason) =%3E %7B%0A    taskCompleted = true;%0A    expect.fail(%22Failed to save the screenshot %22 + file + %22, with: %22 + aReason);%0A  %7D);%0A%0A  expect.waitFor(() =%3E taskCompleted,%0A                 %22Saving the screenshot to the disk action has finished.%22
 );%0A%7D

Replace deprecated nsIWebBrowserPersist.saveURI