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: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef vm_DateTime_h |
michael@0 | 8 | #define vm_DateTime_h |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/FloatingPoint.h" |
michael@0 | 11 | #include "mozilla/MathAlgorithms.h" |
michael@0 | 12 | |
michael@0 | 13 | #include <stdint.h> |
michael@0 | 14 | |
michael@0 | 15 | #include "js/Value.h" |
michael@0 | 16 | #include "vm/NumericConversions.h" |
michael@0 | 17 | |
michael@0 | 18 | namespace js { |
michael@0 | 19 | |
michael@0 | 20 | /* Constants defined by ES5 15.9.1.10. */ |
michael@0 | 21 | const double HoursPerDay = 24; |
michael@0 | 22 | const double MinutesPerHour = 60; |
michael@0 | 23 | const double SecondsPerMinute = 60; |
michael@0 | 24 | const double msPerSecond = 1000; |
michael@0 | 25 | const double msPerMinute = msPerSecond * SecondsPerMinute; |
michael@0 | 26 | const double msPerHour = msPerMinute * MinutesPerHour; |
michael@0 | 27 | |
michael@0 | 28 | /* ES5 15.9.1.2. */ |
michael@0 | 29 | const double msPerDay = msPerHour * HoursPerDay; |
michael@0 | 30 | |
michael@0 | 31 | /* |
michael@0 | 32 | * Additional quantities not mentioned in the spec. Be careful using these! |
michael@0 | 33 | * They aren't doubles (and aren't defined in terms of all the other constants |
michael@0 | 34 | * so that they can be used in constexpr scenarios; if you need constants that |
michael@0 | 35 | * trigger floating point semantics, you'll have to manually cast to get it. |
michael@0 | 36 | */ |
michael@0 | 37 | const unsigned SecondsPerHour = 60 * 60; |
michael@0 | 38 | const unsigned SecondsPerDay = SecondsPerHour * 24; |
michael@0 | 39 | |
michael@0 | 40 | const double StartOfTime = -8.64e15; |
michael@0 | 41 | const double EndOfTime = 8.64e15; |
michael@0 | 42 | const double MaxTimeMagnitude = 8.64e15; |
michael@0 | 43 | |
michael@0 | 44 | /* ES5 15.9.1.14. */ |
michael@0 | 45 | inline double |
michael@0 | 46 | TimeClip(double time) |
michael@0 | 47 | { |
michael@0 | 48 | /* Steps 1-2. */ |
michael@0 | 49 | if (!mozilla::IsFinite(time) || mozilla::Abs(time) > MaxTimeMagnitude) |
michael@0 | 50 | return JS::GenericNaN(); |
michael@0 | 51 | |
michael@0 | 52 | /* Step 3. */ |
michael@0 | 53 | return ToInteger(time + (+0.0)); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | /* |
michael@0 | 57 | * Stores date/time information, particularly concerning the current local |
michael@0 | 58 | * time zone, and implements a small cache for daylight saving time offset |
michael@0 | 59 | * computation. |
michael@0 | 60 | * |
michael@0 | 61 | * The basic idea is premised upon this fact: the DST offset never changes more |
michael@0 | 62 | * than once in any thirty-day period. If we know the offset at t_0 is o_0, |
michael@0 | 63 | * the offset at [t_1, t_2] is also o_0, where t_1 + 3_0 days == t_2, |
michael@0 | 64 | * t_1 <= t_0, and t0 <= t2. (In other words, t_0 is always somewhere within a |
michael@0 | 65 | * thirty-day range where the DST offset is constant: DST changes never occur |
michael@0 | 66 | * more than once in any thirty-day period.) Therefore, if we intelligently |
michael@0 | 67 | * retain knowledge of the offset for a range of dates (which may vary over |
michael@0 | 68 | * time), and if requests are usually for dates within that range, we can often |
michael@0 | 69 | * provide a response without repeated offset calculation. |
michael@0 | 70 | * |
michael@0 | 71 | * Our caching strategy is as follows: on the first request at date t_0 compute |
michael@0 | 72 | * the requested offset o_0. Save { start: t_0, end: t_0, offset: o_0 } as the |
michael@0 | 73 | * cache's state. Subsequent requests within that range are straightforwardly |
michael@0 | 74 | * handled. If a request for t_i is far outside the range (more than thirty |
michael@0 | 75 | * days), compute o_i = dstOffset(t_i) and save { start: t_i, end: t_i, |
michael@0 | 76 | * offset: t_i }. Otherwise attempt to *overextend* the range to either |
michael@0 | 77 | * [start - 30d, end] or [start, end + 30d] as appropriate to encompass |
michael@0 | 78 | * t_i. If the offset o_i30 is the same as the cached offset, extend the |
michael@0 | 79 | * range. Otherwise the over-guess crossed a DST change -- compute |
michael@0 | 80 | * o_i = dstOffset(t_i) and either extend the original range (if o_i == offset) |
michael@0 | 81 | * or start a new one beneath/above the current one with o_i30 as the offset. |
michael@0 | 82 | * |
michael@0 | 83 | * This cache strategy results in 0 to 2 DST offset computations. The naive |
michael@0 | 84 | * always-compute strategy is 1 computation, and since cache maintenance is a |
michael@0 | 85 | * handful of integer arithmetic instructions the speed difference between |
michael@0 | 86 | * always-1 and 1-with-cache is negligible. Caching loses if two computations |
michael@0 | 87 | * happen: when the date is within 30 days of the cached range and when that |
michael@0 | 88 | * 30-day range crosses a DST change. This is relatively uncommon. Further, |
michael@0 | 89 | * instances of such are often dominated by in-range hits, so caching is an |
michael@0 | 90 | * overall slight win. |
michael@0 | 91 | * |
michael@0 | 92 | * Why 30 days? For correctness the duration must be smaller than any possible |
michael@0 | 93 | * duration between DST changes. Past that, note that 1) a large duration |
michael@0 | 94 | * increases the likelihood of crossing a DST change while reducing the number |
michael@0 | 95 | * of cache misses, and 2) a small duration decreases the size of the cached |
michael@0 | 96 | * range while producing more misses. Using a month as the interval change is |
michael@0 | 97 | * a balance between these two that tries to optimize for the calendar month at |
michael@0 | 98 | * a time that a site might display. (One could imagine an adaptive duration |
michael@0 | 99 | * that accommodates near-DST-change dates better; we don't believe the |
michael@0 | 100 | * potential win from better caching offsets the loss from extra complexity.) |
michael@0 | 101 | */ |
michael@0 | 102 | class DateTimeInfo |
michael@0 | 103 | { |
michael@0 | 104 | public: |
michael@0 | 105 | DateTimeInfo(); |
michael@0 | 106 | |
michael@0 | 107 | /* |
michael@0 | 108 | * Get the DST offset in milliseconds at a UTC time. This is usually |
michael@0 | 109 | * either 0 or |msPerSecond * SecondsPerHour|, but at least one exotic time |
michael@0 | 110 | * zone (Lord Howe Island, Australia) has a fractional-hour offset, just to |
michael@0 | 111 | * keep things interesting. |
michael@0 | 112 | */ |
michael@0 | 113 | int64_t getDSTOffsetMilliseconds(int64_t utcMilliseconds); |
michael@0 | 114 | |
michael@0 | 115 | void updateTimeZoneAdjustment(); |
michael@0 | 116 | |
michael@0 | 117 | /* ES5 15.9.1.7. */ |
michael@0 | 118 | double localTZA() { return localTZA_; } |
michael@0 | 119 | |
michael@0 | 120 | private: |
michael@0 | 121 | /* |
michael@0 | 122 | * The current local time zone adjustment, cached because retrieving this |
michael@0 | 123 | * dynamically is Slow, and a certain venerable benchmark which shall not |
michael@0 | 124 | * be named depends on it being fast. |
michael@0 | 125 | * |
michael@0 | 126 | * SpiderMonkey occasionally and arbitrarily updates this value from the |
michael@0 | 127 | * system time zone to attempt to keep this reasonably up-to-date. If |
michael@0 | 128 | * temporary inaccuracy can't be tolerated, JSAPI clients may call |
michael@0 | 129 | * JS_ClearDateCaches to forcibly sync this with the system time zone. |
michael@0 | 130 | */ |
michael@0 | 131 | double localTZA_; |
michael@0 | 132 | |
michael@0 | 133 | /* |
michael@0 | 134 | * Compute the DST offset at the given UTC time in seconds from the epoch. |
michael@0 | 135 | * (getDSTOffsetMilliseconds attempts to return a cached value, but in case |
michael@0 | 136 | * of a cache miss it calls this method. The cache is represented through |
michael@0 | 137 | * the offset* and *{Start,End}Seconds fields below.) |
michael@0 | 138 | */ |
michael@0 | 139 | int64_t computeDSTOffsetMilliseconds(int64_t utcSeconds); |
michael@0 | 140 | |
michael@0 | 141 | int64_t offsetMilliseconds; |
michael@0 | 142 | int64_t rangeStartSeconds, rangeEndSeconds; // UTC-based |
michael@0 | 143 | |
michael@0 | 144 | int64_t oldOffsetMilliseconds; |
michael@0 | 145 | int64_t oldRangeStartSeconds, oldRangeEndSeconds; // UTC-based |
michael@0 | 146 | |
michael@0 | 147 | /* |
michael@0 | 148 | * Cached offset in seconds from the current UTC time to the current |
michael@0 | 149 | * local standard time (i.e. not including any offset due to DST). |
michael@0 | 150 | */ |
michael@0 | 151 | int32_t utcToLocalStandardOffsetSeconds; |
michael@0 | 152 | |
michael@0 | 153 | static const int64_t MaxUnixTimeT = 2145859200; /* time_t 12/31/2037 */ |
michael@0 | 154 | |
michael@0 | 155 | static const int64_t RangeExpansionAmount = 30 * SecondsPerDay; |
michael@0 | 156 | |
michael@0 | 157 | void sanityCheck(); |
michael@0 | 158 | }; |
michael@0 | 159 | |
michael@0 | 160 | } /* namespace js */ |
michael@0 | 161 | |
michael@0 | 162 | #endif /* vm_DateTime_h */ |