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.

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

mercurial