js/xpconnect/src/xpcObjectHelper.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     7 #ifndef xpcObjectHelper_h
     8 #define xpcObjectHelper_h
    10 // Including 'windows.h' will #define GetClassInfo to something else.
    11 #ifdef XP_WIN
    12 #ifdef GetClassInfo
    13 #undef GetClassInfo
    14 #endif
    15 #endif
    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"
    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     }
    42     nsISupports *Object()
    43     {
    44         return mObject;
    45     }
    47     nsISupports *GetCanonical()
    48     {
    49         if (!mCanonical) {
    50             mCanonicalStrong = do_QueryInterface(mObject);
    51             mCanonical = mCanonicalStrong;
    52         }
    53         return mCanonical;
    54     }
    56     already_AddRefed<nsISupports> forgetCanonical()
    57     {
    58         MOZ_ASSERT(mCanonical, "Huh, no canonical to forget?");
    60         if (!mCanonicalStrong)
    61             mCanonicalStrong = mCanonical;
    62         mCanonical = nullptr;
    63         return mCanonicalStrong.forget();
    64     }
    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     }
    82     already_AddRefed<nsXPCClassInfo> forgetXPCClassInfo()
    83     {
    84         GetXPCClassInfo();
    86         return mXPCClassInfo.forget();
    87     }
    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();
    95         // If that didn't work, try just QI-ing. This handles BackstagePass.
    96         if (!sinfo)
    97             sinfo = do_QueryInterface(GetCanonical());
    99         // We should have something by now.
   100         MOZ_ASSERT(sinfo);
   102         // Grab the flags.
   103         return sinfo->GetScriptableFlags();
   104     }
   106     nsWrapperCache *GetWrapperCache()
   107     {
   108         return mCache;
   109     }
   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     }
   122     nsCOMPtr<nsISupports>    mCanonicalStrong;
   123     nsISupports*             mCanonical;
   125 private:
   126     xpcObjectHelper(xpcObjectHelper& aOther) MOZ_DELETE;
   128     nsISupports*             mObject;
   129     nsWrapperCache*          mCache;
   130     nsCOMPtr<nsIClassInfo>   mClassInfo;
   131     nsRefPtr<nsXPCClassInfo> mXPCClassInfo;
   132 };
   134 #endif

mercurial