Skip to content

Commit

Permalink
[UA-CH] Add userAgentData worker support
Browse files Browse the repository at this point in the history
This implements PR #80 [1] and exposes userAgentData in workers.

[1] WICG/ua-client-hints#80

Bug: 1062192
Change-Id: I1de6067ab6c00855d7bf820e9646eb4e5a8c5fdb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2093220
Commit-Queue: Yoav Weiss <yoavweiss@chromium.org>
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: Kinuko Yasuda <kinuko@chromium.org>
Reviewed-by: Matt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#751295}
  • Loading branch information
Yoav Weiss authored and Commit Bot committed Mar 18, 2020
1 parent 1386d9f commit 5453312
Show file tree
Hide file tree
Showing 55 changed files with 232 additions and 82 deletions.
1 change: 1 addition & 0 deletions content/browser/service_worker/service_worker_version.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,7 @@ void ServiceWorkerVersion::StartWorkerInternal() {
params->outside_fetch_client_settings_object =
outside_fetch_client_settings_object_.Clone();
params->user_agent = GetContentClient()->browser()->GetUserAgent();
params->ua_metadata = GetContentClient()->browser()->GetUserAgentMetadata();
params->is_installed = IsInstalled(status_);
params->script_url_to_skip_throttling = updated_script_url_;

Expand Down
1 change: 1 addition & 0 deletions content/browser/worker_host/mock_shared_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ void MockSharedWorkerFactory::CreateSharedWorker(
blink::mojom::SharedWorkerInfoPtr info,
const url::Origin& constructor_origin,
const std::string& user_agent,
const blink::UserAgentMetadata& ua_metadata,
bool pause_on_start,
const base::UnguessableToken& devtools_worker_token,
blink::mojom::RendererPreferencesPtr renderer_preferences,
Expand Down
1 change: 1 addition & 0 deletions content/browser/worker_host/mock_shared_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class MockSharedWorkerFactory : public blink::mojom::SharedWorkerFactory {
blink::mojom::SharedWorkerInfoPtr info,
const url::Origin& constructor_origin,
const std::string& user_agent,
const blink::UserAgentMetadata& ua_metadata,
bool pause_on_start,
const base::UnguessableToken& devtools_worker_token,
blink::mojom::RendererPreferencesPtr renderer_preferences,
Expand Down
3 changes: 2 additions & 1 deletion content/browser/worker_host/shared_worker_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ void SharedWorkerHost::Start(
factory_.Bind(std::move(factory));
factory_->CreateSharedWorker(
std::move(info), instance_.constructor_origin(),
GetContentClient()->browser()->GetUserAgent(), pause_on_start,
GetContentClient()->browser()->GetUserAgent(),
GetContentClient()->browser()->GetUserAgentMetadata(), pause_on_start,
dev_tools_token_, std::move(renderer_preferences),
std::move(preference_watcher_receiver), std::move(content_settings),
service_worker_handle_->TakeProviderInfo(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ EmbeddedWorkerInstanceClientImpl::BuildStartData(

start_data->script_url = params.script_url;
start_data->user_agent = blink::WebString::FromUTF8(params.user_agent);
start_data->ua_metadata = params.ua_metadata;
start_data->script_type = params.script_type;
start_data->wait_for_debugger_mode =
params.wait_for_debugger
Expand Down
3 changes: 2 additions & 1 deletion content/renderer/worker/embedded_shared_worker_stub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ EmbeddedSharedWorkerStub::EmbeddedSharedWorkerStub(
blink::mojom::SharedWorkerInfoPtr info,
const url::Origin& constructor_origin,
const std::string& user_agent,
const blink::UserAgentMetadata& ua_metadata,
bool pause_on_start,
const base::UnguessableToken& devtools_worker_token,
const blink::mojom::RendererPreferences& renderer_preferences,
Expand Down Expand Up @@ -105,7 +106,7 @@ EmbeddedSharedWorkerStub::EmbeddedSharedWorkerStub(
url_, info->options->type, info->options->credentials,
blink::WebString::FromUTF8(info->options->name),
blink::WebSecurityOrigin(constructor_origin),
blink::WebString::FromUTF8(user_agent),
blink::WebString::FromUTF8(user_agent), ua_metadata,
blink::WebString::FromUTF8(info->content_security_policy),
info->content_security_policy_type, info->creation_address_space,
FetchClientSettingsObjectFromMojomToWeb(
Expand Down
2 changes: 2 additions & 0 deletions content/renderer/worker/embedded_shared_worker_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/network/public/mojom/url_loader_factory.mojom-forward.h"
#include "third_party/blink/public/common/user_agent/user_agent_metadata.h"
#include "third_party/blink/public/mojom/browser_interface_broker.mojom-forward.h"
#include "third_party/blink/public/mojom/devtools/devtools_agent.mojom-forward.h"
#include "third_party/blink/public/mojom/renderer_preference_watcher.mojom-forward.h"
Expand Down Expand Up @@ -58,6 +59,7 @@ class EmbeddedSharedWorkerStub : public blink::WebSharedWorkerClient,
blink::mojom::SharedWorkerInfoPtr info,
const url::Origin& constructor_origin,
const std::string& user_agent,
const blink::UserAgentMetadata& ua_metadata,
bool pause_on_start,
const base::UnguessableToken& devtools_worker_token,
const blink::mojom::RendererPreferences& renderer_preferences,
Expand Down
5 changes: 3 additions & 2 deletions content/renderer/worker/shared_worker_factory_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void SharedWorkerFactoryImpl::CreateSharedWorker(
blink::mojom::SharedWorkerInfoPtr info,
const url::Origin& constructor_origin,
const std::string& user_agent,
const blink::UserAgentMetadata& ua_metadata,
bool pause_on_start,
const base::UnguessableToken& devtools_worker_token,
blink::mojom::RendererPreferencesPtr renderer_preferences,
Expand All @@ -45,8 +46,8 @@ void SharedWorkerFactoryImpl::CreateSharedWorker(
browser_interface_broker) {
// Bound to the lifetime of the underlying blink::WebSharedWorker instance.
new EmbeddedSharedWorkerStub(
std::move(info), constructor_origin, user_agent, pause_on_start,
devtools_worker_token, *renderer_preferences,
std::move(info), constructor_origin, user_agent, ua_metadata,
pause_on_start, devtools_worker_token, *renderer_preferences,
std::move(preference_watcher_receiver), std::move(content_settings),
std::move(service_worker_provider_info),
appcache_host_id.value_or(base::UnguessableToken()),
Expand Down
1 change: 1 addition & 0 deletions content/renderer/worker/shared_worker_factory_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SharedWorkerFactoryImpl : public blink::mojom::SharedWorkerFactory {
blink::mojom::SharedWorkerInfoPtr info,
const url::Origin& constructor_origin,
const std::string& user_agent,
const blink::UserAgentMetadata& ua_metadata,
bool pause_on_start,
const base::UnguessableToken& devtools_worker_token,
blink::mojom::RendererPreferencesPtr renderer_preferences,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import "third_party/blink/public/mojom/service_worker/service_worker_installed_s
import "third_party/blink/public/mojom/service_worker/service_worker_event_status.mojom";
import "third_party/blink/public/mojom/service_worker/service_worker_provider.mojom";
import "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom";
import "third_party/blink/public/mojom/user_agent/user_agent_metadata.mojom";
import "third_party/blink/public/mojom/worker/worker_content_settings_proxy.mojom";
import "third_party/blink/public/mojom/web_feature/web_feature.mojom";
import "url/mojom/url.mojom";
Expand Down Expand Up @@ -52,6 +53,10 @@ struct EmbeddedWorkerStartParams {
// The string used for "user-agent" HTTP header.
string user_agent;

// The metadata for userAgentData and the UA-CH headers.
// See https://wicg.github.io/ua-client-hints/ for more details.
UserAgentMetadata ua_metadata;

// The id this service worker uses as render_frame_id in
// network::ResourceRequests it makes.
int32 service_worker_route_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module blink.mojom;

import "mojo/public/mojom/base/unguessable_token.mojom";
import "third_party/blink/public/mojom/user_agent/user_agent_metadata.mojom";
import "third_party/blink/public/mojom/browser_interface_broker.mojom";
import "third_party/blink/public/mojom/loader/url_loader_factory_bundle.mojom";
import "third_party/blink/public/mojom/renderer_preference_watcher.mojom";
Expand Down Expand Up @@ -42,6 +43,7 @@ interface SharedWorkerFactory {
url.mojom.Origin constructor_origin,

string user_agent,
UserAgentMetadata ua_metadata,
bool pause_on_start,
mojo_base.mojom.UnguessableToken devtools_worker_token,
RendererPreferences renderer_preferences,
Expand Down
2 changes: 2 additions & 0 deletions third_party/blink/public/web/web_embedded_worker_start_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "base/unguessable_token.h"
#include "services/network/public/mojom/ip_address_space.mojom-shared.h"
#include "third_party/blink/public/common/user_agent/user_agent_metadata.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom-shared.h"
#include "third_party/blink/public/platform/web_fetch_client_settings_object.h"
#include "third_party/blink/public/platform/web_string.h"
Expand All @@ -45,6 +46,7 @@ struct WebEmbeddedWorkerStartData {

WebURL script_url;
WebString user_agent;
UserAgentMetadata ua_metadata;
mojom::ScriptType script_type;
// Whether to pause the initialization and wait for debugger to attach
// before proceeding. This technique allows debugging worker startup.
Expand Down
2 changes: 2 additions & 0 deletions third_party/blink/public/web/web_shared_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "services/network/public/mojom/content_security_policy.mojom-shared.h"
#include "services/network/public/mojom/fetch_api.mojom-shared.h"
#include "services/network/public/mojom/ip_address_space.mojom-shared.h"
#include "third_party/blink/public/common/user_agent/user_agent_metadata.h"
#include "third_party/blink/public/mojom/script/script_type.mojom-shared.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/public/platform/web_common.h"
Expand Down Expand Up @@ -67,6 +68,7 @@ class BLINK_EXPORT WebSharedWorker {
const WebString& name,
WebSecurityOrigin constructor_origin,
const WebString& user_agent,
const UserAgentMetadata& ua_metadata,
const WebString& content_security_policy,
network::mojom::ContentSecurityPolicyType,
network::mojom::IPAddressSpace,
Expand Down
2 changes: 1 addition & 1 deletion third_party/blink/renderer/bindings/idl_in_core.gni
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ static_idl_files_in_core = get_path_info(
"//third_party/blink/renderer/core/frame/navigator_language.idl",
"//third_party/blink/renderer/core/frame/navigator_on_line.idl",
"//third_party/blink/renderer/core/frame/navigator_scheduling.idl",
"//third_party/blink/renderer/core/frame/navigator_ua.idl",
"//third_party/blink/renderer/core/frame/navigator_ua_brand_version.idl",
"//third_party/blink/renderer/core/frame/navigator_ua_data.idl",
"//third_party/blink/renderer/core/frame/navigator_user_activation.idl",
"//third_party/blink/renderer/core/frame/navigator_user_agent.idl",
"//third_party/blink/renderer/core/frame/report.idl",
"//third_party/blink/renderer/core/frame/report_body.idl",
"//third_party/blink/renderer/core/frame/reporting_observer.idl",
Expand Down
2 changes: 1 addition & 1 deletion third_party/blink/renderer/core/core_idl_files.gni
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ core_dependency_idl_files =
"frame/navigator_language.idl",
"frame/navigator_on_line.idl",
"frame/navigator_scheduling.idl",
"frame/navigator_ua.idl",
"frame/navigator_user_activation.idl",
"frame/navigator_user_agent.idl",
"frame/window_event_handlers.idl",
"frame/window_or_worker_global_scope.idl",
"fullscreen/document_fullscreen.idl",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ void WebSharedWorkerImpl::StartWorkerContext(
const WebString& name,
WebSecurityOrigin constructor_origin,
const WebString& user_agent,
const UserAgentMetadata& ua_metadata,
const WebString& content_security_policy,
network::mojom::ContentSecurityPolicyType policy_type,
network::mojom::IPAddressSpace creation_address_space,
Expand Down Expand Up @@ -228,7 +229,7 @@ void WebSharedWorkerImpl::StartWorkerContext(
// dummy values. They will be updated after worker script fetch on the worker
// thread.
auto creation_params = std::make_unique<GlobalScopeCreationParams>(
script_request_url, script_type, name, user_agent,
script_request_url, script_type, name, user_agent, ua_metadata,
std::move(web_worker_fetch_context), outside_csp_headers,
outside_settings_object->GetReferrerPolicy(),
outside_settings_object->GetSecurityOrigin(), constructor_secure_context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "services/network/public/mojom/fetch_api.mojom-shared.h"
#include "services/network/public/mojom/ip_address_space.mojom-blink-forward.h"
#include "third_party/blink/public/mojom/script/script_type.mojom-blink.h"
#include "third_party/blink/public/mojom/user_agent/user_agent_metadata.mojom-blink.h"
#include "third_party/blink/public/platform/web_fetch_client_settings_object.h"
#include "third_party/blink/public/web/web_shared_worker_client.h"
#include "third_party/blink/renderer/core/core_export.h"
Expand Down Expand Up @@ -76,6 +77,7 @@ class CORE_EXPORT WebSharedWorkerImpl final : public WebSharedWorker {
const WebString& name,
WebSecurityOrigin constructor_origin,
const WebString& user_agent,
const blink::UserAgentMetadata& ua_metadata,
const WebString& content_security_policy,
network::mojom::ContentSecurityPolicyType,
network::mojom::IPAddressSpace,
Expand Down
4 changes: 2 additions & 2 deletions third_party/blink/renderer/core/frame/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ blink_core_sources("frame") {
"navigator_on_line.h",
"navigator_scheduling.cc",
"navigator_scheduling.h",
"navigator_ua.cc",
"navigator_ua.h",
"navigator_ua_data.cc",
"navigator_ua_data.h",
"navigator_user_activation.cc",
"navigator_user_activation.h",
"navigator_user_agent.cc",
"navigator_user_agent.h",
"opened_frame_tracker.cc",
"opened_frame_tracker.h",
"overlay_interstitial_ad_detector.cc",
Expand Down
7 changes: 7 additions & 0 deletions third_party/blink/renderer/core/frame/navigator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,11 @@ void Navigator::Trace(Visitor* visitor) {
Supplementable<Navigator>::Trace(visitor);
}

ExecutionContext* Navigator::GetUAExecutionContext() const {
if (GetFrame() && GetFrame()->GetDocument()) {
return GetFrame()->GetDocument()->GetExecutionContext();
}
return nullptr;
}

} // namespace blink
6 changes: 3 additions & 3 deletions third_party/blink/renderer/core/frame/navigator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "third_party/blink/renderer/core/frame/navigator_id.h"
#include "third_party/blink/renderer/core/frame/navigator_language.h"
#include "third_party/blink/renderer/core/frame/navigator_on_line.h"
#include "third_party/blink/renderer/core/frame/navigator_user_agent.h"
#include "third_party/blink/renderer/core/frame/navigator_ua.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/supplementable.h"
Expand All @@ -44,7 +44,7 @@ class CORE_EXPORT Navigator final : public ScriptWrappable,
public NavigatorID,
public NavigatorLanguage,
public NavigatorOnLine,
public NavigatorUserAgent,
public NavigatorUA,
public DOMWindowClient,
public Supplementable<Navigator> {
DEFINE_WRAPPERTYPEINFO();
Expand Down Expand Up @@ -72,7 +72,7 @@ class CORE_EXPORT Navigator final : public ScriptWrappable,
void Trace(Visitor*) override;

protected:
LocalFrame* GetLocalFrame() const override { return GetFrame(); }
ExecutionContext* GetUAExecutionContext() const override;

private:
UserAgentMetadata metadata_;
Expand Down
2 changes: 1 addition & 1 deletion third_party/blink/renderer/core/frame/navigator.idl
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ Navigator includes NavigatorID;
Navigator includes NavigatorLanguage;
Navigator includes NavigatorOnLine;
Navigator includes NavigatorAutomationInformation;
Navigator includes NavigatorUserAgent;
Navigator includes NavigatorUA;
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/frame/navigator_user_agent.h"

#include "third_party/blink/renderer/core/frame/navigator_ua.h"

namespace blink {

NavigatorUAData* NavigatorUserAgent::userAgentData() {
blink::NavigatorUAData* ua_data =
blink::NavigatorUAData::Create(GetLocalFrame());
NavigatorUAData* NavigatorUA::userAgentData() {
NavigatorUAData* ua_data =
MakeGarbageCollected<NavigatorUAData>(GetUAExecutionContext());

blink::UserAgentMetadata metadata = GetUserAgentMetadata();
UserAgentMetadata metadata = GetUserAgentMetadata();
ua_data->AddBrand(String::FromUTF8(metadata.brand),
String::FromUTF8(metadata.major_version));
ua_data->SetMobile(metadata.mobile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_NAVIGATOR_USER_AGENT_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_NAVIGATOR_USER_AGENT_H_
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_NAVIGATOR_UA_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_NAVIGATOR_UA_H_

#include "third_party/blink/public/common/user_agent/user_agent_metadata.h"
#include "third_party/blink/renderer/core/core_export.h"
Expand All @@ -12,15 +12,15 @@

namespace blink {

class CORE_EXPORT NavigatorUserAgent {
class CORE_EXPORT NavigatorUA {
public:
NavigatorUAData* userAgentData();

protected:
virtual UserAgentMetadata GetUserAgentMetadata() const = 0;
virtual LocalFrame* GetLocalFrame() const = 0;
virtual ExecutionContext* GetUAExecutionContext() const = 0;
};

} // namespace blink

#endif // THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_NAVIGATOR_USER_AGENT_H_
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_NAVIGATOR_UA_H_
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
// https://github.com/WICG/ua-client-hints

[
RuntimeEnabled=UserAgentClientHint,
Exposed=Window
] interface mixin NavigatorUserAgent {
RuntimeEnabled=UserAgentClientHint
] interface mixin NavigatorUA {
[SecureContext] readonly attribute NavigatorUAData userAgentData;
};
13 changes: 7 additions & 6 deletions third_party/blink/renderer/core/frame/navigator_ua_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

namespace blink {

NavigatorUAData::NavigatorUAData(LocalFrame* frame) : DOMWindowClient(frame) {
NavigatorUAData::NavigatorUAData(ExecutionContext* context)
: ExecutionContextClient(context) {
NavigatorUABrandVersion* dict = NavigatorUABrandVersion::Create();
dict->setBrand("");
dict->setVersion("");
Expand Down Expand Up @@ -46,18 +47,18 @@ void NavigatorUAData::SetUAFullVersion(const String& ua_full_version) {
}

bool NavigatorUAData::mobile() const {
if (GetFrame() && GetFrame()->GetPage()) {
if (GetExecutionContext()) {
return is_mobile_;
}
return false;
}

const HeapVector<Member<NavigatorUABrandVersion>>& NavigatorUAData::uaList()
const {
if (!GetFrame() || !GetFrame()->GetPage()) {
return empty_brand_set_;
if (GetExecutionContext()) {
return brand_set_;
}
return brand_set_;
return empty_brand_set_;
}

ScriptPromise NavigatorUAData::getHighEntropyValues(
Expand Down Expand Up @@ -87,7 +88,7 @@ void NavigatorUAData::Trace(Visitor* visitor) {
visitor->Trace(brand_set_);
visitor->Trace(empty_brand_set_);
ScriptWrappable::Trace(visitor);
DOMWindowClient::Trace(visitor);
ExecutionContextClient::Trace(visitor);
}

} // namespace blink
Loading

0 comments on commit 5453312

Please sign in to comment.