Bug 786419 - Part 8 - Several improvements to per-app-offline behaviour r=jduell

Make GetAppId() pure-virtual and add implementation for TCPSocketParent, UDPSocketParent, and one for Necko parent that returns UNKNOWN_APP_ID
Change nsIOService to use IsNeckoChild instead of XRE_ method.
Include nsThreadUtils.h (for nsRunnable and dispatchToMainThread) and NeckoCommon for IsNeckoChild.
Make HttpChannelParent set LOAD_ONLY_FROM_CACHE, LOAD_FROM_CACHE and LOAD_NO_NETWORK_IO when offline.
This commit is contained in:
Valentin Gosu 2014-08-22 20:15:00 +03:00
parent 804753cf6d
commit 0db7cb932a
9 changed files with 49 additions and 34 deletions

View File

@ -80,6 +80,19 @@ TCPSocketParentBase::~TCPSocketParentBase()
mozilla::DropJSObjects(this);
}
uint32_t
TCPSocketParent::GetAppId()
{
uint32_t appId = nsIScriptSecurityManager::UNKNOWN_APP_ID;
const PContentParent *content = Manager()->Manager();
const InfallibleTArray<PBrowserParent*>& browsers = content->ManagedPBrowserParent();
if (browsers.Length() > 0) {
TabParent *tab = static_cast<TabParent*>(browsers[0]);
appId = tab->OwnAppId();
}
return appId;
};
nsresult
TCPSocketParent::OfflineNotification(nsISupports *aSubject)
{
@ -92,14 +105,7 @@ TCPSocketParent::OfflineNotification(nsISupports *aSubject)
info->GetAppId(&targetAppId);
// Obtain App ID
uint32_t appId = nsIScriptSecurityManager::UNKNOWN_APP_ID;
const PContentParent *content = Manager()->Manager();
const InfallibleTArray<PBrowserParent*>& browsers = content->ManagedPBrowserParent();
if (browsers.Length() > 0) {
TabParent *tab = static_cast<TabParent*>(browsers[0]);
appId = tab->OwnAppId();
}
uint32_t appId = GetAppId();
if (appId != targetAppId) {
return NS_OK;
}
@ -155,13 +161,7 @@ TCPSocketParent::RecvOpen(const nsString& aHost, const uint16_t& aPort, const bo
}
// Obtain App ID
uint32_t appId = nsIScriptSecurityManager::UNKNOWN_APP_ID;
const PContentParent *content = Manager()->Manager();
const InfallibleTArray<PBrowserParent*>& browsers = content->ManagedPBrowserParent();
if (browsers.Length() > 0) {
TabParent *tab = static_cast<TabParent*>(browsers[0]);
appId = tab->OwnAppId();
}
uint32_t appId = GetAppId();
if (NS_IsAppOffline(appId)) {
NS_ERROR("Can't open socket because app is offline");

View File

@ -62,6 +62,7 @@ public:
const uint32_t& aTrackingNumber) MOZ_OVERRIDE;
virtual bool RecvRequestDelete() MOZ_OVERRIDE;
virtual nsresult OfflineNotification(nsISupports *) MOZ_OVERRIDE;
virtual uint32_t GetAppId() MOZ_OVERRIDE;
private:
virtual void ActorDestroy(ActorDestroyReason why) MOZ_OVERRIDE;

View File

@ -49,14 +49,7 @@ UDPSocketParent::OfflineNotification(nsISupports *aSubject)
info->GetAppId(&targetAppId);
// Obtain App ID
uint32_t appId = nsIScriptSecurityManager::UNKNOWN_APP_ID;
const PContentParent *content = Manager()->Manager();
const InfallibleTArray<PBrowserParent*>& browsers = content->ManagedPBrowserParent();
if (browsers.Length() > 0) {
TabParent *tab = static_cast<TabParent*>(browsers[0]);
appId = tab->OwnAppId();
}
uint32_t appId = GetAppId();
if (appId != targetAppId) {
return NS_OK;
}
@ -69,6 +62,19 @@ UDPSocketParent::OfflineNotification(nsISupports *aSubject)
return NS_OK;
}
uint32_t
UDPSocketParent::GetAppId()
{
uint32_t appId = nsIScriptSecurityManager::UNKNOWN_APP_ID;
const PContentParent *content = Manager()->Manager();
const InfallibleTArray<PBrowserParent*>& browsers = content->ManagedPBrowserParent();
if (browsers.Length() > 0) {
TabParent *tab = static_cast<TabParent*>(browsers[0]);
appId = tab->OwnAppId();
}
return appId;
}
bool
UDPSocketParent::Init(const nsACString& aFilter)
{

View File

@ -41,6 +41,7 @@ public:
virtual bool RecvLeaveMulticast(const nsCString& aMulticastAddress,
const nsCString& aInterface) MOZ_OVERRIDE;
virtual nsresult OfflineNotification(nsISupports *) MOZ_OVERRIDE;
virtual uint32_t GetAppId() MOZ_OVERRIDE;
private:
virtual ~UDPSocketParent();

View File

@ -89,12 +89,6 @@ OfflineObserver::Observe(nsISupports *aSubject,
return NS_OK;
}
uint32_t
DisconnectableParent::GetAppId()
{
return NECKO_UNKNOWN_APP_ID;
}
nsresult
DisconnectableParent::OfflineNotification(nsISupports *aSubject)
{

View File

@ -29,7 +29,7 @@ public:
virtual nsresult OfflineNotification(nsISupports *aSubject);
// GetAppId returns the appId for the app associated with the parent
virtual uint32_t GetAppId();
virtual uint32_t GetAppId() = 0;
// OfflineDisconnect cancels all existing connections in the parent when
// the app becomes offline.

View File

@ -39,6 +39,8 @@
#include "nsIProtocolProxyService2.h"
#include "MainThreadUtils.h"
#include "nsIWidget.h"
#include "nsThreadUtils.h"
#include "mozilla/net/NeckoCommon.h"
#ifdef MOZ_WIDGET_GONK
#include "nsINetworkManager.h"
@ -49,6 +51,7 @@
#endif
using namespace mozilla;
using mozilla::net::IsNeckoChild;
#define PORT_PREF_PREFIX "network.security.ports."
#define PORT_PREF(x) PORT_PREF_PREFIX x
@ -925,11 +928,12 @@ nsIOService::GetPrefBranch(nsIPrefBranch **result)
}
// This returns true if wifi-only apps should have connectivity.
// Always returns false in the child process (should not depend on this method)
static bool
IsWifiActive()
{
// We don't need to do this check inside the child process
if (XRE_GetProcessType() != GeckoProcessType_Default) {
if (IsNeckoChild()) {
return false;
}
#ifdef MOZ_WIDGET_GONK
@ -1429,7 +1433,7 @@ private:
NS_IMETHODIMP
nsIOService::SetAppOffline(uint32_t aAppId, int32_t aState)
{
NS_ENSURE_TRUE(XRE_GetProcessType() == GeckoProcessType_Default,
NS_ENSURE_TRUE(!IsNeckoChild(),
NS_ERROR_FAILURE);
NS_ENSURE_TRUE(aAppId != nsIScriptSecurityManager::NO_APP_ID,
NS_ERROR_INVALID_ARG);
@ -1470,6 +1474,7 @@ nsIOService::SetAppOfflineInternal(uint32_t aAppId, int32_t aState)
}
break;
case nsIAppOfflineInfo::WIFI_ONLY:
MOZ_RELEASE_ASSERT(!IsNeckoChild());
mAppsOfflineStatus.Put(aAppId, nsIAppOfflineInfo::WIFI_ONLY);
if (offline && wifiActive) {
NotifyAppOfflineStatus(aAppId, nsIAppOfflineInfo::ONLINE);
@ -1512,6 +1517,7 @@ nsIOService::IsAppOffline(uint32_t aAppId, bool* aResult)
*aResult = true;
break;
case nsIAppOfflineInfo::WIFI_ONLY:
MOZ_RELEASE_ASSERT(!IsNeckoChild());
*aResult = !IsWifiActive();
break;
default:

View File

@ -54,6 +54,7 @@ public:
virtual void ActorDestroy(ActorDestroyReason aWhy) MOZ_OVERRIDE;
virtual nsresult OfflineNotification(nsISupports *) MOZ_OVERRIDE;
virtual uint32_t GetAppId() MOZ_OVERRIDE { return NECKO_UNKNOWN_APP_ID; }
virtual void
CloneManagees(ProtocolBase* aSource,
mozilla::ipc::ProtocolCloneContext* aCtx) MOZ_OVERRIDE;

View File

@ -231,6 +231,8 @@ HttpChannelParent::DoAsyncOpen( const URIParams& aURI,
uint32_t loadFlags = aLoadFlags;
if (appOffline) {
loadFlags |= nsICachingChannel::LOAD_ONLY_FROM_CACHE;
loadFlags |= nsIRequest::LOAD_FROM_CACHE;
loadFlags |= nsICachingChannel::LOAD_NO_NETWORK_IO;
}
nsCOMPtr<nsIChannel> channel;
@ -383,8 +385,12 @@ HttpChannelParent::ConnectChannel(const uint32_t& channelId)
}
if (appOffline) {
mChannel->Cancel(NS_ERROR_OFFLINE);
mStatus = NS_ERROR_OFFLINE;
uint32_t loadFlags;
mChannel->GetLoadFlags(&loadFlags);
loadFlags |= nsICachingChannel::LOAD_ONLY_FROM_CACHE;
loadFlags |= nsIRequest::LOAD_FROM_CACHE;
loadFlags |= nsICachingChannel::LOAD_NO_NETWORK_IO;
mChannel->SetLoadFlags(loadFlags);
}
return true;