toolkit/components/thumbnails/PageThumbsWorker.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /**
michael@0 6 * A worker dedicated for the I/O component of PageThumbs storage.
michael@0 7 *
michael@0 8 * Do not rely on the API of this worker. In a future version, it might be
michael@0 9 * fully replaced by a OS.File global I/O worker.
michael@0 10 */
michael@0 11
michael@0 12 "use strict";
michael@0 13
michael@0 14 importScripts("resource://gre/modules/osfile.jsm");
michael@0 15
michael@0 16 let File = OS.File;
michael@0 17 let Type = OS.Shared.Type;
michael@0 18
michael@0 19 /**
michael@0 20 * Communications with the controller.
michael@0 21 *
michael@0 22 * Accepts messages:
michael@0 23 * {fun:function_name, args:array_of_arguments_or_null}
michael@0 24 *
michael@0 25 * Sends messages:
michael@0 26 * {ok: result} / {fail: serialized_form_of_OS.File.Error}
michael@0 27 */
michael@0 28 self.onmessage = function onmessage(msg) {
michael@0 29 let data = msg.data;
michael@0 30 let id = data.id;
michael@0 31 let result;
michael@0 32 if (!(data.fun in Agent)) {
michael@0 33 throw new Error("Cannot find method " + data.fun);
michael@0 34 }
michael@0 35 try {
michael@0 36 result = Agent[data.fun].apply(Agent, data.args);
michael@0 37 } catch (ex if ex instanceof StopIteration) {
michael@0 38 // StopIteration cannot be serialized automatically
michael@0 39 self.postMessage({StopIteration: true, id: id});
michael@0 40 return;
michael@0 41 } catch (ex if ex instanceof OS.File.Error) {
michael@0 42 // Instances of OS.File.Error know how to serialize themselves
michael@0 43 // (deserialization ensures that we end up with OS-specific
michael@0 44 // instances of |OS.File.Error|)
michael@0 45 self.postMessage({fail: OS.File.Error.toMsg(ex), id:id});
michael@0 46 return;
michael@0 47 }
michael@0 48 // Other exceptions do not, and should be propagated through DOM's
michael@0 49 // built-in mechanism for uncaught errors, although this mechanism
michael@0 50 // may lose interesting information.
michael@0 51 self.postMessage({ok: result, id:id});
michael@0 52 };
michael@0 53
michael@0 54
michael@0 55 let Agent = {
michael@0 56 // Checks if the specified file exists and has an age less than as
michael@0 57 // specifed (in seconds).
michael@0 58 isFileRecent: function Agent_isFileRecent(path, maxAge) {
michael@0 59 try {
michael@0 60 let stat = OS.File.stat(path);
michael@0 61 let maxDate = new Date();
michael@0 62 maxDate.setSeconds(maxDate.getSeconds() - maxAge);
michael@0 63 return stat.lastModificationDate > maxDate;
michael@0 64 } catch (ex if ex instanceof OS.File.Error) {
michael@0 65 // file doesn't exist (or can't be stat'd) - must be stale.
michael@0 66 return false;
michael@0 67 }
michael@0 68 },
michael@0 69
michael@0 70 remove: function Agent_removeFile(path) {
michael@0 71 try {
michael@0 72 OS.File.remove(path);
michael@0 73 return true;
michael@0 74 } catch (e) {
michael@0 75 return false;
michael@0 76 }
michael@0 77 },
michael@0 78
michael@0 79 expireFilesInDirectory:
michael@0 80 function Agent_expireFilesInDirectory(path, filesToKeep, minChunkSize) {
michael@0 81 let entries = this.getFileEntriesInDirectory(path, filesToKeep);
michael@0 82 let limit = Math.max(minChunkSize, Math.round(entries.length / 2));
michael@0 83
michael@0 84 for (let entry of entries) {
michael@0 85 this.remove(entry.path);
michael@0 86
michael@0 87 // Check if we reached the limit of files to remove.
michael@0 88 if (--limit <= 0) {
michael@0 89 break;
michael@0 90 }
michael@0 91 }
michael@0 92
michael@0 93 return true;
michael@0 94 },
michael@0 95
michael@0 96 getFileEntriesInDirectory:
michael@0 97 function Agent_getFileEntriesInDirectory(path, skipFiles) {
michael@0 98 let iter = new OS.File.DirectoryIterator(path);
michael@0 99 if (!iter.exists()) {
michael@0 100 return [];
michael@0 101 }
michael@0 102
michael@0 103 let skip = new Set(skipFiles);
michael@0 104
michael@0 105 return [entry
michael@0 106 for (entry in iter)
michael@0 107 if (!entry.isDir && !entry.isSymLink && !skip.has(entry.name))];
michael@0 108 },
michael@0 109
michael@0 110 moveOrDeleteAllThumbnails:
michael@0 111 function Agent_moveOrDeleteAllThumbnails(pathFrom, pathTo) {
michael@0 112 OS.File.makeDir(pathTo, {ignoreExisting: true});
michael@0 113 if (pathFrom == pathTo) {
michael@0 114 return true;
michael@0 115 }
michael@0 116 let iter = new OS.File.DirectoryIterator(pathFrom);
michael@0 117 if (iter.exists()) {
michael@0 118 for (let entry in iter) {
michael@0 119 if (entry.isDir || entry.isSymLink) {
michael@0 120 continue;
michael@0 121 }
michael@0 122
michael@0 123
michael@0 124 let from = OS.Path.join(pathFrom, entry.name);
michael@0 125 let to = OS.Path.join(pathTo, entry.name);
michael@0 126
michael@0 127 try {
michael@0 128 OS.File.move(from, to, {noOverwrite: true, noCopy: true});
michael@0 129 } catch (e) {
michael@0 130 OS.File.remove(from);
michael@0 131 }
michael@0 132 }
michael@0 133 }
michael@0 134 iter.close();
michael@0 135
michael@0 136 try {
michael@0 137 OS.File.removeEmptyDir(pathFrom);
michael@0 138 } catch (e) {
michael@0 139 // This could fail if there's something in
michael@0 140 // the folder we're not permitted to remove.
michael@0 141 }
michael@0 142
michael@0 143 return true;
michael@0 144 },
michael@0 145
michael@0 146 writeAtomic: function Agent_writeAtomic(path, buffer, options) {
michael@0 147 return File.writeAtomic(path,
michael@0 148 buffer,
michael@0 149 options);
michael@0 150 },
michael@0 151
michael@0 152 makeDir: function Agent_makeDir(path, options) {
michael@0 153 return File.makeDir(path, options);
michael@0 154 },
michael@0 155
michael@0 156 copy: function Agent_copy(source, dest, options) {
michael@0 157 return File.copy(source, dest, options);
michael@0 158 },
michael@0 159
michael@0 160 wipe: function Agent_wipe(path) {
michael@0 161 let iterator = new File.DirectoryIterator(path);
michael@0 162 try {
michael@0 163 for (let entry in iterator) {
michael@0 164 try {
michael@0 165 File.remove(entry.path);
michael@0 166 } catch (ex) {
michael@0 167 // If a file cannot be removed, we should still continue.
michael@0 168 // This can happen at least for any of the following reasons:
michael@0 169 // - access denied;
michael@0 170 // - file has been removed recently during a previous wipe
michael@0 171 // and the file system has not flushed that yet (yes, this
michael@0 172 // can happen under Windows);
michael@0 173 // - file has been removed by the user or another process.
michael@0 174 }
michael@0 175 }
michael@0 176 } finally {
michael@0 177 iterator.close();
michael@0 178 }
michael@0 179 },
michael@0 180
michael@0 181 exists: function Agent_exists(path) {
michael@0 182 return File.exists(path);
michael@0 183 },
michael@0 184 };
michael@0 185

mercurial