Tue, 06 Jan 2015 21:39:09 +0100
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 | /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 2; -*- */ |
michael@0 | 2 | /* vim: set sw=4 ts=8 et tw=80 : */ |
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 mozilla_dom_TaskThrottler_h |
michael@0 | 8 | #define mozilla_dom_TaskThrottler_h |
michael@0 | 9 | |
michael@0 | 10 | #include <stdint.h> // for uint32_t |
michael@0 | 11 | #include "base/task.h" // for CancelableTask |
michael@0 | 12 | #include "mozilla/TimeStamp.h" // for TimeDuration, TimeStamp |
michael@0 | 13 | #include "mozilla/RollingMean.h" // for RollingMean |
michael@0 | 14 | #include "mozilla/mozalloc.h" // for operator delete |
michael@0 | 15 | #include "nsAutoPtr.h" // for nsAutoPtr |
michael@0 | 16 | #include "nsTArray.h" // for nsTArray |
michael@0 | 17 | |
michael@0 | 18 | namespace tracked_objects { |
michael@0 | 19 | class Location; |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | namespace mozilla { |
michael@0 | 23 | namespace layers { |
michael@0 | 24 | |
michael@0 | 25 | /** The TaskThrottler prevents update event overruns. It is used in cases where |
michael@0 | 26 | * you're sending an async message and waiting for a reply. You need to call |
michael@0 | 27 | * PostTask to queue a task and TaskComplete when you get a response. |
michael@0 | 28 | * |
michael@0 | 29 | * The call to TaskComplete will run the recent task posted since the last |
michael@0 | 30 | * request was sent, if any. This means that at any time there can be at most 1 |
michael@0 | 31 | * outstanding request being processed and at most 1 queued behind it. |
michael@0 | 32 | * |
michael@0 | 33 | * This is used in the context of repainting a scrollable region. While another |
michael@0 | 34 | * process is painting you might get several updates from the UI thread but when |
michael@0 | 35 | * the paint is complete you want to send the most recent. |
michael@0 | 36 | */ |
michael@0 | 37 | |
michael@0 | 38 | class TaskThrottler { |
michael@0 | 39 | public: |
michael@0 | 40 | TaskThrottler(const TimeStamp& aTimeStamp); |
michael@0 | 41 | |
michael@0 | 42 | /** Post a task to be run as soon as there are no outstanding tasks. |
michael@0 | 43 | * |
michael@0 | 44 | * @param aLocation Use the macro FROM_HERE |
michael@0 | 45 | * @param aTask Ownership of this object is transferred to TaskThrottler |
michael@0 | 46 | * which will delete it when it is either run or becomes |
michael@0 | 47 | * obsolete or the TaskThrottler is destructed. |
michael@0 | 48 | */ |
michael@0 | 49 | void PostTask(const tracked_objects::Location& aLocation, |
michael@0 | 50 | CancelableTask* aTask, const TimeStamp& aTimeStamp); |
michael@0 | 51 | /** |
michael@0 | 52 | * Mark the task as complete and process the next queued task. |
michael@0 | 53 | */ |
michael@0 | 54 | void TaskComplete(const TimeStamp& aTimeStamp); |
michael@0 | 55 | |
michael@0 | 56 | /** |
michael@0 | 57 | * Calculate the average time between processing the posted task and getting |
michael@0 | 58 | * the TaskComplete() call back. |
michael@0 | 59 | */ |
michael@0 | 60 | TimeDuration AverageDuration() |
michael@0 | 61 | { |
michael@0 | 62 | return mMean.empty() ? TimeDuration() : mMean.mean(); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | /** |
michael@0 | 66 | * return true if Throttler has an outstanding task |
michael@0 | 67 | */ |
michael@0 | 68 | bool IsOutstanding() { return mOutstanding; } |
michael@0 | 69 | |
michael@0 | 70 | /** |
michael@0 | 71 | * Return the time elapsed since the last request was processed |
michael@0 | 72 | */ |
michael@0 | 73 | TimeDuration TimeSinceLastRequest(const TimeStamp& aTimeStamp); |
michael@0 | 74 | |
michael@0 | 75 | /** |
michael@0 | 76 | * Clear average history. |
michael@0 | 77 | */ |
michael@0 | 78 | void ClearHistory() { mMean.clear(); } |
michael@0 | 79 | |
michael@0 | 80 | /** |
michael@0 | 81 | * @param aMaxDurations The maximum number of durations to measure. |
michael@0 | 82 | */ |
michael@0 | 83 | |
michael@0 | 84 | void SetMaxDurations(uint32_t aMaxDurations) |
michael@0 | 85 | { |
michael@0 | 86 | if (aMaxDurations != mMean.maxValues()) { |
michael@0 | 87 | mMean = RollingMean<TimeDuration, TimeDuration>(aMaxDurations); |
michael@0 | 88 | } |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | private: |
michael@0 | 92 | bool mOutstanding; |
michael@0 | 93 | nsAutoPtr<CancelableTask> mQueuedTask; |
michael@0 | 94 | TimeStamp mStartTime; |
michael@0 | 95 | RollingMean<TimeDuration, TimeDuration> mMean; |
michael@0 | 96 | }; |
michael@0 | 97 | |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | #endif // mozilla_dom_TaskThrottler_h |