media/mtransport/runnable_utils.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.

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 // Original author: ekr@rtfm.com
michael@0 8
michael@0 9 #ifndef runnable_utils_h__
michael@0 10 #define runnable_utils_h__
michael@0 11
michael@0 12 #include "nsThreadUtils.h"
michael@0 13 #include "mozilla/RefPtr.h"
michael@0 14
michael@0 15 // Abstract base class for all of our templates
michael@0 16 namespace mozilla {
michael@0 17
michael@0 18 class runnable_args_base : public nsRunnable {
michael@0 19 public:
michael@0 20 NS_IMETHOD Run() = 0;
michael@0 21 virtual bool returns_value() const { return false; }
michael@0 22 };
michael@0 23
michael@0 24 // The generated file contains four major function templates
michael@0 25 // (in variants for arbitrary numbers of arguments up to 10,
michael@0 26 // which is why it is machine generated). The four templates
michael@0 27 // are:
michael@0 28 //
michael@0 29 // WrapRunnable(o, m, ...) -- wraps a member function m of an object ptr o
michael@0 30 // WrapRunnableRet(o, m, ..., r) -- wraps a member function m of an object ptr o
michael@0 31 // the function returns something that can
michael@0 32 // be assigned to *r
michael@0 33 // WrapRunnableNM(f, ...) -- wraps a function f
michael@0 34 // WrapRunnableNMRet(f, ..., r) -- wraps a function f that returns something
michael@0 35 // that can be assigned to *r
michael@0 36 //
michael@0 37 // All of these template functions return a Runnable* which can be passed
michael@0 38 // to Dispatch().
michael@0 39 #include "runnable_utils_generated.h"
michael@0 40
michael@0 41 // Temporary hack. Really we want to have a template which will do this
michael@0 42 static inline nsresult RUN_ON_THREAD(nsIEventTarget *thread, nsIRunnable *runnable, uint32_t flags) {
michael@0 43 RefPtr<nsIRunnable> runnable_ref(runnable);
michael@0 44 if (thread) {
michael@0 45 bool on;
michael@0 46 nsresult rv;
michael@0 47 rv = thread->IsOnCurrentThread(&on);
michael@0 48
michael@0 49 // If the target thread has already shut down, we don't want to assert.
michael@0 50 if (rv != NS_ERROR_NOT_INITIALIZED) {
michael@0 51 MOZ_ASSERT(NS_SUCCEEDED(rv));
michael@0 52 }
michael@0 53
michael@0 54 NS_ENSURE_SUCCESS(rv, rv);
michael@0 55 if(!on) {
michael@0 56 return thread->Dispatch(runnable_ref, flags);
michael@0 57 }
michael@0 58 }
michael@0 59 return runnable_ref->Run();
michael@0 60 }
michael@0 61
michael@0 62 static inline nsresult RUN_ON_THREAD(nsIEventTarget *thread, runnable_args_base *runnable, uint32_t flags) {
michael@0 63 // Detect attempts to return a value when in async mode, since this
michael@0 64 // most likely means someone is trying to assign to a heap variable
michael@0 65 // which is now out of scope.
michael@0 66 MOZ_ASSERT((!(runnable->returns_value()) || (flags != NS_DISPATCH_NORMAL)));
michael@0 67
michael@0 68 return RUN_ON_THREAD(thread, static_cast<nsIRunnable *>(runnable), flags);
michael@0 69 }
michael@0 70
michael@0 71 #ifdef DEBUG
michael@0 72 #define ASSERT_ON_THREAD(t) do { \
michael@0 73 if (t) { \
michael@0 74 bool on; \
michael@0 75 nsresult rv; \
michael@0 76 rv = t->IsOnCurrentThread(&on); \
michael@0 77 MOZ_ASSERT(NS_SUCCEEDED(rv)); \
michael@0 78 MOZ_ASSERT(on); \
michael@0 79 } \
michael@0 80 } while(0)
michael@0 81 #else
michael@0 82 #define ASSERT_ON_THREAD(t)
michael@0 83 #endif
michael@0 84
michael@0 85 } /* namespace mozilla */
michael@0 86
michael@0 87 #endif

mercurial