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 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_AT_EXIT_H_ |
michael@0 | 6 | #define BASE_AT_EXIT_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include <stack> |
michael@0 | 9 | |
michael@0 | 10 | #include "base/base_export.h" |
michael@0 | 11 | #include "base/basictypes.h" |
michael@0 | 12 | #include "base/callback.h" |
michael@0 | 13 | #include "base/synchronization/lock.h" |
michael@0 | 14 | |
michael@0 | 15 | namespace base { |
michael@0 | 16 | |
michael@0 | 17 | // This class provides a facility similar to the CRT atexit(), except that |
michael@0 | 18 | // we control when the callbacks are executed. Under Windows for a DLL they |
michael@0 | 19 | // happen at a really bad time and under the loader lock. This facility is |
michael@0 | 20 | // mostly used by base::Singleton. |
michael@0 | 21 | // |
michael@0 | 22 | // The usage is simple. Early in the main() or WinMain() scope create an |
michael@0 | 23 | // AtExitManager object on the stack: |
michael@0 | 24 | // int main(...) { |
michael@0 | 25 | // base::AtExitManager exit_manager; |
michael@0 | 26 | // |
michael@0 | 27 | // } |
michael@0 | 28 | // When the exit_manager object goes out of scope, all the registered |
michael@0 | 29 | // callbacks and singleton destructors will be called. |
michael@0 | 30 | |
michael@0 | 31 | class BASE_EXPORT AtExitManager { |
michael@0 | 32 | public: |
michael@0 | 33 | typedef void (*AtExitCallbackType)(void*); |
michael@0 | 34 | |
michael@0 | 35 | AtExitManager(); |
michael@0 | 36 | |
michael@0 | 37 | // The dtor calls all the registered callbacks. Do not try to register more |
michael@0 | 38 | // callbacks after this point. |
michael@0 | 39 | ~AtExitManager(); |
michael@0 | 40 | |
michael@0 | 41 | // Registers the specified function to be called at exit. The prototype of |
michael@0 | 42 | // the callback function is void func(void*). |
michael@0 | 43 | static void RegisterCallback(AtExitCallbackType func, void* param); |
michael@0 | 44 | |
michael@0 | 45 | // Registers the specified task to be called at exit. |
michael@0 | 46 | static void RegisterTask(base::Closure task); |
michael@0 | 47 | |
michael@0 | 48 | // Calls the functions registered with RegisterCallback in LIFO order. It |
michael@0 | 49 | // is possible to register new callbacks after calling this function. |
michael@0 | 50 | static void ProcessCallbacksNow(); |
michael@0 | 51 | |
michael@0 | 52 | protected: |
michael@0 | 53 | // This constructor will allow this instance of AtExitManager to be created |
michael@0 | 54 | // even if one already exists. This should only be used for testing! |
michael@0 | 55 | // AtExitManagers are kept on a global stack, and it will be removed during |
michael@0 | 56 | // destruction. This allows you to shadow another AtExitManager. |
michael@0 | 57 | explicit AtExitManager(bool shadow); |
michael@0 | 58 | |
michael@0 | 59 | private: |
michael@0 | 60 | base::Lock lock_; |
michael@0 | 61 | std::stack<base::Closure> stack_; |
michael@0 | 62 | AtExitManager* next_manager_; // Stack of managers to allow shadowing. |
michael@0 | 63 | |
michael@0 | 64 | DISALLOW_COPY_AND_ASSIGN(AtExitManager); |
michael@0 | 65 | }; |
michael@0 | 66 | |
michael@0 | 67 | #if defined(UNIT_TEST) |
michael@0 | 68 | class ShadowingAtExitManager : public AtExitManager { |
michael@0 | 69 | public: |
michael@0 | 70 | ShadowingAtExitManager() : AtExitManager(true) {} |
michael@0 | 71 | }; |
michael@0 | 72 | #endif // defined(UNIT_TEST) |
michael@0 | 73 | |
michael@0 | 74 | } // namespace base |
michael@0 | 75 | |
michael@0 | 76 | #endif // BASE_AT_EXIT_H_ |