File: clipboard_recent_content.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (118 lines) | stat: -rw-r--r-- 5,240 bytes parent folder | download | duplicates (9)
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
110
111
112
113
114
115
116
117
118
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_RECENT_CONTENT_H_
#define COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_RECENT_CONTENT_H_

#include <memory>
#include <optional>
#include <set>
#include <string>

#include "base/functional/callback.h"
#include "base/time/time.h"
#include "ui/gfx/image/image.h"
#include "url/gurl.h"

enum class ClipboardContentType { URL, Text, Image };

// Helper class returning an URL if the content of the clipboard can be turned
// into an URL, and if it estimates that the content of the clipboard is not too
// old.
class ClipboardRecentContent {
 public:
  ClipboardRecentContent();

  ClipboardRecentContent(const ClipboardRecentContent&) = delete;
  ClipboardRecentContent& operator=(const ClipboardRecentContent&) = delete;

  virtual ~ClipboardRecentContent();

  // Returns the global instance of the ClipboardRecentContent singleton. This
  // method does *not* create the singleton and will return null if no instance
  // was registered via SetInstance().
  static ClipboardRecentContent* GetInstance();

  // Sets the global instance of ClipboardRecentContent singleton.
  static void SetInstance(std::unique_ptr<ClipboardRecentContent> new_instance);

  // Returns clipboard content as URL, if it has a compatible type,
  // is recent enough, has not been suppressed and will not trigger a system
  // notification that the clipboard has been accessed.
  virtual std::optional<GURL> GetRecentURLFromClipboard() = 0;

  // Returns clipboard content as text, if it has a compatible type,
  // is recent enough, has not been suppressed and will not trigger a system
  // notification that the clipboard has been accessed.
  virtual std::optional<std::u16string> GetRecentTextFromClipboard() = 0;

  // Return if system's clipboard contains an image that will not trigger a
  // system notification that the clipboard has been accessed.
  virtual bool HasRecentImageFromClipboard() = 0;

  // Returns current clipboard content type(s) if it is recent enough and has
  // not been suppressed. This value will be nullopt during the brief period
  // when the clipboard is updating its cache. More succintly, this value will
  // be nullopt when the app is not sure what the latest clipboard contents are,
  // or when the value should not be returned due to the clipboard content's age
  // being too old. Differently, this value will be the non-nullopt empty set
  // when nothing is copied on the clipboard.
  //
  // Finally, this synchronous method slightly differs from the asynchronous
  // method HasRecentContentFromClipboard. This method synchronously returns the
  // ContentTypes being used given current pasteboard contents. Whereas
  // HasRecentContentFromClipboard exposes functionality to ask the application
  // if certain ContentTypes are being used on the clipboard, and retrieve a
  // response with the results.
  virtual std::optional<std::set<ClipboardContentType>>
  GetCachedClipboardContentTypes() = 0;

  /*
   On iOS, iOS 14 introduces new clipboard APIs that are async. The asynchronous
   forms of clipboard access below should be preferred.
   */
  using HasDataCallback =
      base::OnceCallback<void(std::set<ClipboardContentType>)>;
  using GetRecentURLCallback = base::OnceCallback<void(std::optional<GURL>)>;
  using GetRecentTextCallback =
      base::OnceCallback<void(std::optional<std::u16string>)>;
  using GetRecentImageCallback =
      base::OnceCallback<void(std::optional<gfx::Image>)>;

  // Returns whether the clipboard contains a URL to |HasDataCallback| if it
  // is recent enough and has not been suppressed.
  virtual void HasRecentContentFromClipboard(
      std::set<ClipboardContentType> types,
      HasDataCallback callback) = 0;

  // Returns clipboard content as URL to |GetRecentURLCallback|, if it has a
  // compatible type, is recent enough and has not been suppressed.
  virtual void GetRecentURLFromClipboard(GetRecentURLCallback callback) = 0;

  // Returns clipboard content as a string to |GetRecentTextCallback|, if it has
  // a compatible type, is recent enough and has not been suppressed.
  virtual void GetRecentTextFromClipboard(GetRecentTextCallback callback) = 0;

  // Returns clipboard content as image to |GetRecentImageCallback|, if it has a
  // compatible type, is recent enough and has not been suppressed.
  virtual void GetRecentImageFromClipboard(GetRecentImageCallback callback) = 0;

  // Returns how old the content of the clipboard is.
  virtual base::TimeDelta GetClipboardContentAge() const = 0;

  // Prevent GetRecentURLFromClipboard from returning anything until the
  // clipboard's content changed.
  virtual void SuppressClipboardContent() = 0;

  // Clear clipboard content. Different with |SuppressClipboardContent|, this
  // function will clear content in the clipboard.
  virtual void ClearClipboardContent() = 0;

 protected:
  // GetRecentURLFromClipboard() should never return a URL from a clipboard
  // older than this.
  static base::TimeDelta MaximumAgeOfClipboard();
};

#endif  // COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_RECENT_CONTENT_H_