Firefox26からDownload.jsmを使用するようになったので試行錯誤してみた
Private と Public を区別して監視
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Services",
"resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
"resource://gre/modules/Downloads.jsm");
var myDownloads = {
PublicView : null,
PrivateView : null,
addView : function() {
if (this.PublicView) this.PublicView.addView();
else this.PublicView = new DownloadsView(false);
if (this.PrivateView) this.PrivateView.addView();
else this.PrivateView = new DownloadsView(true);
},
removeView : function() {
if (this.PublicView) this.PublicView.removeView();
if (this.PrivateView) this.PrivateView.removeView();
},
}
// DownloadsViewクラス
function DownloadsView(aPrivate) {
this.isPrivate = aPrivate;
this.list = null;
this.summary = null;
this.init();
}
DownloadsView.prototype = {
// View作成で自動的に登録
init : function() {
let type = this.isPrivate ? Downloads.PRIVATE : Downloads.PUBLIC;
Downloads.getList(type).then(aList => {
this.list = aList;
return this.list.addView(this);
}).then(this.log("init list"), Cu.reportError);
Downloads.getSummary(type).then(aSummary => {
this.summary = aSummary;
return this.summary.addView(this);
}).then(this.log("init summary"), Cu.reportError);
},
// Viewの登録と解除
addView : function() {
this.list.addView(this).then(this.log("addView list"), Cu.reportError);
this.summary.addView(this).then(this.log("addView summary"), Cu.reportError);
},
removeView : function() {
this.list.removeView(this).then(this.log("removeView list"), Cu.reportError);
this.summary.removeView(this).then(this.log("removeView summary"), Cu.reportError);
},
// listView用のメソッド
onDownloadAdded : function(aDownload) {
this.logDownload("onDownloadAdded", aDownload);
},
onDownloadChanged : function(aDownload) {
this.logDownload("onDownloadChanged", aDownload);
},
onDownloadRemoved : function(aDownload) {
this.logDownload("onDownloadRemoved", aDownload);
},
// summaryView用メソッド
onSummaryChanged : function() {
this.logSummary();
},
// その他
logDownload : function(str, aDownload) {
str += "\nSource URL : " + aDownload.source.url
+ "\nstopped : " + aDownload.stopped
+ "\nsucceeded : " + aDownload.succeeded
+ "\ncanceled : " + aDownload.canceled
+ "\nhasProgress : " + aDownload.hasProgress;
this.log(str);
},
logSummary : function() {
let str = "onSummaryChanged"
+ "\nStopped :" + this.summary.allHaveStopped
+ "\nTotalBytes :" + this.summary.progressTotalBytes
+ "\nCurrentBytes:" + this.summary.progressCurrentBytes;
this.log(str);
},
log : function(str) {
let self = this.isPrivate ? "PrivateView" : "PublicView";
str = "[" + self + "] " + str;
Services.console.logStringMessage(str);
},
}
myDownloads.addView();
メモ
- DownloadsCommon.jsmはFirefox本体使用に特化されてて汎用性が無いので拡張機能ではほぼ使えないと思われる(内部定義のオブジェクトを分析して複製するのは面倒すぎる)。でもコードの参考にはなる。
最終更新:2013年12月21日 11:05