media/mtransport/transportlayer.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 // Original author: ekr@rtfm.com
     9 #ifndef transportlayer_h__
    10 #define transportlayer_h__
    12 #include "sigslot.h"
    14 #include "mozilla/DebugOnly.h"
    15 #include "mozilla/RefPtr.h"
    16 #include "nsCOMPtr.h"
    17 #include "nsIEventTarget.h"
    19 #include "m_cpp_utils.h"
    21 namespace mozilla {
    23 class TransportFlow;
    25 typedef int TransportResult;
    27 enum {
    28   TE_WOULDBLOCK = -1, TE_ERROR = -2, TE_INTERNAL = -3
    29 };
    31 #define TRANSPORT_LAYER_ID(name) \
    32   virtual const std::string id() { return name; } \
    33   static std::string ID() { return name; }
    35 // Abstract base class for network transport layers.
    36 class TransportLayer : public sigslot::has_slots<> {
    37  public:
    38   // The state of the transport flow
    39   // We can't use "ERROR" because Windows has a macro named "ERROR"
    40   enum State { TS_NONE, TS_INIT, TS_CONNECTING, TS_OPEN, TS_CLOSED, TS_ERROR };
    41   enum Mode { STREAM, DGRAM };
    43   // Is this a stream or datagram flow
    44   TransportLayer(Mode mode = STREAM) :
    45     mode_(mode),
    46     state_(TS_NONE),
    47     flow_id_(),
    48     downward_(nullptr) {}
    50   virtual ~TransportLayer() {}
    52   // Called to initialize
    53   nsresult Init();  // Called by Insert() to set up -- do not override
    54   virtual nsresult InitInternal() { return NS_OK; } // Called by Init
    56   // Called when inserted into a flow
    57   virtual void Inserted(TransportFlow *flow, TransportLayer *downward);
    59   // Downward interface
    60   TransportLayer *downward() { return downward_; }
    62   // Dispatch a call onto our thread (or run on the same thread if
    63   // thread is not set). This is always synchronous.
    64   nsresult RunOnThread(nsIRunnable *event);
    66   // Get the state
    67   State state() const { return state_; }
    68   // Must be implemented by derived classes
    69   virtual TransportResult SendPacket(const unsigned char *data, size_t len) = 0;
    71   // Get the thread.
    72   const nsCOMPtr<nsIEventTarget> GetThread() const {
    73     return target_;
    74   }
    76   // Event definitions that one can register for
    77   // State has changed
    78   sigslot::signal2<TransportLayer*, State> SignalStateChange;
    79   // Data received on the flow
    80   sigslot::signal3<TransportLayer*, const unsigned char *, size_t>
    81                          SignalPacketReceived;
    83   // Return the layer id for this layer
    84   virtual const std::string id() = 0;
    86   // The id of the flow
    87   const std::string& flow_id() {
    88     return flow_id_;
    89   }
    91  protected:
    92   virtual void WasInserted() {}
    93   virtual void SetState(State state, const char *file, unsigned line);
    94   // Check if we are on the right thread
    95   void CheckThread() {
    96     NS_ABORT_IF_FALSE(CheckThreadInt(), "Wrong thread");
    97   }
    99   Mode mode_;
   100   State state_;
   101   std::string flow_id_;
   102   TransportLayer *downward_; // The next layer in the stack
   103   nsCOMPtr<nsIEventTarget> target_;
   105  private:
   106   DISALLOW_COPY_ASSIGN(TransportLayer);
   108   bool CheckThreadInt() {
   109     bool on;
   111     if (!target_)  // OK if no thread set.
   112       return true;
   114     NS_ENSURE_SUCCESS(target_->IsOnCurrentThread(&on), false);
   115     NS_ENSURE_TRUE(on, false);
   117     return true;
   118   }
   119 };
   121 #define LAYER_INFO "Flow[" << flow_id() << "(none)" << "]; Layer[" << id() << "]: "
   122 #define TL_SET_STATE(x) SetState((x), __FILE__, __LINE__)
   124 }  // close namespace
   125 #endif

mercurial