|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* vim: set ts=8 sts=4 et sw=4 tw=99: */ |
|
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 #ifndef xpcObjectHelper_h |
|
8 #define xpcObjectHelper_h |
|
9 |
|
10 // Including 'windows.h' will #define GetClassInfo to something else. |
|
11 #ifdef XP_WIN |
|
12 #ifdef GetClassInfo |
|
13 #undef GetClassInfo |
|
14 #endif |
|
15 #endif |
|
16 |
|
17 #include "mozilla/Attributes.h" |
|
18 #include <stdint.h> |
|
19 #include "nsAutoPtr.h" |
|
20 #include "nsCOMPtr.h" |
|
21 #include "nsIClassInfo.h" |
|
22 #include "nsISupports.h" |
|
23 #include "nsIXPCScriptable.h" |
|
24 #include "nsWrapperCache.h" |
|
25 |
|
26 class xpcObjectHelper |
|
27 { |
|
28 public: |
|
29 xpcObjectHelper(nsISupports *aObject, nsWrapperCache *aCache = nullptr) |
|
30 : mCanonical(nullptr) |
|
31 , mObject(aObject) |
|
32 , mCache(aCache) |
|
33 { |
|
34 if (!mCache) { |
|
35 if (aObject) |
|
36 CallQueryInterface(aObject, &mCache); |
|
37 else |
|
38 mCache = nullptr; |
|
39 } |
|
40 } |
|
41 |
|
42 nsISupports *Object() |
|
43 { |
|
44 return mObject; |
|
45 } |
|
46 |
|
47 nsISupports *GetCanonical() |
|
48 { |
|
49 if (!mCanonical) { |
|
50 mCanonicalStrong = do_QueryInterface(mObject); |
|
51 mCanonical = mCanonicalStrong; |
|
52 } |
|
53 return mCanonical; |
|
54 } |
|
55 |
|
56 already_AddRefed<nsISupports> forgetCanonical() |
|
57 { |
|
58 MOZ_ASSERT(mCanonical, "Huh, no canonical to forget?"); |
|
59 |
|
60 if (!mCanonicalStrong) |
|
61 mCanonicalStrong = mCanonical; |
|
62 mCanonical = nullptr; |
|
63 return mCanonicalStrong.forget(); |
|
64 } |
|
65 |
|
66 nsIClassInfo *GetClassInfo() |
|
67 { |
|
68 if (mXPCClassInfo) |
|
69 return mXPCClassInfo; |
|
70 if (!mClassInfo) |
|
71 mClassInfo = do_QueryInterface(mObject); |
|
72 return mClassInfo; |
|
73 } |
|
74 nsXPCClassInfo *GetXPCClassInfo() |
|
75 { |
|
76 if (!mXPCClassInfo) { |
|
77 CallQueryInterface(mObject, getter_AddRefs(mXPCClassInfo)); |
|
78 } |
|
79 return mXPCClassInfo; |
|
80 } |
|
81 |
|
82 already_AddRefed<nsXPCClassInfo> forgetXPCClassInfo() |
|
83 { |
|
84 GetXPCClassInfo(); |
|
85 |
|
86 return mXPCClassInfo.forget(); |
|
87 } |
|
88 |
|
89 // We assert that we can reach an nsIXPCScriptable somehow. |
|
90 uint32_t GetScriptableFlags() |
|
91 { |
|
92 // Try getting an nsXPCClassInfo - this handles DOM scriptable helpers. |
|
93 nsCOMPtr<nsIXPCScriptable> sinfo = GetXPCClassInfo(); |
|
94 |
|
95 // If that didn't work, try just QI-ing. This handles BackstagePass. |
|
96 if (!sinfo) |
|
97 sinfo = do_QueryInterface(GetCanonical()); |
|
98 |
|
99 // We should have something by now. |
|
100 MOZ_ASSERT(sinfo); |
|
101 |
|
102 // Grab the flags. |
|
103 return sinfo->GetScriptableFlags(); |
|
104 } |
|
105 |
|
106 nsWrapperCache *GetWrapperCache() |
|
107 { |
|
108 return mCache; |
|
109 } |
|
110 |
|
111 protected: |
|
112 xpcObjectHelper(nsISupports *aObject, nsISupports *aCanonical, |
|
113 nsWrapperCache *aCache) |
|
114 : mCanonical(aCanonical) |
|
115 , mObject(aObject) |
|
116 , mCache(aCache) |
|
117 { |
|
118 if (!mCache && aObject) |
|
119 CallQueryInterface(aObject, &mCache); |
|
120 } |
|
121 |
|
122 nsCOMPtr<nsISupports> mCanonicalStrong; |
|
123 nsISupports* mCanonical; |
|
124 |
|
125 private: |
|
126 xpcObjectHelper(xpcObjectHelper& aOther) MOZ_DELETE; |
|
127 |
|
128 nsISupports* mObject; |
|
129 nsWrapperCache* mCache; |
|
130 nsCOMPtr<nsIClassInfo> mClassInfo; |
|
131 nsRefPtr<nsXPCClassInfo> mXPCClassInfo; |
|
132 }; |
|
133 |
|
134 #endif |