From f0c84ea9eb9bd43f0428aa739bc037f75eb4c941 Mon Sep 17 00:00:00 2001 From: Bill McCloskey Date: Wed, 28 Jan 2015 11:53:46 -0800 Subject: [PATCH] Bug 1126042 - Use different window IDs for different processes (r=smaug) --- dom/base/nsGlobalWindow.cpp | 10 ++++++++-- dom/ipc/ContentChild.cpp | 35 +++++++++++++++++++++++++++++++++++ dom/ipc/ContentChild.h | 3 +++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index c8d03dd35f5d..042d7ea265ae 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -274,7 +274,6 @@ static int32_t gRunningTimeoutDepth = 0; static bool gMouseDown = false; static bool gDragServiceDisabled = false; static FILE *gDumpFile = nullptr; -static uint64_t gNextWindowID = 0; static uint32_t gSerialCounter = 0; static uint32_t gTimeoutsRecentlySet = 0; static TimeStamp gLastRecordedRecentTimeouts; @@ -562,6 +561,13 @@ nsTimeout::HasRefCntOne() return mRefCnt.get() == 1; } +namespace mozilla { +namespace dom { +extern uint64_t +NextWindowID(); +} +} + nsPIDOMWindow::nsPIDOMWindow(nsPIDOMWindow *aOuterWindow) : mFrameElement(nullptr), mDocShell(nullptr), mModalStateDepth(0), mRunningTimeout(nullptr), mMutationBits(0), mIsDocumentLoaded(false), @@ -576,7 +582,7 @@ nsPIDOMWindow::nsPIDOMWindow(nsPIDOMWindow *aOuterWindow) mAudioMuted(false), mAudioVolume(1.0), mInnerWindow(nullptr), mOuterWindow(aOuterWindow), // Make sure no actual window ends up with mWindowID == 0 - mWindowID(++gNextWindowID), mHasNotifiedGlobalCreated(false), + mWindowID(NextWindowID()), mHasNotifiedGlobalCreated(false), mMarkedCCGeneration(0), mSendAfterRemotePaint(false) {} diff --git a/dom/ipc/ContentChild.cpp b/dom/ipc/ContentChild.cpp index 21021a9426d3..690becc1d95a 100644 --- a/dom/ipc/ContentChild.cpp +++ b/dom/ipc/ContentChild.cpp @@ -2524,6 +2524,41 @@ ContentChild::GetBrowserOrId(TabChild* aTabChild) } } +// This code goes here rather than nsGlobalWindow.cpp because nsGlobalWindow.cpp +// can't include ContentChild.h since it includes windows.h. + +static uint64_t gNextWindowID = 0; + +// We use only 53 bits for the window ID so that it can be converted to and from +// a JS value without loss of precision. The upper bits of the window ID hold the +// process ID. The lower bits identify the window. +static const uint64_t kWindowIDTotalBits = 53; +static const uint64_t kWindowIDProcessBits = 22; +static const uint64_t kWindowIDWindowBits = kWindowIDTotalBits - kWindowIDProcessBits; + +// Try to return a window ID that is unique across processes and that will never +// be recycled. +uint64_t +NextWindowID() +{ + uint64_t processID = 0; + if (XRE_GetProcessType() == GeckoProcessType_Content) { + ContentChild* cc = ContentChild::GetSingleton(); + processID = cc->GetID(); + } + + MOZ_RELEASE_ASSERT(processID < (uint64_t(1) << kWindowIDProcessBits)); + uint64_t processBits = processID & ((uint64_t(1) << kWindowIDProcessBits) - 1); + + // Make sure no actual window ends up with mWindowID == 0. + uint64_t windowID = ++gNextWindowID; + + MOZ_RELEASE_ASSERT(windowID < (uint64_t(1) << kWindowIDWindowBits)); + uint64_t windowBits = windowID & ((uint64_t(1) << kWindowIDWindowBits) - 1); + + return (processBits << kWindowIDWindowBits) | windowBits; +} + } // namespace dom } // namespace mozilla diff --git a/dom/ipc/ContentChild.h b/dom/ipc/ContentChild.h index cf9b7e7434b2..cc850c39cd32 100644 --- a/dom/ipc/ContentChild.h +++ b/dom/ipc/ContentChild.h @@ -481,6 +481,9 @@ private: DISALLOW_EVIL_CONSTRUCTORS(ContentChild); }; +uint64_t +NextWindowID(); + } // namespace dom } // namespace mozilla