/* -*- 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/. */
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;
class UserConfirmationRequest final
: public mozilla::dom::PromiseNativeHandler { public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(UserConfirmationRequest)
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;
}
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 (constauto& flavor : flavors) {
MOZ_CLIPBOARD_LOG(" MIME %s", flavor.get());
}
}
}
/** * 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);
});
}
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;
}
}
}
// TODO: enable showing the "Paste" button in this case; see bug 1773681. if (aRequestingPrincipal->GetIsAddonOrExpandedAddonPrincipal()) {
MOZ_CLIPBOARD_LOG("%s: Addon without read permission.", __FUNCTION__); return aCallback->OnError(NS_ERROR_FAILURE);
}
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;
}
nsTArray<nsCString> transferableFlavors; if (NS_FAILED(cachedTransferable->FlavorsTransferableCanExport(
transferableFlavors))) { return nullptr;
}
nsTArray<nsCString> results; for (constauto& flavor : aFlavorList) { for (constauto& 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);
}
// 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);
}
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;
}
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 (constauto& flavor : aFlavorList) { auto resultOrError = HasNativeClipboardDataMatchingFlavors(
AutoTArray<nsCString, 1>{flavor}, aWhichClipboard); if (resultOrError.isOk() && resultOrError.unwrap()) {
results.AppendElement(flavor);
}
}
if (!nsIClipboard::IsClipboardTypeSupported(aWhichClipboard)) {
MOZ_CLIPBOARD_LOG("%s: clipboard %d is not supported.", __FUNCTION__,
aWhichClipboard); return NS_ERROR_FAILURE;
}
if (mIgnoreEmptyNotification) {
MOZ_DIAGNOSTIC_ASSERT(!clipboardCache->GetTransferable() &&
!clipboardCache->GetClipboardOwner() &&
clipboardCache->GetSequenceNumber() == -1, "How did we have data in clipboard cache here?"); return NS_OK;
}
if (!nsIClipboard::IsClipboardTypeSupported(aWhichClipboard)) {
MOZ_CLIPBOARD_LOG("%s: clipboard %d is not supported.", __FUNCTION__,
aWhichClipboard); return NS_ERROR_FAILURE;
}
if (MOZ_CLIPBOARD_LOG_ENABLED()) {
MOZ_CLIPBOARD_LOG(" Asking for content clipboard=%i:\n",
aWhichClipboard); for (constauto& flavor : aFlavorList) {
MOZ_CLIPBOARD_LOG(" MIME %s", flavor.get());
}
}
*aOutResult = false;
if (mozilla::StaticPrefs::widget_clipboard_use_cached_data_enabled()) { // First, check if we have valid data in our cached transferable. auto flavorsOrError = GetFlavorsFromClipboardCache(aWhichClipboard); if (flavorsOrError.isOk()) { for (constauto& transferableFlavor : flavorsOrError.unwrap()) { for (constauto& flavor : aFlavorList) { if (transferableFlavor.Equals(flavor)) {
MOZ_CLIPBOARD_LOG(" has %s", flavor.get());
*aOutResult = true; return NS_OK;
}
}
}
}
}
auto resultOrError =
HasNativeClipboardDataMatchingFlavors(aFlavorList, aWhichClipboard); if (resultOrError.isErr()) {
MOZ_CLIPBOARD_LOG( "%s: checking native clipboard data matching flavors falied.",
__FUNCTION__); return resultOrError.unwrapErr();
}
if (!aWindowContext) {
aCallback->OnError(NS_ERROR_FAILURE); return;
}
CanonicalBrowsingContext* cbc =
CanonicalBrowsingContext::Cast(aWindowContext->GetBrowsingContext());
MOZ_ASSERT(
cbc->IsContent(), "Should not require user confirmation when access from chrome window");
RefPtr<CanonicalBrowsingContext> chromeTop = cbc->TopCrossChromeBoundary();
Document* chromeDoc = chromeTop ? chromeTop->GetDocument() : nullptr; if (!chromeDoc || !chromeDoc->HasFocus(mozilla::IgnoreErrors())) {
MOZ_CLIPBOARD_LOG("%s: reject due to not in the focused window",
__FUNCTION__);
aCallback->OnError(NS_ERROR_FAILURE); return;
}
mozilla::dom::Element* activeElementInChromeDoc =
chromeDoc->GetActiveElement(); if (activeElementInChromeDoc != cbc->Top()->GetEmbedderElement()) { // Reject if the request is not from web content that is in the focused tab.
MOZ_CLIPBOARD_LOG("%s: reject due to not in the focused tab", __FUNCTION__);
aCallback->OnError(NS_ERROR_FAILURE); return;
}
// If there is a pending user confirmation request, check if we could reuse // it. If not, reject the request. if (sUserConfirmationRequest) { if (sUserConfirmationRequest->IsEqual(
aClipboardType, chromeDoc, aRequestingPrincipal, aWindowContext)) {
sUserConfirmationRequest->AddClipboardGetRequest(aFlavorList, aCallback); return;
}
// If the requested flavor is not in the list, throw an error. for (constauto& flavor : flavors) { if (!mFlavors.Contains(flavor)) { return NS_ERROR_FAILURE;
}
}
if (!IsValid()) {
aCallback->OnComplete(NS_ERROR_NOT_AVAILABLE); return NS_OK;
}
if (mFromCache) { constauto* clipboardCache =
mClipboard->GetClipboardCacheIfValid(mClipboardType); // `IsValid()` above ensures we should get a valid cache and matched // sequence number here.
MOZ_DIAGNOSTIC_ASSERT(clipboardCache);
MOZ_DIAGNOSTIC_ASSERT(clipboardCache->GetSequenceNumber() ==
mSequenceNumber); if (NS_SUCCEEDED(clipboardCache->GetData(aTransferable))) {
mozilla::contentanalysis::ContentAnalysis::CheckClipboardContentAnalysis(
mClipboard,
mRequestingWindowContext ? mRequestingWindowContext->Canonical()
: nullptr,
aTransferable, mClipboardType, contentAnalysisCallback); 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.
}
// Since this is an async operation, we need to check if the data is still // valid after we get the result.
mClipboard->AsyncGetNativeClipboardData(
aTransferable, mClipboardType,
[callback = nsCOMPtr{aCallback}, self = RefPtr{this},
transferable = nsCOMPtr{aTransferable},
contentAnalysisCallback =
std::move(contentAnalysisCallback)](nsresult aResult) mutable { if (NS_FAILED(aResult)) {
callback->OnComplete(aResult); return;
} // `IsValid()` checks the clipboard sequence number to ensure the data // we are requesting is still valid. if (!self->IsValid()) {
callback->OnComplete(NS_ERROR_NOT_AVAILABLE); return;
}
mozilla::contentanalysis::ContentAnalysis::
CheckClipboardContentAnalysis(
self->mClipboard,
self->mRequestingWindowContext
? self->mRequestingWindowContext->Canonical()
: nullptr,
transferable, self->mClipboardType, contentAnalysisCallback);
}); return NS_OK;
}
// If the requested flavor is not in the list, throw an error. for (constauto& flavor : flavors) { if (!mFlavors.Contains(flavor)) { return NS_ERROR_FAILURE;
}
}
if (!IsValid()) { return NS_ERROR_NOT_AVAILABLE;
}
MOZ_ASSERT(mClipboard);
if (mFromCache) { constauto* clipboardCache =
mClipboard->GetClipboardCacheIfValid(mClipboardType); // `IsValid()` above ensures we should get a valid cache and matched // sequence number here.
MOZ_DIAGNOSTIC_ASSERT(clipboardCache);
MOZ_DIAGNOSTIC_ASSERT(clipboardCache->GetSequenceNumber() ==
mSequenceNumber); if (NS_SUCCEEDED(clipboardCache->GetData(aTransferable))) { bool shouldAllowContent = mozilla::contentanalysis::ContentAnalysis::
CheckClipboardContentAnalysisSync(
mClipboard,
mRequestingWindowContext ? mRequestingWindowContext->Canonical()
: nullptr,
aTransferable, mClipboardType); if (shouldAllowContent) { return NS_OK;
}
aTransferable->ClearAllData(); return NS_ERROR_CONTENT_BLOCKED;
}
// 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.
}
rv = mClipboard->GetNativeClipboardData(aTransferable, mClipboardType); if (NS_FAILED(rv)) { return rv;
}
bool nsBaseClipboard::ClipboardDataSnapshot::IsValid() { if (!mClipboard) { returnfalse;
}
// If the data should from cache, check if cache is still valid or the // sequence numbers are matched. if (mFromCache) { constauto* clipboardCache =
mClipboard->GetClipboardCacheIfValid(mClipboardType); if (!clipboardCache) {
mClipboard = nullptr; returnfalse;
}
NS_IMETHODIMP nsBaseClipboard::ClipboardPopulatedDataSnapshot::GetValid( bool* aOutResult) { // Since this is a snapshot of what the clipboard data was, this is always // valid
*aOutResult = true; return NS_OK;
}
// If the requested flavor is not in the list, throw an error. for (constauto& flavor : flavors) { if (!mFlavors.Contains(flavor)) { return NS_ERROR_FAILURE;
}
}
// This method only fills in the data for the first flavor passed in. This // seems weird but matches the IDL documentation and behavior. if (!flavors.IsEmpty()) {
nsCOMPtr<nsISupports> data;
rv = mTransferable->GetTransferData(flavors[0].get(), getter_AddRefs(data)); if (NS_FAILED(rv)) {
aTransferable->ClearAllData(); return rv;
}
rv = aTransferable->SetTransferData(flavors[0].get(), data); if (NS_FAILED(rv)) {
aTransferable->ClearAllData(); return rv;
}
} return NS_OK;
}
// get flavor list that includes all acceptable flavors (including ones // obtained through conversion)
nsTArray<nsCString> flavors; if (NS_FAILED(aTransferable->FlavorsTransferableCanImport(flavors))) { return NS_ERROR_FAILURE;
}
MOZ_ASSERT(mTransferable); for (constauto& flavor : flavors) {
nsCOMPtr<nsISupports> dataSupports; // XXX Maybe we need special check for image as we always put the image as // "native" on the clipboard. if (NS_SUCCEEDED(mTransferable->GetTransferData(
flavor.get(), getter_AddRefs(dataSupports)))) {
MOZ_CLIPBOARD_LOG("%s: getting %s from cache.", __FUNCTION__,
flavor.get());
aTransferable->SetTransferData(flavor.get(), dataSupports); // XXX we only read the first available type from native clipboard, so // make cache behave the same. return NS_OK;
}
}
return NS_ERROR_FAILURE;
}
¤ Dauer der Verarbeitung: 0.25 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.