1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=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/. */
6 
7 #ifndef mozilla_AutoCopyListener_h
8 #define mozilla_AutoCopyListener_h
9 
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/Selection.h"
12 #include "mozilla/StaticPrefs_clipboard.h"
13 #include "nsIClipboard.h"
14 
15 namespace mozilla {
16 
17 class AutoCopyListener final {
18  public:
19   /**
20    * OnSelectionChange() is called when a Selection whose NotifyAutoCopy() was
21    * called is changed.
22    *
23    * @param aDocument           The document of the Selection.  May be nullptr.
24    * @param aSelection          The selection.
25    * @param aReason             The reasons of the change.
26    *                            See nsISelectionListener::*_REASON.
27    */
28   static void OnSelectionChange(dom::Document* aDocument,
29                                 dom::Selection& aSelection, int16_t aReason);
30 
31   /**
32    * Init() initializes all static members of this class.  Should be called
33    * only once.
34    */
Init(int16_t aClipboardID)35   static void Init(int16_t aClipboardID) {
36     MOZ_ASSERT(IsValidClipboardID(aClipboardID));
37     static bool sInitialized = false;
38     if (!sInitialized && IsValidClipboardID(aClipboardID)) {
39       sClipboardID = aClipboardID;
40       sInitialized = true;
41     }
42   }
43 
44   /**
45    * IsPrefEnabled() returns true if the pref enables auto-copy feature.
46    */
IsPrefEnabled()47   static bool IsPrefEnabled() { return StaticPrefs::clipboard_autocopy(); }
48 
49  private:
IsValidClipboardID(int16_t aClipboardID)50   static bool IsValidClipboardID(int16_t aClipboardID) {
51     return aClipboardID >= nsIClipboard::kSelectionClipboard &&
52            aClipboardID <= nsIClipboard::kSelectionCache;
53   }
54 
55   static int16_t sClipboardID;
56 };
57 
58 }  // namespace mozilla
59 
60 #endif  // #ifndef mozilla_AutoCopyListener_h
61