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.

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

mercurial