author dmose@mozilla.org
Thu, 05 Jul 2007 12:31:44 -0700
changeset 3161 024c13a97e3a8c793f527bad445b61b03b3aaf14
parent 1856 13bbe5fb49aaeacf1426920369ecf628a79cb375
child 3233 ab58bdbde9e03330d3a406d8b95aa3877152cf36
permissions -rw-r--r--
Refactor nsIMIMEInfo and nsExternalHelperAppService to support local and web-based protocol handlers (bug 384374), r=biesi, sr=sicking

/* -*- 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>
 *   Benjamin Smedberg <benjamin@smedbergs.us>
 * 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 "nsProxyEventPrivate.h"
#include "nsIThreadInternal.h"

#include "nsServiceManagerUtils.h"

#include "nsHashtable.h"

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

#include "nsAutoLock.h"

nsProxyEventObject::nsProxyEventObject(nsProxyObject *aParent,
                            nsProxyEventClass* aClass,
                            already_AddRefed<nsISomeInterface> aRealInterface,
                            nsresult *rv)
    : mClass(aClass),
    *rv = InitStub(aClass->GetProxiedIID());

    // This destructor is always called within the POM monitor.
    // XXX assert this!


    // mRealInterface must be released before mProxyObject so that the last
    // release of the proxied object is proxied to the correct thread.
    // See bug 337492.
    mRealInterface = nsnull;

// nsISupports implementation...


    nsAutoMonitor mon(nsProxyObjectManager::GetInstance()->GetMonitor());

    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...
        return 0;
    return count;

nsProxyEventObject::QueryInterface(REFNSIID aIID, void** aInstancePtr)
    if( aIID.Equals(GetClass()->GetProxiedIID()) )
        *aInstancePtr = NS_STATIC_CAST(nsISupports*, mXPTCStub);
        return NS_OK;
    return mProxyObject->QueryInterface(aIID, aInstancePtr);

// nsXPTCStubBase implementation...

nsProxyEventObject::convertMiniVariantToVariant(const XPTMethodDescriptor *methodInfo, 
                                                nsXPTCMiniVariant * params, 
                                                nsXPTCVariant **fullParam, 
                                                uint8 *outParamCount)
    uint8 paramCount = methodInfo->num_args;
    *outParamCount = paramCount;
    *fullParam = nsnull;

    if (!paramCount) return NS_OK;
    *fullParam = (nsXPTCVariant*)malloc(sizeof(nsXPTCVariant) * paramCount);
    if (*fullParam == nsnull)
        return NS_ERROR_OUT_OF_MEMORY;
    for (int i = 0; i < paramCount; i++)
        const nsXPTParamInfo& paramInfo = methodInfo->params[i];
        if ((GetProxyType() & NS_PROXY_ASYNC) && paramInfo.IsDipper())
            NS_WARNING("Async proxying of out parameters is not supported"); 
        uint8 flags = paramInfo.IsOut() ? nsXPTCVariant::PTR_IS_DATA : 0;
        (*fullParam)[i].Init(params[i], paramInfo.GetType(), flags);
    return NS_OK;

class nsProxyThreadFilter : public nsIThreadEventFilter

NS_IMPL_THREADSAFE_ISUPPORTS1(nsProxyThreadFilter, nsIThreadEventFilter)


nsProxyThreadFilter::AcceptEvent(nsIRunnable *event)
    PROXY_LOG(("PROXY(%p): filter event [%p]\n", this, event));

    // If we encounter one of our proxy events that is for a synchronous method
    // call, then we want to put it in our event queue for processing.  Else,
    // we want to allow the event to be dispatched to the thread's event queue
    // for processing later once we complete the current sync method call.
    nsRefPtr<nsProxyObjectCallInfo> poci;
    event->QueryInterface(kFilterIID, getter_AddRefs(poci));
    return poci && poci->IsSync();

nsProxyEventObject::CallMethod(PRUint16 methodIndex,
                               const XPTMethodDescriptor* methodInfo,
                               nsXPTCMiniVariant * params)
    NS_ASSERTION(methodIndex > 2,
                 "Calling QI/AddRef/Release through CallMethod");
    nsresult rv;

    if (XPT_MD_IS_NOTXPCOM(methodInfo->flags))

    nsXPTCVariant *fullParam;
    uint8 paramCount;
    rv = convertMiniVariantToVariant(methodInfo, params,
                                     &fullParam, &paramCount);
    if (NS_FAILED(rv))
        return rv;

    PRBool callDirectly = PR_FALSE;
    if (GetProxyType() & NS_PROXY_SYNC &&
        NS_SUCCEEDED(GetTarget()->IsOnCurrentThread(&callDirectly)) &&
        callDirectly) {

        // invoke directly using xptc
        rv = NS_InvokeByIndex(mRealInterface, methodIndex,
                              paramCount, fullParam);

        if (fullParam)

        return rv;

    nsRefPtr<nsProxyObjectCallInfo> proxyInfo =
        new nsProxyObjectCallInfo(this, methodInfo, methodIndex,
                                  fullParam, paramCount);
    if (!proxyInfo)
        return NS_ERROR_OUT_OF_MEMORY;

    if (! (GetProxyType() & NS_PROXY_SYNC)) {
        return GetTarget()->Dispatch(proxyInfo, NS_DISPATCH_NORMAL);

    // Post synchronously

    nsIThread *thread = NS_GetCurrentThread();
    nsCOMPtr<nsIThreadInternal> threadInt = do_QueryInterface(thread);

    // Install  thread filter to limit event processing only to 
    // nsProxyObjectCallInfo instances.  XXX Add support for sequencing?
    nsRefPtr<nsProxyThreadFilter> filter = new nsProxyThreadFilter();
    if (!filter)
        return NS_ERROR_OUT_OF_MEMORY;

    // Dispatch can fail if the thread is shutting down
    rv = GetTarget()->Dispatch(proxyInfo, NS_DISPATCH_NORMAL);
    if (NS_SUCCEEDED(rv)) {
        while (!proxyInfo->GetCompleted()) {
            if (!NS_ProcessNextEvent(thread)) {
                rv = NS_ERROR_UNEXPECTED;
        rv = proxyInfo->GetResult();
    } else {
        NS_WARNING("Failed to dispatch nsProxyCallEvent");


    PROXY_LOG(("PROXY(%p): PostAndWait exit [%p %x]\n", this, proxyInfo.get(), rv));
    return rv;