| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik |
| | | 2 | | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| | | 3 | | // |
| | | 4 | | // Cookie store access for the Qt runtime (mirroring, writes, deletion, clear). |
| | | 5 | | // |
| | | 6 | | // THREADING: every function here runs on the Qt GUI thread (the thread that |
| | | 7 | | // created the QApplication). `QWebEngineView` / `QWebEnginePage` / |
| | | 8 | | // `QWebEngineProfile` / `QWebEngineCookieStore` are GUI-thread-affine, and the |
| | | 9 | | // Rust side only calls these from the dispatchers tauri invokes on that thread. |
| | | 10 | | // The cookie signals are documented as *not* a place for heavy work (they block |
| | | 11 | | // Chromium's IO thread while a blocking connection runs); the forwarding below |
| | | 12 | | // is a few map lookups plus one Rust call each, so it stays cheap. |
| | | 13 | | // |
| | | 14 | | // HANDLE CONTRACT: `webview_ptr` is the value returned by |
| | | 15 | | // `qt_webview_native_handle()` — the address of a `QWebEngineView` created by |
| | | 16 | | // `qt_webview_create()`. It is cast back with `reinterpret_cast`. A null or |
| | | 17 | | // unknown pointer makes every function here a no-op (the caller decided the |
| | | 18 | | // handle is valid when it registered the view; the C++ side must not throw |
| | | 19 | | // inside webview creation). |
| | | 20 | | // |
| | | 21 | | // WHAT QT REPORTS (verified against Qt 6.11.2 / its Chromium 140): |
| | | 22 | | // * `cookieAdded` / `cookieRemoved` carry the *canonical* cookie Chromium |
| | | 23 | | // stored: a host-only cookie keeps its bare host as the domain, a domain |
| | | 24 | | // cookie keeps the leading dot (".example.com"), and the path is never |
| | | 25 | | // empty ("/" at minimum). |
| | | 26 | | // * `QWebEngineCookieStore::setCookie()` takes a `QNetworkCookie` and turns it |
| | | 27 | | // into a `Set-Cookie` line (`QNetworkCookie::toRawForm()`), which Chromium |
| | | 28 | | // parses against a URL it synthesises from the cookie's own domain — so a |
| | | 29 | | // write works from any page scheme, including the runtime's custom |
| | | 30 | | // `tauri://` scheme. A *dotless* domain attribute is made a domain cookie by |
| | | 31 | | // Chromium (it prepends the dot) unless the host has no registrable domain |
| | | 32 | | // (`localhost`, IP literals), in which case it stays host-only. |
| | | 33 | | // * `QWebEngineCookieStore::deleteCookie()` builds Chromium's deletion filter |
| | | 34 | | // from the cookie *name* plus a URL derived from the domain — the cookie's |
| | | 35 | | // path is NOT part of the filter, so a delete removes every path variant of |
| | | 36 | | // that name for the host. |
| | | 37 | | // * Qt 6.11 never fills `QNetworkCookie::sameSitePolicy()` when converting a |
| | | 38 | | // stored cookie back for a signal (`type_conversion.h` drops it), so the |
| | | 39 | | // callbacks below always report `Default` (0) for events. The mapping is |
| | | 40 | | // still implemented for the values the enum defines. |
| | | 41 | | // * `loadAllCookies()` emits one `cookieAdded` per stored cookie and never |
| | | 42 | | // signals completion — which is exactly why the Rust side keeps a mirror. |
| | | 43 | | |
| | | 44 | | #include "qt_cookies.hpp" |
| | | 45 | | |
| | | 46 | | // cxx puts the `extern "Rust"` callbacks declared in the bridge module into the |
| | | 47 | | // generated header; never hand-declare them here. |
| | | 48 | | #include "tauri-runtime-qt/src/bridge/cookies.rs.h" |
| | | 49 | | |
| | | 50 | | #include <QtCore/QByteArray> |
| | | 51 | | #include <QtCore/QDateTime> |
| | | 52 | | #include <QtCore/QObject> |
| | | 53 | | #include <QtCore/QString> |
| | | 54 | | #include <QtCore/QUrl> |
| | | 55 | | #include <QtNetwork/QNetworkCookie> |
| | | 56 | | #include <QtWebEngineCore/QWebEngineCookieStore> |
| | | 57 | | #include <QtWebEngineCore/QWebEnginePage> |
| | | 58 | | #include <QtWebEngineCore/QWebEngineProfile> |
| | | 59 | | #include <QtWebEngineWidgets/QWebEngineView> |
| | | 60 | | |
| | | 61 | | #include <cstddef> |
| | | 62 | | #include <cstdint> |
| | | 63 | | #include <unordered_map> |
| | | 64 | | #include <vector> |
| | | 65 | | |
| | | 66 | | namespace { |
| | | 67 | | |
| | | 68 | | /// Views whose stores are mirrored, by view pointer. |
| | | 69 | | std::unordered_map<std::uint64_t, QWebEngineCookieStore*> g_view_stores; |
| | | 70 | | /// Every view watching a store. A key's presence means the store's signals are |
| | | 71 | | /// connected (the vector may be empty after a view is destroyed). |
| | | 72 | | std::unordered_map<QWebEngineCookieStore*, std::vector<std::uint64_t>> g_store_views; |
| | | 73 | | |
| | | 74 | | /// `QNetworkCookie` value -> the bridge's `same_site` code. |
| | 84 | 75 | | std::int32_t same_site_code(QNetworkCookie::SameSite policy) { |
| | 84 | 76 | | switch (policy) { |
| | 0 | 77 | | case QNetworkCookie::SameSite::None: |
| | 0 | 78 | | return 1; |
| | 0 | 79 | | case QNetworkCookie::SameSite::Lax: |
| | 0 | 80 | | return 2; |
| | 0 | 81 | | case QNetworkCookie::SameSite::Strict: |
| | 0 | 82 | | return 3; |
| | 84 | 83 | | case QNetworkCookie::SameSite::Default: |
| | 84 | 84 | | break; |
| | 84 | 85 | | } |
| | 84 | 86 | | return 0; |
| | 84 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// `rust::Str` view over a byte array that stays alive for the call. |
| | 588 | 90 | | rust::Str str_of(const QByteArray& bytes) { |
| | 588 | 91 | | return rust::Str(bytes.constData(), static_cast<std::size_t>(bytes.size())); |
| | 588 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// `rust::Str` bytes as a `QByteArray` (both are UTF-8 by contract). |
| | 105 | 95 | | QByteArray to_qbytearray(rust::Str s) { |
| | 105 | 96 | | return QByteArray(s.data(), static_cast<qsizetype>(s.size())); |
| | 105 | 97 | | } |
| | | 98 | | |
| | 126 | 99 | | QString to_qstring(rust::Str s) { |
| | 126 | 100 | | return QString::fromUtf8(s.data(), static_cast<qsizetype>(s.size())); |
| | 126 | 101 | | } |
| | | 102 | | |
| | 42 | 103 | | QNetworkCookie::SameSite same_site_from_code(std::int32_t code) { |
| | 42 | 104 | | switch (code) { |
| | 42 | 105 | | case 1: |
| | 42 | 106 | | return QNetworkCookie::SameSite::None; |
| | 0 | 107 | | case 2: |
| | 0 | 108 | | return QNetworkCookie::SameSite::Lax; |
| | 0 | 109 | | case 3: |
| | 0 | 110 | | return QNetworkCookie::SameSite::Strict; |
| | 0 | 111 | | default: |
| | 0 | 112 | | return QNetworkCookie::SameSite::Default; |
| | 42 | 113 | | } |
| | 42 | 114 | | } |
| | | 115 | | |
| | 105 | 116 | | QWebEngineCookieStore* store_of(QWebEngineView* view) { |
| | 105 | 117 | | if (view == nullptr || view->page() == nullptr) { |
| | 0 | 118 | | return nullptr; |
| | 0 | 119 | | } |
| | 105 | 120 | | QWebEngineProfile* profile = view->page()->profile(); |
| | 105 | 121 | | return profile != nullptr ? profile->cookieStore() : nullptr; |
| | 105 | 122 | | } |
| | | 123 | | |
| | 105 | 124 | | QWebEngineView* view_of(std::uint64_t webview_ptr) { |
| | 105 | 125 | | return reinterpret_cast<QWebEngineView*>(webview_ptr); |
| | 105 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// Forwards one store event to every Rust mirror watching that store. |
| | 168 | 129 | | void forward_cookie(QWebEngineCookieStore* store, const QNetworkCookie& cookie, bool removed) { |
| | 168 | 130 | | auto it = g_store_views.find(store); |
| | 168 | 131 | | if (it == g_store_views.end() || it->second.empty()) { |
| | 0 | 132 | | return; |
| | 0 | 133 | | } |
| | 168 | 134 | | const QByteArray name = cookie.name(); |
| | 168 | 135 | | const QByteArray domain = cookie.domain().toUtf8(); |
| | 168 | 136 | | const QByteArray path = cookie.path().toUtf8(); |
| | | 137 | | |
| | 168 | 138 | | if (removed) { |
| | 84 | 139 | | for (const std::uint64_t view_ptr : it->second) { |
| | 84 | 140 | | qt_on_cookie_removed(view_ptr, str_of(name), str_of(domain), str_of(path)); |
| | 84 | 141 | | } |
| | 84 | 142 | | return; |
| | 84 | 143 | | } |
| | | 144 | | |
| | 84 | 145 | | const QByteArray value = cookie.value(); |
| | 84 | 146 | | const bool secure = cookie.isSecure(); |
| | 84 | 147 | | const bool http_only = cookie.isHttpOnly(); |
| | 84 | 148 | | const std::int32_t same_site = same_site_code(cookie.sameSitePolicy()); |
| | 84 | 149 | | const std::int64_t expires_ms = cookie.isSessionCookie() |
| | 84 | 150 | | ? -1 |
| | 84 | 151 | | : static_cast<std::int64_t>(cookie.expirationDate().toMSecsSinceEpoch()); |
| | 84 | 152 | | for (const std::uint64_t view_ptr : it->second) { |
| | 84 | 153 | | qt_on_cookie_added(view_ptr, str_of(name), str_of(value), str_of(domain), str_of(path), |
| | 84 | 154 | | secure, http_only, same_site, expires_ms); |
| | 84 | 155 | | } |
| | 84 | 156 | | } |
| | | 157 | | |
| | | 158 | | } // namespace |
| | | 159 | | |
| | 21 | 160 | | void qt_cookies_watch(std::uint64_t webview_ptr) { |
| | 21 | 161 | | QWebEngineView* view = view_of(webview_ptr); |
| | 21 | 162 | | if (view == nullptr || g_view_stores.count(webview_ptr) != 0) { |
| | 0 | 163 | | return; // unknown handle, or already watching this pointer |
| | 0 | 164 | | } |
| | 21 | 165 | | QWebEngineCookieStore* store = store_of(view); |
| | 21 | 166 | | if (store == nullptr) { |
| | 0 | 167 | | return; |
| | 0 | 168 | | } |
| | | 169 | | |
| | 21 | 170 | | g_view_stores.emplace(webview_ptr, store); |
| | 21 | 171 | | std::vector<std::uint64_t>& watchers = g_store_views[store]; |
| | 21 | 172 | | const bool first_watcher = watchers.empty(); |
| | 21 | 173 | | watchers.push_back(webview_ptr); |
| | | 174 | | |
| | | 175 | | // One connection per store: the default profile's store is shared by every |
| | | 176 | | // view the runtime creates, so connecting per view would deliver every |
| | | 177 | | // event once per connected view. |
| | 21 | 178 | | if (first_watcher) { |
| | 21 | 179 | | QObject::connect(store, &QWebEngineCookieStore::cookieAdded, store, |
| | 84 | 180 | | [store](const QNetworkCookie& cookie) { |
| | 84 | 181 | | forward_cookie(store, cookie, false); |
| | 84 | 182 | | }); |
| | 21 | 183 | | QObject::connect(store, &QWebEngineCookieStore::cookieRemoved, store, |
| | 84 | 184 | | [store](const QNetworkCookie& cookie) { |
| | 84 | 185 | | forward_cookie(store, cookie, true); |
| | 84 | 186 | | }); |
| | 21 | 187 | | } |
| | | 188 | | |
| | | 189 | | // Drop the registration with the view so its address can be recycled by a |
| | | 190 | | // later view without leaking events into the old mirror. No context object: |
| | | 191 | | // the functor must run *during* the view's destruction (it only touches this |
| | | 192 | | // file's registries, never the dying view). |
| | 21 | 193 | | QObject::connect(view, &QObject::destroyed, [webview_ptr]() { |
| | 0 | 194 | | auto entry = g_view_stores.find(webview_ptr); |
| | 0 | 195 | | if (entry == g_view_stores.end()) { |
| | 0 | 196 | | return; |
| | 0 | 197 | | } |
| | 0 | 198 | | QWebEngineCookieStore* store = entry->second; |
| | 0 | 199 | | g_view_stores.erase(entry); |
| | 0 | 200 | | auto watchers = g_store_views.find(store); |
| | 0 | 201 | | if (watchers == g_store_views.end()) { |
| | 0 | 202 | | return; |
| | 0 | 203 | | } |
| | 0 | 204 | | std::vector<std::uint64_t>& list = watchers->second; |
| | 0 | 205 | | for (auto it = list.begin(); it != list.end(); ++it) { |
| | 0 | 206 | | if (*it == webview_ptr) { |
| | 0 | 207 | | list.erase(it); |
| | 0 | 208 | | break; |
| | 0 | 209 | | } |
| | 0 | 210 | | } |
| | 0 | 211 | | }); |
| | | 212 | | |
| | | 213 | | // Deterministic fill: emits `cookieAdded` for every cookie the store holds |
| | | 214 | | // (no completion signal, hence the Rust mirror). Repeating it for a second |
| | | 215 | | // view on the same store is harmless — the mirror is keyed by |
| | | 216 | | // name/domain/path and treats an add as an upsert. |
| | 21 | 217 | | store->loadAllCookies(); |
| | 21 | 218 | | } |
| | | 219 | | |
| | | 220 | | void qt_cookies_set(std::uint64_t webview_ptr, |
| | | 221 | | rust::Str name, |
| | | 222 | | rust::Str value, |
| | | 223 | | rust::Str domain, |
| | | 224 | | rust::Str path, |
| | | 225 | | bool secure, |
| | | 226 | | bool http_only, |
| | | 227 | | std::int32_t same_site, |
| | 42 | 228 | | std::int64_t expires_ms) { |
| | 42 | 229 | | QWebEngineView* view = view_of(webview_ptr); |
| | 42 | 230 | | QWebEngineCookieStore* store = store_of(view); |
| | 42 | 231 | | if (store == nullptr) { |
| | 0 | 232 | | return; |
| | 0 | 233 | | } |
| | | 234 | | |
| | 42 | 235 | | QNetworkCookie cookie(to_qbytearray(name), to_qbytearray(value)); |
| | 42 | 236 | | const QString domain_str = to_qstring(domain); |
| | 42 | 237 | | const QString path_str = to_qstring(path); |
| | 42 | 238 | | if (!path_str.isEmpty()) { |
| | 42 | 239 | | cookie.setPath(path_str); |
| | 42 | 240 | | } |
| | 42 | 241 | | cookie.setSecure(secure); |
| | 42 | 242 | | cookie.setHttpOnly(http_only); |
| | 42 | 243 | | if (same_site != 0) { |
| | 42 | 244 | | cookie.setSameSitePolicy(same_site_from_code(same_site)); |
| | 42 | 245 | | } |
| | 42 | 246 | | if (expires_ms >= 0) { |
| | | 247 | | // `expires_ms == -1` (session cookie) leaves the date invalid, which is |
| | | 248 | | // what `isSessionCookie()` and `toRawForm()` key on. |
| | 0 | 249 | | cookie.setExpirationDate(QDateTime::fromMSecsSinceEpoch(expires_ms)); |
| | 0 | 250 | | } |
| | | 251 | | |
| | 42 | 252 | | if (!domain_str.isEmpty()) { |
| | | 253 | | // Domain variant. `setCookie()` without an origin lets Chromium build |
| | | 254 | | // the source URL from `domain + path`, which is what makes the write |
| | | 255 | | // valid from a custom-scheme page. Chromium normalises this to |
| | | 256 | | // ".domain" (domain cookie) when the host has a registrable domain, and |
| | | 257 | | // keeps it bare (host-only) for `localhost` / IP literals — the Rust |
| | | 258 | | // mirror normalises its optimistic key the same way. |
| | 42 | 259 | | cookie.setDomain(domain_str); |
| | 42 | 260 | | store->setCookie(cookie); |
| | 42 | 261 | | return; |
| | 42 | 262 | | } |
| | | 263 | | |
| | | 264 | | // Host-only variant: no domain attribute, the origin URL decides the host. |
| | | 265 | | // (A cookie with neither a domain nor an origin serialises to a URL like |
| | | 266 | | // "http:///" and is dropped by Chromium, so the view's host is used.) |
| | 0 | 267 | | const QUrl page_url = view->url(); |
| | 0 | 268 | | if (page_url.host().isEmpty()) { |
| | 0 | 269 | | return; |
| | 0 | 270 | | } |
| | 0 | 271 | | QUrl origin; |
| | 0 | 272 | | origin.setScheme(secure ? QStringLiteral("https") : QStringLiteral("http")); |
| | 0 | 273 | | origin.setHost(page_url.host()); |
| | 0 | 274 | | origin.setPath(QStringLiteral("/")); |
| | 0 | 275 | | store->setCookie(cookie, origin); |
| | 0 | 276 | | } |
| | | 277 | | |
| | | 278 | | void qt_cookies_delete(std::uint64_t webview_ptr, |
| | | 279 | | rust::Str name, |
| | | 280 | | rust::Str domain, |
| | 21 | 281 | | rust::Str path) { |
| | 21 | 282 | | QWebEngineCookieStore* store = store_of(view_of(webview_ptr)); |
| | 21 | 283 | | if (store == nullptr) { |
| | 0 | 284 | | return; |
| | 0 | 285 | | } |
| | 21 | 286 | | QNetworkCookie cookie(to_qbytearray(name), QByteArray()); |
| | 21 | 287 | | const QString domain_str = to_qstring(domain); |
| | 21 | 288 | | if (!domain_str.isEmpty()) { |
| | | 289 | | // Chromium's deletion filter is built from the name plus the URL |
| | | 290 | | // synthesised from the domain; the path does not restrict the filter. |
| | 21 | 291 | | cookie.setDomain(domain_str); |
| | 21 | 292 | | } |
| | 21 | 293 | | const QString path_str = to_qstring(path); |
| | 21 | 294 | | if (!path_str.isEmpty()) { |
| | 21 | 295 | | cookie.setPath(path_str); |
| | 21 | 296 | | } |
| | 21 | 297 | | store->deleteCookie(cookie); |
| | 21 | 298 | | } |
| | | 299 | | |
| | 21 | 300 | | void qt_cookies_clear_all(std::uint64_t webview_ptr) { |
| | 21 | 301 | | QWebEngineView* view = view_of(webview_ptr); |
| | 21 | 302 | | QWebEngineCookieStore* store = store_of(view); |
| | 21 | 303 | | if (store == nullptr) { |
| | 0 | 304 | | return; |
| | 0 | 305 | | } |
| | 21 | 306 | | store->deleteAllCookies(); |
| | 21 | 307 | | QWebEngineProfile* profile = view->page()->profile(); |
| | 21 | 308 | | if (profile != nullptr) { |
| | 21 | 309 | | profile->clearHttpCache(); |
| | 21 | 310 | | } |
| | | 311 | | // NOT cleared (no API for it on a live profile): localStorage / |
| | | 312 | | // sessionStorage, IndexedDB, service-worker registrations and Cache |
| | | 313 | | // Storage. All of them live under the profile's storage directory, which |
| | | 314 | | // this runtime does not delete while the profile is alive. Visited links |
| | | 315 | | // are cleared by `qt_webview_clear_browsing_data()` (the W3 webview op), |
| | | 316 | | // not here: this function is the cookie/HTTP-cache half. |
| | 21 | 317 | | } |