media/mtransport/nr_timer.cpp

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.

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 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 8 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 9 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 10
michael@0 11 // Original code by: ekr@rtfm.com
michael@0 12
michael@0 13 // Implementation of the NR timer interface
michael@0 14
michael@0 15 // Some code here copied from nrappkit. The license was.
michael@0 16
michael@0 17 /**
michael@0 18 Copyright (C) 2004, Network Resonance, Inc.
michael@0 19 Copyright (C) 2006, Network Resonance, Inc.
michael@0 20 All Rights Reserved
michael@0 21
michael@0 22 Redistribution and use in source and binary forms, with or without
michael@0 23 modification, are permitted provided that the following conditions
michael@0 24 are met:
michael@0 25
michael@0 26 1. Redistributions of source code must retain the above copyright
michael@0 27 notice, this list of conditions and the following disclaimer.
michael@0 28 2. Redistributions in binary form must reproduce the above copyright
michael@0 29 notice, this list of conditions and the following disclaimer in the
michael@0 30 documentation and/or other materials provided with the distribution.
michael@0 31 3. Neither the name of Network Resonance, Inc. nor the name of any
michael@0 32 contributors to this software may be used to endorse or promote
michael@0 33 products derived from this software without specific prior written
michael@0 34 permission.
michael@0 35
michael@0 36 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
michael@0 37 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 38 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
michael@0 39 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
michael@0 40 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
michael@0 41 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
michael@0 42 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
michael@0 43 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
michael@0 44 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
michael@0 45 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
michael@0 46 POSSIBILITY OF SUCH DAMAGE.
michael@0 47
michael@0 48
michael@0 49 ekr@rtfm.com Sun Feb 22 19:35:24 2004
michael@0 50 */
michael@0 51
michael@0 52 #include <string>
michael@0 53
michael@0 54 #include "nsCOMPtr.h"
michael@0 55 #include "nsComponentManagerUtils.h"
michael@0 56 #include "nsServiceManagerUtils.h"
michael@0 57 #include "nsIEventTarget.h"
michael@0 58 #include "nsITimer.h"
michael@0 59 #include "nsNetCID.h"
michael@0 60 #include "runnable_utils.h"
michael@0 61
michael@0 62 extern "C" {
michael@0 63 #include "nr_api.h"
michael@0 64 #include "async_timer.h"
michael@0 65 }
michael@0 66
michael@0 67
michael@0 68 namespace mozilla {
michael@0 69
michael@0 70 class nrappkitTimerCallback : public nsITimerCallback
michael@0 71 {
michael@0 72 public:
michael@0 73 // We're going to release ourself in the callback, so we need to be threadsafe
michael@0 74 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 75 NS_DECL_NSITIMERCALLBACK
michael@0 76
michael@0 77 nrappkitTimerCallback(NR_async_cb cb, void *cb_arg,
michael@0 78 const char *function, int line)
michael@0 79 : cb_(cb), cb_arg_(cb_arg), function_(function), line_(line) {
michael@0 80 }
michael@0 81
michael@0 82 private:
michael@0 83 virtual ~nrappkitTimerCallback() {}
michael@0 84
michael@0 85 protected:
michael@0 86 /* additional members */
michael@0 87 NR_async_cb cb_;
michael@0 88 void *cb_arg_;
michael@0 89 std::string function_;
michael@0 90 int line_;
michael@0 91 };
michael@0 92
michael@0 93 NS_IMPL_ISUPPORTS(nrappkitTimerCallback, nsITimerCallback)
michael@0 94
michael@0 95 NS_IMETHODIMP nrappkitTimerCallback::Notify(nsITimer *timer) {
michael@0 96 r_log(LOG_GENERIC, LOG_DEBUG, "Timer callback fired (set in %s:%d)",
michael@0 97 function_.c_str(), line_);
michael@0 98
michael@0 99 cb_(0, 0, cb_arg_);
michael@0 100
michael@0 101 // Allow the timer to go away.
michael@0 102 timer->Release();
michael@0 103 return NS_OK;
michael@0 104 }
michael@0 105 } // close namespace
michael@0 106
michael@0 107
michael@0 108 using namespace mozilla;
michael@0 109
michael@0 110 // These timers must only be used from the STS thread.
michael@0 111 // This function is a helper that enforces that.
michael@0 112 static void CheckSTSThread() {
michael@0 113 nsresult rv;
michael@0 114
michael@0 115 nsCOMPtr<nsIEventTarget> sts_thread;
michael@0 116
michael@0 117 sts_thread = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
michael@0 118
michael@0 119 MOZ_ASSERT(NS_SUCCEEDED(rv));
michael@0 120 ASSERT_ON_THREAD(sts_thread);
michael@0 121 }
michael@0 122
michael@0 123 int NR_async_timer_set(int timeout, NR_async_cb cb, void *arg, char *func,
michael@0 124 int l, void **handle) {
michael@0 125 nsresult rv;
michael@0 126 CheckSTSThread();
michael@0 127
michael@0 128 nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
michael@0 129 if (NS_FAILED(rv)) {
michael@0 130 return(R_FAILED);
michael@0 131 }
michael@0 132
michael@0 133 rv = timer->InitWithCallback(new nrappkitTimerCallback(cb, arg, func, l),
michael@0 134 timeout, nsITimer::TYPE_ONE_SHOT);
michael@0 135 if (NS_FAILED(rv)) {
michael@0 136 return R_FAILED;
michael@0 137 }
michael@0 138
michael@0 139 // We need an AddRef here to keep the timer alive, per the spec.
michael@0 140 timer->AddRef();
michael@0 141
michael@0 142 if (handle)
michael@0 143 *handle = timer.get();
michael@0 144 // Bug 818806: if we have no handle to the timer, we have no way to avoid
michael@0 145 // it leaking (though not the callback object) if it never fires (or if
michael@0 146 // we exit before it fires).
michael@0 147
michael@0 148 return 0;
michael@0 149 }
michael@0 150
michael@0 151 int NR_async_schedule(NR_async_cb cb, void *arg, char *func, int l) {
michael@0 152 // No need to check the thread because we check it next in the
michael@0 153 // timer set.
michael@0 154 return NR_async_timer_set(0, cb, arg, func, l, nullptr);
michael@0 155 }
michael@0 156
michael@0 157 int NR_async_timer_cancel(void *handle) {
michael@0 158 // Check for the handle being nonzero because sometimes we get
michael@0 159 // no-op cancels that aren't on the STS thread. This can be
michael@0 160 // non-racy as long as the upper-level code is careful.
michael@0 161 if (!handle)
michael@0 162 return 0;
michael@0 163
michael@0 164 CheckSTSThread();
michael@0 165
michael@0 166 nsITimer *timer = static_cast<nsITimer *>(handle);
michael@0 167
michael@0 168 timer->Cancel();
michael@0 169 // Allow the timer to go away.
michael@0 170 timer->Release();
michael@0 171
michael@0 172 return 0;
michael@0 173 }
michael@0 174

mercurial