|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "nsInterfaceRequestorAgg.h" |
|
6 #include "nsIInterfaceRequestor.h" |
|
7 #include "nsCOMPtr.h" |
|
8 #include "mozilla/Attributes.h" |
|
9 #include "nsThreadUtils.h" |
|
10 #include "nsProxyRelease.h" |
|
11 |
|
12 class nsInterfaceRequestorAgg MOZ_FINAL : public nsIInterfaceRequestor |
|
13 { |
|
14 public: |
|
15 // XXX This needs to support threadsafe refcounting until we fix bug 243591. |
|
16 NS_DECL_THREADSAFE_ISUPPORTS |
|
17 NS_DECL_NSIINTERFACEREQUESTOR |
|
18 |
|
19 nsInterfaceRequestorAgg(nsIInterfaceRequestor *aFirst, |
|
20 nsIInterfaceRequestor *aSecond, |
|
21 nsIEventTarget *aConsumerTarget = nullptr) |
|
22 : mFirst(aFirst) |
|
23 , mSecond(aSecond) |
|
24 , mConsumerTarget(aConsumerTarget) |
|
25 { |
|
26 if (!mConsumerTarget) { |
|
27 mConsumerTarget = NS_GetCurrentThread(); |
|
28 } |
|
29 } |
|
30 ~nsInterfaceRequestorAgg(); |
|
31 |
|
32 private: |
|
33 nsCOMPtr<nsIInterfaceRequestor> mFirst, mSecond; |
|
34 nsCOMPtr<nsIEventTarget> mConsumerTarget; |
|
35 }; |
|
36 |
|
37 NS_IMPL_ISUPPORTS(nsInterfaceRequestorAgg, nsIInterfaceRequestor) |
|
38 |
|
39 NS_IMETHODIMP |
|
40 nsInterfaceRequestorAgg::GetInterface(const nsIID &aIID, void **aResult) |
|
41 { |
|
42 nsresult rv = NS_ERROR_NO_INTERFACE; |
|
43 if (mFirst) |
|
44 rv = mFirst->GetInterface(aIID, aResult); |
|
45 if (mSecond && NS_FAILED(rv)) |
|
46 rv = mSecond->GetInterface(aIID, aResult); |
|
47 return rv; |
|
48 } |
|
49 |
|
50 nsInterfaceRequestorAgg::~nsInterfaceRequestorAgg() |
|
51 { |
|
52 nsIInterfaceRequestor* iir = nullptr; |
|
53 mFirst.swap(iir); |
|
54 if (iir) { |
|
55 NS_ProxyRelease(mConsumerTarget, iir); |
|
56 } |
|
57 iir = nullptr; |
|
58 mSecond.swap(iir); |
|
59 if (iir) { |
|
60 NS_ProxyRelease(mConsumerTarget, iir); |
|
61 } |
|
62 } |
|
63 |
|
64 nsresult |
|
65 NS_NewInterfaceRequestorAggregation(nsIInterfaceRequestor *aFirst, |
|
66 nsIInterfaceRequestor *aSecond, |
|
67 nsIInterfaceRequestor **aResult) |
|
68 { |
|
69 *aResult = new nsInterfaceRequestorAgg(aFirst, aSecond); |
|
70 if (!*aResult) |
|
71 return NS_ERROR_OUT_OF_MEMORY; |
|
72 NS_ADDREF(*aResult); |
|
73 return NS_OK; |
|
74 } |
|
75 |
|
76 nsresult |
|
77 NS_NewInterfaceRequestorAggregation(nsIInterfaceRequestor *aFirst, |
|
78 nsIInterfaceRequestor *aSecond, |
|
79 nsIEventTarget* aTarget, |
|
80 nsIInterfaceRequestor **aResult) |
|
81 { |
|
82 *aResult = new nsInterfaceRequestorAgg(aFirst, aSecond, aTarget); |
|
83 if (!*aResult) |
|
84 return NS_ERROR_OUT_OF_MEMORY; |
|
85 NS_ADDREF(*aResult); |
|
86 return NS_OK; |
|
87 } |