Commit 7ec9c288 authored by Kinuko Yasuda's avatar Kinuko Yasuda Committed by Commit Bot

[Copy from 1057969] ResourceLoadScheduler: show console messages on reaching the limit

This patch makes the ResourceLoadScheduler show a console message
on receiving a network request when running requests reach to the limit.

This will help users to understand what is happening around the
background tab throttling.

Change from the original change (crrev.com/c/1057969):
- Rebase
- Not to show the message for kStopped (i.e. when a frame is paused)

The latter can be added when the owners of frame-pausing/stopping want
to add that later.

Bug: 837771
Change-Id: Ie17bb21a7172d2108d9cd10c4f3bde8b066b6fdb
Reviewed-on: https://chromium-review.googlesource.com/1136167
Commit-Queue: Kinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarYutaka Hirano <yhirano@chromium.org>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/master@{#574937}
parent c1b78a91
...@@ -86,6 +86,21 @@ const char* GetDestinationFromContext(WebURLRequest::RequestContext context) { ...@@ -86,6 +86,21 @@ const char* GetDestinationFromContext(WebURLRequest::RequestContext context) {
return ""; return "";
} }
MessageSource ConvertLogSourceToMessageSource(FetchContext::LogSource source) {
// When LogSource is extended, this switch statement should be modified to
// convert LogSource to blink::MessageSource.
switch (source) {
case FetchContext::kJSSource:
return kJSMessageSource;
case FetchContext::kSecuritySource:
return kSecurityMessageSource;
case FetchContext::kOtherSource:
return kOtherMessageSource;
}
NOTREACHED();
return kOtherMessageSource;
}
} // namespace } // namespace
void BaseFetchContext::AddAdditionalRequestHeaders(ResourceRequest& request, void BaseFetchContext::AddAdditionalRequestHeaders(ResourceRequest& request,
...@@ -159,19 +174,16 @@ base::Optional<ResourceRequestBlockedReason> BaseFetchContext::CanRequest( ...@@ -159,19 +174,16 @@ base::Optional<ResourceRequestBlockedReason> BaseFetchContext::CanRequest(
return blocked_reason; return blocked_reason;
} }
void BaseFetchContext::AddInfoConsoleMessage(const String& message,
LogSource source) const {
AddConsoleMessage(ConsoleMessage::Create(
ConvertLogSourceToMessageSource(source), kInfoMessageLevel, message));
}
void BaseFetchContext::AddErrorConsoleMessage(const String& message, void BaseFetchContext::AddErrorConsoleMessage(const String& message,
LogSource source) const { LogSource source) const {
switch (source) { AddConsoleMessage(ConsoleMessage::Create(
case kJSSource: ConvertLogSourceToMessageSource(source), kErrorMessageLevel, message));
AddConsoleMessage(ConsoleMessage::Create(kJSMessageSource,
kErrorMessageLevel, message));
return;
case kSecuritySource:
AddConsoleMessage(ConsoleMessage::Create(kSecurityMessageSource,
kErrorMessageLevel, message));
return;
}
NOTREACHED();
} }
bool BaseFetchContext::IsAdResource( bool BaseFetchContext::IsAdResource(
......
...@@ -58,6 +58,7 @@ class CORE_EXPORT BaseFetchContext : public FetchContext { ...@@ -58,6 +58,7 @@ class CORE_EXPORT BaseFetchContext : public FetchContext {
virtual std::unique_ptr<WebSocketHandshakeThrottle> virtual std::unique_ptr<WebSocketHandshakeThrottle>
CreateWebSocketHandshakeThrottle() = 0; CreateWebSocketHandshakeThrottle() = 0;
void AddInfoConsoleMessage(const String&, LogSource) const override;
void AddErrorConsoleMessage(const String&, LogSource) const override; void AddErrorConsoleMessage(const String&, LogSource) const override;
bool IsAdResource(const KURL&, bool IsAdResource(const KURL&,
Resource::Type, Resource::Type,
......
...@@ -110,6 +110,8 @@ void FetchContext::DidLoadResource(Resource*) {} ...@@ -110,6 +110,8 @@ void FetchContext::DidLoadResource(Resource*) {}
void FetchContext::AddResourceTiming(const ResourceTimingInfo&) {} void FetchContext::AddResourceTiming(const ResourceTimingInfo&) {}
void FetchContext::AddInfoConsoleMessage(const String&, LogSource) const {}
void FetchContext::AddErrorConsoleMessage(const String&, LogSource) const {} void FetchContext::AddErrorConsoleMessage(const String&, LogSource) const {}
void FetchContext::PopulateResourceRequest( void FetchContext::PopulateResourceRequest(
......
...@@ -82,14 +82,14 @@ class PLATFORM_EXPORT FetchContext ...@@ -82,14 +82,14 @@ class PLATFORM_EXPORT FetchContext
// This enum corresponds to blink::MessageSource. We have this not to // This enum corresponds to blink::MessageSource. We have this not to
// introduce any dependency to core/. // introduce any dependency to core/.
// //
// Currently only kJSMessageSource and kSecurityMessageSource are used, but // Currently only kJSMessageSource, kSecurityMessageSource and
// not to impress readers that AddConsoleMessage() call from FetchContext() // kOtherMessageSource are used, but not to impress readers that
// should always use them, which is not true, we ask users of the // AddConsoleMessage() call from FetchContext() should always use them,
// Add.*ConsoleMessage() methods to explicitly specify the MessageSource to // which is not true, we ask users of the Add.*ConsoleMessage() methods
// use. // to explicitly specify the MessageSource to use.
// //
// Extend this when needed. // Extend this when needed.
enum LogSource { kJSSource, kSecuritySource }; enum LogSource { kJSSource, kSecuritySource, kOtherSource };
static FetchContext& NullInstance(); static FetchContext& NullInstance();
...@@ -210,6 +210,7 @@ class PLATFORM_EXPORT FetchContext ...@@ -210,6 +210,7 @@ class PLATFORM_EXPORT FetchContext
return false; return false;
} }
virtual void AddInfoConsoleMessage(const String&, LogSource) const;
virtual void AddErrorConsoleMessage(const String&, LogSource) const; virtual void AddErrorConsoleMessage(const String&, LogSource) const;
virtual const SecurityOrigin* GetSecurityOrigin() const { return nullptr; } virtual const SecurityOrigin* GetSecurityOrigin() const { return nullptr; }
......
...@@ -469,7 +469,24 @@ void ResourceLoadScheduler::Request(ResourceLoadSchedulerClient* client, ...@@ -469,7 +469,24 @@ void ResourceLoadScheduler::Request(ResourceLoadSchedulerClient* client,
pending_requests_[option].insert(request_info); pending_requests_[option].insert(request_info);
pending_request_map_.insert( pending_request_map_.insert(
*id, new ClientInfo(client, option, priority, intra_priority)); *id, new ClientInfo(client, option, priority, intra_priority));
// Remember the ClientId since MaybeRun() below may destruct the caller
// instance and |id| may be inaccessible after the call.
ResourceLoadScheduler::ClientId client_id = *id;
MaybeRun(); MaybeRun();
if (IsThrottledState() &&
pending_request_map_.find(client_id) != pending_request_map_.end()) {
// Note that this doesn't show the message when a frame is stopped (vs.
// this DOES when throttled).
context_->AddInfoConsoleMessage(
"Active resource loading counts reached to a per-frame limit while the "
"tab is in background. Network requests will be delayed until a "
"previous loading finishes, or the tab is foregrounded. See "
"https://www.chromestatus.com/feature/5527160148197376 for more "
"details",
FetchContext::kOtherSource);
}
} }
void ResourceLoadScheduler::SetPriority(ClientId client_id, void ResourceLoadScheduler::SetPriority(ClientId client_id,
...@@ -740,4 +757,16 @@ size_t ResourceLoadScheduler::GetOutstandingLimit() const { ...@@ -740,4 +757,16 @@ size_t ResourceLoadScheduler::GetOutstandingLimit() const {
return limit; return limit;
} }
bool ResourceLoadScheduler::IsThrottledState() const {
switch (frame_scheduler_lifecycle_state_) {
case scheduler::SchedulingLifecycleState::kHidden:
case scheduler::SchedulingLifecycleState::kThrottled:
return true;
case scheduler::SchedulingLifecycleState::kStopped:
case scheduler::SchedulingLifecycleState::kNotThrottled:
break;
}
return false;
}
} // namespace blink } // namespace blink
...@@ -265,6 +265,8 @@ class PLATFORM_EXPORT ResourceLoadScheduler final ...@@ -265,6 +265,8 @@ class PLATFORM_EXPORT ResourceLoadScheduler final
size_t GetOutstandingLimit() const; size_t GetOutstandingLimit() const;
bool IsThrottledState() const;
// A flag to indicate an internal running state. // A flag to indicate an internal running state.
// TODO(toyoshim): We may want to use enum once we start to have more states. // TODO(toyoshim): We may want to use enum once we start to have more states.
bool is_shutdown_ = false; bool is_shutdown_ = false;
......
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