media/mtransport/rlogringbuffer.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

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.

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

mercurial