netwerk/cache/nsDeleteDir.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/cache/nsDeleteDir.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,79 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef nsDeleteDir_h__
    1.11 +#define nsDeleteDir_h__
    1.12 +
    1.13 +#include "nsCOMPtr.h"
    1.14 +#include "nsCOMArray.h"
    1.15 +#include "mozilla/Mutex.h"
    1.16 +#include "mozilla/CondVar.h"
    1.17 +
    1.18 +class nsIFile;
    1.19 +class nsIThread;
    1.20 +class nsITimer;
    1.21 +
    1.22 +
    1.23 +class nsDeleteDir {
    1.24 +public:
    1.25 +  nsDeleteDir();
    1.26 +  ~nsDeleteDir();
    1.27 +
    1.28 +  static nsresult Init();
    1.29 +  static nsresult Shutdown(bool finishDeleting);
    1.30 +
    1.31 +  /**
    1.32 +   * This routine attempts to delete a directory that may contain some files
    1.33 +   * that are still in use. This latter point is only an issue on Windows and
    1.34 +   * a few other systems.
    1.35 +   *
    1.36 +   * If the moveToTrash parameter is true we first rename the given directory
    1.37 +   * "foo.Trash123" (where "foo" is the original directory name, and "123" is
    1.38 +   * a random number, in order to support multiple concurrent deletes). The
    1.39 +   * directory is then deleted, file-by-file, on a background thread.
    1.40 +   *
    1.41 +   * If the moveToTrash parameter is false, then the given directory is deleted
    1.42 +   * directly.
    1.43 +   *
    1.44 +   * If 'delay' is non-zero, the directory will not be deleted until the
    1.45 +   * specified number of milliseconds have passed. (The directory is still
    1.46 +   * renamed immediately if 'moveToTrash' is passed, so upon return it is safe
    1.47 +   * to create a directory with the same name).
    1.48 +   */
    1.49 +  static nsresult DeleteDir(nsIFile *dir, bool moveToTrash, uint32_t delay = 0);
    1.50 +
    1.51 +  /**
    1.52 +   * Returns the trash directory corresponding to the given directory.
    1.53 +   */
    1.54 +  static nsresult GetTrashDir(nsIFile *dir, nsCOMPtr<nsIFile> *result);
    1.55 +
    1.56 +  /**
    1.57 +   * Remove all trashes left from previous run. This function does nothing when
    1.58 +   * called second and more times.
    1.59 +   */
    1.60 +  static nsresult RemoveOldTrashes(nsIFile *cacheDir);
    1.61 +
    1.62 +  static void TimerCallback(nsITimer *aTimer, void *arg);
    1.63 +
    1.64 +private:
    1.65 +  friend class nsBlockOnBackgroundThreadEvent;
    1.66 +  friend class nsDestroyThreadEvent;
    1.67 +
    1.68 +  nsresult InitThread();
    1.69 +  void     DestroyThread();
    1.70 +  nsresult PostTimer(void *arg, uint32_t delay);
    1.71 +  nsresult RemoveDir(nsIFile *file, bool *stopDeleting);
    1.72 +
    1.73 +  static nsDeleteDir * gInstance;
    1.74 +  mozilla::Mutex       mLock;
    1.75 +  mozilla::CondVar     mCondVar;
    1.76 +  nsCOMArray<nsITimer> mTimers;
    1.77 +  nsCOMPtr<nsIThread>  mThread;
    1.78 +  bool                 mShutdownPending;
    1.79 +  bool                 mStopDeleting;
    1.80 +};
    1.81 +
    1.82 +#endif  // nsDeleteDir_h__

mercurial