|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Estefania Sanchez <[email protected]> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <LibWeb/Bindings/Intrinsics.h> |
| 8 | +#include <LibWeb/DOM/Document.h> |
| 9 | +#include <LibWeb/HTML/Navigator.h> |
| 10 | +#include <LibWeb/HTML/NavigatorBadge.h> |
| 11 | +#include <LibWeb/HTML/Scripting/Environments.h> |
| 12 | +#include <LibWeb/HTML/Window.h> |
| 13 | +#include <LibWeb/WebIDL/DOMException.h> |
| 14 | +#include <LibWeb/WebIDL/Promise.h> |
| 15 | + |
| 16 | +namespace Web::HTML { |
| 17 | + |
| 18 | +// https://w3c.github.io/badging/#setting-the-application-badge |
| 19 | +GC::Ref<WebIDL::Promise> NavigatorBadgeMixin::set_app_badge(Optional<u64> contents) |
| 20 | +{ |
| 21 | + // 1. Let global be context's relevant global object. |
| 22 | + auto& window_object = window(); |
| 23 | + auto& realm = window_object.realm(); |
| 24 | + |
| 25 | + // 2. If global is a Window object, then: |
| 26 | + // 2-1. Let document be global's associated Document. |
| 27 | + auto& document = window_object.associated_document(); |
| 28 | + |
| 29 | + // 2-2. If document is not fully active, return a promise rejected with a "InvalidStateError" DOMException. |
| 30 | + if (!document.is_fully_active()) { |
| 31 | + auto exception = WebIDL::InvalidStateError::create(realm, "Document is not fully active"_utf16); |
| 32 | + return WebIDL::create_rejected_promise(realm, exception); |
| 33 | + } |
| 34 | + |
| 35 | + // 2-3. If document's relevant settings object's origin is not same origin-domain with this's relevant settings |
| 36 | + // object's top-level origin, return a promise rejected with a "SecurityError" DOMException. |
| 37 | + auto const document_origin = document.relevant_settings_object().origin(); |
| 38 | + auto navigator = window_object.navigator(); |
| 39 | + auto& this_settings = HTML::relevant_settings_object(*navigator); |
| 40 | + if (this_settings.top_level_origin.has_value() && !document_origin.is_same_origin_domain(this_settings.top_level_origin.value())) { |
| 41 | + auto exception = WebIDL::SecurityError::create(realm, "Document's origin is not same origin-domain with top-level origin"_utf16); |
| 42 | + return WebIDL::create_rejected_promise(realm, exception); |
| 43 | + } |
| 44 | + |
| 45 | + // 3. Let promise be a new promise. |
| 46 | + auto promise = WebIDL::create_promise(realm); |
| 47 | + |
| 48 | + // FIXME: 4. In parallel: |
| 49 | + // FIXME: 4-1. If the user agent requires express permission to set the application badge, then: |
| 50 | + // FIXME: 4-1-1. Let permissionState be the result of getting the current permission state with "notifications". |
| 51 | + // FIXME: 4-1-2. If permissionState is not "granted", queue a global task on the user interaction task source given |
| 52 | + // global to reject promise with a NotAllowedError and terminate this algorithm. |
| 53 | + |
| 54 | + // FIXME: 4-2. Switching on contents, if it happens to be the case that: |
| 55 | + // contents was not passed: Set badge to "flag". |
| 56 | + // contents is 0: Set badge to "nothing". |
| 57 | + // contents: Set badge to contents. |
| 58 | + (void)contents; |
| 59 | + |
| 60 | + // FIXME: 4-3. Queue a global task on the DOM manipulation task source given global to resolve promise with undefined. |
| 61 | + WebIDL::resolve_promise(realm, promise, JS::js_undefined()); |
| 62 | + |
| 63 | + // 5. Return promise. |
| 64 | + return promise; |
| 65 | +} |
| 66 | + |
| 67 | +// https://w3c.github.io/badging/#clearappbadge-method |
| 68 | +GC::Ref<WebIDL::Promise> NavigatorBadgeMixin::clear_app_badge() |
| 69 | +{ |
| 70 | + // When the clearAppBadge() method is called, the user agent MUST set the application badge of this to 0. |
| 71 | + return set_app_badge(0); |
| 72 | +} |
| 73 | + |
| 74 | +} |
0 commit comments