Bug 1212027 - part 5 - add LoneManagedOrNull for simplifying a lot of upcoming code; r=jld

A lot of existing code has variations on:

  if (ManagedPFooChild().Length()) {
    ...(ManagedPFooChild()[0])...
  }
  // Do something with nullptr, or some other action.

It's pretty reasonable to repeat this code when the managed protocols
are stored in an array; the code gets much less nice when managed
protocols are stored in a hashtable.  Let's write a small utility
function to handle those details for us.  Then when we change the
underlying storage, we only need to update this function, rather than a
bunch of callsites.

ProtocolUtils.h is included by all the generated IPDL headers, so
LoneManagedOrNull should be available everywhere the above pattern would
be encountered.
This commit is contained in:
Nathan Froyd 2015-10-07 20:15:56 -04:00
parent 59dc807786
commit 64047aa718
11 changed files with 59 additions and 58 deletions

View File

@ -98,8 +98,8 @@ ContentBridgeChild::SendPBrowserConstructor(PBrowserChild* aActor,
jsipc::CPOWManager*
ContentBridgeChild::GetCPOWManager()
{
if (ManagedPJavaScriptChild().Length()) {
return CPOWManagerFor(ManagedPJavaScriptChild()[0]);
if (PJavaScriptChild* c = LoneManagedOrNull(ManagedPJavaScriptChild())) {
return CPOWManagerFor(c);
}
return CPOWManagerFor(SendPJavaScriptConstructor());
}

View File

@ -180,8 +180,8 @@ ContentBridgeParent::NotifyTabDestroyed()
jsipc::CPOWManager*
ContentBridgeParent::GetCPOWManager()
{
if (ManagedPJavaScriptParent().Length()) {
return CPOWManagerFor(ManagedPJavaScriptParent()[0]);
if (PJavaScriptParent* p = LoneManagedOrNull(ManagedPJavaScriptParent())) {
return CPOWManagerFor(p);
}
return nullptr;
}

View File

@ -1549,8 +1549,8 @@ ContentChild::DeallocPTestShellChild(PTestShellChild* shell)
jsipc::CPOWManager*
ContentChild::GetCPOWManager()
{
if (ManagedPJavaScriptChild().Length()) {
return CPOWManagerFor(ManagedPJavaScriptChild()[0]);
if (PJavaScriptChild* c = LoneManagedOrNull(ManagedPJavaScriptChild())) {
return CPOWManagerFor(c);
}
return CPOWManagerFor(SendPJavaScriptConstructor());
}
@ -2038,11 +2038,11 @@ ContentChild::ProcessingError(Result aCode, const char* aReason)
}
#if defined(MOZ_CRASHREPORTER) && !defined(MOZ_B2G)
if (ManagedPCrashReporterChild().Length() > 0) {
if (PCrashReporterChild* c = LoneManagedOrNull(ManagedPCrashReporterChild())) {
CrashReporterChild* crashReporter =
static_cast<CrashReporterChild*>(ManagedPCrashReporterChild()[0]);
nsDependentCString reason(aReason);
crashReporter->SendAnnotateCrashReport(
static_cast<CrashReporterChild*>(c);
nsDependentCString reason(aReason);
crashReporter->SendAnnotateCrashReport(
NS_LITERAL_CSTRING("ipc_channel_error"),
reason);
}

View File

@ -2081,9 +2081,9 @@ ContentParent::ActorDestroy(ActorDestroyReason why)
// There's a window in which child processes can crash
// after IPC is established, but before a crash reporter
// is created.
if (ManagedPCrashReporterParent().Length() > 0) {
if (PCrashReporterParent* p = LoneManagedOrNull(ManagedPCrashReporterParent())) {
CrashReporterParent* crashReporter =
static_cast<CrashReporterParent*>(ManagedPCrashReporterParent()[0]);
static_cast<CrashReporterParent*>(p);
// If we're an app process, always stomp the latest URI
// loaded in the child process with our manifest URL. We
@ -2265,8 +2265,8 @@ ContentParent::NotifyTabDestroyed(const TabId& aTabId,
jsipc::CPOWManager*
ContentParent::GetCPOWManager()
{
if (ManagedPJavaScriptParent().Length()) {
return CPOWManagerFor(ManagedPJavaScriptParent()[0]);
if (PJavaScriptParent* p = LoneManagedOrNull(ManagedPJavaScriptParent())) {
return CPOWManagerFor(p);
}
return nullptr;
}
@ -2286,9 +2286,8 @@ ContentParent::DestroyTestShell(TestShellParent* aTestShell)
TestShellParent*
ContentParent::GetTestShellSingleton()
{
if (!ManagedPTestShellParent().Length())
return nullptr;
return static_cast<TestShellParent*>(ManagedPTestShellParent()[0]);
PTestShellParent* p = LoneManagedOrNull(ManagedPTestShellParent());
return static_cast<TestShellParent*>(p);
}
void
@ -3545,9 +3544,9 @@ ContentParent::KillHard(const char* aReason)
// We're about to kill the child process associated with this content.
// Something has gone wrong to get us here, so we generate a minidump
// of the parent and child for submission to the crash server.
if (ManagedPCrashReporterParent().Length() > 0) {
if (PCrashReporterParent* p = LoneManagedOrNull(ManagedPCrashReporterParent())) {
CrashReporterParent* crashReporter =
static_cast<CrashReporterParent*>(ManagedPCrashReporterParent()[0]);
static_cast<CrashReporterParent*>(p);
// GeneratePairedMinidump creates two minidumps for us - the main
// one is for the content process we're about to kill, and the other
// one is for the main browser process. That second one is the extra

View File

@ -32,10 +32,10 @@ CrashReporterChild::GetCrashReporter()
default:
break;
}
if (reporters && reporters->Length() > 0) {
return reporters->ElementAt(0);
if (!reporters) {
return nullptr;
}
return nullptr;
return LoneManagedOrNull(*reporters);
}
} // namespace dom

View File

@ -2579,10 +2579,8 @@ TabParent::GetTabIdFrom(nsIDocShell *docShell)
RenderFrameParent*
TabParent::GetRenderFrame()
{
if (ManagedPRenderFrameParent().IsEmpty()) {
return nullptr;
}
return static_cast<RenderFrameParent*>(ManagedPRenderFrameParent()[0]);
PRenderFrameParent* p = LoneManagedOrNull(ManagedPRenderFrameParent());
return static_cast<RenderFrameParent*>(p);
}
bool

View File

@ -583,10 +583,8 @@ GMPParent::WriteExtraDataForMinidump(CrashReporter::AnnotationTable& notes)
void
GMPParent::GetCrashID(nsString& aResult)
{
CrashReporterParent* cr = nullptr;
if (ManagedPCrashReporterParent().Length() > 0) {
cr = static_cast<CrashReporterParent*>(ManagedPCrashReporterParent()[0]);
}
CrashReporterParent* cr =
static_cast<CrashReporterParent*>(LoneManagedOrNull(ManagedPCrashReporterParent()));
if (NS_WARN_IF(!cr)) {
return;
}

View File

@ -65,27 +65,25 @@ TCPServerSocketParent::Init()
uint32_t
TCPServerSocketParent::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 = TabParent::GetFrom(browsers[0]);
appId = tab->OwnAppId();
if (PBrowserParent* browser = LoneManagedOrNull(content->ManagedPBrowserParent())) {
TabParent *tab = TabParent::GetFrom(browser);
return tab->OwnAppId();
} else {
return nsIScriptSecurityManager::UNKNOWN_APP_ID;
}
return appId;
};
bool
TCPServerSocketParent::GetInBrowser()
{
bool inBrowser = false;
const PContentParent *content = Manager()->Manager();
const InfallibleTArray<PBrowserParent*>& browsers = content->ManagedPBrowserParent();
if (browsers.Length() > 0) {
TabParent *tab = TabParent::GetFrom(browsers[0]);
inBrowser = tab->IsBrowserElement();
if (PBrowserParent* browser = LoneManagedOrNull(content->ManagedPBrowserParent())) {
TabParent *tab = TabParent::GetFrom(browser);
return tab->IsBrowserElement();
} else {
return false;
}
return inBrowser;
}
nsresult

View File

@ -67,27 +67,25 @@ TCPSocketParentBase::~TCPSocketParentBase()
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 = TabParent::GetFrom(browsers[0]);
appId = tab->OwnAppId();
if (PBrowserParent* browser = LoneManagedOrNull(content->ManagedPBrowserParent())) {
TabParent *tab = TabParent::GetFrom(browser);
return tab->OwnAppId();
} else {
return nsIScriptSecurityManager::UNKNOWN_APP_ID;
}
return appId;
};
bool
TCPSocketParent::GetInBrowser()
{
bool inBrowser = false;
const PContentParent *content = Manager()->Manager();
const InfallibleTArray<PBrowserParent*>& browsers = content->ManagedPBrowserParent();
if (browsers.Length() > 0) {
TabParent *tab = TabParent::GetFrom(browsers[0]);
inBrowser = tab->IsBrowserElement();
if (PBrowserParent* browser = LoneManagedOrNull(content->ManagedPBrowserParent())) {
TabParent *tab = TabParent::GetFrom(browser);
return tab->IsBrowserElement();
} else {
return false;
}
return inBrowser;
}
nsresult
@ -225,9 +223,8 @@ TCPSocketParent::RecvOpenBind(const nsCString& aRemoteHost,
uint32_t appId = nsIScriptSecurityManager::NO_APP_ID;
bool inBrowser = false;
const PContentParent *content = Manager()->Manager();
const InfallibleTArray<PBrowserParent*>& browsers = content->ManagedPBrowserParent();
if (browsers.Length() > 0) {
TabParent *tab = static_cast<TabParent*>(browsers[0]);
if (PBrowserParent* browser = LoneManagedOrNull(content->ManagedPBrowserParent())) {
TabParent *tab = TabParent::GetFrom(browser);
appId = tab->OwnAppId();
inBrowser = tab->IsBrowserElement();
}

View File

@ -1484,7 +1484,7 @@ PluginModuleChromeParent::OnHangUIContinue()
CrashReporterParent*
PluginModuleChromeParent::CrashReporter()
{
return static_cast<CrashReporterParent*>(ManagedPCrashReporterParent()[0]);
return static_cast<CrashReporterParent*>(LoneManagedOrNull(ManagedPCrashReporterParent()));
}
#ifdef MOZ_CRASHREPORTER_INJECTOR

View File

@ -327,6 +327,17 @@ DuplicateHandle(HANDLE aSourceHandle,
#endif
} // namespace ipc
template<typename Protocol>
Protocol*
LoneManagedOrNull(const nsTArray<Protocol*>& aManagees)
{
if (aManagees.Length() == 0) {
return nullptr;
}
return aManagees[0];
}
} // namespace mozilla