Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/HTML/Navigator.cpp (C++)

Code before:
    return 0;
}

GC::Ref<ServiceWorker::ServiceWorkerContainer> Navigator::service_worker()
{
    if (!m_service_worker_container)
        m_service_worker_container = realm().create<ServiceWorker::ServiceWorkerContainer>(realm());
    return *m_service_worker_container;
}

GC::Ref<MediaCapabilitiesAPI::MediaCapabilities> Navigator::media_capabilities()
{
    if (!m_media_capabilities)
        m_media_capabilities = realm().create<MediaCapabilitiesAPI::MediaCapabilities>(realm());
    return *m_media_capabilities;
}

// https://w3c.github.io/battery/
GC::Ref<WebIDL::Promise> Navigator::get_battery()
{
    auto& realm = this->realm();

    // FIXME: This is a stub implementation. getBattery() should:
    //        1. Query actual system battery status
    //        2. Resolve with a BatteryManager object
    //        3. Update when battery status changes

    // Return cached promise to satisfy spec requirement that
    // "the same Promise object MUST be returned each time"
    if (m_battery_promise)
        return *m_battery_promise;

    // Create and cache a rejected promise
    WebIDL::SimpleException exception {
        WebIDL::SimpleExceptionType::TypeError,
        "Battery Status API is not yet implemented"sv
    };
    m_battery_promise = WebIDL::create_rejected_promise_from_exception(realm, move(exception));

    return *m_battery_promise;
}

}


Review comment (suggestion):
This method is specced here:
https://w3c.github.io/battery/#the-getbattery-method

Could you please use this link instead, and copy over the spec steps? Any spec step you don't intend to implement should be marked as a `// FIXME: ...` comment.

Code after:
    return 0;
}

GC::Ref<ServiceWorker::ServiceWorkerContainer> Navigator::service_worker()
{
    if (!m_service_worker_container)
        m_service_worker_container = realm().create<ServiceWorker::ServiceWorkerContainer>(realm());
    return *m_service_worker_container;
}

GC::Ref<MediaCapabilitiesAPI::MediaCapabilities> Navigator::media_capabilities()
{
    if (!m_media_capabilities)
        m_media_capabilities = realm().create<MediaCapabilitiesAPI::MediaCapabilities>(realm());
    return *m_media_capabilities;
}

// https://w3c.github.io/battery/#the-getbattery-method
GC::Ref<WebIDL::Promise> Navigator::get_battery()
{
    auto& realm = this->realm();

    // FIXME: 1. If this.[[BatteryPromise]] is null, then set it to a new promise in this's relevant realm.
    if (!m_battery_promise) {
        WebIDL::SimpleException exception {
            WebIDL::SimpleExceptionType::TypeError,
            "Battery Status API is not yet implemented"sv
        };
        m_battery_promise = WebIDL::create_rejected_promise_from_exception(realm, move(exception));
    }

    // FIXME: 2. If this's relevant global object's associated Document is not allowed to use the "battery"
    // policy-controlled feature, then reject this.[[BatteryPromise]] with a "NotAllowedError" DOMException.

    // FIXME: 3. Otherwise:
    //    1. If this.[[BatteryManager]] is null, then set it to the result of creating a new BatteryManager
    //       in this's relevant realm.

    //    2. Resolve this.[[BatteryPromise]] with this.[[BatteryManager]].

    // 4. Return this.[[BatteryPromise]].
    return *m_battery_promise;
}

}