no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / widget / gtk / AsyncGtkClipboardRequest.h
blob1d7269156099c0c688a9e26c32bcc3c0c9b8bc65
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_AsyncGtkClipboardRequest_h
8 #define mozilla_AsyncGtkClipboardRequest_h
10 #include "nsClipboard.h"
12 namespace mozilla {
14 // An asynchronous clipboard request that we wait for synchronously by
15 // spinning the event loop.
16 class MOZ_STACK_CLASS AsyncGtkClipboardRequest {
17 // Heap-allocated object that we give GTK as a callback.
18 struct Request {
19 explicit Request(ClipboardDataType aDataType) : mDataType(aDataType) {}
21 void Complete(const void*);
23 const ClipboardDataType mDataType;
24 Maybe<ClipboardData> mData;
25 bool mTimedOut = false;
28 UniquePtr<Request> mRequest;
30 static void OnDataReceived(GtkClipboard*, GtkSelectionData*, gpointer);
31 static void OnTextReceived(GtkClipboard*, const gchar*, gpointer);
33 public:
34 // Launch a request for a particular GTK clipboard. The current status of the
35 // request can be observed by calling HasCompleted() and TakeResult().
36 AsyncGtkClipboardRequest(ClipboardDataType, int32_t aWhichClipboard,
37 const char* aMimeType = nullptr);
39 // Returns whether the request has been answered already.
40 bool HasCompleted() const { return mRequest->mData.isSome(); }
42 // Takes the result from the current request if completed, or a
43 // default-constructed data otherwise. The destructor will take care of
44 // flagging the request as timed out in that case.
45 ClipboardData TakeResult() {
46 if (!HasCompleted()) {
47 return {};
49 auto request = std::move(mRequest);
50 return request->mData.extract();
53 // If completed, frees the request if needed. Otherwise, marks it as a timed
54 // out request so that when it completes the Request object is properly
55 // freed.
56 ~AsyncGtkClipboardRequest();
59 }; // namespace mozilla
61 #endif