1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
/* 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";
this.EXPORTED_SYMBOLS = [
"DownloadImport",
];
// 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, "OS",
"resource://gre/modules/osfile.jsm")
XPCOMUtils.defineLazyModuleGetter(this, "Task",
"resource://gre/modules/Task.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Sqlite",
"resource://gre/modules/Sqlite.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
"resource://gre/modules/NetUtil.jsm");
/**
* These values come from the previous interface
* nsIDownloadManager, which has now been deprecated.
* These are the only types of download states that
* we will import.
*/
const DOWNLOAD_NOTSTARTED = -1;
const DOWNLOAD_DOWNLOADING = 0;
const DOWNLOAD_PAUSED = 4;
const DOWNLOAD_QUEUED = 5;
// DownloadImport
/**
* Provides an object that has a method to import downloads
* from the previous SQLite storage format.
*
* @param aList A DownloadList where each successfully
* imported download will be added.
* @param aPath The path to the database file.
*/
this.DownloadImport = function (aList, aPath)
{
this.list = aList;
this.path = aPath;
}
this.DownloadImport.prototype = {
/**
* Imports unfinished downloads from the previous SQLite storage
* format (supporting schemas 7 and up), to the new Download object
* format. Each imported download will be added to the DownloadList
*
* @return {Promise}
* @resolves When the operation has completed (i.e., every download
* from the previous database has been read and added to
* the DownloadList)
*/
import: function () {
return Task.spawn(function* task_DI_import() {
let connection = yield Sqlite.openConnection({ path: this.path });
try {
let schemaVersion = yield connection.getSchemaVersion();
// We don't support schemas older than version 7 (from 2007)
// - Version 7 added the columns mimeType, preferredApplication
// and preferredAction in 2007
// - Version 8 added the column autoResume in 2007
// (if we encounter version 7 we will treat autoResume = false)
// - Version 9 is the last known version, which added a unique
// GUID text column that is not used here
if (schemaVersion < 7) {
throw new Error("Unable to import in-progress downloads because "
+ "the existing profile is too old.");
}
let rows = yield connection.execute("SELECT * FROM moz_downloads");
for (let row of rows) {
try {
// Get the DB row data
let source = row.getResultByName("source");
let target = row.getResultByName("target");
let tempPath = row.getResultByName("tempPath");
let startTime = row.getResultByName("startTime");
let state = row.getResultByName("state");
let referrer = row.getResultByName("referrer");
let maxBytes = row.getResultByName("maxBytes");
let mimeType = row.getResultByName("mimeType");
let preferredApplication = row.getResultByName("preferredApplication");
let preferredAction = row.getResultByName("preferredAction");
let entityID = row.getResultByName("entityID");
let autoResume = false;
try {
autoResume = (row.getResultByName("autoResume") == 1);
} catch (ex) {
// autoResume wasn't present in schema version 7
}
if (!source) {
throw new Error("Attempted to import a row with an empty " +
"source column.");
}
let resumeDownload = false;
switch (state) {
case DOWNLOAD_NOTSTARTED:
case DOWNLOAD_QUEUED:
case DOWNLOAD_DOWNLOADING:
resumeDownload = true;
break;
case DOWNLOAD_PAUSED:
resumeDownload = autoResume;
break;
default:
// We won't import downloads in other states
continue;
}
// Transform the data
let targetPath = NetUtil.newURI(target)
.QueryInterface(Ci.nsIFileURL).file.path;
let launchWhenSucceeded = (preferredAction != Ci.nsIMIMEInfo.saveToDisk);
let downloadOptions = {
source: {
url: source,
referrer: referrer
},
target: {
path: targetPath,
partFilePath: tempPath,
},
saver: {
type: "copy",
entityID: entityID
},
startTime: new Date(startTime / 1000),
totalBytes: maxBytes,
hasPartialData: !!tempPath,
tryToKeepPartialData: true,
launchWhenSucceeded: launchWhenSucceeded,
contentType: mimeType,
launcherPath: preferredApplication
};
// Paused downloads that should not be auto-resumed are considered
// in a "canceled" state.
if (!resumeDownload) {
downloadOptions.canceled = true;
}
let download = yield Downloads.createDownload(downloadOptions);
yield this.list.add(download);
if (resumeDownload) {
download.start().catch(() => {});
} else {
yield download.refresh();
}
} catch (ex) {
Cu.reportError("Error importing download: " + ex);
}
}
} catch (ex) {
Cu.reportError(ex);
} finally {
yield connection.close();
}
}.bind(this));
}
}
|