File: clipboard_history_resource_manager.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (109 lines) | stat: -rw-r--r-- 4,273 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_CLIPBOARD_CLIPBOARD_HISTORY_RESOURCE_MANAGER_H_
#define ASH_CLIPBOARD_CLIPBOARD_HISTORY_RESOURCE_MANAGER_H_

#include <string>
#include <vector>

#include "ash/ash_export.h"
#include "ash/clipboard/clipboard_history.h"
#include "ash/clipboard/clipboard_history_item.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/unguessable_token.h"
#include "ui/base/models/image_model.h"

namespace ash {

// Helper class that augments certain instances of `ClipboardHistoryItem` with
// asynchronously retrieved metadata.
class ASH_EXPORT ClipboardHistoryResourceManager
    : public ClipboardHistory::Observer {
 public:
  class Observer : public base::CheckedObserver {
   public:
    // Called when a rendered image model is set on the clipboard history items
    // specified by `menu_item_ids`.
    virtual void OnCachedImageModelUpdated(
        const std::vector<base::UnguessableToken>& menu_item_ids) = 0;
  };

  explicit ClipboardHistoryResourceManager(ClipboardHistory* clipboard_history);
  ClipboardHistoryResourceManager(const ClipboardHistoryResourceManager&) =
      delete;
  ClipboardHistoryResourceManager& operator=(
      const ClipboardHistoryResourceManager&) = delete;
  ~ClipboardHistoryResourceManager() override;

  void AddObserver(Observer* observer) const;
  void RemoveObserver(Observer* observer) const;

 private:
  struct ImageModelRequest {
    ImageModelRequest();
    ImageModelRequest(const ImageModelRequest&);
    ImageModelRequest& operator=(const ImageModelRequest&);
    ~ImageModelRequest();

    // Unique identifier.
    base::UnguessableToken id;

    // IDs of items whose image model will be set to this request's result.
    std::vector<base::UnguessableToken> clipboard_history_item_ids;
  };

  // If `item`'s display text is a URL, queries the primary user profile's
  // browsing history for an associated page title. Asynchronously sets `item`'s
  // secondary display text if a title is found.
  void MaybeQueryUrlTitle(const ClipboardHistoryItem& item);

  // Sets the secondary display text of the `ClipboardHistoryItem` specified by
  // `item_id` with the page title found in the primary user profile's browsing
  // history, if any.
  void OnHistoryQueryComplete(const base::UnguessableToken& item_id,
                              std::optional<std::u16string> maybe_title);

  // Sets `item`'s rendered HTML preview if one is cached; otherwise, ensures
  // that `item` is associated with an asynchronous `ImageModelRequest`.
  void SetOrRequestHtmlPreview(const ClipboardHistoryItem& item);

  // Sets the result `image_model` on each `ClipboardHistoryItem` waiting on the
  // `ImageModelRequest` specified by `id`.
  void OnImageModelRendered(const base::UnguessableToken& id,
                            ui::ImageModel image_model);

  // Finds the pending image model request that `item` is waiting on.
  std::vector<ImageModelRequest>::iterator GetImageModelRequestForItem(
      const ClipboardHistoryItem& item);

  // Cancels all unfinished requests.
  void CancelUnfinishedRequests();

  // ClipboardHistory::Observer:
  void OnClipboardHistoryItemAdded(const ClipboardHistoryItem& item,
                                   bool is_duplicate) override;
  void OnClipboardHistoryItemRemoved(const ClipboardHistoryItem& item) override;
  void OnClipboardHistoryCleared() override;

  // Owned by `ClipboardHistoryController`.
  const raw_ptr<ClipboardHistory> clipboard_history_;

  // Pending requests for image models to be rendered. Once a request finishes,
  // all of the clipboard history items waiting on that image model will be
  // updated, and the request will be removed from this list.
  std::vector<ImageModelRequest> image_model_requests_;

  // Mutable to allow adding/removing from `observers_` through a const
  // `ClipboardHistoryResourceManager`.
  mutable base::ObserverList<Observer> observers_;

  base::WeakPtrFactory<ClipboardHistoryResourceManager> weak_factory_{this};
};

}  // namespace ash

#endif  // ASH_CLIPBOARD_CLIPBOARD_HISTORY_RESOURCE_MANAGER_H_