1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_MaybeDiscarded_h
#define mozilla_dom_MaybeDiscarded_h
#include "mozilla/RefPtr.h"
namespace mozilla::dom {
// Wrapper type for a WindowContext or BrowsingContext instance which may be
// discarded, and thus unavailable in the current process. This type is used to
// pass WindowContext and BrowsingContext instances over IPC, as they may be
// discarded in the receiving process.
//
// A MaybeDiscarded can generally be implicitly converted to from a
// BrowsingContext* or WindowContext*, but requires an explicit check of
// |IsDiscarded| and call to |get| to read from.
template <typename T>
class MaybeDiscarded {
public:
MaybeDiscarded() = default;
MaybeDiscarded(MaybeDiscarded<T>&&) = default;
MaybeDiscarded(const MaybeDiscarded<T>&) = default;
// Construct from raw pointers and |nullptr|.
MOZ_IMPLICIT MaybeDiscarded(T* aRawPtr)
: mId(aRawPtr ? aRawPtr->Id() : 0), mPtr(aRawPtr) {}
MOZ_IMPLICIT MaybeDiscarded(decltype(nullptr)) {}
// Construct from |RefPtr<I>|
template <typename I,
typename = std::enable_if_t<std::is_convertible_v<I*, T*>>>
MOZ_IMPLICIT MaybeDiscarded(RefPtr<I>&& aPtr)
: mId(aPtr ? aPtr->Id() : 0), mPtr(std::move(aPtr)) {}
template <typename I,
typename = std::enable_if_t<std::is_convertible_v<I*, T*>>>
MOZ_IMPLICIT MaybeDiscarded(const RefPtr<I>& aPtr)
: mId(aPtr ? aPtr->Id() : 0), mPtr(aPtr) {}
// Basic assignment operators.
MaybeDiscarded<T>& operator=(const MaybeDiscarded<T>&) = default;
MaybeDiscarded<T>& operator=(MaybeDiscarded<T>&&) = default;
MaybeDiscarded<T>& operator=(decltype(nullptr)) {
mId = 0;
mPtr = nullptr;
return *this;
}
MaybeDiscarded<T>& operator=(T* aRawPtr) {
mId = aRawPtr ? aRawPtr->Id() : 0;
mPtr = aRawPtr;
return *this;
}
template <typename I>
MaybeDiscarded<T>& operator=(const RefPtr<I>& aRhs) {
mId = aRhs ? aRhs->Id() : 0;
mPtr = aRhs;
return *this;
}
template <typename I>
MaybeDiscarded<T>& operator=(RefPtr<I>&& aRhs) {
mId = aRhs ? aRhs->Id() : 0;
mPtr = std::move(aRhs);
return *this;
}
// Validate that the value is neither discarded nor null.
bool IsNullOrDiscarded() const { return !mPtr || mPtr->IsDiscarded(); }
bool IsDiscarded() const { return IsNullOrDiscarded() && !IsNull(); }
bool IsNull() const { return mId == 0; }
explicit operator bool() const { return !IsNullOrDiscarded(); }
// Extract the wrapped |T|. Must not be called on a discarded |T|.
T* get() const {
MOZ_DIAGNOSTIC_ASSERT(!IsDiscarded());
return mPtr.get();
}
already_AddRefed<T> forget() {
MOZ_DIAGNOSTIC_ASSERT(!IsDiscarded());
return mPtr.forget();
}
T* operator->() const {
MOZ_ASSERT(!IsNull());
return get();
}
// Like "get", but gets the "Canonical" version of the type. This method may
// only be called in the parent process.
auto get_canonical() const -> decltype(get()->Canonical()) {
if (get()) {
return get()->Canonical();
} else {
return nullptr;
}
}
// The ID for the context wrapped by this MaybeDiscarded. This ID comes from a
// remote process, and should generally only be used for logging. A
// BrowsingContext with this ID may not exist in the current process.
uint64_t ContextId() const { return mId; }
// Tries to get the wrapped value, disregarding discarded status.
// This may return |nullptr| for a non-null |MaybeDiscarded|, in the case that
// the target is no longer available in this process.
T* GetMaybeDiscarded() const { return mPtr.get(); }
// Clear the value to a discarded state with the given ID.
void SetDiscarded(uint64_t aId) {
mId = aId;
mPtr = nullptr;
}
// Comparison operators required by IPDL
bool operator==(const MaybeDiscarded<T>& aRhs) const {
return mId == aRhs.mId && mPtr == aRhs.mPtr;
}
bool operator!=(const MaybeDiscarded<T>& aRhs) const {
return !operator==(aRhs);
}
private:
uint64_t mId = 0;
RefPtr<T> mPtr;
};
} // namespace mozilla::dom
#endif // mozilla_dom_MaybeDiscarded_h
|