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 #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 "nsCOMPtr.h"
20 #include "nsIClassInfo.h"
21 #include "nsISupports.h"
22 #include "nsIXPCScriptable.h"
23 #include "nsWrapperCache.h"
24 
25 class xpcObjectHelper {
26  public:
27   explicit xpcObjectHelper(nsISupports* aObject,
28                            nsWrapperCache* aCache = nullptr)
mObject(aObject)29       : mObject(aObject), mCache(aCache) {
30     if (!mCache && aObject) {
31       CallQueryInterface(aObject, &mCache);
32     }
33   }
34 
Object()35   nsISupports* Object() { return mObject; }
36 
GetClassInfo()37   nsIClassInfo* GetClassInfo() {
38     if (!mClassInfo) {
39       mClassInfo = do_QueryInterface(mObject);
40     }
41     return mClassInfo;
42   }
43 
44   // We assert that we can reach an nsIXPCScriptable somehow.
GetScriptableFlags()45   uint32_t GetScriptableFlags() {
46     nsCOMPtr<nsIXPCScriptable> sinfo = do_QueryInterface(mObject);
47 
48     // We should have something by now.
49     MOZ_ASSERT(sinfo);
50 
51     // Grab the flags.
52     return sinfo->GetScriptableFlags();
53   }
54 
GetWrapperCache()55   nsWrapperCache* GetWrapperCache() { return mCache; }
56 
57  private:
58   xpcObjectHelper(xpcObjectHelper& aOther) = delete;
59 
60   nsISupports* MOZ_UNSAFE_REF(
61       "xpcObjectHelper has been specifically optimized "
62       "to avoid unnecessary AddRefs and Releases. "
63       "(see bug 565742)") mObject;
64   nsWrapperCache* mCache;
65   nsCOMPtr<nsIClassInfo> mClassInfo;
66 };
67 
68 #endif
69