Avi Drissman | 3a215d1e | 2022-09-07 19:43:09 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Alex Newcomer | 46bfe14 | 2020-07-10 20:28:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef ASH_CLIPBOARD_CLIPBOARD_HISTORY_H_ |
| 6 | #define ASH_CLIPBOARD_CLIPBOARD_HISTORY_H_ |
| 7 | |
Colin Kincaid | e6b1693474 | 2022-07-12 22:50:31 | [diff] [blame] | 8 | #include <deque> |
David Black | 381395a9 | 2020-07-30 00:42:41 | [diff] [blame] | 9 | #include <list> |
Alex Newcomer | 46bfe14 | 2020-07-10 20:28:56 | [diff] [blame] | 10 | |
| 11 | #include "ash/ash_export.h" |
Alex Newcomer | f58dffe | 2020-08-20 21:59:27 | [diff] [blame] | 12 | #include "ash/clipboard/clipboard_history_item.h" |
Alex Newcomer | 8aa90a9 | 2020-07-16 19:37:02 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
Alex Newcomer | f58dffe | 2020-08-20 21:59:27 | [diff] [blame] | 14 | #include "base/observer_list.h" |
Colin Kincaid | e6b1693474 | 2022-07-12 22:50:31 | [diff] [blame] | 15 | #include "base/token.h" |
Alex Newcomer | 46bfe14 | 2020-07-10 20:28:56 | [diff] [blame] | 16 | #include "ui/base/clipboard/clipboard_data.h" |
| 17 | #include "ui/base/clipboard/clipboard_observer.h" |
| 18 | |
| 19 | namespace ash { |
Alex Newcomer | 104c214 | 2020-11-19 02:06:38 | [diff] [blame] | 20 | class ScopedClipboardHistoryPauseImpl; |
| 21 | |
Colin Kincaid | 8e1f7b6 | 2022-08-27 05:09:53 | [diff] [blame] | 22 | namespace clipboard_history_util { |
| 23 | enum class PauseBehavior; |
| 24 | } // namespace clipboard_history_util |
| 25 | |
Andrew Xu | 16c5371 | 2020-08-12 16:30:55 | [diff] [blame] | 26 | // Keeps track of the last few things saved in the clipboard. |
| 27 | class ASH_EXPORT ClipboardHistory : public ui::ClipboardObserver { |
Alex Newcomer | 46bfe14 | 2020-07-10 20:28:56 | [diff] [blame] | 28 | public: |
Alex Newcomer | f58dffe | 2020-08-20 21:59:27 | [diff] [blame] | 29 | class ASH_EXPORT Observer : public base::CheckedObserver { |
| 30 | public: |
| 31 | // Called when a ClipboardHistoryItem has been added. |
Matthew Mourgos | feb96c0 | 2020-12-11 00:47:30 | [diff] [blame] | 32 | virtual void OnClipboardHistoryItemAdded(const ClipboardHistoryItem& item, |
| 33 | bool is_duplicate) {} |
Andrew Xu | e57b31d6 | 2021-01-26 16:32:20 | [diff] [blame] | 34 | |
Alex Newcomer | f58dffe | 2020-08-20 21:59:27 | [diff] [blame] | 35 | // Called when a ClipboardHistoryItem has been removed. |
| 36 | virtual void OnClipboardHistoryItemRemoved( |
| 37 | const ClipboardHistoryItem& item) {} |
Andrew Xu | e57b31d6 | 2021-01-26 16:32:20 | [diff] [blame] | 38 | |
Alex Newcomer | f58dffe | 2020-08-20 21:59:27 | [diff] [blame] | 39 | // Called when ClipboardHistory is Clear()-ed. |
| 40 | virtual void OnClipboardHistoryCleared() {} |
Andrew Xu | e57b31d6 | 2021-01-26 16:32:20 | [diff] [blame] | 41 | |
| 42 | // Called when the operation on clipboard data is confirmed. |
| 43 | virtual void OnOperationConfirmed(bool copy) {} |
Alex Newcomer | f58dffe | 2020-08-20 21:59:27 | [diff] [blame] | 44 | }; |
| 45 | |
Alex Newcomer | 46bfe14 | 2020-07-10 20:28:56 | [diff] [blame] | 46 | ClipboardHistory(); |
| 47 | ClipboardHistory(const ClipboardHistory&) = delete; |
| 48 | ClipboardHistory& operator=(const ClipboardHistory&) = delete; |
| 49 | ~ClipboardHistory() override; |
| 50 | |
Alex Newcomer | f58dffe | 2020-08-20 21:59:27 | [diff] [blame] | 51 | void AddObserver(Observer* observer) const; |
| 52 | void RemoveObserver(Observer* observer) const; |
| 53 | |
Andrew Xu | 16c5371 | 2020-08-12 16:30:55 | [diff] [blame] | 54 | // Returns the list of most recent items. The returned list is sorted by |
| 55 | // recency. |
Alex Newcomer | f58dffe | 2020-08-20 21:59:27 | [diff] [blame] | 56 | const std::list<ClipboardHistoryItem>& GetItems() const; |
Colin Kincaid | 53a2c64c | 2023-02-21 18:15:48 | [diff] [blame] | 57 | std::list<ClipboardHistoryItem>& GetItems(); |
David Black | 381395a9 | 2020-07-30 00:42:41 | [diff] [blame] | 58 | |
Andrew Xu | 16c5371 | 2020-08-12 16:30:55 | [diff] [blame] | 59 | // Deletes clipboard history. Does not modify content stored in the clipboard. |
David Black | 381395a9 | 2020-07-30 00:42:41 | [diff] [blame] | 60 | void Clear(); |
Alex Newcomer | 46bfe14 | 2020-07-10 20:28:56 | [diff] [blame] | 61 | |
Andrew Xu | 0188e45 | 2020-07-25 06:43:01 | [diff] [blame] | 62 | // Returns whether the clipboard history of the active account is empty. |
Alex Newcomer | 46bfe14 | 2020-07-10 20:28:56 | [diff] [blame] | 63 | bool IsEmpty() const; |
| 64 | |
Andrew Xu | 8a988dfc | 2020-08-31 17:44:30 | [diff] [blame] | 65 | // Remove the item specified by `id`. If the target item does not exist, |
| 66 | // do nothing. |
| 67 | void RemoveItemForId(const base::UnguessableToken& id); |
| 68 | |
Alex Newcomer | b6de0c06 | 2021-01-12 00:51:21 | [diff] [blame] | 69 | // ui::ClipboardObserver: |
Alex Newcomer | 46bfe14 | 2020-07-10 20:28:56 | [diff] [blame] | 70 | void OnClipboardDataChanged() override; |
Alex Newcomer | b6de0c06 | 2021-01-12 00:51:21 | [diff] [blame] | 71 | void OnClipboardDataRead() override; |
Alex Newcomer | 46bfe14 | 2020-07-10 20:28:56 | [diff] [blame] | 72 | |
Alex Newcomer | 104c214 | 2020-11-19 02:06:38 | [diff] [blame] | 73 | base::WeakPtr<ClipboardHistory> GetWeakPtr(); |
| 74 | |
Alex Newcomer | 46bfe14 | 2020-07-10 20:28:56 | [diff] [blame] | 75 | private: |
Colin Kincaid | 53a2c64c | 2023-02-21 18:15:48 | [diff] [blame] | 76 | // Friended to allow `ScopedClipboardHistoryPauseImpl` to `Pause()` and |
Alex Newcomer | 104c214 | 2020-11-19 02:06:38 | [diff] [blame] | 77 | // `Resume()`. |
Colin Kincaid | 53a2c64c | 2023-02-21 18:15:48 | [diff] [blame] | 78 | // TODO(b/269470292): Use a `PassKey` for this. |
Alex Newcomer | 104c214 | 2020-11-19 02:06:38 | [diff] [blame] | 79 | friend class ScopedClipboardHistoryPauseImpl; |
| 80 | |
Colin Kincaid | 88a3e84 | 2022-06-16 00:07:16 | [diff] [blame] | 81 | // Ensures that the clipboard buffer contains the same data as the item at the |
| 82 | // top of clipboard history. If clipboard history is empty, then the clipboard |
| 83 | // is cleared. |
| 84 | void SyncClipboardToClipboardHistory(); |
| 85 | |
Colin Kincaid | adbfb08 | 2022-05-26 03:30:34 | [diff] [blame] | 86 | // Adds `data` to the top of the history list if `data` is supported by |
| 87 | // clipboard history. If `data` is not supported, this method no-ops. If |
| 88 | // `data` is already in the history list, `data` will be moved to the top of |
| 89 | // the list. |
Colin Kincaid | f13ac06 | 2022-07-13 21:08:43 | [diff] [blame] | 90 | void MaybeCommitData(ui::ClipboardData data, bool is_reorder_on_paste); |
Andrew Xu | 16c5371 | 2020-08-12 16:30:55 | [diff] [blame] | 91 | |
Colin Kincaid | e6b1693474 | 2022-07-12 22:50:31 | [diff] [blame] | 92 | // When `Pause()` is called, clipboard accesses will modify clipboard history |
| 93 | // according to `pause_behavior` until `Resume()` is called with that pause's |
| 94 | // `pause_id`. If `Pause()` is called while another pause is active, the |
| 95 | // newest pause's behavior will be respected. When the newest pause ends, the |
| 96 | // next newest pause's behavior will be restored. |
Colin Kincaid | 8e1f7b6 | 2022-08-27 05:09:53 | [diff] [blame] | 97 | const base::Token& Pause( |
| 98 | clipboard_history_util::PauseBehavior pause_behavior); |
Colin Kincaid | e6b1693474 | 2022-07-12 22:50:31 | [diff] [blame] | 99 | void Resume(const base::Token& pause_id); |
| 100 | struct PauseInfo { |
| 101 | base::Token pause_id; |
Colin Kincaid | 8e1f7b6 | 2022-08-27 05:09:53 | [diff] [blame] | 102 | clipboard_history_util::PauseBehavior pause_behavior; |
Colin Kincaid | e6b1693474 | 2022-07-12 22:50:31 | [diff] [blame] | 103 | }; |
Alex Newcomer | 46bfe14 | 2020-07-10 20:28:56 | [diff] [blame] | 104 | |
Alex Newcomer | b6de0c06 | 2021-01-12 00:51:21 | [diff] [blame] | 105 | // Keeps track of consecutive clipboard operations and records metrics. |
| 106 | void OnClipboardOperation(bool copy); |
| 107 | |
Colin Kincaid | e6b1693474 | 2022-07-12 22:50:31 | [diff] [blame] | 108 | // Active clipboard history pauses, stored in LIFO order so that the newest |
| 109 | // pause dictates behavior. Rather than a stack, we use a deque where the |
| 110 | // newest pause is added to and removed from the front. Not using a stack |
| 111 | // allows us to find and remove the correct pause in cases where pauses are |
| 112 | // not destroyed in LIFO order, and adding to the front of the deque rather |
| 113 | // than the back allows us to iterate forward when searching for the correct |
| 114 | // pause, simplifying removal logic. |
| 115 | std::deque<PauseInfo> pauses_; |
Colin Kincaid | adbfb08 | 2022-05-26 03:30:34 | [diff] [blame] | 116 | |
Alex Newcomer | b6de0c06 | 2021-01-12 00:51:21 | [diff] [blame] | 117 | // The number of consecutive copies, reset after a paste. |
| 118 | int consecutive_copies_ = 0; |
| 119 | |
| 120 | // The number of consecutive pastes, reset after a copy. |
| 121 | int consecutive_pastes_ = 0; |
| 122 | |
Andrew Xu | 16c5371 | 2020-08-12 16:30:55 | [diff] [blame] | 123 | // The history of data copied to the Clipboard. Items of the list are sorted |
| 124 | // by recency. |
Alex Newcomer | f58dffe | 2020-08-20 21:59:27 | [diff] [blame] | 125 | std::list<ClipboardHistoryItem> history_list_; |
| 126 | |
| 127 | // Mutable to allow adding/removing from |observers_| through a const |
| 128 | // ClipboardHistory. |
| 129 | mutable base::ObserverList<Observer> observers_; |
David Black | a6a0c3ba | 2020-07-30 18:31:33 | [diff] [blame] | 130 | |
Alex Newcomer | b6de0c06 | 2021-01-12 00:51:21 | [diff] [blame] | 131 | // Factory to create WeakPtrs used to debounce calls to `CommitData()`. |
David Black | a6a0c3ba | 2020-07-30 18:31:33 | [diff] [blame] | 132 | base::WeakPtrFactory<ClipboardHistory> commit_data_weak_factory_{this}; |
Alex Newcomer | 104c214 | 2020-11-19 02:06:38 | [diff] [blame] | 133 | |
Alex Newcomer | b6de0c06 | 2021-01-12 00:51:21 | [diff] [blame] | 134 | // Factory to create WeakPtrs used to debounce calls to |
| 135 | // `OnClipboardOperation()`. |
| 136 | base::WeakPtrFactory<ClipboardHistory> clipboard_histogram_weak_factory_{ |
| 137 | this}; |
| 138 | |
Alex Newcomer | 104c214 | 2020-11-19 02:06:38 | [diff] [blame] | 139 | // Factory to create WeakPtrs for ClipboardHistory. |
| 140 | base::WeakPtrFactory<ClipboardHistory> weak_factory_{this}; |
Alex Newcomer | 46bfe14 | 2020-07-10 20:28:56 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | } // namespace ash |
| 144 | |
Andrew Xu | 0188e45 | 2020-07-25 06:43:01 | [diff] [blame] | 145 | #endif // ASH_CLIPBOARD_CLIPBOARD_HISTORY_H_ |