webview_interface.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "webview/webview_common.h"
  9. #include <memory>
  10. #include <string>
  11. #include <optional>
  12. #include <functional>
  13. #include <rpl/never.h>
  14. #include <rpl/producer.h>
  15. #include <QtGui/QColor>
  16. // Inspired by https://github.com/webview/webview.
  17. class QWidget;
  18. namespace Webview {
  19. class DataStream;
  20. struct NavigationHistoryState {
  21. std::string url;
  22. std::string title;
  23. bool canGoBack : 1 = false;
  24. bool canGoForward : 1 = false;
  25. friend inline constexpr bool operator==(
  26. NavigationHistoryState,
  27. NavigationHistoryState) = default;
  28. };
  29. class ZoomController {
  30. public:
  31. ZoomController() = default;
  32. [[nodiscard]] virtual rpl::producer<int> zoomValue() {
  33. return rpl::never<int>();
  34. }
  35. virtual void setZoom(int) {
  36. }
  37. };
  38. class Interface {
  39. public:
  40. virtual ~Interface() = default;
  41. virtual void navigate(std::string url) = 0;
  42. virtual void navigateToData(std::string id) = 0;
  43. virtual void reload() = 0;
  44. virtual void init(std::string js) = 0;
  45. virtual void eval(std::string js) = 0;
  46. virtual void focus() = 0;
  47. virtual void setOpaqueBg(QColor opaqueBg) = 0;
  48. [[nodiscard]] virtual QWidget *widget() = 0;
  49. virtual void refreshNavigationHistoryState() = 0;
  50. [[nodiscard]] virtual auto navigationHistoryState()
  51. -> rpl::producer<NavigationHistoryState> = 0;
  52. [[nodiscard]] virtual ZoomController *zoomController() {
  53. return nullptr;
  54. }
  55. };
  56. enum class DialogType {
  57. Alert,
  58. Confirm,
  59. Prompt,
  60. };
  61. struct DialogArgs {
  62. QWidget *parent = nullptr;
  63. DialogType type = DialogType::Alert;
  64. std::string value;
  65. std::string text;
  66. std::string url;
  67. };
  68. struct DialogResult {
  69. std::string text;
  70. bool accepted = false;
  71. };
  72. struct DataResponse {
  73. std::unique_ptr<DataStream> stream;
  74. std::int64_t streamOffset = 0;
  75. std::int64_t totalSize = 0;
  76. };
  77. struct DataRequest {
  78. std::string id;
  79. std::int64_t offset = 0;
  80. std::int64_t limit = 0; // < 0 means "Range: bytes=offset-" header.
  81. std::function<void(DataResponse)> done;
  82. };
  83. enum class DataResult {
  84. Done,
  85. Pending,
  86. Failed,
  87. };
  88. struct Config {
  89. QWidget *parent = nullptr;
  90. QColor opaqueBg;
  91. std::function<void(std::string)> messageHandler;
  92. std::function<bool(std::string,bool)> navigationStartHandler;
  93. std::function<void(bool)> navigationDoneHandler;
  94. std::function<DialogResult(DialogArgs)> dialogHandler;
  95. std::function<DataResult(DataRequest)> dataRequestHandler;
  96. std::string dataProtocolOverride;
  97. std::string userDataPath;
  98. std::string userDataToken;
  99. bool debug = false;
  100. };
  101. struct Available {
  102. enum class Error {
  103. None,
  104. NoWebview2,
  105. NoWebKitGTK,
  106. NoOpenGL,
  107. NonX11,
  108. OldWindows,
  109. };
  110. Error error = Error::None;
  111. bool customSchemeRequests = false;
  112. bool customRangeRequests = false;
  113. bool customReferer = false;
  114. std::string details;
  115. };
  116. void ParseRangeHeaderFor(DataRequest &request, std::string_view header);
  117. [[nodiscard]] Available Availability();
  118. [[nodiscard]] inline bool Supported() {
  119. return Availability().error == Available::Error::None;
  120. }
  121. [[nodiscard]] bool SupportsEmbedAfterCreate();
  122. [[nodiscard]] bool SeparateStorageIdSupported();
  123. // HWND on Windows, nullptr on macOS, GtkWindow on Linux.
  124. [[nodiscard]] std::unique_ptr<Interface> CreateInstance(Config config);
  125. [[nodiscard]] std::string GenerateStorageToken();
  126. void ClearStorageDataByToken(const std::string &token);
  127. } // namespace Webview