1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/compute_pressure/pressure_service_base.h"
#include <algorithm>
#include <utility>
#include "base/feature_list.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/device_service.h"
#include "content/public/browser/video_picture_in_picture_window_controller.h"
#include "mojo/public/cpp/bindings/message.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "services/device/public/cpp/device_features.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "base/check.h"
#endif
namespace {
constexpr char kVirtualPressureSourceStartConsoleMessage[] =
"Non-virtual observers are running while a virtual source is being added. "
"The virtual source will have no effect on any observer as long as "
"observers are still running. Stop all the observers and restart them if "
"they want to use the virtual source.";
constexpr char kVirtualPressureSourceStopConsoleMessage[] =
"Virtual observers are still running after the virtual source was "
"disconnected. The non-virtual source will have no effect on any observer "
"as long as observers are still running. Stop all the observers and "
"restart them if they want to use the non-virtual source.";
} // namespace
namespace content {
PressureServiceBase::PressureServiceBase()
: source_to_client_{PressureClientImpl(this)} {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (base::FeatureList::IsEnabled(
features::kComputePressureBreakCalibrationMitigation)) {
converter_.EnableStateRandomizationMitigation();
}
}
PressureServiceBase::~PressureServiceBase() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Manually remove the observer here instead of using
// base::ScopedObserver. In general, this class will be destroyed before
// the observable (WebContentsPressureManagerProxy) but in some cases
// (e.g. active PressureObserver instances in both a shared worker and a
// dedicated worker may cause the PressureServiceForDedicatedWorker to be
// destroyed only when its DedicatedWorkerHost's RenderProcessHost is
// destroyed, which happens after WebContentsPressureManagerProxy object is
// destroyed) this is not true. The condition above can be reproduced by
// ComputePressureBrowserTest when SupportsSharedWorker() is true and shared
// workers are used.
auto* pressure_manager_proxy = GetWebContentsPressureManagerProxy();
if (pressure_manager_proxy) {
pressure_manager_proxy->RemoveObserver(this);
}
}
// static
// https://www.w3.org/TR/compute-pressure/#dfn-document-has-implicit-focus
bool PressureServiceBase::HasImplicitFocus(RenderFrameHost* render_frame_host) {
#if BUILDFLAG(IS_CHROMEOS)
// TODO: http://crbug.com/407801065
// On ChromeOS, in rare occasions render_frame_host may be nullptr. The
// following DUMP_WILL_BE_CHECK() is used to provide additional information
// for diagnosis.
DUMP_WILL_BE_CHECK(render_frame_host);
#endif
if (!render_frame_host) {
return false;
}
// 1. If document is not fully active, return false.
if (!render_frame_host->IsActive()) {
return false;
}
WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
WebContents::FromRenderFrameHost(render_frame_host));
const auto& current_origin = render_frame_host->GetLastCommittedOrigin();
// 3. If associated document is same origin with initiators of active
// Picture-in-Picture sessions, return true.
if (std::ranges::any_of(
WebContentsImpl::GetAllWebContents(), [&](WebContentsImpl* wc) {
if (!wc->HasPictureInPictureVideo()) {
return false;
}
auto origin = PictureInPictureWindowController::
GetOrCreateVideoPictureInPictureController(wc)
->GetOrigin();
return current_origin == origin.value() &&
wc->GetBrowserContext() == web_contents->GetBrowserContext();
})) {
return true;
}
// 4. If browsing context is capturing, return true.
// TODO(crbug.com/40945930): Take muted state into account.
if (static_cast<RenderFrameHostImpl*>(render_frame_host)
->HasMediaStreams(
RenderFrameHostImpl::MediaStreamType::kCapturingMediaStream)) {
return true;
}
// 6. If top-level browsing context does not have system focus, return false.
RenderWidgetHostImpl* rwh = static_cast<RenderWidgetHostImpl*>(
render_frame_host->GetRenderWidgetHost());
if (!rwh->is_focused()) {
return false;
}
// 7. Let focused document be the currently focused area's node document.
auto* focused_frame = web_contents->GetFocusedFrame();
if (!focused_frame) {
return false;
}
// 8. If origin is same origin with focused document, return true.
// 9. Otherwise, return false.
return current_origin.IsSameOriginWith(
focused_frame->GetLastCommittedOrigin());
}
device::mojom::PressureState PressureServiceBase::CalculateState(
double pressure_value) {
return converter_.CalculateState(pressure_value);
}
bool PressureServiceBase::CanCallAddClient() const {
return true;
}
void PressureServiceBase::BindReceiver(
mojo::PendingReceiver<blink::mojom::WebPressureManager> receiver) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (manager_receiver_.is_bound()) {
mojo::ReportBadMessage("PressureService is already connected.");
return;
}
manager_receiver_.Bind(std::move(receiver));
// base::Unretained is safe because Mojo guarantees the callback will not
// be called after `associated_manager_receiver_` is deallocated,
// and `associated_manager_receiver_` is owned by this class.
manager_receiver_.set_disconnect_handler(
base::BindRepeating(&PressureServiceBase::OnPressureManagerDisconnected,
base::Unretained(this)));
auto* pressure_manager_proxy =
GetWebContentsPressureManagerProxy(/*allow_creation*/true);
if (pressure_manager_proxy) {
pressure_manager_proxy->AddObserver(this);
}
}
void PressureServiceBase::AddClient(
device::mojom::PressureSource source,
mojo::PendingAssociatedRemote<blink::mojom::WebPressureClient> client,
AddClientCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!CanCallAddClient()) {
std::move(callback).Run(
device::mojom::PressureManagerAddClientResult::kNotSupported);
return;
}
auto& pressure_client = source_to_client_[static_cast<size_t>(source)];
if (pressure_client.is_client_associated_remote_bound()) {
manager_receiver_.ReportBadMessage(
"PressureClientImpl is already connected.");
// manager_receiver_.ReportBadMessage() will reset `manager_receiver_` and
// so clean up as if the pipe had been disconnected.
OnPressureManagerDisconnected();
return;
}
pressure_client.BindPendingAssociatedRemote(std::move(client));
if (!manager_remote_.is_bound()) {
auto manager_receiver = manager_remote_.BindNewPipeAndPassReceiver();
manager_remote_.set_disconnect_handler(
base::BindRepeating(&PressureServiceBase::OnPressureManagerDisconnected,
base::Unretained(this)));
GetDeviceService().BindPressureManager(std::move(manager_receiver));
}
if (pressure_client.is_client_receiver_bound()) {
std::move(callback).Run(device::mojom::PressureManagerAddClientResult::kOk);
} else {
const std::optional<base::UnguessableToken>& token = GetTokenFor(source);
manager_remote_->AddClient(
source, token, pressure_client.BindNewEndpointAndPassRemote(),
base::BindOnce(&PressureServiceBase::DidAddClient,
weak_ptr_factory_.GetWeakPtr(), source, token,
std::move(callback)));
}
}
void PressureServiceBase::DidAddVirtualPressureSource(
device::mojom::PressureSource source) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const auto& pressure_client = source_to_client_[static_cast<size_t>(source)];
if (pressure_client.pressure_source_type() ==
PressureClientImpl::PressureSourceType::kNonVirtual) {
AddMessageToConsole(kVirtualPressureSourceStartConsoleMessage);
}
}
void PressureServiceBase::DidRemoveVirtualPressureSource(
device::mojom::PressureSource source) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const auto& pressure_client = source_to_client_[static_cast<size_t>(source)];
if (pressure_client.pressure_source_type() ==
PressureClientImpl::PressureSourceType::kVirtual) {
AddMessageToConsole(kVirtualPressureSourceStopConsoleMessage);
}
}
WebContentsPressureManagerProxy*
PressureServiceBase::GetWebContentsPressureManagerProxy(
bool allow_creation) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
RenderFrameHost* rfh = GetRenderFrameHost();
auto* web_contents = WebContents::FromRenderFrameHost(rfh);
// Checking the validity of RenderFrameHost* because in some cases as
// explained in ~PressureServiceBase(), the order of destruction might not be
// as expected.
if (rfh && web_contents) {
return allow_creation
? WebContentsPressureManagerProxy::GetOrCreate(web_contents)
: WebContentsPressureManagerProxy::FromWebContents(web_contents);
} else {
return nullptr;
}
}
RenderFrameHost* PressureServiceBase::GetRenderFrameHost() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return nullptr;
}
void PressureServiceBase::AddMessageToConsole(
const std::string& message) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
RenderFrameHost* rfh = GetRenderFrameHost();
CHECK(rfh);
rfh->AddMessageToConsole(blink::mojom::ConsoleMessageLevel::kInfo, message);
}
// Disconnection handler for |associated_manager_receiver_| and
// |associated_manager_remote_|. If either of the connections breaks, we should
// disconnect all connections and let //services know we do not need more
// updates.
void PressureServiceBase::OnPressureManagerDisconnected() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
manager_receiver_.reset();
manager_remote_.reset();
// In case the client was removed from Blink before service binding
// has occurred. Reset is needed.
for (auto& client : source_to_client_) {
client.Reset();
}
auto* pressure_manager_proxy = GetWebContentsPressureManagerProxy();
if (pressure_manager_proxy) {
pressure_manager_proxy->RemoveObserver(this);
}
}
void PressureServiceBase::DidAddClient(
device::mojom::PressureSource source,
const std::optional<base::UnguessableToken>& token,
AddClientCallback client_callback,
device::mojom::PressureManagerAddClientResult result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto& pressure_client = source_to_client_[static_cast<size_t>(source)];
if (result == device::mojom::PressureManagerAddClientResult::kNotSupported) {
std::move(client_callback)
.Run(device::mojom::PressureManagerAddClientResult::kNotSupported);
pressure_client.Reset();
return;
}
pressure_client.SetPressureSourceType(token.has_value());
std::move(client_callback)
.Run(device::mojom::PressureManagerAddClientResult::kOk);
}
} // namespace content
|