Commit f47b94ff authored by Michal Novotny's avatar Michal Novotny
Browse files

Bug 488270 - "New APIs for precise time measurement of net requests" [r=mayhemer sr=shaver]

parent 94cda604
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -567,6 +567,18 @@
    {0x9f, 0xd4, 0xe0, 0x65, 0xe8, 0x55, 0x68, 0xf4} \
}

#define NS_HTTPACTIVITYDISTRIBUTOR_CLASSNAME \
    "nsHttpActivityDistributor"
#define NS_HTTPACTIVITYDISTRIBUTOR_CONTRACTID \
    "@mozilla.org/network/http-activity-distributor;1"
#define NS_HTTPACTIVITYDISTRIBUTOR_CID \
{ /* 15629ada-a41c-4a09-961f-6553cd60b1a2 */         \
    0x15629ada,                                      \
    0xa41c,                                          \
    0x4a09,                                          \
    {0x96, 0x1f, 0x65, 0x53, 0xcd, 0x60, 0xb1, 0xa2} \
}

/******************************************************************************
 * netwerk/protocol/ftp/ classes
 */
+8 −1
Original line number Diff line number Diff line
@@ -231,12 +231,14 @@ NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsFtpProtocolHandler, Init)
#include "nsHttpBasicAuth.h"
#include "nsHttpDigestAuth.h"
#include "nsHttpNTLMAuth.h"
#include "nsHttpActivityDistributor.h"
NS_GENERIC_FACTORY_CONSTRUCTOR(nsHttpNTLMAuth)
#undef LOG
#undef LOG_ENABLED
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsHttpHandler, Init)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsHttpsHandler, Init)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsHttpAuthManager, Init)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsHttpActivityDistributor, Init)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsHttpBasicAuth)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsHttpDigestAuth)
#endif // !NECKO_PROTOCOL_http
@@ -990,6 +992,11 @@ static const nsModuleComponentInfo gNetModuleInfo[] = {
      NS_HTTPAUTHMANAGER_CID,
      NS_HTTPAUTHMANAGER_CONTRACTID,
      nsHttpAuthManagerConstructor },

   { NS_HTTPACTIVITYDISTRIBUTOR_CLASSNAME,
     NS_HTTPACTIVITYDISTRIBUTOR_CID,
     NS_HTTPACTIVITYDISTRIBUTOR_CONTRACTID,
     nsHttpActivityDistributorConstructor },
#endif // !NECKO_PROTOCOL_http
      
#ifdef NECKO_PROTOCOL_ftp
+14 −8
Original line number Diff line number Diff line
@@ -36,7 +36,6 @@

#include "nsISupports.idl"


/**
 * nsIHttpActivityObserver
 *
@@ -87,7 +86,8 @@ interface nsIHttpActivityObserver : nsISupports
    /**
     * This attribute is true when this interface is active and should
     * observe http activities. When false, observeActivity() should not
     * be called.
     * be called. It is present for compatibility reasons and should be
     * implemented only by nsHttpActivityDistributor.
     */
    readonly attribute boolean isActive;

@@ -125,12 +125,6 @@ interface nsIHttpActivityObserver : nsISupports

%{C++

#define NS_HTTPACTIVITYOBSERVER_TOPIC \
  "http-activity-observer"

#define NS_HTTPACTIVITYDISTRIBUTOR_CONTRACTID \
  "@mozilla.org/netwerk/protocol/http/http-activity-distributor;1"

#define NS_HTTP_ACTIVITY_TYPE_SOCKET_TRANSPORT    \
            nsIHttpActivityObserver::ACTIVITY_TYPE_SOCKET_TRANSPORT
#define NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION    \
@@ -151,3 +145,15 @@ interface nsIHttpActivityObserver : nsISupports

%}

/**
 * nsIHttpActivityDistributor
 *
 * This interface provides a way to register and unregister observers to the
 * http activities.
 */
[scriptable, uuid(7C512CB8-582A-4625-B5B6-8639755271B5)]
interface nsIHttpActivityDistributor : nsIHttpActivityObserver
{
    void addObserver(in nsIHttpActivityObserver aObserver);
    void removeObserver(in nsIHttpActivityObserver aObserver);
};
+1 −0
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ CPPSRCS = \
		nsHttpHandler.cpp \
		nsHttpChannel.cpp \
		nsHttpPipeline.cpp \
		nsHttpActivityDistributor.cpp \
		$(NULL)

LOCAL_INCLUDES=-I$(srcdir)/../../../base/src -I$(topsrcdir)/xpcom/ds
+169 −0
Original line number Diff line number Diff line
/* ***** 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
 * Michal Novotny <michal.novotny@gmail.com>.
 * Portions created by the Initial Developer are Copyright (C) 2009
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * 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 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 "nsHttpActivityDistributor.h"
#include "nsIChannel.h"
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "nsAutoLock.h"
#include "nsNetUtil.h"
#include "nsThreadUtils.h"

class nsHttpActivityEvent : public nsRunnable
{
public:
    nsHttpActivityEvent(nsISupports *aHttpChannel,
                        PRUint32 aActivityType,
                        PRUint32 aActivitySubtype,
                        PRTime aTimestamp,
                        PRUint64 aExtraSizeData,
                        const nsACString & aExtraStringData,
                        nsCOMArray<nsIHttpActivityObserver> *aObservers)
        : mHttpChannel(aHttpChannel)
        , mActivityType(aActivityType)
        , mActivitySubtype(aActivitySubtype)
        , mTimestamp(aTimestamp)
        , mExtraSizeData(aExtraSizeData)
        , mExtraStringData(aExtraStringData)
        , mObservers(*aObservers)
    {
    }

    NS_IMETHOD Run()
    {
        for (PRInt32 i = 0 ; i < mObservers.Count() ; i++)
            mObservers[i]->ObserveActivity(mHttpChannel, mActivityType,
                                           mActivitySubtype, mTimestamp,
                                           mExtraSizeData, mExtraStringData);
        return NS_OK;
    }

private:
    virtual ~nsHttpActivityEvent()
    {
    }

    nsCOMPtr<nsISupports> mHttpChannel;
    PRUint32 mActivityType;
    PRUint32 mActivitySubtype;
    PRTime mTimestamp;
    PRUint64 mExtraSizeData;
    nsCString mExtraStringData;

    nsCOMArray<nsIHttpActivityObserver> mObservers;
};

NS_IMPL_THREADSAFE_ISUPPORTS2(nsHttpActivityDistributor,
                              nsIHttpActivityDistributor,
                              nsIHttpActivityObserver)

nsHttpActivityDistributor::nsHttpActivityDistributor()
    : mLock(nsnull)
{
}

nsHttpActivityDistributor::~nsHttpActivityDistributor()
{
    if (mLock)
        PR_DestroyLock(mLock);
}

NS_IMETHODIMP
nsHttpActivityDistributor::ObserveActivity(nsISupports *aHttpChannel,
                                           PRUint32 aActivityType,
                                           PRUint32 aActivitySubtype,
                                           PRTime aTimestamp,
                                           PRUint64 aExtraSizeData,
                                           const nsACString & aExtraStringData)
{
    nsRefPtr<nsIRunnable> event;
    {
        nsAutoLock lock(mLock);

        if (!mObservers.Count())
            return NS_OK;

        event = new nsHttpActivityEvent(aHttpChannel, aActivityType,
                                        aActivitySubtype, aTimestamp,
                                        aExtraSizeData, aExtraStringData,
                                        &mObservers);
    }
    NS_ENSURE_TRUE(event, NS_ERROR_OUT_OF_MEMORY);
    return NS_DispatchToMainThread(event);
}

NS_IMETHODIMP
nsHttpActivityDistributor::GetIsActive(PRBool *isActive)
{
    NS_ENSURE_ARG_POINTER(isActive);
    nsAutoLock lock(mLock);
    *isActive = !!mObservers.Count();
    return NS_OK;
}

NS_IMETHODIMP
nsHttpActivityDistributor::AddObserver(nsIHttpActivityObserver *aObserver)
{
    nsAutoLock lock(mLock);

    if (!mObservers.AppendObject(aObserver))
        return NS_ERROR_OUT_OF_MEMORY;

    return NS_OK;
}

NS_IMETHODIMP
nsHttpActivityDistributor::RemoveObserver(nsIHttpActivityObserver *aObserver)
{
    nsAutoLock lock(mLock);

    if (!mObservers.RemoveObject(aObserver))
        return NS_ERROR_FAILURE;

    return NS_OK;
}

nsresult
nsHttpActivityDistributor::Init()
{
    NS_ENSURE_TRUE(!mLock, NS_ERROR_ALREADY_INITIALIZED);

    mLock = PR_NewLock();
    if (!mLock)
        return NS_ERROR_OUT_OF_MEMORY;

    return NS_OK;
}
Loading