使用Addon SDK API下载许多文件时,Firefox UI无响应

时间:2014-10-29 11:12:27

标签: javascript firefox firefox-addon firefox-addon-sdk

我有一个很难调试的问题,我需要使用firefox addon sdk API在后台下载很多(~400个)相当小的(~3-4mb)文件。

我尝试使用旧API(nsIWebBrowserPersist)以及新API(Downloads.jsm)(缩短代码):

Task.spawn(function () {                
  for (var i = 0; i < documents.length; i++) {
    var url = ...;
    var file = ...;

    let download = yield Downloads.createDownload({
      source: url,
      target: file,
    });

    yield download.start();
    yield download.finalize();
  }
});

但是UI在一段时间后变得非常无响应,我尝试使用相同的文件并覆盖它,因为我的第一个猜测是Windows文件处理累积的时间,但它没有帮助。它似乎与系统性能无关,也有时在5分钟内在同一台机器上运行失败。

使用firefox sdk api下载大量文件是否存在已知问题,或者我做错了什么?

1 个答案:

答案 0 :(得分:1)

我发现通过使用替代API,下载变得更快,并且ui更具响应性:

function downloadFromUrl(url, file, callback) {
  var channel = chrome.Cc["@mozilla.org/network/io-service;1"]
               .getService(chrome.Ci.nsIIOService)
               .newChannel(url, 0, null);

  var bstream = chrome.Cc["@mozilla.org/binaryinputstream;1"]
                .createInstance(chrome.Ci.nsIBinaryInputStream);
  bstream.setInputStream(channel.open());

  var fos = chrome.Cc["@mozilla.org/network/safe-file-output-stream;1"]
               .createInstance(chrome.Ci.nsIFileOutputStream);
  try {
    fos.init(file, 0x04 | 0x08 | 0x10 | 0x20 | 0x40, 0600, 0); // see:https://developer.mozilla.org/en-US/docs/PR_Open#Parameters

    var length = 0;
    var size = 0;
    while(size = bstream.available()) {
      fos.write(bstream.readBytes(size), size);
      length += size;
      callback(length);
    }
  } finally {
    if (fos instanceof chrome.Ci.nsISafeOutputStream) {
      fos.finish();
    } else {
      fos.close();
    }
  }
}

我知道这是一种原始的api,但它的工作方式比其他选择更好..

编辑:

我改进了上面的功能,但它可能太臃肿了,不管怎么说都是这样:

/**
 * Downloads from a given url to a local file
 * @param url url to download
 * @param file local file
 * @param callback called during the download, signature: callback(currentBytes)
 * @returns downloadResult {contentType, error: false | ExceptionObject}
 */
function downloadFromUrl(url, file, callback) {
  let result = {
    contentType: null,
    error: false
  };

  try {
    let channel = chrome.Cc["@mozilla.org/network/io-service;1"]
                    .getService(chrome.Ci.nsIIOService)
                    .newChannel(url, 0, null);

    let bstream = chrome.Cc["@mozilla.org/binaryinputstream;1"]
                    .createInstance(chrome.Ci.nsIBinaryInputStream);
    bstream.setInputStream(channel.open());

    let fos = chrome.Cc["@mozilla.org/network/safe-file-output-stream;1"]
                .createInstance(chrome.Ci.nsIFileOutputStream);
    try {
      // const values from https://developer.mozilla.org/en-US/docs/PR_Open#Parameters
      const PR_RDWR = 0x04; // Open for reading and writing.
      const PR_CREATE_FILE = 0x08; // If the file does not exist, the file is created. If the file exists, this flag has no effect.
      const PR_APPEND = 0x10; // The file pointer is set to the end of the file prior to each write.
      const PR_TRUNCATE = 0x20; // If the file exists, its length is truncated to 0.
      const PR_SYNC = 0x40; // If set, each write will wait for both the file data and file status to be physically updated.

      fos.init(file, PR_RDWR | PR_CREATE_FILE | PR_APPEND | PR_TRUNCATE | PR_SYNC, 0600, 0);

      let length = 0;
      let size = bstream.available();
      while(size) {
        fos.write(bstream.readBytes(size), size);

        length += size;
        callback(length);

        size = bstream.available();
      }

      fos.flush();

      result.contentType = channel.contentType;
    } finally {
      if (fos instanceof chrome.Ci.nsISafeOutputStream) {
        fos.finish();
      } else {
        fos.close();
      }
    }
  } catch (e) {
    result.error = e;
  }

  return result;
}