|
1 /* vim:set ts=4 sw=4 sts=4 et cin: */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "nsProxyRelease.h" |
|
7 #include "nsThreadUtils.h" |
|
8 #include "nsAutoPtr.h" |
|
9 |
|
10 class nsProxyReleaseEvent : public nsRunnable |
|
11 { |
|
12 public: |
|
13 nsProxyReleaseEvent(nsISupports *doomed) |
|
14 : mDoomed(doomed) { |
|
15 } |
|
16 |
|
17 NS_IMETHOD Run() |
|
18 { |
|
19 mDoomed->Release(); |
|
20 return NS_OK; |
|
21 } |
|
22 |
|
23 private: |
|
24 nsISupports *mDoomed; |
|
25 }; |
|
26 |
|
27 nsresult |
|
28 NS_ProxyRelease(nsIEventTarget *target, nsISupports *doomed, |
|
29 bool alwaysProxy) |
|
30 { |
|
31 nsresult rv; |
|
32 |
|
33 if (!doomed) { |
|
34 // nothing to do |
|
35 return NS_OK; |
|
36 } |
|
37 |
|
38 if (!target) { |
|
39 NS_RELEASE(doomed); |
|
40 return NS_OK; |
|
41 } |
|
42 |
|
43 if (!alwaysProxy) { |
|
44 bool onCurrentThread = false; |
|
45 rv = target->IsOnCurrentThread(&onCurrentThread); |
|
46 if (NS_SUCCEEDED(rv) && onCurrentThread) { |
|
47 NS_RELEASE(doomed); |
|
48 return NS_OK; |
|
49 } |
|
50 } |
|
51 |
|
52 nsRefPtr<nsIRunnable> ev = new nsProxyReleaseEvent(doomed); |
|
53 if (!ev) { |
|
54 // we do not release doomed here since it may cause a delete on the |
|
55 // wrong thread. better to leak than crash. |
|
56 return NS_ERROR_OUT_OF_MEMORY; |
|
57 } |
|
58 |
|
59 rv = target->Dispatch(ev, NS_DISPATCH_NORMAL); |
|
60 if (NS_FAILED(rv)) { |
|
61 NS_WARNING("failed to post proxy release event"); |
|
62 // again, it is better to leak the doomed object than risk crashing as |
|
63 // a result of deleting it on the wrong thread. |
|
64 } |
|
65 return rv; |
|
66 } |