clipboard_recent_content.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_RECENT_CONTENT_H_
  5. #define COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_RECENT_CONTENT_H_
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include "base/callback.h"
  10. #include "base/time/time.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. #include "ui/gfx/image/image.h"
  13. #include "url/gurl.h"
  14. enum class ClipboardContentType { URL, Text, Image };
  15. // Helper class returning an URL if the content of the clipboard can be turned
  16. // into an URL, and if it estimates that the content of the clipboard is not too
  17. // old.
  18. class ClipboardRecentContent {
  19. public:
  20. ClipboardRecentContent();
  21. ClipboardRecentContent(const ClipboardRecentContent&) = delete;
  22. ClipboardRecentContent& operator=(const ClipboardRecentContent&) = delete;
  23. virtual ~ClipboardRecentContent();
  24. // Returns the global instance of the ClipboardRecentContent singleton. This
  25. // method does *not* create the singleton and will return null if no instance
  26. // was registered via SetInstance().
  27. static ClipboardRecentContent* GetInstance();
  28. // Sets the global instance of ClipboardRecentContent singleton.
  29. static void SetInstance(std::unique_ptr<ClipboardRecentContent> new_instance);
  30. // Returns clipboard content as URL, if it has a compatible type,
  31. // is recent enough, has not been suppressed and will not trigger a system
  32. // notification that the clipboard has been accessed.
  33. virtual absl::optional<GURL> GetRecentURLFromClipboard() = 0;
  34. // Returns clipboard content as text, if it has a compatible type,
  35. // is recent enough, has not been suppressed and will not trigger a system
  36. // notification that the clipboard has been accessed.
  37. virtual absl::optional<std::u16string> GetRecentTextFromClipboard() = 0;
  38. // Return if system's clipboard contains an image that will not trigger a
  39. // system notification that the clipboard has been accessed.
  40. virtual bool HasRecentImageFromClipboard() = 0;
  41. // Returns current clipboard content type(s) if it is recent enough and has
  42. // not been suppressed. This value will be nullopt during the brief period
  43. // when the clipboard is updating its cache. More succintly, this value will
  44. // be nullopt when the app is not sure what the latest clipboard contents are,
  45. // or when the value should not be returned due to the clipboard content's age
  46. // being too old. Differently, this value will be the non-nullopt empty set
  47. // when nothing is copied on the clipboard.
  48. //
  49. // Finally, this synchronous method slightly differs from the asynchronous
  50. // method HasRecentContentFromClipboard. This method synchronously returns the
  51. // ContentTypes being used given current pasteboard contents. Whereas
  52. // HasRecentContentFromClipboard exposes functionality to ask the application
  53. // if certain ContentTypes are being used on the clipboard, and retrieve a
  54. // response with the results.
  55. virtual absl::optional<std::set<ClipboardContentType>>
  56. GetCachedClipboardContentTypes() = 0;
  57. /*
  58. On iOS, iOS 14 introduces new clipboard APIs that are async. The asynchronous
  59. forms of clipboard access below should be preferred.
  60. */
  61. using HasDataCallback =
  62. base::OnceCallback<void(std::set<ClipboardContentType>)>;
  63. using GetRecentURLCallback = base::OnceCallback<void(absl::optional<GURL>)>;
  64. using GetRecentTextCallback =
  65. base::OnceCallback<void(absl::optional<std::u16string>)>;
  66. using GetRecentImageCallback =
  67. base::OnceCallback<void(absl::optional<gfx::Image>)>;
  68. // Returns whether the clipboard contains a URL to |HasDataCallback| if it
  69. // is recent enough and has not been suppressed.
  70. virtual void HasRecentContentFromClipboard(
  71. std::set<ClipboardContentType> types,
  72. HasDataCallback callback) = 0;
  73. // Returns clipboard content as URL to |GetRecentURLCallback|, if it has a
  74. // compatible type, is recent enough and has not been suppressed.
  75. virtual void GetRecentURLFromClipboard(GetRecentURLCallback callback) = 0;
  76. // Returns clipboard content as a string to |GetRecentTextCallback|, if it has
  77. // a compatible type, is recent enough and has not been suppressed.
  78. virtual void GetRecentTextFromClipboard(GetRecentTextCallback callback) = 0;
  79. // Returns clipboard content as image to |GetRecentImageCallback|, if it has a
  80. // compatible type, is recent enough and has not been suppressed.
  81. virtual void GetRecentImageFromClipboard(GetRecentImageCallback callback) = 0;
  82. // Returns how old the content of the clipboard is.
  83. virtual base::TimeDelta GetClipboardContentAge() const = 0;
  84. // Prevent GetRecentURLFromClipboard from returning anything until the
  85. // clipboard's content changed.
  86. virtual void SuppressClipboardContent() = 0;
  87. // Clear clipboard content. Different with |SuppressClipboardContent|, this
  88. // function will clear content in the clipboard.
  89. virtual void ClearClipboardContent() = 0;
  90. protected:
  91. // GetRecentURLFromClipboard() should never return a URL from a clipboard
  92. // older than this.
  93. static base::TimeDelta MaximumAgeOfClipboard();
  94. };
  95. #endif // COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_RECENT_CONTENT_H_