From 5dc554280af584764f53df092b752c0541946586 Mon Sep 17 00:00:00 2001 From: Nika Layzell Date: Thu, 18 Jul 2019 19:38:12 +0000 Subject: [PATCH] Bug 1523638 - Part 4: Move NextWindowId logic into nsContentUtils, r=kmag Differential Revision: https://phabricator.services.mozilla.com/D37651 --HG-- extra : moz-landing-system : lando --- docshell/base/nsDocShell.cpp | 2 +- dom/base/nsContentUtils.cpp | 20 ++++++++++++--- dom/base/nsContentUtils.h | 6 +++++ dom/base/nsGlobalWindowInner.cpp | 10 +------- dom/ipc/ContentChild.cpp | 42 -------------------------------- dom/ipc/ContentChild.h | 2 -- 6 files changed, 24 insertions(+), 58 deletions(-) diff --git a/docshell/base/nsDocShell.cpp b/docshell/base/nsDocShell.cpp index b478e2498871..f1afbb16914b 100644 --- a/docshell/base/nsDocShell.cpp +++ b/docshell/base/nsDocShell.cpp @@ -309,7 +309,7 @@ static void DecreasePrivateDocShellCount() { nsDocShell::nsDocShell(BrowsingContext* aBrowsingContext) : nsDocLoader(), - mContentWindowID(NextWindowID()), + mContentWindowID(nsContentUtils::GenerateWindowId()), mBrowsingContext(aBrowsingContext), mForcedCharset(nullptr), mParentCharset(nullptr), diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index 3b578455130b..cc696608dbed 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -9848,8 +9848,12 @@ bool nsContentUtils::IsLocalRefURL(const nsString& aString) { return !aString.IsEmpty() && aString[0] == '#'; } -static const uint64_t kIdProcessBits = 32; -static const uint64_t kIdBits = 64 - kIdProcessBits; +// We use only 53 bits for the ID so that it can be converted to and from a JS +// value without loss of precision. The upper bits of the ID hold the process +// ID. The lower bits identify the object itself. +static constexpr uint64_t kIdTotalBits = 53; +static constexpr uint64_t kIdProcessBits = 22; +static constexpr uint64_t kIdBits = kIdTotalBits - kIdProcessBits; /* static */ uint64_t GenerateProcessSpecificId(uint64_t aId) { uint64_t processId = 0; @@ -9874,7 +9878,7 @@ static const uint64_t kIdBits = 64 - kIdProcessBits; return (processBits << kIdBits) | bits; } -// Tab ID is composed in a similar manner of Window ID. +// Next process-local Tab ID. static uint64_t gNextTabId = 0; /* static */ @@ -9882,7 +9886,7 @@ uint64_t nsContentUtils::GenerateTabId() { return GenerateProcessSpecificId(++gNextTabId); } -// Browsing context ID is composed in a similar manner of Window ID. +// Next process-local Browsing Context ID. static uint64_t gNextBrowsingContextId = 0; /* static */ @@ -9890,6 +9894,14 @@ uint64_t nsContentUtils::GenerateBrowsingContextId() { return GenerateProcessSpecificId(++gNextBrowsingContextId); } +// Next process-local Window ID. +static uint64_t gNextWindowId = 0; + +/* static */ +uint64_t nsContentUtils::GenerateWindowId() { + return GenerateProcessSpecificId(++gNextWindowId); +} + /* static */ bool nsContentUtils::GetUserIsInteracting() { return UserInteractionObserver::sUserActive; diff --git a/dom/base/nsContentUtils.h b/dom/base/nsContentUtils.h index 65751fbdf3f2..04441dbe8d24 100644 --- a/dom/base/nsContentUtils.h +++ b/dom/base/nsContentUtils.h @@ -3075,6 +3075,12 @@ class nsContentUtils { */ static uint64_t GenerateBrowsingContextId(); + /** + * Generate a window ID which is unique across processes and will never be + * recycled. + */ + static uint64_t GenerateWindowId(); + /** * Determine whether or not the user is currently interacting with the web * browser. This method is safe to call from off of the main thread. diff --git a/dom/base/nsGlobalWindowInner.cpp b/dom/base/nsGlobalWindowInner.cpp index cd0d474321ee..e6b540d5c161 100644 --- a/dom/base/nsGlobalWindowInner.cpp +++ b/dom/base/nsGlobalWindowInner.cpp @@ -7199,13 +7199,6 @@ bool nsPIDOMWindowInner::HasStorageAccessGranted( return mStorageAccessGranted.Contains(aPermissionKey); } -// XXX: Can we define this in a header instead of here? -namespace mozilla { -namespace dom { -extern uint64_t NextWindowID(); -} // namespace dom -} // namespace mozilla - nsPIDOMWindowInner::nsPIDOMWindowInner(nsPIDOMWindowOuter* aOuterWindow) : mMutationBits(0), mActivePeerConnections(0), @@ -7218,8 +7211,7 @@ nsPIDOMWindowInner::nsPIDOMWindowInner(nsPIDOMWindowOuter* aOuterWindow) mMayHavePointerEnterLeaveEventListener(false), mMayHaveTextEventListenerInDefaultGroup(false), mOuterWindow(aOuterWindow), - // Make sure no actual window ends up with mWindowID == 0 - mWindowID(NextWindowID()), + mWindowID(nsContentUtils::GenerateWindowId()), mHasNotifiedGlobalCreated(false), mMarkedCCGeneration(0), mHasTriedToCacheTopInnerWindow(false), diff --git a/dom/ipc/ContentChild.cpp b/dom/ipc/ContentChild.cpp index 3824a96a68b4..7e9d8b080cb6 100644 --- a/dom/ipc/ContentChild.cpp +++ b/dom/ipc/ContentChild.cpp @@ -3213,48 +3213,6 @@ mozilla::ipc::IPCResult ContentChild::RecvSetAudioSessionData( #endif } -// 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_IsContentProcess()) { - 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); - - // Make sure that the middleman process doesn't generate WindowIDs which - // conflict with the process it's wrapping (which shares a ContentParentID - // with it). - if (recordreplay::IsMiddleman()) { - windowBits |= uint64_t(1) << (kWindowIDWindowBits - 1); - } - - return (processBits << kWindowIDWindowBits) | windowBits; -} - mozilla::ipc::IPCResult ContentChild::RecvInvokeDragSession( nsTArray&& aTransfers, const uint32_t& aAction) { nsCOMPtr dragService = diff --git a/dom/ipc/ContentChild.h b/dom/ipc/ContentChild.h index 4544beec53b7..8a0390fc1cfe 100644 --- a/dom/ipc/ContentChild.h +++ b/dom/ipc/ContentChild.h @@ -835,8 +835,6 @@ class ContentChild final : public PContentChild, DISALLOW_EVIL_CONSTRUCTORS(ContentChild); }; -uint64_t NextWindowID(); - } // namespace dom } // namespace mozilla