Bug 1869043 assert that graph set access is main thread only r=padenot
[gecko.git] / widget / gtk / nsClipboardWayland.cpp
blobd179f242dd47a601db1048d6a900629913688df0
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=2:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "nsClipboardWayland.h"
10 #include "AsyncGtkClipboardRequest.h"
11 #include "mozilla/TimeStamp.h"
12 #include "mozilla/ScopeExit.h"
13 #include "prtime.h"
15 #include <gtk/gtk.h>
16 #include <stdlib.h>
17 #include <string.h>
19 using namespace mozilla;
21 nsRetrievalContextWayland::nsRetrievalContextWayland() = default;
23 ClipboardTargets nsRetrievalContextWayland::GetTargetsImpl(
24 int32_t aWhichClipboard) {
25 LOGCLIP("nsRetrievalContextWayland::GetTargetsImpl()\n");
27 return WaitForClipboardData(ClipboardDataType::Targets, aWhichClipboard)
28 .ExtractTargets();
31 ClipboardData nsRetrievalContextWayland::GetClipboardData(
32 const char* aMimeType, int32_t aWhichClipboard) {
33 LOGCLIP("nsRetrievalContextWayland::GetClipboardData() mime %s\n", aMimeType);
35 return WaitForClipboardData(ClipboardDataType::Data, aWhichClipboard,
36 aMimeType);
39 GUniquePtr<char> nsRetrievalContextWayland::GetClipboardText(
40 int32_t aWhichClipboard) {
41 GdkAtom selection = GetSelectionAtom(aWhichClipboard);
43 LOGCLIP("nsRetrievalContextWayland::GetClipboardText(), clipboard %s\n",
44 (selection == GDK_SELECTION_PRIMARY) ? "Primary" : "Selection");
46 return WaitForClipboardData(ClipboardDataType::Text, aWhichClipboard)
47 .ExtractText();
50 ClipboardData nsRetrievalContextWayland::WaitForClipboardData(
51 ClipboardDataType aDataType, int32_t aWhichClipboard,
52 const char* aMimeType) {
53 LOGCLIP("nsRetrievalContextWayland::WaitForClipboardData, MIME %s\n",
54 aMimeType);
56 AsyncGtkClipboardRequest request(aDataType, aWhichClipboard, aMimeType);
57 int iteration = 1;
59 PRTime entryTime = PR_Now();
60 while (!request.HasCompleted()) {
61 if (iteration++ > kClipboardFastIterationNum) {
62 /* sleep for 10 ms/iteration */
63 PR_Sleep(PR_MillisecondsToInterval(10));
64 if (PR_Now() - entryTime > kClipboardTimeout) {
65 LOGCLIP(" failed to get async clipboard data in time limit\n");
66 break;
69 LOGCLIP("doing iteration %d msec %ld ...\n", (iteration - 1),
70 (long)((PR_Now() - entryTime) / 1000));
71 gtk_main_iteration();
74 return request.TakeResult();