Bug 1540573 - P3. Expose network link type on ContentChild for use in content process. r=snorp

In GeckoView the nsINetworkLinkService doesn't work in the content process, as
we don't seem to have an AndroidBridge there, so just maintain the network
connection type on the ContentChild.

(I had considered keeping this on the NeckoChild, but the creation of that is
initiated from the content process side, and there's not an easy and clean way
to have the parent process send us the connection type after construction of
the NeckoParent, other than have the NeckoChild request it either
synchronously, or doing it async and hoping it's not asked for the value before
the response comes in.)

Differential Revision: https://phabricator.services.mozilla.com/D26232

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Chris Pearce 2019-05-03 02:43:47 +00:00
parent 921421911b
commit 693f25ad50
7 changed files with 91 additions and 0 deletions

View File

@ -2029,6 +2029,17 @@ mozilla::ipc::IPCResult ContentChild::RecvPScriptCacheConstructor(
PNeckoChild* ContentChild::AllocPNeckoChild() { return new NeckoChild(); }
mozilla::ipc::IPCResult ContentChild::RecvNetworkLinkTypeChange(
const uint32_t& aType) {
mNetworkLinkType = aType;
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->NotifyObservers(nullptr, "contentchild:network-link-type-changed",
nullptr);
}
return IPC_OK();
}
bool ContentChild::DeallocPNeckoChild(PNeckoChild* necko) {
delete necko;
return true;

View File

@ -489,6 +489,9 @@ class ContentChild final : public PContentChild,
mozilla::ipc::IPCResult RecvRefreshScreens(
nsTArray<ScreenDetails>&& aScreens);
mozilla::ipc::IPCResult RecvNetworkLinkTypeChange(const uint32_t& aType);
uint32_t NetworkLinkType() const { return mNetworkLinkType; }
// Get the directory for IndexedDB files. We query the parent for this and
// cache the value
nsString& GetIndexedDBPath();
@ -811,6 +814,8 @@ class ContentChild final : public PContentChild,
mozilla::Atomic<uint32_t> mPendingInputEvents;
#endif
uint32_t mNetworkLinkType = 0;
DISALLOW_EVIL_CONSTRUCTORS(ContentChild);
};

View File

@ -158,6 +158,7 @@
#include "nsIMemoryReporter.h"
#include "nsIMozBrowserFrame.h"
#include "nsIMutable.h"
#include "nsINetworkLinkService.h"
#include "nsIObserverService.h"
#include "nsIParentChannel.h"
#include "nsIRemoteWindowContext.h"
@ -614,6 +615,7 @@ static const char* sObserverTopics[] = {
"cookie-changed",
"private-cookie-changed",
"clear-site-data-reload-needed",
NS_NETWORK_LINK_TYPE_TOPIC,
};
#if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
@ -3120,10 +3122,30 @@ ContentParent::Observe(nsISupports* aSubject, const char* aTopic,
} else if (!strcmp(aTopic, "clear-site-data-reload-needed")) {
// Rebroadcast "clear-site-data-reload-needed".
Unused << SendClearSiteDataReloadNeeded(nsString(aData));
} else if (!strcmp(aTopic, NS_NETWORK_LINK_TYPE_TOPIC)) {
UpdateNetworkLinkType();
}
return NS_OK;
}
void ContentParent::UpdateNetworkLinkType() {
nsresult rv;
nsCOMPtr<nsINetworkLinkService> nls =
do_GetService(NS_NETWORK_LINK_SERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
return;
}
uint32_t linkType = nsINetworkLinkService::LINK_TYPE_UNKNOWN;
rv = nls->GetLinkType(&linkType);
if (NS_FAILED(rv)) {
return;
}
Unused << SendNetworkLinkTypeChange(linkType);
}
NS_IMETHODIMP
ContentParent::GetInterface(const nsIID& aIID, void** aResult) {
NS_ENSURE_ARG_POINTER(aResult);

View File

@ -1213,6 +1213,8 @@ class ContentParent final : public PContentParent,
void OnBrowsingContextGroupSubscribe(BrowsingContextGroup* aGroup);
void OnBrowsingContextGroupUnsubscribe(BrowsingContextGroup* aGroup);
void UpdateNetworkLinkType();
private:
// Released in ActorDestroy; deliberately not exposed to the CC.
RefPtr<ContentParent> mSelfRef;

View File

@ -430,6 +430,8 @@ child:
async AudioDefaultDeviceChange();
async NetworkLinkTypeChange(uint32_t type);
// Re-create the rendering stack for a device reset.
async ReinitRenderingForDeviceReset();

View File

@ -14,6 +14,7 @@
#include "TimeUnits.h"
#include "VorbisUtils.h"
#include "mozilla/Base64.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/SharedThreadPool.h"
#include "mozilla/StaticPrefs.h"
#include "mozilla/SystemGroup.h"
@ -23,9 +24,11 @@
#include "nsCharSeparatedTokenizer.h"
#include "nsContentTypeParser.h"
#include "nsIConsoleService.h"
#include "nsINetworkLinkService.h"
#include "nsIRandomGenerator.h"
#include "nsIServiceManager.h"
#include "nsMathUtils.h"
#include "nsNetCID.h"
#include "nsServiceManagerUtils.h"
#include "nsThreadUtils.h"
@ -712,4 +715,46 @@ UniquePtr<TrackInfo> CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
return trackInfo;
}
bool OnCellularConnection() {
uint32_t linkType = nsINetworkLinkService::LINK_TYPE_UNKNOWN;
if (XRE_IsContentProcess()) {
mozilla::dom::ContentChild* cpc =
mozilla::dom::ContentChild::GetSingleton();
if (!cpc) {
NS_WARNING("Can't get ContentChild singleton in content process!");
return false;
}
linkType = cpc->NetworkLinkType();
} else {
nsresult rv;
nsCOMPtr<nsINetworkLinkService> nls =
do_GetService(NS_NETWORK_LINK_SERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
NS_WARNING("Can't get nsINetworkLinkService.");
return false;
}
rv = nls->GetLinkType(&linkType);
if (NS_FAILED(rv)) {
NS_WARNING("Can't get network link type.");
return false;
}
}
switch (linkType) {
case nsINetworkLinkService::LINK_TYPE_UNKNOWN:
case nsINetworkLinkService::LINK_TYPE_ETHERNET:
case nsINetworkLinkService::LINK_TYPE_USB:
case nsINetworkLinkService::LINK_TYPE_WIFI:
return false;
case nsINetworkLinkService::LINK_TYPE_WIMAX:
case nsINetworkLinkService::LINK_TYPE_2G:
case nsINetworkLinkService::LINK_TYPE_3G:
case nsINetworkLinkService::LINK_TYPE_4G:
return true;
}
return false;
}
} // end namespace mozilla

View File

@ -545,6 +545,10 @@ inline void AppendStringIfNotEmpty(nsACString& aDest, nsACString&& aSrc) {
}
}
// Returns true if we're running on a cellular connection; 2G, 3G, etc.
// Main thread only.
bool OnCellularConnection();
} // end namespace mozilla
#endif