Bug 940740 - Make sure do_GetService called in mainthread. r=mcmanus

This commit is contained in:
John Shih 2013-12-13 16:33:46 +08:00
parent 287ef9237a
commit 9779367721
2 changed files with 63 additions and 11 deletions

View File

@ -1083,7 +1083,10 @@ WebSocketChannel::BeginOpen()
#ifdef MOZ_WIDGET_GONK
if (mAppId != NECKO_NO_APP_ID) {
NS_GetActiveNetworkInterface(mActiveNetwork);
nsCOMPtr<nsINetworkInterface> activeNetwork;
NS_GetActiveNetworkInterface(activeNetwork);
mActiveNetwork =
new nsMainThreadPtrHolder<nsINetworkInterface>(activeNetwork);
}
#endif
@ -3263,6 +3266,57 @@ WebSocketChannel::OnDataAvailable(nsIRequest *aRequest,
return NS_OK;
}
//-----------------------------------------------------------------------------
// WebSocketChannel save network statistics event
//-----------------------------------------------------------------------------
#ifdef MOZ_WIDGET_GONK
namespace {
class SaveNetworkStatsEvent : public nsRunnable {
public:
SaveNetworkStatsEvent(uint32_t aAppId,
nsMainThreadPtrHandle<nsINetworkInterface> &aActiveNetwork,
uint64_t aCountRecv,
uint64_t aCountSent)
: mAppId(aAppId),
mActiveNetwork(aActiveNetwork),
mCountRecv(aCountRecv),
mCountSent(aCountSent)
{
MOZ_ASSERT(mAppId != NECKO_NO_APP_ID);
MOZ_ASSERT(mActiveNetwork);
}
NS_IMETHOD Run()
{
MOZ_ASSERT(NS_IsMainThread());
nsresult rv;
nsCOMPtr<nsINetworkStatsServiceProxy> mNetworkStatsServiceProxy =
do_GetService("@mozilla.org/networkstatsServiceProxy;1", &rv);
if (NS_FAILED(rv)) {
return rv;
}
// save the network stats through NetworkStatsServiceProxy
mNetworkStatsServiceProxy->SaveAppStats(mAppId,
mActiveNetwork,
PR_Now() / 1000,
mCountRecv,
mCountSent,
nullptr);
return NS_OK;
}
private:
uint32_t mAppId;
nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork;
uint64_t mCountRecv;
uint64_t mCountSent;
};
};
#endif
nsresult
WebSocketChannel::SaveNetworkStats(bool enforce)
{
@ -3285,15 +3339,12 @@ WebSocketChannel::SaveNetworkStats(bool enforce)
return NS_OK;
}
nsresult rv;
nsCOMPtr<nsINetworkStatsServiceProxy> mNetworkStatsServiceProxy =
do_GetService("@mozilla.org/networkstatsServiceProxy;1", &rv);
if (NS_FAILED(rv)) {
return rv;
}
mNetworkStatsServiceProxy->SaveAppStats(mAppId, mActiveNetwork, PR_Now() / 1000,
mCountRecv, mCountSent, nullptr);
// Create the event to save the network statistics.
// the event is then dispathed to the main thread.
nsRefPtr<nsRunnable> event =
new SaveNetworkStatsEvent(mAppId, mActiveNetwork,
mCountRecv, mCountSent);
NS_DispatchToMainThread(event);
// Reset the counters after saving.
mCountSent = 0;

View File

@ -20,6 +20,7 @@
#ifdef MOZ_WIDGET_GONK
#include "nsINetworkManager.h"
#include "nsProxyRelease.h" // for nsMainThreadPtrHandle
#endif
#include "nsCOMPtr.h"
@ -261,7 +262,7 @@ private:
uint64_t mCountSent;
uint32_t mAppId;
#ifdef MOZ_WIDGET_GONK
nsCOMPtr<nsINetworkInterface> mActiveNetwork;
nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork;
#endif
nsresult SaveNetworkStats(bool);
void CountRecvBytes(uint64_t recvBytes)