1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 /* entry point wrappers. */
7 
8 #include "xptcprivate.h"
9 #include "xptiprivate.h"
10 #include "mozilla/XPTInterfaceInfoManager.h"
11 #include "nsPrintfCString.h"
12 
13 using namespace mozilla;
14 
15 NS_IMETHODIMP
QueryInterface(REFNSIID aIID,void ** aInstancePtr)16 nsXPTCStubBase::QueryInterface(REFNSIID aIID, void** aInstancePtr) {
17   if (aIID.Equals(mEntry->IID())) {
18     NS_ADDREF_THIS();
19     *aInstancePtr = static_cast<nsISupports*>(this);
20     return NS_OK;
21   }
22 
23   return mOuter->QueryInterface(aIID, aInstancePtr);
24 }
25 
NS_IMETHODIMP_(MozExternalRefCountType)26 NS_IMETHODIMP_(MozExternalRefCountType)
27 nsXPTCStubBase::AddRef() { return mOuter->AddRef(); }
28 
NS_IMETHODIMP_(MozExternalRefCountType)29 NS_IMETHODIMP_(MozExternalRefCountType)
30 nsXPTCStubBase::Release() { return mOuter->Release(); }
31 
32 EXPORT_XPCOM_API(nsresult)
NS_GetXPTCallStub(REFNSIID aIID,nsIXPTCProxy * aOuter,nsISomeInterface ** aResult)33 NS_GetXPTCallStub(REFNSIID aIID, nsIXPTCProxy* aOuter,
34                   nsISomeInterface** aResult) {
35   if (NS_WARN_IF(!aOuter) || NS_WARN_IF(!aResult)) return NS_ERROR_INVALID_ARG;
36 
37   XPTInterfaceInfoManager* iim = XPTInterfaceInfoManager::GetSingleton();
38   if (NS_WARN_IF(!iim)) return NS_ERROR_NOT_INITIALIZED;
39 
40   xptiInterfaceEntry* iie = iim->GetInterfaceEntryForIID(&aIID);
41   if (!iie || !iie->EnsureResolved() || iie->GetBuiltinClassFlag())
42     return NS_ERROR_FAILURE;
43 
44   if (iie->GetHasNotXPCOMFlag()) {
45 #ifdef DEBUG
46     nsPrintfCString msg(
47         "XPTCall will not implement interface %s because of [notxpcom] "
48         "members.",
49         iie->GetTheName());
50     NS_WARNING(msg.get());
51 #endif
52     return NS_ERROR_FAILURE;
53   }
54 
55   *aResult = new nsXPTCStubBase(aOuter, iie);
56   return NS_OK;
57 }
58 
59 EXPORT_XPCOM_API(void)
NS_DestroyXPTCallStub(nsISomeInterface * aStub)60 NS_DestroyXPTCallStub(nsISomeInterface* aStub) {
61   nsXPTCStubBase* stub = static_cast<nsXPTCStubBase*>(aStub);
62   delete (stub);
63 }
64 
65 EXPORT_XPCOM_API(size_t)
NS_SizeOfIncludingThisXPTCallStub(const nsISomeInterface * aStub,mozilla::MallocSizeOf aMallocSizeOf)66 NS_SizeOfIncludingThisXPTCallStub(const nsISomeInterface* aStub,
67                                   mozilla::MallocSizeOf aMallocSizeOf) {
68   // We could cast aStub to nsXPTCStubBase, but that class doesn't seem to own
69   // anything, so just measure the size of the object itself.
70   return aMallocSizeOf(aStub);
71 }
72