/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* 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/. */
class nsITransferable; class nsIClipboardOwner; class nsIPrincipal; class nsIWidget;
namespace mozilla::dom { class WindowContext;
} // namespace mozilla::dom
/** * A base clipboard class for all platform, so that they can share the same * implementation.
*/ class nsBaseClipboard : public nsIClipboard { public: explicit nsBaseClipboard( const mozilla::dom::ClipboardCapabilities& aClipboardCaps);
using GetDataCallback = mozilla::MoveOnlyFunction<void(nsresult)>; using HasMatchingFlavorsCallback = mozilla::MoveOnlyFunction<void(
mozilla::Result<nsTArray<nsCString>, nsresult>)>;
class ClipboardPopulatedDataSnapshot final : public nsIClipboardDataSnapshot { public: explicit ClipboardPopulatedDataSnapshot(nsITransferable* aTransferable);
NS_DECL_ISUPPORTS
NS_DECL_NSICLIPBOARDDATASNAPSHOT private: virtual ~ClipboardPopulatedDataSnapshot() = default;
nsCOMPtr<nsITransferable> mTransferable; // List of available data types for clipboard content.
nsTArray<nsCString> mFlavors;
};
private: virtual ~AsyncSetClipboardData() = default; bool IsValid() const { // If this request is no longer valid, the callback should be notified.
MOZ_ASSERT_IF(!mClipboard, !mCallback); return !!mClipboard;
} void MaybeNotifyCallback(nsresult aResult);
// The clipboard type defined in nsIClipboard.
nsIClipboard::ClipboardType mClipboardType; // It is safe to use a raw pointer as it will be nullified (by calling // NotifyCallback()) once nsBaseClipboard stops tracking us. This is // also used to indicate whether this request is valid.
nsBaseClipboard* mClipboard;
RefPtr<mozilla::dom::WindowContext> mWindowContext; // mCallback will be nullified once the callback is notified to ensure the // callback is only notified once.
nsCOMPtr<nsIAsyncClipboardRequestCallback> mCallback;
};
class ClipboardDataSnapshot final : public nsIClipboardDataSnapshot { public:
ClipboardDataSnapshot(
nsIClipboard::ClipboardType aClipboardType, int32_t aSequenceNumber,
nsTArray<nsCString>&& aFlavors, bool aFromCache,
nsBaseClipboard* aClipboard,
mozilla::dom::WindowContext* aRequestingWindowContext);
// The clipboard type defined in nsIClipboard. const nsIClipboard::ClipboardType mClipboardType; // The sequence number associated with the clipboard content for this // request. If it doesn't match with the current sequence number in system // clipboard, this request targets stale data and is deemed invalid. const int32_t mSequenceNumber; // List of available data types for clipboard content. const nsTArray<nsCString> mFlavors; // Data should be read from cache. constbool mFromCache; // This is also used to indicate whether this request is still valid.
RefPtr<nsBaseClipboard> mClipboard; // The requesting window, which is used for Content Analysis purposes.
RefPtr<mozilla::dom::WindowContext> mRequestingWindowContext;
};
class ClipboardCache final { public:
~ClipboardCache() { // In order to notify the old clipboard owner.
Clear();
}
/** * Clear the cached transferable and notify the original clipboard owner * that it has lost ownership.
*/ void Clear(); void Update(nsITransferable* aTransferable,
nsIClipboardOwner* aClipboardOwner, int32_t aSequenceNumber,
mozilla::Maybe<uint64_t> aInnerWindowId) { // Clear first to notify the old clipboard owner.
Clear();
mTransferable = aTransferable;
mClipboardOwner = aClipboardOwner;
mSequenceNumber = aSequenceNumber;
mInnerWindowId = aInnerWindowId;
}
nsITransferable* GetTransferable() const { return mTransferable; }
nsIClipboardOwner* GetClipboardOwner() const { return mClipboardOwner; }
int32_t GetSequenceNumber() const { return mSequenceNumber; }
mozilla::Maybe<uint64_t> GetInnerWindowId() const { return mInnerWindowId; }
nsresult GetData(nsITransferable* aTransferable) const;
// Return clipboard cache if the cached data is valid, otherwise clear the // cached data and returns null.
ClipboardCache* GetClipboardCacheIfValid(ClipboardType aClipboardType);
// Track the pending request for each clipboard type separately. And only need // to track the latest request for each clipboard type as the prior pending // request will be canceled when a new request is made.
mozilla::Array<RefPtr<AsyncSetClipboardData>,
nsIClipboard::kClipboardTypeCount>
mPendingWriteRequests;