1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/xpconnect/src/xpcObjectHelper.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,134 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* vim: set ts=8 sts=4 et sw=4 tw=99: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef xpcObjectHelper_h 1.11 +#define xpcObjectHelper_h 1.12 + 1.13 +// Including 'windows.h' will #define GetClassInfo to something else. 1.14 +#ifdef XP_WIN 1.15 +#ifdef GetClassInfo 1.16 +#undef GetClassInfo 1.17 +#endif 1.18 +#endif 1.19 + 1.20 +#include "mozilla/Attributes.h" 1.21 +#include <stdint.h> 1.22 +#include "nsAutoPtr.h" 1.23 +#include "nsCOMPtr.h" 1.24 +#include "nsIClassInfo.h" 1.25 +#include "nsISupports.h" 1.26 +#include "nsIXPCScriptable.h" 1.27 +#include "nsWrapperCache.h" 1.28 + 1.29 +class xpcObjectHelper 1.30 +{ 1.31 +public: 1.32 + xpcObjectHelper(nsISupports *aObject, nsWrapperCache *aCache = nullptr) 1.33 + : mCanonical(nullptr) 1.34 + , mObject(aObject) 1.35 + , mCache(aCache) 1.36 + { 1.37 + if (!mCache) { 1.38 + if (aObject) 1.39 + CallQueryInterface(aObject, &mCache); 1.40 + else 1.41 + mCache = nullptr; 1.42 + } 1.43 + } 1.44 + 1.45 + nsISupports *Object() 1.46 + { 1.47 + return mObject; 1.48 + } 1.49 + 1.50 + nsISupports *GetCanonical() 1.51 + { 1.52 + if (!mCanonical) { 1.53 + mCanonicalStrong = do_QueryInterface(mObject); 1.54 + mCanonical = mCanonicalStrong; 1.55 + } 1.56 + return mCanonical; 1.57 + } 1.58 + 1.59 + already_AddRefed<nsISupports> forgetCanonical() 1.60 + { 1.61 + MOZ_ASSERT(mCanonical, "Huh, no canonical to forget?"); 1.62 + 1.63 + if (!mCanonicalStrong) 1.64 + mCanonicalStrong = mCanonical; 1.65 + mCanonical = nullptr; 1.66 + return mCanonicalStrong.forget(); 1.67 + } 1.68 + 1.69 + nsIClassInfo *GetClassInfo() 1.70 + { 1.71 + if (mXPCClassInfo) 1.72 + return mXPCClassInfo; 1.73 + if (!mClassInfo) 1.74 + mClassInfo = do_QueryInterface(mObject); 1.75 + return mClassInfo; 1.76 + } 1.77 + nsXPCClassInfo *GetXPCClassInfo() 1.78 + { 1.79 + if (!mXPCClassInfo) { 1.80 + CallQueryInterface(mObject, getter_AddRefs(mXPCClassInfo)); 1.81 + } 1.82 + return mXPCClassInfo; 1.83 + } 1.84 + 1.85 + already_AddRefed<nsXPCClassInfo> forgetXPCClassInfo() 1.86 + { 1.87 + GetXPCClassInfo(); 1.88 + 1.89 + return mXPCClassInfo.forget(); 1.90 + } 1.91 + 1.92 + // We assert that we can reach an nsIXPCScriptable somehow. 1.93 + uint32_t GetScriptableFlags() 1.94 + { 1.95 + // Try getting an nsXPCClassInfo - this handles DOM scriptable helpers. 1.96 + nsCOMPtr<nsIXPCScriptable> sinfo = GetXPCClassInfo(); 1.97 + 1.98 + // If that didn't work, try just QI-ing. This handles BackstagePass. 1.99 + if (!sinfo) 1.100 + sinfo = do_QueryInterface(GetCanonical()); 1.101 + 1.102 + // We should have something by now. 1.103 + MOZ_ASSERT(sinfo); 1.104 + 1.105 + // Grab the flags. 1.106 + return sinfo->GetScriptableFlags(); 1.107 + } 1.108 + 1.109 + nsWrapperCache *GetWrapperCache() 1.110 + { 1.111 + return mCache; 1.112 + } 1.113 + 1.114 +protected: 1.115 + xpcObjectHelper(nsISupports *aObject, nsISupports *aCanonical, 1.116 + nsWrapperCache *aCache) 1.117 + : mCanonical(aCanonical) 1.118 + , mObject(aObject) 1.119 + , mCache(aCache) 1.120 + { 1.121 + if (!mCache && aObject) 1.122 + CallQueryInterface(aObject, &mCache); 1.123 + } 1.124 + 1.125 + nsCOMPtr<nsISupports> mCanonicalStrong; 1.126 + nsISupports* mCanonical; 1.127 + 1.128 +private: 1.129 + xpcObjectHelper(xpcObjectHelper& aOther) MOZ_DELETE; 1.130 + 1.131 + nsISupports* mObject; 1.132 + nsWrapperCache* mCache; 1.133 + nsCOMPtr<nsIClassInfo> mClassInfo; 1.134 + nsRefPtr<nsXPCClassInfo> mXPCClassInfo; 1.135 +}; 1.136 + 1.137 +#endif