DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Untracked file

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Pierre Phaneuf <pp@ludusdesign.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either of the GNU General Public License Version 2 or later (the "GPL"),
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "prprf.h"
#include "prmem.h"

#include "nscore.h"
#include "nsProxyEvent.h"
#include "nsIProxyObjectManager.h"
#include "nsProxyEventPrivate.h"

#include "nsIEventQueueService.h"
#include "nsServiceManagerUtils.h"

#include "nsHashtable.h"

#include "nsIInterfaceInfoManager.h"
#include "xptcall.h"

#include "nsAutoLock.h"

static NS_DEFINE_IID(kProxyObject_Identity_Class_IID, NS_PROXYEVENT_IDENTITY_CLASS_IID);
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);

////////////////////////////////////////////////////////////////////////////////

class nsProxyEventKey : public nsHashKey
{
public:
    nsProxyEventKey(void* rootObjectKey, void* destQueueKey, PRInt32 proxyType)
        : mRootObjectKey(rootObjectKey), mDestQueueKey(destQueueKey), mProxyType(proxyType) {
    }
  
    PRUint32 HashCode(void) const {
        return NS_PTR_TO_INT32(mRootObjectKey) ^ 
            NS_PTR_TO_INT32(mDestQueueKey) ^ mProxyType;
    }

    PRBool Equals(const nsHashKey *aKey) const {
        const nsProxyEventKey* other = (const nsProxyEventKey*)aKey;
        return mRootObjectKey == other->mRootObjectKey
            && mDestQueueKey == other->mDestQueueKey
            && mProxyType == other->mProxyType;
    }

    nsHashKey *Clone() const {
        return new nsProxyEventKey(mRootObjectKey, mDestQueueKey, mProxyType);
    }

protected:
    void*       mRootObjectKey;
    void*       mDestQueueKey;
    PRInt32     mProxyType;
};

////////////////////////////////////////////////////////////////////////////////

#ifdef DEBUG_xpcom_proxy
static PRMonitor* mon = nsnull;
static PRUint32 totalProxyObjects = 0;
static PRUint32 outstandingProxyObjects = 0;

void
nsProxyEventObject::DebugDump(const char * message, PRUint32 hashKey)
{

    if (mon == nsnull)
    {
        mon = PR_NewMonitor();
    }

    PR_EnterMonitor(mon);

    if (message)
    {
        printf("\n-=-=-=-=-=-=-=-=-=-=-=-=-\n");
        printf("%s\n", message);

        if(strcmp(message, "Create") == 0)
        {
            totalProxyObjects++;
            outstandingProxyObjects++;
        }
        else if(strcmp(message, "Delete") == 0)
        {
            outstandingProxyObjects--;
        }
    }
    printf("nsProxyEventObject @ %x with mRefCnt = %d\n", this, mRefCnt);

    PRBool isRoot = mRoot == nsnull;
    printf("%s wrapper around  @ %x\n", isRoot ? "ROOT":"non-root\n", GetRealObject());

    nsCOMPtr<nsISupports> rootObject = do_QueryInterface(mProxyObject->mRealObject);
    nsCOMPtr<nsISupports> rootQueue = do_QueryInterface(mProxyObject->mDestQueue);
    nsProxyEventKey key(rootObject, rootQueue, mProxyObject->mProxyType);
    printf("Hashkey: %d\n", key.HashCode());
        
    char* name;
    GetClass()->GetInterfaceInfo()->GetName(&name);
    printf("interface name is %s\n", name);
    if(name)
        nsMemory::Free(name);
    char * iid = GetClass()->GetProxiedIID().ToString();
    printf("IID number is %s\n", iid);
    delete iid;
    printf("nsProxyEventClass @ %x\n", mClass);
    
    if(mNext)
    {
        if(isRoot)
        {
            printf("Additional wrappers for this object...\n");
        }
        mNext->DebugDump(nsnull, 0);
    }

    printf("[proxyobjects] %d total used in system, %d outstading\n", totalProxyObjects, outstandingProxyObjects);

    if (message)
        printf("-=-=-=-=-=-=-=-=-=-=-=-=-\n");

    PR_ExitMonitor(mon);
}
#endif



//////////////////////////////////////////////////////////////////////////////////////////////////
//
//  nsProxyEventObject
//
//////////////////////////////////////////////////////////////////////////////////////////////////
nsProxyEventObject* 
nsProxyEventObject::GetNewOrUsedProxy(nsIEventQueue *destQueue,
                                      PRInt32 proxyType, 
                                      nsISupports *aObj,
                                      REFNSIID aIID)
{
    nsresult rv;

    if (!aObj)
        return nsnull;

    nsISupports* rawObject = aObj;

    //
    // make sure that the object pass in is not a proxy...
    // if the object *is* a proxy, then be nice and build the proxy
    // for the real object...
    //
    nsCOMPtr<nsProxyEventObject> identificationObject;

    rv = rawObject->QueryInterface(kProxyObject_Identity_Class_IID,
                                   getter_AddRefs(identificationObject));
    if (NS_SUCCEEDED(rv)) {
        //
        // ATTENTION!!!!
        //
        // If you are hitting any of the assertions in this block of code, 
        // please contact dougt@netscape.com.
        //

        // if you hit this assertion, you might want to check out how 
        // you are using proxies.  You shouldn't need to be creating
        // a proxy from a proxy.  -- dougt@netscape.com
        NS_ASSERTION(0, "Someone is building a proxy from a proxy");
        
        NS_ASSERTION(identificationObject, "where did my identification object go!");
        if (!identificationObject) {
            return nsnull;
        }

        // someone is asking us to create a proxy for a proxy.  Lets get
        // the real object and build aproxy for that!
        rawObject = identificationObject->GetRealObject();
        
        NS_ASSERTION(rawObject, "where did my real object go!");
        if (!rawObject) {
            return nsnull;
        }
    }

    //
    // Get the root nsISupports of the |real| object.
    //
    nsCOMPtr<nsISupports> rootObject;

    rootObject = do_QueryInterface(rawObject, &rv);
    if (NS_FAILED(rv) || !rootObject) {
        NS_ASSERTION(NS_FAILED(rv), "where did my root object go!");
        return nsnull;
    }

    // Get the root nsISupports of the event queue...  This is used later,
    // as part of the hashtable key...
    nsCOMPtr<nsISupports> destQRoot = do_QueryInterface(destQueue, &rv);
    if (NS_FAILED(rv) || !destQRoot) {
        return nsnull;
    }

    //
    // Enter the proxy object creation lock.
    //
    // This lock protects thev linked list which chains proxies together 
    // (ie. mRoot and mNext) and ensures that there is no hashtable contention
    // between the time that a proxy is looked up (and not found) in the
    // hashtable and then added...
    //
    nsProxyObjectManager *manager = nsProxyObjectManager::GetInstance();
    if (!manager) {
        return nsnull;
    }

    nsCOMPtr<nsIEventQueueService> eventQService(do_GetService(kEventQueueServiceCID, &rv));
    if (NS_FAILED(rv))
        return nsnull;
    
    nsAutoMonitor mon(manager->GetMonitor());

    // Get the hash table containing root proxy objects...
    nsHashtable *realToProxyMap = manager->GetRealObjectToProxyObjectMap();
    if (!realToProxyMap) {
        return nsnull;
    }

    // Now, lookup the root nsISupports of the raw object in the hashtable
    // The key consists of 3 pieces of information:
    //    - root nsISupports of the raw object
    //    - event queue of the current thread
    //    - type of proxy being constructed...
    //
    nsProxyEventKey rootkey(rootObject.get(), destQRoot.get(), proxyType);

    nsCOMPtr<nsProxyEventObject> rootProxy;
    nsCOMPtr<nsProxyEventObject> proxy;
    nsProxyEventObject* peo;

    // find in our hash table 
    rootProxy = (nsProxyEventObject*) realToProxyMap->Get(&rootkey);

    if(rootProxy) {
        //
        // At least one proxy has already been created for this raw object...
        //
        // Look for the specific interface proxy in the list off of
        // the root proxy...
        //
        peo = rootProxy->LockedFind(aIID);

        if(peo) {
            // An existing proxy is available... So use it.
            NS_ADDREF(peo);
            return peo;  
        }
    }
    else {
        // build the root proxy
        nsCOMPtr<nsProxyEventClass> rootClazz;

        rootClazz = dont_AddRef(nsProxyEventClass::GetNewOrUsedClass(
                                NS_GET_IID(nsISupports)));
        if (!rootClazz) {
            return nsnull;
        }

        peo = new nsProxyEventObject(destQueue, 
                                     proxyType, 
                                     rootObject, 
                                     rootClazz, 
                                     nsnull,
                                     eventQService);
        if(!peo) {
            // Ouch... Out of memory!
            return nsnull;
        }

        // Add this root proxy into the hash table...
        realToProxyMap->Put(&rootkey, peo);

        if(aIID.Equals(NS_GET_IID(nsISupports))) {
            //
            // Since the requested proxy is for the nsISupports interface of
            // the raw object, use the new root proxy as the specific proxy
            // too...
            //
            NS_ADDREF(peo);
            return peo;
        }

        // This assignment is an owning reference to the new ProxyEventObject.
        // So, it will automatically get deleted if any subsequent early 
        // returns are taken...
        rootProxy = do_QueryInterface(peo);
    }

    //
    // at this point we have a proxy for the root nsISupports (ie. rootProxy)
    // but we need to build the specific proxy for this interface...
    //
    NS_ASSERTION(rootProxy, "What happened to the root proxy!");

    // Get a class for this IID.
    nsCOMPtr<nsProxyEventClass> proxyClazz;
        
    proxyClazz = dont_AddRef(nsProxyEventClass::GetNewOrUsedClass(aIID));
    if(!proxyClazz) {
        return nsnull;
    }

    // Get the raw interface for this IID
    nsCOMPtr<nsISupports> rawInterface;

    rv = rawObject->QueryInterface(aIID, getter_AddRefs(rawInterface));
    if (NS_FAILED(rv) || !rawInterface) {
        NS_ASSERTION(NS_FAILED(rv), "where did my rawInterface object go!");
        return nsnull;
    }

    peo = new nsProxyEventObject(destQueue, 
                                 proxyType, 
                                 rawInterface, 
                                 proxyClazz, 
                                 rootProxy,
                                 eventQService);
    if (!peo) {
        // Ouch... Out of memory!
        return nsnull;
    }

    //
    // Add the new specific proxy to the head of the list of proxies hanging
    // off of the rootProxy...
    //
    peo->mNext       = rootProxy->mNext;
    rootProxy->mNext = peo;

    NS_ADDREF(peo);
    return peo;  

}

nsProxyEventObject* nsProxyEventObject::LockedFind(REFNSIID aIID)
{
    if(aIID.Equals(mClass->GetProxiedIID())) {
        return this;
    }

    if(aIID.Equals(NS_GET_IID(nsISupports))) {
        return this;
    }

    nsProxyEventObject* cur = (mRoot ? mRoot : mNext);
    while(cur) {
        if(aIID.Equals(cur->GetClass()->GetProxiedIID())) {
            return cur;
        }
        cur = cur->mNext;
    }

    return nsnull;
}

nsProxyEventObject::nsProxyEventObject()
: mNext(nsnull)
{
     NS_WARNING("This constructor should never be called");
}

nsProxyEventObject::nsProxyEventObject(nsIEventQueue *destQueue,
                                       PRInt32 proxyType,
                                       nsISupports* aObj,
                                       nsProxyEventClass* aClass,
                                       nsProxyEventObject* root,
                                       nsIEventQueueService* eventQService)
    : mClass(aClass),
      mRoot(root),
      mNext(nsnull)
{
    NS_IF_ADDREF(mRoot);

    mProxyObject = new nsProxyObject(destQueue, proxyType, aObj, eventQService);

#ifdef DEBUG_xpcom_proxy
    DebugDump("Create", 0);
#endif
}

nsProxyEventObject::~nsProxyEventObject()
{
#ifdef DEBUG_xpcom_proxy
    DebugDump("Delete", 0);
#endif
    if (mRoot) {
        //
        // This proxy is not the root interface so it must be removed
        // from the chain of proxies...
        //
        nsProxyEventObject* cur = mRoot;
        while(cur) {
            if(cur->mNext == this) {
                cur->mNext = mNext;
                mNext = nsnull;
                break;
            }
            cur = cur->mNext;
        }
        NS_ASSERTION(cur, "failed to find wrapper in its own chain");
    }
    else {
        //
        // This proxy is for the root interface.  Each proxy in the chain
        // has a strong reference to the root... So, when its refcount goes
        // to zero, it safe to remove it because no proxies are in its chain.
        //
        if (! nsProxyObjectManager::IsManagerShutdown()) {
            nsProxyObjectManager* manager = nsProxyObjectManager::GetInstance();
            nsHashtable *realToProxyMap = manager->GetRealObjectToProxyObjectMap();

            NS_ASSERTION(!mNext, "There are still proxies in the chain!");

            if (realToProxyMap != nsnull) {
                nsCOMPtr<nsISupports> rootObject = do_QueryInterface(mProxyObject->mRealObject);
                nsCOMPtr<nsISupports> rootQueue = do_QueryInterface(mProxyObject->mDestQueue);
                nsProxyEventKey key(rootObject, rootQueue, mProxyObject->mProxyType);
#ifdef DEBUG_dougt
                void* value =
#endif
                    realToProxyMap->Remove(&key);
#ifdef DEBUG_dougt
                NS_ASSERTION(value, "failed to remove from realToProxyMap");
#endif
            }
        }
    }

    // I am worried about ordering.
    // do not remove assignments.
    mProxyObject = nsnull;
    mClass       = nsnull;
    NS_IF_RELEASE(mRoot);
}

//
// nsISupports implementation...
//

NS_IMPL_THREADSAFE_ADDREF(nsProxyEventObject)

NS_IMETHODIMP_(nsrefcnt)
nsProxyEventObject::Release(void)
{
    //
    // Be pessimistic about whether the manager or even the monitor exist...
    // This is to protect against shutdown issues where a proxy object could
    // be destroyed after (or while) the Proxy Manager is being destroyed...
    //
    nsProxyObjectManager* manager = nsProxyObjectManager::GetInstance();
    nsAutoMonitor mon(manager ? manager->GetMonitor() : nsnull);

    nsrefcnt count;
    NS_PRECONDITION(0 != mRefCnt, "dup release");
    // Decrement atomically - in case the Proxy Object Manager has already
    // been deleted and the monitor is unavailable...
    count = PR_AtomicDecrement((PRInt32 *)&mRefCnt);
    NS_LOG_RELEASE(this, count, "nsProxyEventObject");
    if (0 == count) {
        mRefCnt = 1; /* stabilize */
        //
        // Remove the proxy from the hashtable (if necessary) or its
        // proxy chain.  This must be done inside of the proxy lock to
        // prevent GetNewOrUsedProxy(...) from ressurecting it...
        //
        NS_DELETEXPCOM(this);
        return 0;
    }
    return count;
}

NS_IMETHODIMP
nsProxyEventObject::QueryInterface(REFNSIID aIID, void** aInstancePtr)
{
    if( aIID.Equals(GetIID()) )
    {
        *aInstancePtr = NS_STATIC_CAST(nsISupports*, this);
        NS_ADDREF_THIS();
        return NS_OK;
    }
        
    return mClass->DelegatedQueryInterface(this, aIID, aInstancePtr);
}

//
// nsXPTCStubBase implementation...
//

NS_IMETHODIMP
nsProxyEventObject::GetInterfaceInfo(nsIInterfaceInfo** info)
{
    NS_ENSURE_ARG_POINTER(info);

    *info = mClass->GetInterfaceInfo();

    NS_ASSERTION(*info, "proxy class without interface");
    if (!*info) {
        return NS_ERROR_UNEXPECTED;
    }

    NS_ADDREF(*info);
    return NS_OK;
}

NS_IMETHODIMP
nsProxyEventObject::CallMethod(PRUint16 methodIndex,
                               const nsXPTMethodInfo* info,
                               nsXPTCMiniVariant * params)
{
    nsresult rv;

    if (mProxyObject) {
        rv = mProxyObject->Post(methodIndex,
                                (nsXPTMethodInfo*)info,
                                params,
                                mClass->GetInterfaceInfo());
    } else {
        rv = NS_ERROR_NULL_POINTER;
    }

    return rv;
}