/src/mozilla-central/media/mtransport/transportflow.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | // Original author: ekr@rtfm.com |
8 | | #include <deque> |
9 | | |
10 | | #include "logging.h" |
11 | | #include "runnable_utils.h" |
12 | | #include "transportflow.h" |
13 | | #include "transportlayer.h" |
14 | | |
15 | | namespace mozilla { |
16 | | |
17 | | NS_IMPL_ISUPPORTS0(TransportFlow) |
18 | | |
19 | | // There are some hacks here to allow destruction off of |
20 | | // the main thread. |
21 | 0 | TransportFlow::~TransportFlow() { |
22 | 0 | // Push the destruction onto the STS thread. Note that there |
23 | 0 | // is still some possibility that someone is accessing this |
24 | 0 | // object simultaneously, but as long as smart pointer discipline |
25 | 0 | // is maintained, it shouldn't be possible to access and |
26 | 0 | // destroy it simultaneously. The conversion to an nsAutoPtr |
27 | 0 | // ensures automatic destruction of the queue at exit of |
28 | 0 | // DestroyFinal. |
29 | 0 | if (target_) { |
30 | 0 | nsAutoPtr<std::deque<TransportLayer*>> layers_tmp(layers_.release()); |
31 | 0 | RUN_ON_THREAD(target_, |
32 | 0 | WrapRunnableNM(&TransportFlow::DestroyFinal, layers_tmp), |
33 | 0 | NS_DISPATCH_NORMAL); |
34 | 0 | } |
35 | 0 | } |
36 | | |
37 | 0 | void TransportFlow::DestroyFinal(nsAutoPtr<std::deque<TransportLayer *> > layers) { |
38 | 0 | ClearLayers(layers.get()); |
39 | 0 | } |
40 | | |
41 | 0 | void TransportFlow::ClearLayers(std::deque<TransportLayer *>* layers) { |
42 | 0 | while (!layers->empty()) { |
43 | 0 | delete layers->front(); |
44 | 0 | layers->pop_front(); |
45 | 0 | } |
46 | 0 | } |
47 | | |
48 | 0 | void TransportFlow::PushLayer(TransportLayer* layer) { |
49 | 0 | CheckThread(); |
50 | 0 | layers_->push_front(layer); |
51 | 0 | EnsureSameThread(layer); |
52 | 0 | layer->SetFlowId(id_); |
53 | 0 | } |
54 | | |
55 | 0 | TransportLayer *TransportFlow::GetLayer(const std::string& id) const { |
56 | 0 | CheckThread(); |
57 | 0 |
|
58 | 0 | if (layers_) { |
59 | 0 | for (TransportLayer* layer : *layers_) { |
60 | 0 | if (layer->id() == id) |
61 | 0 | return layer; |
62 | 0 | } |
63 | 0 | } |
64 | 0 |
|
65 | 0 | return nullptr; |
66 | 0 | } |
67 | | |
68 | 0 | void TransportFlow::EnsureSameThread(TransportLayer *layer) { |
69 | 0 | // Enforce that if any of the layers have a thread binding, |
70 | 0 | // they all have the same binding. |
71 | 0 | if (target_) { |
72 | 0 | const nsCOMPtr<nsIEventTarget>& lthread = layer->GetThread(); |
73 | 0 |
|
74 | 0 | if (lthread && (lthread != target_)) |
75 | 0 | MOZ_CRASH(); |
76 | 0 | } |
77 | 0 | else { |
78 | 0 | target_ = layer->GetThread(); |
79 | 0 | } |
80 | 0 | } |
81 | | |
82 | | } // close namespace |