Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=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
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsInterfaceRequestorAgg.h"
8 : #include "nsIInterfaceRequestor.h"
9 : #include "nsCOMPtr.h"
10 : #include "mozilla/Attributes.h"
11 : #include "nsThreadUtils.h"
12 : #include "nsProxyRelease.h"
13 :
14 : class nsInterfaceRequestorAgg final : public nsIInterfaceRequestor
15 : {
16 : public:
17 : // XXX This needs to support threadsafe refcounting until we fix bug 243591.
18 : NS_DECL_THREADSAFE_ISUPPORTS
19 : NS_DECL_NSIINTERFACEREQUESTOR
20 :
21 23 : nsInterfaceRequestorAgg(nsIInterfaceRequestor* aFirst,
22 : nsIInterfaceRequestor* aSecond,
23 : nsIEventTarget* aConsumerTarget = nullptr)
24 23 : : mFirst(aFirst)
25 : , mSecond(aSecond)
26 23 : , mConsumerTarget(aConsumerTarget)
27 : {
28 23 : if (!mConsumerTarget) {
29 13 : mConsumerTarget = GetCurrentThreadEventTarget();
30 : }
31 23 : }
32 :
33 : private:
34 : ~nsInterfaceRequestorAgg();
35 :
36 : nsCOMPtr<nsIInterfaceRequestor> mFirst, mSecond;
37 : nsCOMPtr<nsIEventTarget> mConsumerTarget;
38 : };
39 :
40 278 : NS_IMPL_ISUPPORTS(nsInterfaceRequestorAgg, nsIInterfaceRequestor)
41 :
42 : NS_IMETHODIMP
43 5 : nsInterfaceRequestorAgg::GetInterface(const nsIID& aIID, void** aResult)
44 : {
45 5 : nsresult rv = NS_ERROR_NO_INTERFACE;
46 5 : if (mFirst) {
47 5 : rv = mFirst->GetInterface(aIID, aResult);
48 : }
49 5 : if (mSecond && NS_FAILED(rv)) {
50 0 : rv = mSecond->GetInterface(aIID, aResult);
51 : }
52 5 : return rv;
53 : }
54 :
55 44 : nsInterfaceRequestorAgg::~nsInterfaceRequestorAgg()
56 : {
57 22 : NS_ProxyRelease(
58 44 : "nsInterfaceRequestorAgg::mFirst", mConsumerTarget, mFirst.forget());
59 22 : NS_ProxyRelease(
60 44 : "nsInterfaceRequestorAgg::mSecond", mConsumerTarget, mSecond.forget());
61 22 : }
62 :
63 : nsresult
64 5 : NS_NewInterfaceRequestorAggregation(nsIInterfaceRequestor* aFirst,
65 : nsIInterfaceRequestor* aSecond,
66 : nsIInterfaceRequestor** aResult)
67 : {
68 5 : *aResult = new nsInterfaceRequestorAgg(aFirst, aSecond);
69 5 : if (!*aResult) {
70 0 : return NS_ERROR_OUT_OF_MEMORY;
71 : }
72 5 : NS_ADDREF(*aResult);
73 5 : return NS_OK;
74 : }
75 :
76 : nsresult
77 18 : NS_NewInterfaceRequestorAggregation(nsIInterfaceRequestor* aFirst,
78 : nsIInterfaceRequestor* aSecond,
79 : nsIEventTarget* aTarget,
80 : nsIInterfaceRequestor** aResult)
81 : {
82 18 : *aResult = new nsInterfaceRequestorAgg(aFirst, aSecond, aTarget);
83 18 : if (!*aResult) {
84 0 : return NS_ERROR_OUT_OF_MEMORY;
85 : }
86 18 : NS_ADDREF(*aResult);
87 18 : return NS_OK;
88 : }
|