Bug 1861467 - [wpt-sync] Update web-platform-tests to eedf737ce39c512d0ca3471f988972e...
[gecko.git] / dom / ipc / TabContext.cpp
blobc4fff2b66c02c0a2b640180672aeb5bf0f46ff52
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/TabContext.h"
8 #include "mozilla/dom/PTabContext.h"
9 #include "mozilla/dom/BrowserParent.h"
10 #include "mozilla/dom/BrowserChild.h"
11 #include "mozilla/StaticPrefs_dom.h"
12 #include "nsServiceManagerUtils.h"
14 using namespace mozilla::dom::ipc;
15 using namespace mozilla::layout;
17 namespace mozilla::dom {
19 TabContext::TabContext()
20 : mInitialized(false),
21 mChromeOuterWindowID(0),
22 mJSPluginID(-1),
23 mMaxTouchPoints(0) {}
25 bool TabContext::IsJSPlugin() const { return mJSPluginID >= 0; }
27 int32_t TabContext::JSPluginId() const { return mJSPluginID; }
29 uint64_t TabContext::ChromeOuterWindowID() const {
30 return mChromeOuterWindowID;
33 bool TabContext::SetTabContext(const TabContext& aContext) {
34 NS_ENSURE_FALSE(mInitialized, false);
36 *this = aContext;
37 mInitialized = true;
39 return true;
42 bool TabContext::UpdateTabContextAfterSwap(const TabContext& aContext) {
43 // This is only used after already initialized.
44 MOZ_ASSERT(mInitialized);
46 // The only permissable changes are to mChromeOuterWindowID. All other fields
47 // must match for the change to be accepted.
49 mChromeOuterWindowID = aContext.mChromeOuterWindowID;
50 return true;
53 bool TabContext::SetTabContext(uint64_t aChromeOuterWindowID,
54 uint32_t aMaxTouchPoints) {
55 NS_ENSURE_FALSE(mInitialized, false);
57 mInitialized = true;
58 mChromeOuterWindowID = aChromeOuterWindowID;
59 mMaxTouchPoints = aMaxTouchPoints;
60 return true;
63 bool TabContext::SetTabContextForJSPluginFrame(int32_t aJSPluginID) {
64 NS_ENSURE_FALSE(mInitialized, false);
66 mInitialized = true;
67 mJSPluginID = aJSPluginID;
68 return true;
71 IPCTabContext TabContext::AsIPCTabContext() const {
72 if (IsJSPlugin()) {
73 return IPCTabContext(JSPluginFrameIPCTabContext(mJSPluginID));
76 return IPCTabContext(
77 FrameIPCTabContext(mChromeOuterWindowID, mMaxTouchPoints));
80 MaybeInvalidTabContext::MaybeInvalidTabContext(const IPCTabContext& aParams)
81 : mInvalidReason(nullptr) {
82 uint64_t chromeOuterWindowID = 0;
83 int32_t jsPluginId = -1;
84 uint32_t maxTouchPoints = 0;
86 switch (aParams.type()) {
87 case IPCTabContext::TPopupIPCTabContext: {
88 const PopupIPCTabContext& ipcContext = aParams.get_PopupIPCTabContext();
90 chromeOuterWindowID = ipcContext.chromeOuterWindowID();
91 break;
93 case IPCTabContext::TJSPluginFrameIPCTabContext: {
94 const JSPluginFrameIPCTabContext& ipcContext =
95 aParams.get_JSPluginFrameIPCTabContext();
97 jsPluginId = ipcContext.jsPluginId();
98 break;
100 case IPCTabContext::TFrameIPCTabContext: {
101 const FrameIPCTabContext& ipcContext = aParams.get_FrameIPCTabContext();
103 chromeOuterWindowID = ipcContext.chromeOuterWindowID();
104 maxTouchPoints = ipcContext.maxTouchPoints();
105 break;
107 default: {
108 MOZ_CRASH();
112 bool rv;
113 if (jsPluginId >= 0) {
114 rv = mTabContext.SetTabContextForJSPluginFrame(jsPluginId);
115 } else {
116 rv = mTabContext.SetTabContext(chromeOuterWindowID, maxTouchPoints);
118 if (!rv) {
119 mInvalidReason = "Couldn't initialize TabContext.";
123 bool MaybeInvalidTabContext::IsValid() { return mInvalidReason == nullptr; }
125 const char* MaybeInvalidTabContext::GetInvalidReason() {
126 return mInvalidReason;
129 const TabContext& MaybeInvalidTabContext::GetTabContext() {
130 if (!IsValid()) {
131 MOZ_CRASH("Can't GetTabContext() if !IsValid().");
134 return mTabContext;
137 } // namespace mozilla::dom