Commit 15b160ca authored by Rune Lillesveen's avatar Rune Lillesveen Committed by Commit Bot

Support for battery-savings meta element.

See:
https://github.com/chrishtr/battery-savings/blob/master/explainer.md

Bug: 1114184

Change-Id: I6772febbb0a511f523aab1bc34b2368695d7cad5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2343105Reviewed-by: default avatarChris Harrelson <chrishtr@chromium.org>
Commit-Queue: Chris Harrelson <chrishtr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#795940}
parent 747fc3a0
......@@ -166,6 +166,7 @@ source_set("blink_headers") {
"platform/web_audio_device.h",
"platform/web_audio_latency_hint.h",
"platform/web_audio_source_provider.h",
"platform/web_battery_savings.h",
"platform/web_blob_info.h",
"platform/web_cache.h",
"platform/web_callbacks.h",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_BATTERY_SAVINGS_H_
#define THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_BATTERY_SAVINGS_H_
namespace blink {
// These are constants for the various keywords allowed for the battery-savings
// meta element. For instance:
//
// <meta name="battery-savings" content="allow-reduced-frame-rate">
//
// These constants are bits which can be combined.
enum WebBatterySavings {
kAllowReducedFrameRate = 1 << 0,
kAllowReducedScriptSpeed = 1 << 1,
};
using WebBatterySavingsFlags = unsigned;
} // namespace blink
#endif // THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_BATTERY_SAVINGS_H_
......@@ -69,6 +69,7 @@
#include "third_party/blink/public/mojom/ukm/ukm.mojom-blink.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/public/platform/web_battery_savings.h"
#include "third_party/blink/public/platform/web_content_settings_client.h"
#include "third_party/blink/public/platform/web_theme_engine.h"
#include "third_party/blink/public/web/web_print_page_description.h"
......@@ -7013,6 +7014,32 @@ void Document::ColorSchemeMetaChanged() {
GetStyleEngine().SetColorSchemeFromMeta(color_scheme);
}
void Document::BatterySavingsMetaChanged() {
if (!RuntimeEnabledFeatures::BatterySavingsMetaEnabled())
return;
if (!IsInMainFrame())
return;
auto* root_element = documentElement();
if (!root_element)
return;
for (HTMLMetaElement& meta_element :
Traversal<HTMLMetaElement>::DescendantsOf(*root_element)) {
if (EqualIgnoringASCIICase(meta_element.GetName(), "battery-savings")) {
SpaceSplitString split_content(
AtomicString(meta_element.Content().GetString().LowerASCII()));
WebBatterySavingsFlags savings = 0;
if (split_content.Contains("allow-reduced-framerate"))
savings |= kAllowReducedFrameRate;
if (split_content.Contains("allow-reduced-script-speed"))
savings |= kAllowReducedScriptSpeed;
GetPage()->GetChromeClient().BatterySavingsChanged(*GetFrame(), savings);
return;
}
}
}
static HTMLLinkElement* GetLinkElement(const Document* doc,
bool (*match_fn)(HTMLLinkElement&)) {
HTMLHeadElement* head = doc->head();
......
......@@ -1545,6 +1545,10 @@ class CORE_EXPORT Document : public ContainerNode,
// Update the presentation level color-scheme property for the root element.
void ColorSchemeMetaChanged();
// A META element with name=battery-savings was added, removed, or modified.
// Re-collect the META values that apply and pass to LayerTreeHost.
void BatterySavingsMetaChanged();
// Use counter related functions.
void CountUse(mojom::WebFeature feature) final;
void CountUse(mojom::WebFeature feature) const;
......
......@@ -1885,4 +1885,9 @@ void WebFrameWidgetBase::ForEachRemoteFrameControlledByWidget(
callback);
}
void WebFrameWidgetBase::BatterySavingsChanged(WebBatterySavingsFlags savings) {
widget_base_->LayerTreeHost()->SetEnableFrameRateThrottling(
savings & kAllowReducedFrameRate);
}
} // namespace blink
......@@ -17,6 +17,7 @@
#include "third_party/blink/public/mojom/manifest/display_mode.mojom-blink.h"
#include "third_party/blink/public/mojom/page/widget.mojom-blink.h"
#include "third_party/blink/public/platform/cross_variant_mojo_util.h"
#include "third_party/blink/public/platform/web_battery_savings.h"
#include "third_party/blink/public/platform/web_drag_data.h"
#include "third_party/blink/public/web/web_frame_widget.h"
#include "third_party/blink/renderer/core/clipboard/data_object.h"
......@@ -490,6 +491,10 @@ class CORE_EXPORT WebFrameWidgetBase
// level widget in a tab/window. E.g. a portal is activated or deactivated.
void SetIsNestedMainFrameWidget(bool is_nested);
// The value of the applied battery-savings META element in the document
// changed.
void BatterySavingsChanged(WebBatterySavingsFlags savings);
protected:
enum DragAction { kDragEnter, kDragOver };
......
......@@ -484,6 +484,8 @@ void HTMLMetaElement::NameRemoved(const AtomicString& name_value) {
GetDocument().GetFrame()->DidChangeThemeColor();
} else if (EqualIgnoringASCIICase(name_value, "color-scheme")) {
GetDocument().ColorSchemeMetaChanged();
} else if (EqualIgnoringASCIICase(name_value, "battery-savings")) {
GetDocument().BatterySavingsMetaChanged();
}
}
......@@ -565,6 +567,10 @@ void HTMLMetaElement::ProcessContent() {
GetDocument().ColorSchemeMetaChanged();
return;
}
if (EqualIgnoringASCIICase(name_value, "battery-savings")) {
GetDocument().BatterySavingsMetaChanged();
return;
}
// All situations below require a content attribute (which can be the empty
// string).
......
......@@ -217,6 +217,8 @@ class CORE_EXPORT EmptyChromeClient : public ChromeClient {
void SetCursorForPlugin(const ui::Cursor&, LocalFrame*) override {}
void InstallSupplements(LocalFrame&) override {}
void MainFrameScrollOffsetChanged(LocalFrame& main_frame) const override {}
void BatterySavingsChanged(LocalFrame& main_frame,
WebBatterySavingsFlags savings) override {}
};
class CORE_EXPORT EmptyLocalFrameClient : public LocalFrameClient {
......
......@@ -39,6 +39,7 @@
#include "third_party/blink/public/mojom/devtools/console_message.mojom-blink-forward.h"
#include "third_party/blink/public/mojom/input/focus_type.mojom-blink-forward.h"
#include "third_party/blink/public/platform/blame_context.h"
#include "third_party/blink/public/platform/web_battery_savings.h"
#include "third_party/blink/public/platform/web_float_rect.h"
#include "third_party/blink/public/web/web_swap_result.h"
#include "third_party/blink/public/web/web_widget_client.h"
......@@ -519,6 +520,9 @@ class CORE_EXPORT ChromeClient : public GarbageCollected<ChromeClient> {
LocalFrame* frame,
std::unique_ptr<viz::DelegatedInkMetadata> metadata) {}
virtual void BatterySavingsChanged(LocalFrame& main_frame,
WebBatterySavingsFlags savings) = 0;
protected:
ChromeClient() = default;
......
......@@ -1245,4 +1245,12 @@ void ChromeClientImpl::SetDelegatedInkMetadata(
frame->GetWidgetForLocalRoot()->SetDelegatedInkMetadata(std::move(metadata));
}
void ChromeClientImpl::BatterySavingsChanged(LocalFrame& main_frame,
WebBatterySavingsFlags savings) {
DCHECK(main_frame.IsMainFrame());
WebLocalFrameImpl::FromFrame(main_frame)
->FrameWidgetImpl()
->BatterySavingsChanged(savings);
}
} // namespace blink
......@@ -290,6 +290,9 @@ class CORE_EXPORT ChromeClientImpl final : public ChromeClient {
double UserZoomFactor() const override;
void BatterySavingsChanged(LocalFrame& main_frame,
WebBatterySavingsFlags savings) override;
private:
bool IsChromeClientImpl() const override { return true; }
......
......@@ -239,6 +239,10 @@
name: "BarcodeDetector",
status: "stable",
},
{
// https://github.com/chrishtr/battery-savings/blob/master/explainer.md
name: "BatterySavingsMeta",
},
{
// https://github.com/WICG/display-locking/blob/master/explainer-beforematch.md
name: "BeforeMatchEvent",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment