media/mtransport/transportlayerloopback.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 // Original author: ekr@rtfm.com
michael@0 8
michael@0 9 #include "logging.h"
michael@0 10 #include "nspr.h"
michael@0 11 #include "prlock.h"
michael@0 12
michael@0 13 #include "nsNetCID.h"
michael@0 14 #include "nsIComponentManager.h"
michael@0 15 #include "nsComponentManagerUtils.h"
michael@0 16 #include "nsIComponentRegistrar.h"
michael@0 17 #include "nsIEventTarget.h"
michael@0 18 #include "nsIIOService.h"
michael@0 19 #include "nsIServiceManager.h"
michael@0 20 #include "nsISocketTransportService.h"
michael@0 21 #include "nsServiceManagerUtils.h"
michael@0 22
michael@0 23 #include "transportflow.h"
michael@0 24 #include "transportlayerloopback.h"
michael@0 25
michael@0 26 namespace mozilla {
michael@0 27
michael@0 28 MOZ_MTLOG_MODULE("mtransport")
michael@0 29
michael@0 30 nsresult TransportLayerLoopback::Init() {
michael@0 31 timer_ = do_CreateInstance(NS_TIMER_CONTRACTID);
michael@0 32 MOZ_ASSERT(timer_);
michael@0 33 if (!timer_)
michael@0 34 return NS_ERROR_FAILURE;
michael@0 35
michael@0 36 nsresult rv;
michael@0 37 target_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
michael@0 38 MOZ_ASSERT(NS_SUCCEEDED(rv));
michael@0 39 if (!NS_SUCCEEDED(rv))
michael@0 40 return rv;
michael@0 41
michael@0 42 timer_->SetTarget(target_);
michael@0 43
michael@0 44 packets_lock_ = PR_NewLock();
michael@0 45 MOZ_ASSERT(packets_lock_);
michael@0 46 if (!packets_lock_)
michael@0 47 return NS_ERROR_FAILURE;
michael@0 48
michael@0 49 deliverer_ = new Deliverer(this);
michael@0 50
michael@0 51 timer_->InitWithCallback(deliverer_, 100, nsITimer::TYPE_REPEATING_SLACK);
michael@0 52
michael@0 53 return NS_OK;
michael@0 54 }
michael@0 55
michael@0 56 // Connect to the other side
michael@0 57 void TransportLayerLoopback::Connect(TransportLayerLoopback* peer) {
michael@0 58 peer_ = peer;
michael@0 59
michael@0 60 TL_SET_STATE(TS_OPEN);
michael@0 61 }
michael@0 62
michael@0 63 TransportResult
michael@0 64 TransportLayerLoopback::SendPacket(const unsigned char *data, size_t len) {
michael@0 65 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "SendPacket(" << len << ")");
michael@0 66
michael@0 67 if (!peer_) {
michael@0 68 MOZ_MTLOG(ML_ERROR, "Discarding packet because peer not attached");
michael@0 69 return TE_ERROR;
michael@0 70 }
michael@0 71
michael@0 72 nsresult res = peer_->QueuePacket(data, len);
michael@0 73 if (!NS_SUCCEEDED(res))
michael@0 74 return TE_ERROR;
michael@0 75
michael@0 76 return static_cast<TransportResult>(len);
michael@0 77 }
michael@0 78
michael@0 79 nsresult TransportLayerLoopback::QueuePacket(const unsigned char *data,
michael@0 80 size_t len) {
michael@0 81 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << " Enqueuing packet of length " << len);
michael@0 82 MOZ_ASSERT(packets_lock_);
michael@0 83
michael@0 84 PR_Lock(packets_lock_);
michael@0 85
michael@0 86 packets_.push(new QueuedPacket());
michael@0 87 packets_.back()->Assign(data, len);
michael@0 88
michael@0 89 PRStatus r = PR_Unlock(packets_lock_);
michael@0 90 MOZ_ASSERT(r == PR_SUCCESS);
michael@0 91 if (r != PR_SUCCESS)
michael@0 92 return NS_ERROR_FAILURE;
michael@0 93
michael@0 94 return NS_OK;
michael@0 95 }
michael@0 96
michael@0 97
michael@0 98 void TransportLayerLoopback::DeliverPackets() {
michael@0 99 while (!packets_.empty()) {
michael@0 100 QueuedPacket *packet = packets_.front();
michael@0 101 packets_.pop();
michael@0 102
michael@0 103 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << " Delivering packet of length " <<
michael@0 104 packet->len());
michael@0 105 SignalPacketReceived(this, packet->data(), packet->len());
michael@0 106
michael@0 107 delete packet;
michael@0 108 }
michael@0 109 }
michael@0 110
michael@0 111 NS_IMPL_ISUPPORTS(TransportLayerLoopback::Deliverer, nsITimerCallback)
michael@0 112
michael@0 113 NS_IMETHODIMP TransportLayerLoopback::Deliverer::Notify(nsITimer *timer) {
michael@0 114 if (!layer_)
michael@0 115 return NS_OK;
michael@0 116
michael@0 117 layer_->DeliverPackets();
michael@0 118
michael@0 119 return NS_OK;
michael@0 120 }
michael@0 121 } // close namespace

mercurial