Sat, 03 Jan 2015 20:18:00 +0100
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 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef mozilla_FileUtils_h |
michael@0 | 7 | #define mozilla_FileUtils_h |
michael@0 | 8 | |
michael@0 | 9 | #include "nscore.h" // nullptr |
michael@0 | 10 | |
michael@0 | 11 | #if defined(XP_UNIX) |
michael@0 | 12 | # include <unistd.h> |
michael@0 | 13 | #elif defined(XP_WIN) |
michael@0 | 14 | # include <io.h> |
michael@0 | 15 | #endif |
michael@0 | 16 | #include "prio.h" |
michael@0 | 17 | |
michael@0 | 18 | #include "mozilla/Scoped.h" |
michael@0 | 19 | #include "nsIFile.h" |
michael@0 | 20 | #include <errno.h> |
michael@0 | 21 | #include <limits.h> |
michael@0 | 22 | |
michael@0 | 23 | namespace mozilla { |
michael@0 | 24 | |
michael@0 | 25 | #if defined(XP_WIN) |
michael@0 | 26 | typedef void* filedesc_t; |
michael@0 | 27 | typedef const wchar_t* pathstr_t; |
michael@0 | 28 | #else |
michael@0 | 29 | typedef int filedesc_t; |
michael@0 | 30 | typedef const char* pathstr_t; |
michael@0 | 31 | #endif |
michael@0 | 32 | |
michael@0 | 33 | /** |
michael@0 | 34 | * ScopedCloseFD is a RAII wrapper for POSIX file descriptors |
michael@0 | 35 | * |
michael@0 | 36 | * Instances |close()| their fds when they go out of scope. |
michael@0 | 37 | */ |
michael@0 | 38 | struct ScopedCloseFDTraits |
michael@0 | 39 | { |
michael@0 | 40 | typedef int type; |
michael@0 | 41 | static type empty() { return -1; } |
michael@0 | 42 | static void release(type fd) { |
michael@0 | 43 | if (fd != -1) { |
michael@0 | 44 | while ((close(fd) == -1) && (errno == EINTR)) { |
michael@0 | 45 | ; |
michael@0 | 46 | } |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | }; |
michael@0 | 50 | typedef Scoped<ScopedCloseFDTraits> ScopedClose; |
michael@0 | 51 | |
michael@0 | 52 | #if !defined(XPCOM_GLUE) |
michael@0 | 53 | |
michael@0 | 54 | /** |
michael@0 | 55 | * AutoFDClose is a RAII wrapper for PRFileDesc. |
michael@0 | 56 | * |
michael@0 | 57 | * Instances |PR_Close| their fds when they go out of scope. |
michael@0 | 58 | **/ |
michael@0 | 59 | struct ScopedClosePRFDTraits |
michael@0 | 60 | { |
michael@0 | 61 | typedef PRFileDesc* type; |
michael@0 | 62 | static type empty() { return nullptr; } |
michael@0 | 63 | static void release(type fd) { |
michael@0 | 64 | if (fd != nullptr) { |
michael@0 | 65 | PR_Close(fd); |
michael@0 | 66 | } |
michael@0 | 67 | } |
michael@0 | 68 | }; |
michael@0 | 69 | typedef Scoped<ScopedClosePRFDTraits> AutoFDClose; |
michael@0 | 70 | |
michael@0 | 71 | /* RAII wrapper for FILE descriptors */ |
michael@0 | 72 | struct ScopedCloseFileTraits |
michael@0 | 73 | { |
michael@0 | 74 | typedef FILE *type; |
michael@0 | 75 | static type empty() { return nullptr; } |
michael@0 | 76 | static void release(type f) { |
michael@0 | 77 | if (f) { |
michael@0 | 78 | fclose(f); |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | }; |
michael@0 | 82 | typedef Scoped<ScopedCloseFileTraits> ScopedCloseFile; |
michael@0 | 83 | |
michael@0 | 84 | /** |
michael@0 | 85 | * Fallocate efficiently and continuously allocates files via fallocate-type APIs. |
michael@0 | 86 | * This is useful for avoiding fragmentation. |
michael@0 | 87 | * On sucess the file be padded with zeros to grow to aLength. |
michael@0 | 88 | * |
michael@0 | 89 | * @param aFD file descriptor. |
michael@0 | 90 | * @param aLength length of file to grow to. |
michael@0 | 91 | * @return true on success. |
michael@0 | 92 | */ |
michael@0 | 93 | NS_COM_GLUE bool fallocate(PRFileDesc *aFD, int64_t aLength); |
michael@0 | 94 | |
michael@0 | 95 | /** |
michael@0 | 96 | * Use readahead to preload shared libraries into the file cache before loading. |
michael@0 | 97 | * WARNING: This function should not be used without a telemetry field trial |
michael@0 | 98 | * demonstrating a clear performance improvement! |
michael@0 | 99 | * |
michael@0 | 100 | * @param aFile nsIFile representing path to shared library |
michael@0 | 101 | */ |
michael@0 | 102 | NS_COM_GLUE void ReadAheadLib(nsIFile* aFile); |
michael@0 | 103 | |
michael@0 | 104 | /** |
michael@0 | 105 | * Use readahead to preload a file into the file cache before reading. |
michael@0 | 106 | * WARNING: This function should not be used without a telemetry field trial |
michael@0 | 107 | * demonstrating a clear performance improvement! |
michael@0 | 108 | * |
michael@0 | 109 | * @param aFile nsIFile representing path to shared library |
michael@0 | 110 | * @param aOffset Offset into the file to begin preloading |
michael@0 | 111 | * @param aCount Number of bytes to preload (SIZE_MAX implies file size) |
michael@0 | 112 | * @param aOutFd Pointer to file descriptor. If specified, ReadAheadFile will |
michael@0 | 113 | * return its internal, opened file descriptor instead of closing it. |
michael@0 | 114 | */ |
michael@0 | 115 | NS_COM_GLUE void ReadAheadFile(nsIFile* aFile, const size_t aOffset = 0, |
michael@0 | 116 | const size_t aCount = SIZE_MAX, |
michael@0 | 117 | filedesc_t* aOutFd = nullptr); |
michael@0 | 118 | |
michael@0 | 119 | #endif // !defined(XPCOM_GLUE) |
michael@0 | 120 | |
michael@0 | 121 | /** |
michael@0 | 122 | * Use readahead to preload shared libraries into the file cache before loading. |
michael@0 | 123 | * WARNING: This function should not be used without a telemetry field trial |
michael@0 | 124 | * demonstrating a clear performance improvement! |
michael@0 | 125 | * |
michael@0 | 126 | * @param aFilePath path to shared library |
michael@0 | 127 | */ |
michael@0 | 128 | NS_COM_GLUE void ReadAheadLib(pathstr_t aFilePath); |
michael@0 | 129 | |
michael@0 | 130 | /** |
michael@0 | 131 | * Use readahead to preload a file into the file cache before loading. |
michael@0 | 132 | * WARNING: This function should not be used without a telemetry field trial |
michael@0 | 133 | * demonstrating a clear performance improvement! |
michael@0 | 134 | * |
michael@0 | 135 | * @param aFilePath path to shared library |
michael@0 | 136 | * @param aOffset Offset into the file to begin preloading |
michael@0 | 137 | * @param aCount Number of bytes to preload (SIZE_MAX implies file size) |
michael@0 | 138 | * @param aOutFd Pointer to file descriptor. If specified, ReadAheadFile will |
michael@0 | 139 | * return its internal, opened file descriptor instead of closing it. |
michael@0 | 140 | */ |
michael@0 | 141 | NS_COM_GLUE void ReadAheadFile(pathstr_t aFilePath, const size_t aOffset = 0, |
michael@0 | 142 | const size_t aCount = SIZE_MAX, |
michael@0 | 143 | filedesc_t* aOutFd = nullptr); |
michael@0 | 144 | |
michael@0 | 145 | /** |
michael@0 | 146 | * Use readahead to preload a file into the file cache before reading. |
michael@0 | 147 | * When this function exits, the file pointer is guaranteed to be in the same |
michael@0 | 148 | * position it was in before this function was called. |
michael@0 | 149 | * WARNING: This function should not be used without a telemetry field trial |
michael@0 | 150 | * demonstrating a clear performance improvement! |
michael@0 | 151 | * |
michael@0 | 152 | * @param aFd file descriptor opened for read access |
michael@0 | 153 | * (on Windows, file must be opened with FILE_FLAG_SEQUENTIAL_SCAN) |
michael@0 | 154 | * @param aOffset Offset into the file to begin preloading |
michael@0 | 155 | * @param aCount Number of bytes to preload (SIZE_MAX implies file size) |
michael@0 | 156 | */ |
michael@0 | 157 | NS_COM_GLUE void ReadAhead(filedesc_t aFd, const size_t aOffset = 0, |
michael@0 | 158 | const size_t aCount = SIZE_MAX); |
michael@0 | 159 | |
michael@0 | 160 | |
michael@0 | 161 | /* Define ReadSysFile() only on GONK to avoid unnecessary lubxul bloat. |
michael@0 | 162 | Also define it in debug builds, so that unit tests for it can be written |
michael@0 | 163 | and run in non-GONK builds. */ |
michael@0 | 164 | #if (defined(MOZ_WIDGET_GONK) || defined(DEBUG)) && defined(XP_UNIX) |
michael@0 | 165 | |
michael@0 | 166 | #ifndef ReadSysFile_PRESENT |
michael@0 | 167 | #define ReadSysFile_PRESENT |
michael@0 | 168 | #endif /* ReadSysFile_PRESENT */ |
michael@0 | 169 | |
michael@0 | 170 | #define MOZ_TEMP_FAILURE_RETRY(exp) (__extension__({ \ |
michael@0 | 171 | typeof (exp) _rc; \ |
michael@0 | 172 | do { \ |
michael@0 | 173 | _rc = (exp); \ |
michael@0 | 174 | } while (_rc == -1 && errno == EINTR); \ |
michael@0 | 175 | _rc; \ |
michael@0 | 176 | })) |
michael@0 | 177 | |
michael@0 | 178 | /** |
michael@0 | 179 | * Read the contents of a file. |
michael@0 | 180 | * This function is intended for reading a single-lined text files from |
michael@0 | 181 | * /sys/. If the file ends with a newline ('\n') then it will be discarded. |
michael@0 | 182 | * The output buffer will always be '\0'-terminated on successful completion. |
michael@0 | 183 | * If aBufSize == 0, then this function will return true if the file exists |
michael@0 | 184 | * and is readable (it will not attempt to read anything from it). |
michael@0 | 185 | * On failure the contents of aBuf after this call will be undefined and the |
michael@0 | 186 | * value of the global variable errno will be set accordingly. |
michael@0 | 187 | * @return true on success, notice that less than requested bytes could have |
michael@0 | 188 | * been read if the file was smaller |
michael@0 | 189 | */ |
michael@0 | 190 | bool |
michael@0 | 191 | ReadSysFile( |
michael@0 | 192 | const char* aFilename, |
michael@0 | 193 | char* aBuf, |
michael@0 | 194 | size_t aBufSize); |
michael@0 | 195 | |
michael@0 | 196 | /** |
michael@0 | 197 | * Parse the contents of a file, assuming it contains a decimal integer. |
michael@0 | 198 | * @return true on success |
michael@0 | 199 | */ |
michael@0 | 200 | bool |
michael@0 | 201 | ReadSysFile( |
michael@0 | 202 | const char* aFilename, |
michael@0 | 203 | int* aVal); |
michael@0 | 204 | |
michael@0 | 205 | /** |
michael@0 | 206 | * Parse the contents of a file, assuming it contains a boolean value |
michael@0 | 207 | * (either 0 or 1). |
michael@0 | 208 | * @return true on success |
michael@0 | 209 | */ |
michael@0 | 210 | bool |
michael@0 | 211 | ReadSysFile( |
michael@0 | 212 | const char* aFilename, |
michael@0 | 213 | bool* aVal); |
michael@0 | 214 | |
michael@0 | 215 | #endif /* (MOZ_WIDGET_GONK || DEBUG) && XP_UNIX */ |
michael@0 | 216 | |
michael@0 | 217 | } // namespace mozilla |
michael@0 | 218 | #endif |