Skip to content

Commit

Permalink
Bug 1753067 - Make ClipboardData / ClipboardTargets members private. …
Browse files Browse the repository at this point in the history
…r=stransky

As per https://phabricator.services.mozilla.com/D137549#inline-756817,
though honestly not sure if worth it.

Differential Revision: https://phabricator.services.mozilla.com/D137651
  • Loading branch information
emilio committed Feb 2, 2022
1 parent 7c37841 commit 4755c56
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
13 changes: 8 additions & 5 deletions widget/gtk/nsClipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ bool nsClipboard::FilterImportedFlavors(int32_t aWhichClipboard,
LOGCLIP("nsClipboard::FilterImportedFlavors");

auto targets = mContext->GetTargets(aWhichClipboard);
if (!targets.mTargets) {
if (!targets) {
LOGCLIP(" X11: no targes at clipboard (null), quit.\n");
return true;
}
Expand Down Expand Up @@ -497,8 +497,9 @@ nsClipboard::GetData(nsITransferable* aTransferable, int32_t aWhichClipboard) {
htmlBodyLen * 2);
free(htmlBody);
} else {
SetTransferableData(aTransferable, flavorStr, clipboardData.mData.get(),
clipboardData.mLength);
auto span = clipboardData.AsSpan();
SetTransferableData(aTransferable, flavorStr, span.data(),
span.Length());
}
return NS_OK;
}
Expand Down Expand Up @@ -568,7 +569,8 @@ nsClipboard::HasDataMatchingFlavors(const nsTArray<nsCString>& aFlavorList,

#ifdef MOZ_LOGGING
if (LOGCLIP_ENABLED()) {
LOGCLIP(" Clipboard content (target nums %d):\n", targets.mCount);
LOGCLIP(" Clipboard content (target nums %lu):\n",
targets.AsSpan().Length());
for (const auto& target : targets.AsSpan()) {
GUniquePtr<gchar> atom_name(gdk_atom_name(target));
if (!atom_name) {
Expand All @@ -589,7 +591,8 @@ nsClipboard::HasDataMatchingFlavors(const nsTArray<nsCString>& aFlavorList,
for (auto& flavor : aFlavorList) {
// We special case text/unicode here.
if (flavor.EqualsLiteral(kUnicodeMime) &&
gtk_targets_include_text(targets.mTargets.get(), targets.mCount)) {
gtk_targets_include_text(targets.AsSpan().data(),
targets.AsSpan().Length())) {
*_retval = true;
LOGCLIP(" has kUnicodeMime\n");
break;
Expand Down
18 changes: 16 additions & 2 deletions widget/gtk/nsClipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,37 @@ extern mozilla::LazyLogModule gClipboardLog;
# define LOGCLIP_ENABLED() false
#endif /* MOZ_LOGGING */

struct ClipboardTargets {
class ClipboardTargets {
friend class ClipboardData;

mozilla::GUniquePtr<GdkAtom> mTargets;
uint32_t mCount = 0;

public:
ClipboardTargets() = default;
ClipboardTargets(mozilla::GUniquePtr<GdkAtom> aTargets, uint32_t aCount)
: mTargets(std::move(aTargets)), mCount(aCount) {}

mozilla::Span<GdkAtom> AsSpan() const { return {mTargets.get(), mCount}; }
explicit operator bool() const { return bool(mTargets); }
};

struct ClipboardData {
class ClipboardData {
mozilla::GUniquePtr<char> mData;
uint32_t mLength = 0;

public:
ClipboardData() = default;

void SetData(mozilla::Span<const uint8_t>);
void SetText(mozilla::Span<const char>);
void SetTargets(ClipboardTargets);

ClipboardTargets ExtractTargets();
mozilla::GUniquePtr<char> ExtractText() {
mLength = 0;
return std::move(mData);
}

mozilla::Span<char> AsSpan() const { return {mData.get(), mLength}; }
explicit operator bool() const { return bool(mData); }
Expand Down
10 changes: 6 additions & 4 deletions widget/gtk/nsClipboardWayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,16 @@ void nsRetrievalContextWayland::TransferClipboardData(
LOGCLIP(" getting %d bytes of text.\n", dataLength);
mClipboardData->SetText(
Span(static_cast<const char*>(aData), dataLength));
LOGCLIP(" done, mClipboardData = %p\n", mClipboardData->mData.get());
LOGCLIP(" done, mClipboardData = %p\n",
mClipboardData->AsSpan().data());
break;
}
case ClipboardDataType::Data: {
LOGCLIP(" getting %d bytes of data.\n", dataLength);
mClipboardData->SetData(Span(
gtk_selection_data_get_data((GtkSelectionData*)aData), dataLength));
LOGCLIP(" done, mClipboardData = %p\n", mClipboardData->mData.get());
LOGCLIP(" done, mClipboardData = %p\n",
mClipboardData->AsSpan().data());
break;
}
}
Expand Down Expand Up @@ -194,10 +196,10 @@ GUniquePtr<char> nsRetrievalContextWayland::GetClipboardText(
LOGCLIP("nsRetrievalContextWayland::GetClipboardText(), clipboard %s\n",
(selection == GDK_SELECTION_PRIMARY) ? "Primary" : "Selection");

return WaitForClipboardData(ClipboardDataType::Text, aWhichClipboard).mData;
return WaitForClipboardData(ClipboardDataType::Text, aWhichClipboard)
.ExtractText();
}


ClipboardData nsRetrievalContextWayland::WaitForClipboardContent() {
int iteration = 1;

Expand Down
3 changes: 2 additions & 1 deletion widget/gtk/nsClipboardX11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,6 @@ GUniquePtr<char> nsRetrievalContextX11::GetClipboardText(
aWhichClipboard == nsClipboard::kSelectionClipboard ? "primary"
: "clipboard");

return WaitForClipboardData(ClipboardDataType::Text, aWhichClipboard).mData;
return WaitForClipboardData(ClipboardDataType::Text, aWhichClipboard)
.ExtractText();
}

0 comments on commit 4755c56

Please sign in to comment.