Commit b162df7f authored by Dmitry Rozhkov's avatar Dmitry Rozhkov Committed by chriadam

[download] Reimplement download manager compatible with Downloads.jsm. Fixes JB#32870

parent 0c39aa6f
This diff is collapsed.
......@@ -17,11 +17,6 @@ contract @mozilla.org/network/protocol/about;1?what=home {59f3da9a-6c88-11e2-b87
component {72de694e-6c88-11e2-a4ee-6b515bdf0cb7} LoginManagerPrompter.js
contract @mozilla.org/login-manager/prompter;1 {72de694e-6c88-11e2-a4ee-6b515bdf0cb7}
# DownloadManagerUI.js
component {2137921e-910e-11e2-b344-2bda2844afe1} DownloadManagerUI.js
contract @mozilla.org/download-manager-ui;1 {2137921e-910e-11e2-b344-2bda2844afe1}
category app-startup DownloadManagerUI service,@mozilla.org/download-manager-ui;1
# HelperAppDialog.js
component {e9d277a0-268a-4ec2-bb8c-10fdf3e44611} HelperAppDialog.js
contract @mozilla.org/helperapplauncherdialog;1 {e9d277a0-268a-4ec2-bb8c-10fdf3e44611}
......@@ -107,6 +102,11 @@ contract @mozilla.org/network/protocol;1?name=geo {0b452f2d-c6b3-4aee-9a73-dec5a
component {d43dab24-fab1-11e3-b977-3c970e7aaa3d} RtspProtocolHandler.js
contract @mozilla.org/network/protocol;1?name=rtsp {d43dab24-fab1-11e3-b977-3c970e7aaa3d}
# Sidebar.js
# EmbedliteDownloadManager.js
component {71b0a6e8-83ac-4006-af97-d66009db97c8} EmbedliteDownloadManager.js
contract @mozilla.org/embedlite/download-manager;1 {71b0a6e8-83ac-4006-af97-d66009db97c8}
category app-startup EmbedliteDownloadManager service,@mozilla.org/embedlite/download-manager;1
# Sidebar.js
component {1a8f0aa4-2433-441f-adad-ed3ea5be4b39} Sidebar.js
contract @mozilla.org/sidebar;1 {1a8f0aa4-2433-441f-adad-ed3ea5be4b39}
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* 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/. */
"use strict";
////////////////////////////////////////////////////////////////////////////////
//// Globals
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cr = Components.results;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
"resource://gre/modules/Downloads.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Services",
"resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Task",
"resource://gre/modules/Task.jsm");
////////////////////////////////////////////////////////////////////////////////
//// DownloadViewer
let DownloadView = {
// This is a map of download => their properties since the previos change
prevState: {},
counter: 0,
onDownloadAdded: function(download) {
this.counter++;
this.prevState[download] = {
id: this.counter,
download: download,
progress: download.progress,
succeeded: download.succeeded,
error: download.error,
canceled: download.canceled,
stopped: download.stopped
};
Services.obs.notifyObservers(null, "embed:download",
JSON.stringify({
msg: "dl-start",
id: this.counter,
displayName: download.target.path.split('/').slice(-1)[0],
sourceUrl: download.source.url,
targetPath: download.target.path,
mimeType: download.contentType,
size: download.totalBytes
}));
if (download.progress) {
Services.obs.notifyObservers(null, "embed:download",
JSON.stringify({
msg: "dl-progress",
id: this.prevState[download].id,
percent: download.progress
}));
}
if (download.succeeded) {
Services.obs.notifyObservers(null, "embed:download",
JSON.stringify({
msg: "dl-done",
id: this.prevState[download].id,
targetPath: download.target.path
}));
}
if (download.error) {
dump("EmbedliteDownloadManager error: " + download.error.message + "\n");
Services.obs.notifyObservers(null, "embed:download",
JSON.stringify({
msg: "dl-fail",
id: this.prevState[download].id
}));
}
if (download.canceled) {
Services.obs.notifyObservers(null, "embed:download",
JSON.stringify({
msg: "dl-cancel",
id: this.prevState[download].id
}));
}
},
onDownloadChanged: function(download) {
if (this.prevState[download].progress !== download.progress) {
Services.obs.notifyObservers(null, "embed:download",
JSON.stringify({
msg: "dl-progress",
id: this.prevState[download].id,
percent: download.progress
}));
}
this.prevState[download].progress = download.progress;
if (!this.prevState[download].succeeded && download.succeeded) {
Services.obs.notifyObservers(null, "embed:download",
JSON.stringify({
msg: "dl-done",
id: this.prevState[download].id,
targetPath: download.target.path
}));
}
this.prevState[download].succeeded = download.succeeded;
if (!this.prevState[download].error && download.error) {
dump("EmbedliteDownloadManager error: " + download.error.message + "\n");
Services.obs.notifyObservers(null, "embed:download",
JSON.stringify({
msg: "dl-fail",
id: this.prevState[download].id
}));
}
this.prevState[download].error = download.error;
if (!this.prevState[download].canceled && download.canceled) {
Services.obs.notifyObservers(null, "embed:download",
JSON.stringify({
msg: "dl-cancel",
id: this.prevState[download].id
}));
}
this.prevState[download].canceled = download.canceled;
if (this.prevState[download].stopped && !download.stopped) {
Services.obs.notifyObservers(null, "embed:download",
JSON.stringify({
msg: "dl-start",
id: this.prevState[download].id,
displayName: download.target.path.split('/').slice(-1)[0],
sourceUrl: download.source.url,
targetPath: download.target.path,
mimeType: download.contentType,
size: download.totalBytes
}));
}
this.prevState[download].stopped = download.stopped;
},
onDownloadRemoved: function(download) {
delete this.prevState[download];
}
};
////////////////////////////////////////////////////////////////////////////////
//// EmbedliteDownloadManager
function EmbedliteDownloadManager()
{
dump("EmbedliteDownloadManager initialized\n");
}
EmbedliteDownloadManager.prototype = {
classID: Components.ID("{71b0a6e8-83ac-4006-af97-d66009db97c8}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
observe: function(aSubject, aTopic, aData) {
switch (aTopic) {
case "app-startup":
Services.obs.addObserver(this, "profile-after-change", false);
break;
case "profile-after-change":
Services.obs.removeObserver(this, "profile-after-change");
Services.obs.addObserver(this, "embedui:download", false);
Task.spawn(function() {
let list = yield Downloads.getList(Downloads.ALL);
yield list.addView(DownloadView);
}).then(null, Cu.reportError);
break;
case "embedui:download":
var data = JSON.parse(aData);
switch (data.msg) {
case "retryDownload":
for (var key in DownloadView.prevState) {
if (DownloadView.prevState[key].id === data.id) {
DownloadView.prevState[key].download.start();
}
}
break;
case "cancelDownload":
for (var key in DownloadView.prevState) {
if (DownloadView.prevState[key].id === data.id) {
DownloadView.prevState[key].download.cancel();
}
}
break;
case "addDownload":
Task.spawn(function() {
let list = yield Downloads.getList(Downloads.ALL);
let download = yield Downloads.createDownload({
source: data.from,
target: data.to
});
download.start();
list.add(download);
}).then(null, Cu.reportError);
break;
}
break;
}
}
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([EmbedliteDownloadManager]);
......@@ -5,7 +5,6 @@ jscomps_manifest_DATA = \
AlertsService.js \
LoginManagerPrompter.js \
HelperAppDialog.js \
DownloadManagerUI.js \
EmbedPrefService.js \
ContentPermissionPrompt.js \
EmbedLiteGlobalHelper.js \
......@@ -18,6 +17,7 @@ jscomps_manifest_DATA = \
XPIDialogService.js \
PromptService.js \
PrivateDataManager.js \
EmbedliteDownloadManager.js \
TelProtocolHandler.js \
SmsProtocolHandler.js \
MailtoProtocolHandler.js \
......
......@@ -29,7 +29,6 @@ jscomps/AboutRedirector.js
jscomps/AlertsService.js
jscomps/LoginManagerPrompter.js
jscomps/HelperAppDialog.js
jscomps/DownloadManagerUI.js
jscomps/EmbedPrefService.js
jscomps/ContentPermissionPrompt.js
jscomps/EmbedLiteGlobalHelper.js
......@@ -43,6 +42,7 @@ jscomps/XPIDialogService.js
jscomps/EmbedLiteWebAppInstall.js
jscomps/PromptService.js
jscomps/PrivateDataManager.js
jscomps/EmbedliteDownloadManager.js
jscomps/TelProtocolHandler.js
jscomps/SmsProtocolHandler.js
jscomps/MailtoProtocolHandler.js
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment