Skip to content

Instantly share code, notes, and snippets.

@azu

azu/io.js

Last active Dec 19, 2015
Embed
What would you like to do?
ios.js for Addon SDK original https://github.com/choobin/cmis
/*
Copyright 2012 Christopher Hoobin. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
THIS SOFTWARE IS PROVIDED BY CHRISTOPHER HOOBIN ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CHRISTOPHER HOOBIN OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation
are those of the authors and should not be interpreted as representing
official policies, either expressed or implied, of Christopher Hoobin.
*/
var { Ci,Cu,Cc } = require('chrome');
var data = require("sdk/self").data;
const { defer } = require('sdk/core/promise');
Cu.import("resource://gre/modules/Services.jsm");
/**
* ``saveURL`` Util
* @param {Window} window
* @param {nsIURL} sourceURI
* @param {nsIURL} fileURI
* @param {string} filename
* @return {promise} promise
*/
var saveFile = function (window, sourceURI, fileURI, filename){
var deferred = defer();
let privacy_context = window
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation);
let is_private = privacy_context.usePrivateBrowsing;
// https://developer.mozilla.org/en/nsIWebBrowserPersist
let persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
.createInstance(Ci.nsIWebBrowserPersist);
const nsIWebBrowserPersist = Ci.nsIWebBrowserPersist;
persist.persistFlags =
nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
nsIWebBrowserPersist.PERSIST_FLAGS_FROM_CACHE |
nsIWebBrowserPersist.PERSIST_FLAGS_CLEANUP_ON_FAILURE;
const nsIDownloadManager = Ci.nsIDownloadManager;
// https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIDownloadManager
let manager = Cc["@mozilla.org/download-manager;1"]
.getService(nsIDownloadManager);
manager.addListener({
/**
* ステータスが変わる度に呼ばれる
* @param aState
* @param {nsIDownload} aDownload
*/
onDownloadStateChange: function (aState, aDownload){
const DOWNLOAD_NOTSTARTED = Ci.nsIDownloadManager.DOWNLOAD_NOTSTARTED;
const DOWNLOAD_DOWNLOADING = Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING;
const DOWNLOAD_FINISHED = Ci.nsIDownloadManager.DOWNLOAD_FINISHED;
const DOWNLOAD_FAILED = Ci.nsIDownloadManager.DOWNLOAD_FAILED;
const DOWNLOAD_CANCELED = Ci.nsIDownloadManager.DOWNLOAD_CANCELED;
const DOWNLOAD_PAUSED = Ci.nsIDownloadManager.DOWNLOAD_PAUSED;
const DOWNLOAD_QUEUED = Ci.nsIDownloadManager.DOWNLOAD_QUEUED;
const DOWNLOAD_BLOCKED = Ci.nsIDownloadManager.DOWNLOAD_BLOCKED;
const DOWNLOAD_SCANNING = Ci.nsIDownloadManager.DOWNLOAD_SCANNING;
var state = aDownload.state;
if (state == DOWNLOAD_FAILED) {
deferred.reject(aDownload)
}
if (state == DOWNLOAD_FINISHED) {
// サイズが1MB以下なら失敗とみなす
var limitSize = 1000 * 1000;// 1000 * 1000Byte
if (aDownload.size > limitSize) {
deferred.resolve(aDownload);
return;
}
deferred.reject(aDownload);
}
}
});
let listener = manager.addDownload(
nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD,
sourceURI,
fileURI,
filename,
null, // mime info
null, // start time
null, // tmp file
persist,
is_private);
persist.progressListener = listener;
persist.saveURI(sourceURI, null, null, null, null, listener.targetFile, privacy_context);
// Notification
var notifications = require("sdk/notifications");
notifications.notify({
title: "Download Download",
text: "Start " + filename,
iconURL: data.url("icon.ico")
});
return deferred.promise;
};
exports.saveFile = saveFile;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment