Source code
Revision control
Copy as Markdown
Other Tools
/* -*- 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
#include "nsBaseClipboard.h"
#include "ContentAnalysis.h"
#include "mozilla/Components.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/CanonicalBrowsingContext.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/PromiseNativeHandler.h"
#include "mozilla/dom/WindowGlobalParent.h"
#include "mozilla/dom/WindowContext.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/MoveOnlyFunction.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/StaticPrefs_widget.h"
#include "nsContentUtils.h"
#include "nsFocusManager.h"
#include "nsIClipboardOwner.h"
#include "nsIPromptService.h"
#include "nsError.h"
#include "nsXPCOM.h"
using mozilla::GenericPromise;
using mozilla::LogLevel;
using mozilla::UniquePtr;
using mozilla::dom::BrowsingContext;
using mozilla::dom::CanonicalBrowsingContext;
using mozilla::dom::ClipboardCapabilities;
using mozilla::dom::Document;
mozilla::LazyLogModule gWidgetClipboardLog("WidgetClipboard");
static const int32_t kGetAvailableFlavorsRetryCount = 5;
namespace {
struct ClipboardGetRequest {
ClipboardGetRequest(const nsTArray<nsCString>& aFlavorList,
nsIClipboardGetDataSnapshotCallback* aCallback)
: mFlavorList(aFlavorList.Clone()), mCallback(aCallback) {}
const nsTArray<nsCString> mFlavorList;
const nsCOMPtr<nsIClipboardGetDataSnapshotCallback> mCallback;
};
class UserConfirmationRequest final
: public mozilla::dom::PromiseNativeHandler {
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(UserConfirmationRequest)
UserConfirmationRequest(nsIClipboard::ClipboardType aClipboardType,
Document* aRequestingChromeDocument,
nsIPrincipal* aRequestingPrincipal,
nsBaseClipboard* aClipboard,
mozilla::dom::WindowContext* aRequestingWindowContext)
: mClipboardType(aClipboardType),
mRequestingChromeDocument(aRequestingChromeDocument),
mRequestingPrincipal(aRequestingPrincipal),
mClipboard(aClipboard),
mRequestingWindowContext(aRequestingWindowContext) {
MOZ_ASSERT(
mClipboard->nsIClipboard::IsClipboardTypeSupported(aClipboardType));
}
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
mozilla::ErrorResult& aRv) override;
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
mozilla::ErrorResult& aRv) override;
bool IsEqual(nsIClipboard::ClipboardType aClipboardType,
Document* aRequestingChromeDocument,
nsIPrincipal* aRequestingPrincipal,
mozilla::dom::WindowContext* aRequestingWindowContext) const {
if (!(ClipboardType() == aClipboardType &&
RequestingChromeDocument() == aRequestingChromeDocument &&
RequestingPrincipal()->Equals(aRequestingPrincipal) &&
(mRequestingWindowContext && aRequestingWindowContext))) {
return false;
}
// Only check requesting window contexts if content analysis is active
nsCOMPtr<nsIContentAnalysis> contentAnalysis =
mozilla::components::nsIContentAnalysis::Service();
if (!contentAnalysis) {
return false;
}
bool contentAnalysisIsActive;
nsresult rv = contentAnalysis->GetIsActive(&contentAnalysisIsActive);
if (MOZ_LIKELY(NS_FAILED(rv) || !contentAnalysisIsActive)) {
return true;
}
return mRequestingWindowContext->Id() == aRequestingWindowContext->Id();
}
nsIClipboard::ClipboardType ClipboardType() const { return mClipboardType; }
Document* RequestingChromeDocument() const {
return mRequestingChromeDocument;
}
nsIPrincipal* RequestingPrincipal() const { return mRequestingPrincipal; }
void AddClipboardGetRequest(const nsTArray<nsCString>& aFlavorList,
nsIClipboardGetDataSnapshotCallback* aCallback) {
MOZ_ASSERT(!aFlavorList.IsEmpty());
MOZ_ASSERT(aCallback);
mPendingClipboardGetRequests.AppendElement(
mozilla::MakeUnique<ClipboardGetRequest>(aFlavorList, aCallback));
}
void RejectPendingClipboardGetRequests(nsresult aError) {
MOZ_ASSERT(NS_FAILED(aError));
auto requests = std::move(mPendingClipboardGetRequests);
for (const auto& request : requests) {
MOZ_ASSERT(request);
MOZ_ASSERT(request->mCallback);
request->mCallback->OnError(aError);
}
}
void ProcessPendingClipboardGetRequests() {
auto requests = std::move(mPendingClipboardGetRequests);
for (const auto& request : requests) {
MOZ_ASSERT(request);
MOZ_ASSERT(!request->mFlavorList.IsEmpty());
MOZ_ASSERT(request->mCallback);
mClipboard->GetDataSnapshotInternal(request->mFlavorList, mClipboardType,
mRequestingWindowContext,
request->mCallback);
}
}
nsTArray<UniquePtr<ClipboardGetRequest>>& GetPendingClipboardGetRequests() {
return mPendingClipboardGetRequests;
}
private:
~UserConfirmationRequest() = default;
const nsIClipboard::ClipboardType mClipboardType;
RefPtr<Document> mRequestingChromeDocument;
const nsCOMPtr<nsIPrincipal> mRequestingPrincipal;
const RefPtr<nsBaseClipboard> mClipboard;
const RefPtr<mozilla::dom::WindowContext> mRequestingWindowContext;
// Track the pending read requests that wait for user confirmation.
nsTArray<UniquePtr<ClipboardGetRequest>> mPendingClipboardGetRequests;
};
NS_IMPL_CYCLE_COLLECTION(UserConfirmationRequest, mRequestingChromeDocument)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(UserConfirmationRequest)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(UserConfirmationRequest)
NS_IMPL_CYCLE_COLLECTING_RELEASE(UserConfirmationRequest)
static mozilla::StaticRefPtr<UserConfirmationRequest> sUserConfirmationRequest;
void UserConfirmationRequest::ResolvedCallback(JSContext* aCx,
JS::Handle<JS::Value> aValue,
mozilla::ErrorResult& aRv) {
MOZ_DIAGNOSTIC_ASSERT(sUserConfirmationRequest == this);
sUserConfirmationRequest = nullptr;
JS::Rooted<JSObject*> detailObj(aCx, &aValue.toObject());
nsCOMPtr<nsIPropertyBag2> propBag;
nsresult rv = mozilla::dom::UnwrapArg<nsIPropertyBag2>(
aCx, detailObj, getter_AddRefs(propBag));
if (NS_FAILED(rv)) {
RejectPendingClipboardGetRequests(rv);
return;
}
bool result = false;
rv = propBag->GetPropertyAsBool(u"ok"_ns, &result);
if (NS_FAILED(rv)) {
RejectPendingClipboardGetRequests(rv);
return;
}
if (!result) {
RejectPendingClipboardGetRequests(NS_ERROR_DOM_NOT_ALLOWED_ERR);
return;
}
ProcessPendingClipboardGetRequests();
}
void UserConfirmationRequest::RejectedCallback(JSContext* aCx,
JS::Handle<JS::Value> aValue,
mozilla::ErrorResult& aRv) {
MOZ_DIAGNOSTIC_ASSERT(sUserConfirmationRequest == this);
sUserConfirmationRequest = nullptr;
RejectPendingClipboardGetRequests(NS_ERROR_FAILURE);
}
} // namespace
NS_IMPL_ISUPPORTS(nsBaseClipboard::AsyncSetClipboardData,
nsIAsyncSetClipboardData)
nsBaseClipboard::AsyncSetClipboardData::AsyncSetClipboardData(
nsIClipboard::ClipboardType aClipboardType, nsBaseClipboard* aClipboard,
mozilla::dom::WindowContext* aSettingWindowContext,
nsIAsyncClipboardRequestCallback* aCallback)
: mClipboardType(aClipboardType),
mClipboard(aClipboard),
mWindowContext(aSettingWindowContext),
mCallback(aCallback) {
MOZ_ASSERT(mClipboard);
MOZ_ASSERT(
mClipboard->nsIClipboard::IsClipboardTypeSupported(mClipboardType));
}
NS_IMETHODIMP
nsBaseClipboard::AsyncSetClipboardData::SetData(nsITransferable* aTransferable,
nsIClipboardOwner* aOwner) {
MOZ_CLIPBOARD_LOG("AsyncSetClipboardData::SetData (%p): clipboard=%d", this,
mClipboardType);
if (!IsValid()) {
return NS_ERROR_FAILURE;
}
if (MOZ_CLIPBOARD_LOG_ENABLED()) {
nsTArray<nsCString> flavors;
if (NS_SUCCEEDED(aTransferable->FlavorsTransferableCanImport(flavors))) {
for (const auto& flavor : flavors) {
MOZ_CLIPBOARD_LOG(" MIME %s", flavor.get());
}
}
}
MOZ_ASSERT(mClipboard);
MOZ_ASSERT(
mClipboard->nsIClipboard::IsClipboardTypeSupported(mClipboardType));
MOZ_DIAGNOSTIC_ASSERT(mClipboard->mPendingWriteRequests[mClipboardType] ==
this);
RefPtr<AsyncSetClipboardData> request =
std::move(mClipboard->mPendingWriteRequests[mClipboardType]);
nsresult rv = mClipboard->SetData(aTransferable, aOwner, mClipboardType,
mWindowContext);
MaybeNotifyCallback(rv);
return rv;
}
NS_IMETHODIMP
nsBaseClipboard::AsyncSetClipboardData::Abort(nsresult aReason) {
// Note: This may be called during destructor, so it should not attempt to
// take a reference to mClipboard.
if (!IsValid() || !NS_FAILED(aReason)) {
return NS_ERROR_FAILURE;
}
MaybeNotifyCallback(aReason);
return NS_OK;
}
void nsBaseClipboard::AsyncSetClipboardData::MaybeNotifyCallback(
nsresult aResult) {
// Note: This may be called during destructor, so it should not attempt to
// take a reference to mClipboard.
MOZ_ASSERT(IsValid());
if (nsCOMPtr<nsIAsyncClipboardRequestCallback> callback =
mCallback.forget()) {
callback->OnComplete(aResult);
}
// Once the callback is notified, setData should not be allowed, so invalidate
// this request.
mClipboard = nullptr;
}
void nsBaseClipboard::RejectPendingAsyncSetDataRequestIfAny(
ClipboardType aClipboardType) {
MOZ_ASSERT(nsIClipboard::IsClipboardTypeSupported(aClipboardType));
auto& request = mPendingWriteRequests[aClipboardType];
if (request) {
request->Abort(NS_ERROR_ABORT);
request = nullptr;
}
}
NS_IMETHODIMP nsBaseClipboard::AsyncSetData(
ClipboardType aWhichClipboard,
mozilla::dom::WindowContext* aSettingWindowContext,
nsIAsyncClipboardRequestCallback* aCallback,
nsIAsyncSetClipboardData** _retval) {
MOZ_CLIPBOARD_LOG("%s: clipboard=%d", __FUNCTION__, aWhichClipboard);
*_retval = nullptr;
if (!nsIClipboard::IsClipboardTypeSupported(aWhichClipboard)) {
MOZ_CLIPBOARD_LOG("%s: clipboard %d is not supported.", __FUNCTION__,
aWhichClipboard);
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
}
// Reject existing pending AsyncSetData request if any.
RejectPendingAsyncSetDataRequestIfAny(aWhichClipboard);
// Create a new AsyncSetClipboardData.
RefPtr<AsyncSetClipboardData> request =
mozilla::MakeRefPtr<AsyncSetClipboardData>(
aWhichClipboard, this, aSettingWindowContext, aCallback);
mPendingWriteRequests[aWhichClipboard] = request;
request.forget(_retval);
return NS_OK;
}
nsBaseClipboard::nsBaseClipboard(const ClipboardCapabilities& aClipboardCaps)
: mClipboardCaps(aClipboardCaps) {
using mozilla::MakeUnique;
// Initialize clipboard cache.
mCaches[kGlobalClipboard] = MakeUnique<ClipboardCache>();
if (mClipboardCaps.supportsSelectionClipboard()) {
mCaches[kSelectionClipboard] = MakeUnique<ClipboardCache>();
}
if (mClipboardCaps.supportsFindClipboard()) {
mCaches[kFindClipboard] = MakeUnique<ClipboardCache>();
}
if (mClipboardCaps.supportsSelectionCache()) {
mCaches[kSelectionCache] = MakeUnique<ClipboardCache>();
}
}
nsBaseClipboard::~nsBaseClipboard() {
for (auto& request : mPendingWriteRequests) {
if (request) {
request->Abort(NS_ERROR_ABORT);
request = nullptr;
}
}
}
NS_IMPL_ISUPPORTS(nsBaseClipboard, nsIClipboard)
/**
* Sets the transferable object
*
*/
NS_IMETHODIMP nsBaseClipboard::SetData(
nsITransferable* aTransferable, nsIClipboardOwner* aOwner,
ClipboardType aWhichClipboard,
mozilla::dom::WindowContext* aWindowContext) {
NS_ASSERTION(aTransferable, "clipboard given a null transferable");
MOZ_CLIPBOARD_LOG("%s: clipboard=%d", __FUNCTION__, aWhichClipboard);
if (!nsIClipboard::IsClipboardTypeSupported(aWhichClipboard)) {
MOZ_CLIPBOARD_LOG("%s: clipboard %d is not supported.", __FUNCTION__,
aWhichClipboard);
return NS_ERROR_FAILURE;
}
if (MOZ_CLIPBOARD_LOG_ENABLED()) {
nsTArray<nsCString> flavors;
if (NS_SUCCEEDED(aTransferable->FlavorsTransferableCanImport(flavors))) {
for (const auto& flavor : flavors) {
MOZ_CLIPBOARD_LOG(" MIME %s", flavor.get());
}
}
}
const auto& clipboardCache = mCaches[aWhichClipboard];
MOZ_ASSERT(clipboardCache);
if (aTransferable == clipboardCache->GetTransferable() &&
aOwner == clipboardCache->GetClipboardOwner()) {
MOZ_CLIPBOARD_LOG("%s: skipping update.", __FUNCTION__);
return NS_OK;
}
clipboardCache->Clear();
nsresult rv = NS_ERROR_FAILURE;
if (aTransferable) {
mIgnoreEmptyNotification = true;
// Reject existing pending asyncSetData request if any.
RejectPendingAsyncSetDataRequestIfAny(aWhichClipboard);
rv = SetNativeClipboardData(aTransferable, aWhichClipboard);
mIgnoreEmptyNotification = false;
}
if (NS_FAILED(rv)) {
MOZ_CLIPBOARD_LOG("%s: setting native clipboard data failed.",
__FUNCTION__);
return rv;
}
auto result = GetNativeClipboardSequenceNumber(aWhichClipboard);
if (result.isErr()) {
MOZ_CLIPBOARD_LOG("%s: getting native clipboard change count failed.",
__FUNCTION__);
return result.unwrapErr();
}
clipboardCache->Update(aTransferable, aOwner, result.unwrap(),
aWindowContext
? mozilla::Some(aWindowContext->InnerWindowId())
: mozilla::Nothing());
return NS_OK;
}
nsresult nsBaseClipboard::GetDataFromClipboardCache(
nsITransferable* aTransferable, ClipboardType aClipboardType) {
MOZ_ASSERT(aTransferable);
MOZ_ASSERT(mozilla::StaticPrefs::widget_clipboard_use_cached_data_enabled());
const auto* clipboardCache = GetClipboardCacheIfValid(aClipboardType);
if (!clipboardCache) {
return NS_ERROR_FAILURE;
}
return clipboardCache->GetData(aTransferable);
}
/**
* Gets the transferable object from system clipboard.
*/
NS_IMETHODIMP nsBaseClipboard::GetData(
nsITransferable* aTransferable, ClipboardType aWhichClipboard,
mozilla::dom::WindowContext* aWindowContext) {
MOZ_CLIPBOARD_LOG("%s: clipboard=%d", __FUNCTION__, aWhichClipboard);
if (!aTransferable) {
NS_ASSERTION(false, "clipboard given a null transferable");
return NS_ERROR_FAILURE;
}
if (!nsIClipboard::IsClipboardTypeSupported(aWhichClipboard)) {
MOZ_CLIPBOARD_LOG("%s: clipboard %d is not supported.", __FUNCTION__,
aWhichClipboard);
return NS_ERROR_FAILURE;
}
if (mozilla::StaticPrefs::widget_clipboard_use_cached_data_enabled()) {
// If we were the last ones to put something on the native clipboard, then
// just use the cached transferable. Otherwise clear it because it isn't
// relevant any more.
if (NS_SUCCEEDED(
GetDataFromClipboardCache(aTransferable, aWhichClipboard))) {
// maybe try to fill in more types? Is there a point?
if (!mozilla::contentanalysis::ContentAnalysis::
CheckClipboardContentAnalysisSync(
this, aWindowContext->Canonical(), aTransferable,
aWhichClipboard)) {
aTransferable->ClearAllData();
return NS_ERROR_CONTENT_BLOCKED;
}
return NS_OK;
}
// at this point we can't satisfy the request from cache data so let's look
// for things other people put on the system clipboard
}
nsresult rv = GetNativeClipboardData(aTransferable, aWhichClipboard);
if (NS_FAILED(rv)) {
return rv;
}
if (!mozilla::contentanalysis::ContentAnalysis::
CheckClipboardContentAnalysisSync(this, aWindowContext->Canonical(),
aTransferable, aWhichClipboard)) {
aTransferable->ClearAllData();
return NS_ERROR_CONTENT_BLOCKED;
}
return NS_OK;
}
void nsBaseClipboard::MaybeRetryGetAvailableFlavors(
const nsTArray<nsCString>& aFlavorList, ClipboardType aWhichClipboard,
nsIClipboardGetDataSnapshotCallback* aCallback, int32_t aRetryCount,
mozilla::dom::WindowContext* aRequestingWindowContext) {
// Note we have to get the clipboard sequence number first before the actual
// read. This is to use it to verify the clipboard data is still the one we
// try to read, instead of the later state.
auto sequenceNumberOrError =
GetNativeClipboardSequenceNumber(aWhichClipboard);
if (sequenceNumberOrError.isErr()) {
MOZ_CLIPBOARD_LOG("%s: unable to get sequence number for clipboard %d.",
__FUNCTION__, aWhichClipboard);
aCallback->OnError(sequenceNumberOrError.unwrapErr());
return;
}
int32_t sequenceNumber = sequenceNumberOrError.unwrap();
AsyncHasNativeClipboardDataMatchingFlavors(
aFlavorList, aWhichClipboard,
[self = RefPtr{this}, callback = nsCOMPtr{aCallback}, aWhichClipboard,
aRetryCount, flavorList = aFlavorList.Clone(), sequenceNumber,
requestingWindowContext =
RefPtr{aRequestingWindowContext}](auto aFlavorsOrError) {
if (aFlavorsOrError.isErr()) {
MOZ_CLIPBOARD_LOG(
"%s: unable to get available flavors for clipboard %d.",
__FUNCTION__, aWhichClipboard);
callback->OnError(aFlavorsOrError.unwrapErr());
return;
}
auto sequenceNumberOrError =
self->GetNativeClipboardSequenceNumber(aWhichClipboard);
if (sequenceNumberOrError.isErr()) {
MOZ_CLIPBOARD_LOG(
"%s: unable to get sequence number for clipboard %d.",
__FUNCTION__, aWhichClipboard);
callback->OnError(sequenceNumberOrError.unwrapErr());
return;
}
if (sequenceNumber == sequenceNumberOrError.unwrap()) {
auto clipboardDataSnapshot =
mozilla::MakeRefPtr<ClipboardDataSnapshot>(
aWhichClipboard, sequenceNumber,
std::move(aFlavorsOrError.unwrap()), false, self,
requestingWindowContext);
callback->OnSuccess(clipboardDataSnapshot);
return;
}
if (aRetryCount > 0) {
MOZ_CLIPBOARD_LOG(
"%s: clipboard=%d, ignore the data due to the sequence number "
"doesn't match, retry (%d) ..",
__FUNCTION__, aWhichClipboard, aRetryCount);
self->MaybeRetryGetAvailableFlavors(flavorList, aWhichClipboard,
callback, aRetryCount - 1,
requestingWindowContext);
return;
}
MOZ_DIAGNOSTIC_CRASH("How can this happen?!?");
callback->OnError(NS_ERROR_FAILURE);
});
}
NS_IMETHODIMP nsBaseClipboard::GetDataSnapshot(
const nsTArray<nsCString>& aFlavorList, ClipboardType aWhichClipboard,
mozilla::dom::WindowContext* aRequestingWindowContext,
nsIPrincipal* aRequestingPrincipal,
nsIClipboardGetDataSnapshotCallback* aCallback) {
MOZ_CLIPBOARD_LOG("%s: clipboard=%d", __FUNCTION__, aWhichClipboard);
if (!aCallback || !aRequestingPrincipal || aFlavorList.IsEmpty()) {
return NS_ERROR_INVALID_ARG;
}
if (!nsIClipboard::IsClipboardTypeSupported(aWhichClipboard)) {
MOZ_CLIPBOARD_LOG("%s: clipboard %d is not supported.", __FUNCTION__,
aWhichClipboard);
return NS_ERROR_FAILURE;
}
// We want to disable security check for automated tests that have the pref
// set to true, or extension that have clipboard read permission.
if (mozilla::StaticPrefs::
dom_events_testing_asyncClipboard_DoNotUseDirectly() ||
nsContentUtils::PrincipalHasPermission(*aRequestingPrincipal,
nsGkAtoms::clipboardRead)) {
GetDataSnapshotInternal(aFlavorList, aWhichClipboard,
aRequestingWindowContext, aCallback);
return NS_OK;
}
// If cache data is valid, we are the last ones to put something on the native
// clipboard, then check if the data is from the same-origin page,
if (auto* clipboardCache = GetClipboardCacheIfValid(aWhichClipboard)) {
nsCOMPtr<nsITransferable> trans = clipboardCache->GetTransferable();
MOZ_ASSERT(trans);
if (nsCOMPtr<nsIPrincipal> principal = trans->GetDataPrincipal()) {
if (aRequestingPrincipal->Subsumes(principal)) {
MOZ_CLIPBOARD_LOG("%s: native clipboard data is from same-origin page.",
__FUNCTION__);
GetDataSnapshotInternal(aFlavorList, aWhichClipboard,
aRequestingWindowContext, aCallback);
return NS_OK;
}
}
}
if (aRequestingPrincipal->GetIsAddonOrExpandedAddonPrincipal()) {
MOZ_CLIPBOARD_LOG("%s: Addon without read permission.", __FUNCTION__);
return aCallback->OnError(NS_ERROR_FAILURE);
}
RequestUserConfirmation(aWhichClipboard, aFlavorList,
aRequestingWindowContext, aRequestingPrincipal,
aCallback);
return NS_OK;
}
already_AddRefed<nsIClipboardDataSnapshot>
nsBaseClipboard::MaybeCreateGetRequestFromClipboardCache(
const nsTArray<nsCString>& aFlavorList, ClipboardType aClipboardType,
mozilla::dom::WindowContext* aRequestingWindowContext) {
MOZ_DIAGNOSTIC_ASSERT(nsIClipboard::IsClipboardTypeSupported(aClipboardType));
if (!mozilla::StaticPrefs::widget_clipboard_use_cached_data_enabled()) {
return nullptr;
}
// If we were the last ones to put something on the native clipboard, then
// just use the cached transferable. Otherwise clear it because it isn't
// relevant any more.
ClipboardCache* clipboardCache = GetClipboardCacheIfValid(aClipboardType);
if (!clipboardCache) {
return nullptr;
}
nsITransferable* cachedTransferable = clipboardCache->GetTransferable();
MOZ_ASSERT(cachedTransferable);
nsTArray<nsCString> transferableFlavors;
if (NS_FAILED(cachedTransferable->FlavorsTransferableCanExport(
transferableFlavors))) {
return nullptr;
}
nsTArray<nsCString> results;
for (const auto& flavor : aFlavorList) {
for (const auto& transferableFlavor : transferableFlavors) {
// XXX We need special check for image as we always put the
// image as "native" on the clipboard.
if (transferableFlavor.Equals(flavor) ||
(transferableFlavor.Equals(kNativeImageMime) &&
nsContentUtils::IsFlavorImage(flavor))) {
MOZ_CLIPBOARD_LOG(" has %s", flavor.get());
results.AppendElement(flavor);
}
}
}
// XXX Do we need to check system clipboard for the flavors that cannot
// be found in cache?
return mozilla::MakeAndAddRef<ClipboardDataSnapshot>(
aClipboardType, clipboardCache->GetSequenceNumber(), std::move(results),
true /* aFromCache */, this, aRequestingWindowContext);
}
void nsBaseClipboard::GetDataSnapshotInternal(
const nsTArray<nsCString>& aFlavorList, ClipboardType aClipboardType,
mozilla::dom::WindowContext* aRequestingWindowContext,
nsIClipboardGetDataSnapshotCallback* aCallback) {
MOZ_ASSERT(nsIClipboard::IsClipboardTypeSupported(aClipboardType));
if (nsCOMPtr<nsIClipboardDataSnapshot> clipboardDataSnapshot =
MaybeCreateGetRequestFromClipboardCache(aFlavorList, aClipboardType,
aRequestingWindowContext)) {
aCallback->OnSuccess(clipboardDataSnapshot);
return;
}
// At this point we can't satisfy the request from cache data so let's
// look for things other people put on the system clipboard.
MaybeRetryGetAvailableFlavors(aFlavorList, aClipboardType, aCallback,
kGetAvailableFlavorsRetryCount,
aRequestingWindowContext);
}
NS_IMETHODIMP nsBaseClipboard::GetDataSnapshotSync(
const nsTArray<nsCString>& aFlavorList, ClipboardType aWhichClipboard,
mozilla::dom::WindowContext* aRequestingWindowContext,
nsIClipboardDataSnapshot** _retval) {
MOZ_CLIPBOARD_LOG("%s: clipboard=%d", __FUNCTION__, aWhichClipboard);
*_retval = nullptr;
if (aFlavorList.IsEmpty()) {
return NS_ERROR_INVALID_ARG;
}
if (!nsIClipboard::IsClipboardTypeSupported(aWhichClipboard)) {
MOZ_CLIPBOARD_LOG("%s: clipboard %d is not supported.", __FUNCTION__,
aWhichClipboard);
return NS_ERROR_FAILURE;
}
if (nsCOMPtr<nsIClipboardDataSnapshot> clipboardDataSnapshot =
MaybeCreateGetRequestFromClipboardCache(aFlavorList, aWhichClipboard,
aRequestingWindowContext)) {
clipboardDataSnapshot.forget(_retval);
return NS_OK;
}
auto sequenceNumberOrError =
GetNativeClipboardSequenceNumber(aWhichClipboard);
if (sequenceNumberOrError.isErr()) {
MOZ_CLIPBOARD_LOG("%s: unable to get sequence number for clipboard %d.",
__FUNCTION__, aWhichClipboard);
return sequenceNumberOrError.unwrapErr();
}
nsTArray<nsCString> results;
for (const auto& flavor : aFlavorList) {
auto resultOrError = HasNativeClipboardDataMatchingFlavors(
AutoTArray<nsCString, 1>{flavor}, aWhichClipboard);
if (resultOrError.isOk() && resultOrError.unwrap()) {
results.AppendElement(flavor);
}
}