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++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 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 file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | // Some of this code is cut-and-pasted from nICEr. Copyright is: |
michael@0 | 8 | |
michael@0 | 9 | /* |
michael@0 | 10 | Copyright (c) 2007, Adobe Systems, Incorporated |
michael@0 | 11 | All rights reserved. |
michael@0 | 12 | |
michael@0 | 13 | Redistribution and use in source and binary forms, with or without |
michael@0 | 14 | modification, are permitted provided that the following conditions are |
michael@0 | 15 | met: |
michael@0 | 16 | |
michael@0 | 17 | * Redistributions of source code must retain the above copyright |
michael@0 | 18 | notice, this list of conditions and the following disclaimer. |
michael@0 | 19 | |
michael@0 | 20 | * Redistributions in binary form must reproduce the above copyright |
michael@0 | 21 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 22 | documentation and/or other materials provided with the distribution. |
michael@0 | 23 | |
michael@0 | 24 | * Neither the name of Adobe Systems, Network Resonance nor the names of its |
michael@0 | 25 | contributors may be used to endorse or promote products derived from |
michael@0 | 26 | this software without specific prior written permission. |
michael@0 | 27 | |
michael@0 | 28 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 29 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 30 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 31 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 32 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 33 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 34 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 35 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 36 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 37 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 38 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 39 | */ |
michael@0 | 40 | |
michael@0 | 41 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 42 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 43 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 44 | |
michael@0 | 45 | /* Original author: bcampen@mozilla.com */ |
michael@0 | 46 | |
michael@0 | 47 | /* |
michael@0 | 48 | This file defines an r_dest_vlog that can be used to accumulate log messages |
michael@0 | 49 | for later inspection/filtering. The intent is to use this for interactive |
michael@0 | 50 | debug purposes on an about:webrtc page or similar. |
michael@0 | 51 | */ |
michael@0 | 52 | |
michael@0 | 53 | #ifndef rlogringbuffer_h__ |
michael@0 | 54 | #define rlogringbuffer_h__ |
michael@0 | 55 | |
michael@0 | 56 | #include <stdint.h> |
michael@0 | 57 | |
michael@0 | 58 | #include <deque> |
michael@0 | 59 | #include <string> |
michael@0 | 60 | #include <vector> |
michael@0 | 61 | |
michael@0 | 62 | #include "mozilla/Mutex.h" |
michael@0 | 63 | |
michael@0 | 64 | #include "m_cpp_utils.h" |
michael@0 | 65 | |
michael@0 | 66 | namespace mozilla { |
michael@0 | 67 | |
michael@0 | 68 | class RLogRingBuffer { |
michael@0 | 69 | public: |
michael@0 | 70 | /* |
michael@0 | 71 | NB: These are not threadsafe, nor are they safe to call during static |
michael@0 | 72 | init/deinit. |
michael@0 | 73 | */ |
michael@0 | 74 | static RLogRingBuffer* CreateInstance(); |
michael@0 | 75 | static RLogRingBuffer* GetInstance(); |
michael@0 | 76 | static void DestroyInstance(); |
michael@0 | 77 | |
michael@0 | 78 | /* |
michael@0 | 79 | Retrieves log statements that match a given substring, subject to a |
michael@0 | 80 | limit. |matching_logs| will be filled in chronological order (front() |
michael@0 | 81 | is oldest, back() is newest). |limit| == 0 will be interpreted as no |
michael@0 | 82 | limit. |
michael@0 | 83 | */ |
michael@0 | 84 | void Filter(const std::string& substring, |
michael@0 | 85 | uint32_t limit, |
michael@0 | 86 | std::deque<std::string>* matching_logs); |
michael@0 | 87 | |
michael@0 | 88 | void FilterAny(const std::vector<std::string>& substrings, |
michael@0 | 89 | uint32_t limit, |
michael@0 | 90 | std::deque<std::string>* matching_logs); |
michael@0 | 91 | |
michael@0 | 92 | inline void GetAny(uint32_t limit, |
michael@0 | 93 | std::deque<std::string>* matching_logs) { |
michael@0 | 94 | Filter("", limit, matching_logs); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | void SetLogLimit(uint32_t new_limit); |
michael@0 | 98 | |
michael@0 | 99 | void Log(std::string&& log); |
michael@0 | 100 | |
michael@0 | 101 | private: |
michael@0 | 102 | RLogRingBuffer(); |
michael@0 | 103 | ~RLogRingBuffer(); |
michael@0 | 104 | void RemoveOld(); |
michael@0 | 105 | static RLogRingBuffer* instance; |
michael@0 | 106 | |
michael@0 | 107 | /* |
michael@0 | 108 | * Might be worthwhile making this a circular buffer, but I think it is |
michael@0 | 109 | * preferable to take up as little space as possible if no logging is |
michael@0 | 110 | * happening/the ringbuffer is not being used. |
michael@0 | 111 | */ |
michael@0 | 112 | std::deque<std::string> log_messages_; |
michael@0 | 113 | /* Max size of log buffer (should we use time-depth instead/also?) */ |
michael@0 | 114 | uint32_t log_limit_; |
michael@0 | 115 | OffTheBooksMutex mutex_; |
michael@0 | 116 | |
michael@0 | 117 | DISALLOW_COPY_ASSIGN(RLogRingBuffer); |
michael@0 | 118 | }; // class RLogRingBuffer |
michael@0 | 119 | |
michael@0 | 120 | } // namespace mozilla |
michael@0 | 121 | |
michael@0 | 122 | #endif // rlogringbuffer_h__ |
michael@0 | 123 | |
michael@0 | 124 |