Skip to content

Commit

Permalink
Bug 1513057 - P4: Update online/offline status to socket process r=dr…
Browse files Browse the repository at this point in the history
…agana,mayhemer

Differential Revision: https://phabricator.services.mozilla.com/D14161
  • Loading branch information
KershawChang committed Jan 11, 2019
1 parent 2e66ac1 commit a633a29
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 3 deletions.
2 changes: 1 addition & 1 deletion netwerk/base/nsIOService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,7 @@ nsIOService::GetManageOfflineStatus(bool *aManage) {

// input argument 'data' is already UTF8'ed
nsresult nsIOService::OnNetworkLinkEvent(const char *data) {
if (IsNeckoChild()) {
if (IsNeckoChild() || IsSocketProcessChild()) {
// There is nothing IO service could do on the child process
// with this at the moment. Feel free to add functionality
// here at will, though.
Expand Down
5 changes: 5 additions & 0 deletions netwerk/ipc/NeckoCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ inline bool IsNeckoChild() {
return amChild;
}

inline bool IsSocketProcessChild() {
static bool amChild = (XRE_GetProcessType() == GeckoProcessType_Socket);
return amChild;
}

namespace NeckoCommonInternal {
extern bool gSecurityDisabled;
extern bool gRegisteredBool;
Expand Down
1 change: 1 addition & 0 deletions netwerk/ipc/PSocketProcess.ipdl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ child:
bool anonymize,
bool minimizeMemoryUsage,
MaybeFileDesc DMDFile);
async SetOffline(bool offline);
};

} // namespace net
Expand Down
12 changes: 12 additions & 0 deletions netwerk/ipc/SocketProcessChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,17 @@ mozilla::ipc::IPCResult SocketProcessChild::RecvRequestMemoryReport(
return IPC_OK();
}

mozilla::ipc::IPCResult SocketProcessChild::RecvSetOffline(
const bool& aOffline) {
LOG(("SocketProcessChild::RecvSetOffline aOffline=%d\n", aOffline));

nsCOMPtr<nsIIOService> io(do_GetIOService());
NS_ASSERTION(io, "IO Service can not be null");

io->SetOffline(aOffline);

return IPC_OK();
}

} // namespace net
} // namespace mozilla
1 change: 1 addition & 0 deletions netwerk/ipc/SocketProcessChild.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class SocketProcessChild final : public PSocketProcessChild {
mozilla::ipc::IPCResult RecvRequestMemoryReport(
const uint32_t& generation, const bool& anonymize,
const bool& minimizeMemoryUsage, const MaybeFileDesc& DMDFile) override;
mozilla::ipc::IPCResult RecvSetOffline(const bool& aOffline) override;

void CleanUp();

Expand Down
81 changes: 79 additions & 2 deletions netwerk/ipc/SocketProcessHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,67 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "SocketProcessHost.h"
#include "SocketProcessParent.h"

#include "nsAppRunner.h"
#include "nsIObserverService.h"
#include "SocketProcessParent.h"

namespace mozilla {
namespace net {

#define NS_IPC_IOSERVICE_SET_OFFLINE_TOPIC "ipc:network:set-offline"

class OfflineObserver final : public nsIObserver {
public:
NS_DECL_THREADSAFE_ISUPPORTS
explicit OfflineObserver(SocketProcessHost* aProcessHost)
: mProcessHost(aProcessHost) {
MOZ_ASSERT(NS_IsMainThread());

nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->AddObserver(this, NS_IPC_IOSERVICE_SET_OFFLINE_TOPIC, false);
}
}

void Destroy() {
MOZ_ASSERT(NS_IsMainThread());

nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->RemoveObserver(this, NS_IPC_IOSERVICE_SET_OFFLINE_TOPIC);
}
mProcessHost = nullptr;
}

private:
// nsIObserver implementation.
NS_IMETHOD
Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) override {
if (!mProcessHost) {
return NS_OK;
}

if (!strcmp(aTopic, NS_IPC_IOSERVICE_SET_OFFLINE_TOPIC)) {
NS_ConvertUTF16toUTF8 dataStr(aData);
const char* offline = dataStr.get();
if (!mProcessHost->IsConnected() ||
mProcessHost->GetActor()->SendSetOffline(
!strcmp(offline, "true") ? true : false)) {
return NS_ERROR_NOT_AVAILABLE;
}
}

return NS_OK;
}
virtual ~OfflineObserver() = default;

SocketProcessHost* mProcessHost;
};

NS_IMPL_ISUPPORTS(OfflineObserver, nsIObserver)

SocketProcessHost::SocketProcessHost(Listener* aListener)
: GeckoChildProcessHost(GeckoProcessType_Socket),
mListener(aListener),
Expand All @@ -21,7 +76,15 @@ SocketProcessHost::SocketProcessHost(Listener* aListener)
MOZ_COUNT_CTOR(SocketProcessHost);
}

SocketProcessHost::~SocketProcessHost() { MOZ_COUNT_DTOR(SocketProcessHost); }
SocketProcessHost::~SocketProcessHost() {
MOZ_COUNT_DTOR(SocketProcessHost);
if (mOfflineObserver) {
RefPtr<OfflineObserver> observer = mOfflineObserver;
NS_DispatchToMainThread(
NS_NewRunnableFunction("SocketProcessHost::DestroyOfflineObserver",
[observer]() { observer->Destroy(); }));
}
}

bool SocketProcessHost::Launch() {
MOZ_ASSERT(mLaunchPhase == LaunchPhase::Unlaunched);
Expand Down Expand Up @@ -143,6 +206,16 @@ void SocketProcessHost::InitAfterConnect(bool aSucceeded) {
DebugOnly<bool> rv = mSocketProcessParent->Open(
GetChannel(), base::GetProcId(GetChildProcessHandle()));
MOZ_ASSERT(rv);

nsCOMPtr<nsIIOService> ioService(do_GetIOService());
MOZ_ASSERT(ioService, "No IO service?");
bool offline = false;
DebugOnly<nsresult> result = ioService->GetOffline(&offline);
MOZ_ASSERT(NS_SUCCEEDED(result), "Failed getting offline?");

Unused << GetActor()->SendSetOffline(offline);

mOfflineObserver = new OfflineObserver(this);
}

if (mListener) {
Expand All @@ -155,6 +228,10 @@ void SocketProcessHost::Shutdown() {
MOZ_ASSERT(NS_IsMainThread());

mListener = nullptr;
if (mOfflineObserver) {
mOfflineObserver->Destroy();
mOfflineObserver = nullptr;
}

if (mSocketProcessParent) {
// OnChannelClosed uses this to check if the shutdown was expected or
Expand Down
3 changes: 3 additions & 0 deletions netwerk/ipc/SocketProcessHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace mozilla {
namespace net {

class OfflineObserver;
class SocketProcessParent;

// SocketProcessHost is the "parent process" container for a subprocess handle
Expand Down Expand Up @@ -101,6 +102,8 @@ class SocketProcessHost final : public mozilla::ipc::GeckoChildProcessHost {
// OnProcessUnexpectedShutdown will be invoked.
bool mShutdownRequested;
bool mChannelClosed;

RefPtr<OfflineObserver> mOfflineObserver;
};

class SocketProcessMemoryReporter : public MemoryReportingProcess {
Expand Down

0 comments on commit a633a29

Please sign in to comment.