2012-10-03 00:04:58 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
// Original author: ekr@rtfm.com
|
|
|
|
#include <deque>
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
#include "logging.h"
|
2013-02-19 15:43:34 +04:00
|
|
|
#include "runnable_utils.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "transportflow.h"
|
|
|
|
#include "transportlayer.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-03-15 23:00:17 +04:00
|
|
|
NS_IMPL_ISUPPORTS0(TransportFlow)
|
|
|
|
|
2013-02-19 15:43:34 +04:00
|
|
|
// There are some hacks here to allow destruction off of
|
|
|
|
// the main thread.
|
2012-10-03 00:04:58 +04:00
|
|
|
TransportFlow::~TransportFlow() {
|
2013-02-19 15:43:34 +04:00
|
|
|
// Push the destruction onto the STS thread. Note that there
|
|
|
|
// is still some possibility that someone is accessing this
|
|
|
|
// object simultaneously, but as long as smart pointer discipline
|
|
|
|
// is maintained, it shouldn't be possible to access and
|
|
|
|
// destroy it simultaneously. The conversion to an nsAutoPtr
|
|
|
|
// ensures automatic destruction of the queue at exit of
|
|
|
|
// DestroyFinal.
|
2018-01-25 18:55:12 +03:00
|
|
|
if (target_) {
|
|
|
|
nsAutoPtr<std::deque<TransportLayer*>> layers_tmp(layers_.release());
|
|
|
|
RUN_ON_THREAD(target_,
|
|
|
|
WrapRunnableNM(&TransportFlow::DestroyFinal, layers_tmp),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2013-02-19 15:43:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransportFlow::DestroyFinal(nsAutoPtr<std::deque<TransportLayer *> > layers) {
|
2016-03-02 23:28:27 +03:00
|
|
|
ClearLayers(layers.get());
|
2013-02-19 15:43:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransportFlow::ClearLayers(std::deque<TransportLayer *>* layers) {
|
|
|
|
while (!layers->empty()) {
|
|
|
|
delete layers->front();
|
|
|
|
layers->pop_front();
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-28 00:57:28 +03:00
|
|
|
void TransportFlow::PushLayer(TransportLayer* layer) {
|
2013-02-19 15:43:34 +04:00
|
|
|
CheckThread();
|
2018-04-27 20:14:40 +03:00
|
|
|
layers_->push_front(layer);
|
|
|
|
EnsureSameThread(layer);
|
2018-04-28 00:57:28 +03:00
|
|
|
layer->SetFlowId(id_);
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TransportLayer *TransportFlow::GetLayer(const std::string& id) const {
|
2013-02-19 15:43:34 +04:00
|
|
|
CheckThread();
|
|
|
|
|
2014-01-15 04:29:42 +04:00
|
|
|
if (layers_) {
|
2018-04-27 20:14:40 +03:00
|
|
|
for (TransportLayer* layer : *layers_) {
|
|
|
|
if (layer->id() == id)
|
|
|
|
return layer;
|
2014-01-15 04:29:42 +04:00
|
|
|
}
|
|
|
|
}
|
2018-04-27 20:14:40 +03:00
|
|
|
|
|
|
|
return nullptr;
|
2014-01-15 04:29:42 +04:00
|
|
|
}
|
|
|
|
|
2013-02-19 15:43:34 +04:00
|
|
|
void TransportFlow::EnsureSameThread(TransportLayer *layer) {
|
|
|
|
// Enforce that if any of the layers have a thread binding,
|
|
|
|
// they all have the same binding.
|
|
|
|
if (target_) {
|
|
|
|
const nsCOMPtr<nsIEventTarget>& lthread = layer->GetThread();
|
|
|
|
|
|
|
|
if (lthread && (lthread != target_))
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
target_ = layer->GetThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
} // close namespace
|