toolkit/components/jsdownloads/src/DownloadStore.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /**
michael@0 8 * Handles serialization of Download objects and persistence into a file, so
michael@0 9 * that the state of downloads can be restored across sessions.
michael@0 10 *
michael@0 11 * The file is stored in JSON format, without indentation. With indentation
michael@0 12 * applied, the file would look like this:
michael@0 13 *
michael@0 14 * {
michael@0 15 * "list": [
michael@0 16 * {
michael@0 17 * "source": "http://www.example.com/download.txt",
michael@0 18 * "target": "/home/user/Downloads/download.txt"
michael@0 19 * },
michael@0 20 * {
michael@0 21 * "source": {
michael@0 22 * "url": "http://www.example.com/download.txt",
michael@0 23 * "referrer": "http://www.example.com/referrer.html"
michael@0 24 * },
michael@0 25 * "target": "/home/user/Downloads/download-2.txt"
michael@0 26 * }
michael@0 27 * ]
michael@0 28 * }
michael@0 29 */
michael@0 30
michael@0 31 "use strict";
michael@0 32
michael@0 33 this.EXPORTED_SYMBOLS = [
michael@0 34 "DownloadStore",
michael@0 35 ];
michael@0 36
michael@0 37 ////////////////////////////////////////////////////////////////////////////////
michael@0 38 //// Globals
michael@0 39
michael@0 40 const Cc = Components.classes;
michael@0 41 const Ci = Components.interfaces;
michael@0 42 const Cu = Components.utils;
michael@0 43 const Cr = Components.results;
michael@0 44
michael@0 45 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 46
michael@0 47 XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
michael@0 48 "resource://gre/modules/Downloads.jsm");
michael@0 49 XPCOMUtils.defineLazyModuleGetter(this, "OS",
michael@0 50 "resource://gre/modules/osfile.jsm")
michael@0 51 XPCOMUtils.defineLazyModuleGetter(this, "Task",
michael@0 52 "resource://gre/modules/Task.jsm");
michael@0 53
michael@0 54 XPCOMUtils.defineLazyGetter(this, "gTextDecoder", function () {
michael@0 55 return new TextDecoder();
michael@0 56 });
michael@0 57
michael@0 58 XPCOMUtils.defineLazyGetter(this, "gTextEncoder", function () {
michael@0 59 return new TextEncoder();
michael@0 60 });
michael@0 61
michael@0 62 ////////////////////////////////////////////////////////////////////////////////
michael@0 63 //// DownloadStore
michael@0 64
michael@0 65 /**
michael@0 66 * Handles serialization of Download objects and persistence into a file, so
michael@0 67 * that the state of downloads can be restored across sessions.
michael@0 68 *
michael@0 69 * @param aList
michael@0 70 * DownloadList object to be populated or serialized.
michael@0 71 * @param aPath
michael@0 72 * String containing the file path where data should be saved.
michael@0 73 */
michael@0 74 this.DownloadStore = function (aList, aPath)
michael@0 75 {
michael@0 76 this.list = aList;
michael@0 77 this.path = aPath;
michael@0 78 }
michael@0 79
michael@0 80 this.DownloadStore.prototype = {
michael@0 81 /**
michael@0 82 * DownloadList object to be populated or serialized.
michael@0 83 */
michael@0 84 list: null,
michael@0 85
michael@0 86 /**
michael@0 87 * String containing the file path where data should be saved.
michael@0 88 */
michael@0 89 path: "",
michael@0 90
michael@0 91 /**
michael@0 92 * This function is called with a Download object as its first argument, and
michael@0 93 * should return true if the item should be saved.
michael@0 94 */
michael@0 95 onsaveitem: () => true,
michael@0 96
michael@0 97 /**
michael@0 98 * Loads persistent downloads from the file to the list.
michael@0 99 *
michael@0 100 * @return {Promise}
michael@0 101 * @resolves When the operation finished successfully.
michael@0 102 * @rejects JavaScript exception.
michael@0 103 */
michael@0 104 load: function DS_load()
michael@0 105 {
michael@0 106 return Task.spawn(function task_DS_load() {
michael@0 107 let bytes;
michael@0 108 try {
michael@0 109 bytes = yield OS.File.read(this.path);
michael@0 110 } catch (ex if ex instanceof OS.File.Error && ex.becauseNoSuchFile) {
michael@0 111 // If the file does not exist, there are no downloads to load.
michael@0 112 return;
michael@0 113 }
michael@0 114
michael@0 115 let storeData = JSON.parse(gTextDecoder.decode(bytes));
michael@0 116
michael@0 117 // Create live downloads based on the static snapshot.
michael@0 118 for (let downloadData of storeData.list) {
michael@0 119 try {
michael@0 120 let download = yield Downloads.createDownload(downloadData);
michael@0 121 try {
michael@0 122 if (!download.succeeded && !download.canceled && !download.error) {
michael@0 123 // Try to restart the download if it was in progress during the
michael@0 124 // previous session.
michael@0 125 download.start();
michael@0 126 } else {
michael@0 127 // If the download was not in progress, try to update the current
michael@0 128 // progress from disk. This is relevant in case we retained
michael@0 129 // partially downloaded data.
michael@0 130 yield download.refresh();
michael@0 131 }
michael@0 132 } finally {
michael@0 133 // Add the download to the list if we succeeded in creating it,
michael@0 134 // after we have updated its initial state.
michael@0 135 yield this.list.add(download);
michael@0 136 }
michael@0 137 } catch (ex) {
michael@0 138 // If an item is unrecognized, don't prevent others from being loaded.
michael@0 139 Cu.reportError(ex);
michael@0 140 }
michael@0 141 }
michael@0 142 }.bind(this));
michael@0 143 },
michael@0 144
michael@0 145 /**
michael@0 146 * Saves persistent downloads from the list to the file.
michael@0 147 *
michael@0 148 * If an error occurs, the previous file is not deleted.
michael@0 149 *
michael@0 150 * @return {Promise}
michael@0 151 * @resolves When the operation finished successfully.
michael@0 152 * @rejects JavaScript exception.
michael@0 153 */
michael@0 154 save: function DS_save()
michael@0 155 {
michael@0 156 return Task.spawn(function task_DS_save() {
michael@0 157 let downloads = yield this.list.getAll();
michael@0 158
michael@0 159 // Take a static snapshot of the current state of all the downloads.
michael@0 160 let storeData = { list: [] };
michael@0 161 let atLeastOneDownload = false;
michael@0 162 for (let download of downloads) {
michael@0 163 try {
michael@0 164 if (!this.onsaveitem(download)) {
michael@0 165 continue;
michael@0 166 }
michael@0 167 storeData.list.push(download.toSerializable());
michael@0 168 atLeastOneDownload = true;
michael@0 169 } catch (ex) {
michael@0 170 // If an item cannot be converted to a serializable form, don't
michael@0 171 // prevent others from being saved.
michael@0 172 Cu.reportError(ex);
michael@0 173 }
michael@0 174 }
michael@0 175
michael@0 176 if (atLeastOneDownload) {
michael@0 177 // Create or overwrite the file if there are downloads to save.
michael@0 178 let bytes = gTextEncoder.encode(JSON.stringify(storeData));
michael@0 179 yield OS.File.writeAtomic(this.path, bytes,
michael@0 180 { tmpPath: this.path + ".tmp" });
michael@0 181 } else {
michael@0 182 // Remove the file if there are no downloads to save at all.
michael@0 183 try {
michael@0 184 yield OS.File.remove(this.path);
michael@0 185 } catch (ex if ex instanceof OS.File.Error &&
michael@0 186 (ex.becauseNoSuchFile || ex.becauseAccessDenied)) {
michael@0 187 // On Windows, we may get an access denied error instead of a no such
michael@0 188 // file error if the file existed before, and was recently deleted.
michael@0 189 }
michael@0 190 }
michael@0 191 }.bind(this));
michael@0 192 },
michael@0 193 };

mercurial