media/mtransport/transportflow.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 transportflow_h__
michael@0 10 #define transportflow_h__
michael@0 11
michael@0 12 #include <deque>
michael@0 13 #include <queue>
michael@0 14 #include <string>
michael@0 15
michael@0 16 #include "nscore.h"
michael@0 17 #include "nsISupportsImpl.h"
michael@0 18 #include "mozilla/Scoped.h"
michael@0 19 #include "transportlayer.h"
michael@0 20 #include "m_cpp_utils.h"
michael@0 21 #include "nsAutoPtr.h"
michael@0 22
michael@0 23 // A stack of transport layers acts as a flow.
michael@0 24 // Generally, one reads and writes to the top layer.
michael@0 25
michael@0 26 // This code has a confusing hybrid threading model which
michael@0 27 // probably needs some eventual refactoring.
michael@0 28 // TODO(ekr@rtfm.com): Bug 844891
michael@0 29 //
michael@0 30 // TransportFlows are not inherently bound to a thread *but*
michael@0 31 // TransportLayers can be. If any layer in a flow is bound
michael@0 32 // to a given thread, then all layers in the flow MUST be
michael@0 33 // bound to that thread and you can only manipulate the
michael@0 34 // flow (push layers, write, etc.) on that thread.
michael@0 35 //
michael@0 36 // The sole official exception to this is that you are
michael@0 37 // allowed to *destroy* a flow off the bound thread provided
michael@0 38 // that there are no listeners on its signals. This exception
michael@0 39 // is designed to allow idioms where you create the flow
michael@0 40 // and then something goes wrong and you destroy it and
michael@0 41 // you don't want to bother with a thread dispatch.
michael@0 42 //
michael@0 43 // Eventually we hope to relax the "no listeners"
michael@0 44 // restriction by thread-locking the signals, but previous
michael@0 45 // attempts have caused deadlocks.
michael@0 46 //
michael@0 47 // Most of these invariants are enforced by hard asserts
michael@0 48 // (i.e., those which fire even in production builds).
michael@0 49
michael@0 50 namespace mozilla {
michael@0 51
michael@0 52 class TransportFlow : public nsISupports,
michael@0 53 public sigslot::has_slots<> {
michael@0 54 public:
michael@0 55 TransportFlow()
michael@0 56 : id_("(anonymous)"),
michael@0 57 state_(TransportLayer::TS_NONE),
michael@0 58 layers_(new std::deque<TransportLayer *>) {}
michael@0 59 TransportFlow(const std::string id)
michael@0 60 : id_(id),
michael@0 61 state_(TransportLayer::TS_NONE),
michael@0 62 layers_(new std::deque<TransportLayer *>) {}
michael@0 63
michael@0 64 ~TransportFlow();
michael@0 65
michael@0 66 const std::string& id() const { return id_; }
michael@0 67
michael@0 68 // Layer management. Note PushLayer() is not thread protected, so
michael@0 69 // either:
michael@0 70 // (a) Do it in the thread handling the I/O
michael@0 71 // (b) Do it before you activate the I/O system
michael@0 72 //
michael@0 73 // The flow takes ownership of the layers after a successful
michael@0 74 // push.
michael@0 75 nsresult PushLayer(TransportLayer *layer);
michael@0 76
michael@0 77 // Convenience function to push multiple layers on. Layers
michael@0 78 // are pushed on in the order that they are in the queue.
michael@0 79 // Any failures cause the flow to become inoperable and
michael@0 80 // destroys all the layers including those already pushed.
michael@0 81 // TODO(ekr@rtfm.com): Change layers to be ref-counted.
michael@0 82 nsresult PushLayers(nsAutoPtr<std::queue<TransportLayer *> > layers);
michael@0 83
michael@0 84 TransportLayer *top() const;
michael@0 85 TransportLayer *GetLayer(const std::string& id) const;
michael@0 86
michael@0 87 // Wrappers for whatever TLayer happens to be the top layer
michael@0 88 // at the time. This way you don't need to do top()->Foo().
michael@0 89 TransportLayer::State state(); // Current state
michael@0 90 TransportResult SendPacket(const unsigned char *data, size_t len);
michael@0 91
michael@0 92 // State has changed. Reflects the top flow.
michael@0 93 sigslot::signal2<TransportFlow *, TransportLayer::State>
michael@0 94 SignalStateChange;
michael@0 95
michael@0 96 // Data received on the flow
michael@0 97 sigslot::signal3<TransportFlow*, const unsigned char *, size_t>
michael@0 98 SignalPacketReceived;
michael@0 99
michael@0 100 bool Contains(TransportLayer *layer) const;
michael@0 101
michael@0 102 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 103
michael@0 104 private:
michael@0 105 DISALLOW_COPY_ASSIGN(TransportFlow);
michael@0 106
michael@0 107 // Check if we are on the right thread
michael@0 108 void CheckThread() const {
michael@0 109 if (!CheckThreadInt())
michael@0 110 MOZ_CRASH();
michael@0 111 }
michael@0 112
michael@0 113 bool CheckThreadInt() const {
michael@0 114 bool on;
michael@0 115
michael@0 116 if (!target_) // OK if no thread set.
michael@0 117 return true;
michael@0 118 if (NS_FAILED(target_->IsOnCurrentThread(&on)))
michael@0 119 return false;
michael@0 120
michael@0 121 return on;
michael@0 122 }
michael@0 123
michael@0 124 void EnsureSameThread(TransportLayer *layer);
michael@0 125
michael@0 126 void StateChange(TransportLayer *layer, TransportLayer::State state);
michael@0 127 void StateChangeInt(TransportLayer::State state);
michael@0 128 void PacketReceived(TransportLayer* layer, const unsigned char *data,
michael@0 129 size_t len);
michael@0 130 static void DestroyFinal(nsAutoPtr<std::deque<TransportLayer *> > layers);
michael@0 131
michael@0 132 // Overload needed because we use deque internally and queue externally.
michael@0 133 static void ClearLayers(std::deque<TransportLayer *>* layers);
michael@0 134 static void ClearLayers(std::queue<TransportLayer *>* layers);
michael@0 135
michael@0 136 std::string id_;
michael@0 137 TransportLayer::State state_;
michael@0 138 ScopedDeletePtr<std::deque<TransportLayer *> > layers_;
michael@0 139 nsCOMPtr<nsIEventTarget> target_;
michael@0 140 };
michael@0 141
michael@0 142 } // close namespace
michael@0 143 #endif

mercurial