xpcom/glue/nsVersionComparator.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     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
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #ifndef nsVersionComparator_h__
     6 #define nsVersionComparator_h__
     8 #include "nscore.h"
     9 #include <stdlib.h>
    10 #include <string.h>
    11 #include <assert.h>
    12 #if defined(XP_WIN) && !defined(UPDATER_NO_STRING_GLUE_STL)
    13 #include <wchar.h>
    14 #include "nsStringGlue.h"
    15 #endif
    17 /**
    18  * In order to compare version numbers in Mozilla, you need to use the
    19  * mozilla::Version class.  You can construct an object of this type by passing
    20  * in a string version number to the constructor.  Objects of this type can be
    21  * compared using the standard comparison operators.
    22  *
    23  * For example, let's say that you want to make sure that a given version
    24  * number is not older than 15.a2.  Here's how you would write a function to
    25  * do that.
    26  *
    27  * bool IsVersionValid(const char* version) {
    28  *   return mozilla::Version("15.a2") <= mozilla::Version(version);
    29  * }
    30  *
    31  * Or, since Version's constructor is implicit, you can simplify this code:
    32  *
    33  * bool IsVersionValid(const char* version) {
    34  *   return mozilla::Version("15.a2") <= version;
    35  * }
    36  *
    37  * On Windows, if your version strings are wide characters, you should use the
    38  * mozilla::VersionW variant instead.  The semantics of that class is the same
    39  * as Version.
    40  */
    42 namespace mozilla {
    44 int32_t NS_COM_GLUE
    45 CompareVersions(const char *A, const char *B);
    47 #ifdef XP_WIN
    48 int32_t NS_COM_GLUE
    49 CompareVersions(const char16_t *A, const char16_t *B);
    50 #endif
    52 struct NS_COM_GLUE Version
    53 {
    54   Version(const char* versionString)
    55   {
    56     versionContent = strdup(versionString);
    57   }
    59   const char* ReadContent() const
    60   {
    61     return versionContent;
    62   }
    64   ~Version()
    65   {
    66     free(versionContent);
    67   }
    69   bool operator< (const Version& rhs) const
    70   {
    71     return CompareVersions(versionContent, rhs.ReadContent()) == -1;
    72   }
    73   bool operator<= (const Version& rhs) const
    74   {
    75     return CompareVersions(versionContent, rhs.ReadContent()) < 1;
    76   }
    77   bool operator> (const Version& rhs) const
    78   {
    79     return CompareVersions(versionContent, rhs.ReadContent()) == 1;
    80   }
    81   bool operator>= (const Version& rhs) const
    82   {
    83     return CompareVersions(versionContent, rhs.ReadContent()) > -1;
    84   }
    85   bool operator== (const Version& rhs) const
    86   {
    87     return CompareVersions(versionContent, rhs.ReadContent()) == 0;
    88   }
    89   bool operator!= (const Version& rhs) const
    90   {
    91     return CompareVersions(versionContent, rhs.ReadContent()) != 0;
    92   }
    94  private:
    95   char* versionContent;
    96 };
    98 #ifdef XP_WIN
    99 struct NS_COM_GLUE VersionW
   100 {
   101   VersionW(const char16_t *versionStringW)
   102   {
   103     versionContentW = reinterpret_cast<char16_t*>(wcsdup(char16ptr_t(versionStringW)));
   104   }
   106   const char16_t* ReadContentW() const
   107   {
   108     return versionContentW;
   109   }
   111   ~VersionW()
   112   {
   113     free(versionContentW);
   114   }
   116   bool operator< (const VersionW& rhs) const
   117   {
   118     return CompareVersions(versionContentW, rhs.ReadContentW()) == -1;
   119   }
   120   bool operator<= (const VersionW& rhs) const
   121   {
   122     return CompareVersions(versionContentW, rhs.ReadContentW()) < 1;
   123   }
   124   bool operator> (const VersionW& rhs) const
   125   {
   126     return CompareVersions(versionContentW, rhs.ReadContentW()) == 1;
   127   }
   128   bool operator>= (const VersionW& rhs) const
   129   {
   130     return CompareVersions(versionContentW, rhs.ReadContentW()) > -1;
   131   }
   132   bool operator== (const VersionW& rhs) const
   133   {
   134     return CompareVersions(versionContentW, rhs.ReadContentW()) == 0;
   135   }
   136   bool operator!= (const VersionW& rhs) const
   137   {
   138     return CompareVersions(versionContentW, rhs.ReadContentW()) != 0;
   139   }
   141  private:
   142   char16_t* versionContentW;
   143 };
   144 #endif
   146 } // namespace mozilla
   148 #endif // nsVersionComparator_h__

mercurial