Skip to content

Commit

Permalink
Disable intensive throttling when WebSerial holding an opened port.
Browse files Browse the repository at this point in the history
This CL add WebSerial into `WebSchedulerTrackedFeature` and create
scheduling affecting feature handle when a SerialPort is activated. By
doing so a hidden page won't be intensively throttled when holding a
opened SerialPort.

Change-Id: I2343e663aa34d80a5c51a1dd79542536c6b0f739
Bug: 1442957
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4545501
Commit-Queue: Alvin Ji <alvinji@chromium.org>
Reviewed-by: Andrey Kosyakov <caseq@chromium.org>
Reviewed-by: Rakina Zata Amni <rakina@chromium.org>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1152871}
  • Loading branch information
Alvin Ji authored and Chromium LUCI CQ committed Jun 3, 2023
1 parent d982580 commit f104323
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 5 deletions.
6 changes: 6 additions & 0 deletions content/browser/devtools/protocol/page_handler.cc
Expand Up @@ -1596,6 +1596,10 @@ Page::BackForwardCacheNotRestoredReason BlocklistedFeatureToProtocol(
return Page::BackForwardCacheNotRestoredReasonEnum::Dummy;
case WebSchedulerTrackedFeature::kAuthorizationHeader:
return Page::BackForwardCacheNotRestoredReasonEnum::AuthorizationHeader;
case WebSchedulerTrackedFeature::kWebSerial:
// Currently we add WebSchedulerTrackedFeature::kWebSerial only for
// disabling aggressive throttling.
NOTREACHED_NORETURN();
}
}

Expand Down Expand Up @@ -1809,6 +1813,8 @@ Page::BackForwardCacheNotRestoredReasonType MapBlocklistedFeatureToType(
case WebSchedulerTrackedFeature::kDummy:
case WebSchedulerTrackedFeature::kAuthorizationHeader:
return Page::BackForwardCacheNotRestoredReasonTypeEnum::Circumstantial;
case WebSchedulerTrackedFeature::kWebSerial:
NOTREACHED_NORETURN();
}
}

Expand Down
8 changes: 7 additions & 1 deletion content/browser/renderer_host/back_forward_cache_impl.cc
Expand Up @@ -240,6 +240,11 @@ constexpr WebSchedulerTrackedFeatures kAllowedFeatures = {
// TODO(crbug.com/1357482): Figure out if this should be allowed.
WebSchedulerTrackedFeature::kWebNfc};

// WebSchedulerTrackedFeatures that do not affect back/forward cache, but
// affects other scheduling policies (e.g. aggressive throttling).
constexpr WebSchedulerTrackedFeatures kNonBackForwardCacheAffectingFeatures = {
WebSchedulerTrackedFeature::kWebSerial};

// The BackForwardCache feature is controlled via an experiment. This function
// returns the allowed URL list where it is enabled.
std::string GetAllowedURLList() {
Expand Down Expand Up @@ -410,7 +415,8 @@ bool IsSameOriginForTreeResult(RenderFrameHostImpl* rfh,
// static
BlockListedFeatures BackForwardCacheImpl::GetAllowedFeatures(
RequestedFeatures requested_features) {
WebSchedulerTrackedFeatures result = kAllowedFeatures;
WebSchedulerTrackedFeatures result =
Union(kAllowedFeatures, kNonBackForwardCacheAffectingFeatures);
if (IsContentInjectionSupported()) {
result.PutAll(kInjectionFeatures);
}
Expand Down
Expand Up @@ -127,6 +127,8 @@ FeatureNames FeatureToNames(WebSchedulerTrackedFeature feature) {
return {"AuthorizationHeader", "Authorization header used"};
case WebSchedulerTrackedFeature::kIndexedDBEvent:
return {"IndexedDBEvent", "IndexedDB event is pending"};
case WebSchedulerTrackedFeature::kWebSerial:
return {"WebSerial", "Serial port open"};
}
return {};
}
Expand Down
Expand Up @@ -128,12 +128,14 @@ enum class WebSchedulerTrackedFeature : uint32_t {
// There is a pending IndexedDB event (e.g. versionchange event sent but the
// connection is not closed yet) that requires the page not to enter BFCache.
kIndexedDBEvent = 61,

// Aggressive throttling is disabled when a serial port is opened and
// re-enabled when the opened port is closed.
kWebSerial = 62,
// Please keep in sync with WebSchedulerTrackedFeature in
// tools/metrics/histograms/enums.xml. These values should not be renumbered.

// NB: This enum is used in a bitmask, so kMaxValue must be less than 64.
kMaxValue = kIndexedDBEvent,
kMaxValue = kWebSerial,
};

using WebSchedulerTrackedFeatures =
Expand Down
Expand Up @@ -8523,6 +8523,7 @@ domain Page
IndexedDBEvent
Dummy
AuthorizationHeader
WebSerial
# Disabled for RenderFrameHost reasons
# See content/browser/renderer_host/back_forward_cache_disable.h for explanations.
ContentSecurityHandler
Expand Down
11 changes: 9 additions & 2 deletions third_party/blink/renderer/modules/serial/serial_port.cc
Expand Up @@ -633,13 +633,19 @@ void SerialPort::OnOpen(
return;
}

auto* execution_context = GetExecutionContext();
feature_handle_for_scheduler_ =
execution_context->GetScheduler()->RegisterFeature(
SchedulingPolicy::Feature::kWebSerial,
SchedulingPolicy{SchedulingPolicy::DisableAggressiveThrottling()});

port_.Bind(std::move(port),
GetExecutionContext()->GetTaskRunner(TaskType::kMiscPlatformAPI));
execution_context->GetTaskRunner(TaskType::kMiscPlatformAPI));
port_.set_disconnect_handler(
WTF::BindOnce(&SerialPort::OnConnectionError, WrapWeakPersistent(this)));
client_receiver_.Bind(
std::move(client_receiver),
GetExecutionContext()->GetTaskRunner(TaskType::kMiscPlatformAPI));
execution_context->GetTaskRunner(TaskType::kMiscPlatformAPI));

open_resolver_->Resolve();
open_resolver_ = nullptr;
Expand Down Expand Up @@ -687,6 +693,7 @@ void SerialPort::OnClose() {
DCHECK(IsClosing());
close_resolver_->Resolve();
close_resolver_ = nullptr;
feature_handle_for_scheduler_.reset();
}

void SerialPort::OnForget(ScriptPromiseResolver* resolver) {
Expand Down
4 changes: 4 additions & 0 deletions third_party/blink/renderer/modules/serial/serial_port.h
Expand Up @@ -18,6 +18,7 @@
#include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
#include "third_party/blink/renderer/platform/scheduler/public/frame_scheduler.h"

namespace base {
class UnguessableToken;
Expand Down Expand Up @@ -130,6 +131,9 @@ class SerialPort final : public EventTargetWithInlineData,
HeapHashSet<Member<ScriptPromiseResolver>> signal_resolvers_;
// Resolver for the Promise returned by close().
Member<ScriptPromiseResolver> close_resolver_;

FrameScheduler::SchedulingAffectingFeatureHandle
feature_handle_for_scheduler_;
};

} // namespace blink
Expand Down
1 change: 1 addition & 0 deletions tools/metrics/histograms/enums.xml
Expand Up @@ -111441,6 +111441,7 @@ Called by update_scheduler_enums.py.-->
<int value="59" label="Keepalive request"/>
<int value="60" label="AuthorizationHeader"/>
<int value="61" label="IndexedDBEvent"/>
<int value="62" label="WebSerial"/>
</enum>

<enum name="WebShareMethod">
Expand Down

0 comments on commit f104323

Please sign in to comment.