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
|
// Copyright 2022 The Chromium Authors
// 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/modules/browsing_topics/browsing_topics_document_supplement.h"
#include "base/metrics/histogram_functions.h"
#include "components/browsing_topics/common/common_types.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/network/public/mojom/permissions_policy/permissions_policy_feature.mojom-blink.h"
#include "third_party/blink/public/mojom/permissions_policy/document_policy_feature.mojom-blink.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom-blink.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_throw_dom_exception.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_browsing_topic.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_browsing_topics_options.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/page/page.h"
namespace blink {
namespace {
void RecordInvalidRequestingContextUkmMetrics(Document& document) {
ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult2 builder(
document.UkmSourceID());
builder.SetFailureReason(static_cast<int64_t>(
browsing_topics::ApiAccessResult::kInvalidRequestingContext));
builder.Record(document.UkmRecorder());
}
} // namespace
// static
const char BrowsingTopicsDocumentSupplement::kSupplementName[] =
"BrowsingTopicsDocumentSupplement";
// static
BrowsingTopicsDocumentSupplement* BrowsingTopicsDocumentSupplement::From(
Document& document) {
auto* supplement =
Supplement<Document>::From<BrowsingTopicsDocumentSupplement>(document);
if (!supplement) {
supplement =
MakeGarbageCollected<BrowsingTopicsDocumentSupplement>(document);
Supplement<Document>::ProvideTo(document, supplement);
}
return supplement;
}
// static
ScriptPromise<IDLSequence<BrowsingTopic>>
BrowsingTopicsDocumentSupplement::browsingTopics(
ScriptState* script_state,
Document& document,
ExceptionState& exception_state) {
auto* supplement = From(document);
return supplement->GetBrowsingTopics(
script_state, document, BrowsingTopicsOptions::Create(), exception_state);
}
// static
ScriptPromise<IDLSequence<BrowsingTopic>>
BrowsingTopicsDocumentSupplement::browsingTopics(
ScriptState* script_state,
Document& document,
const BrowsingTopicsOptions* options,
ExceptionState& exception_state) {
auto* supplement = From(document);
return supplement->GetBrowsingTopics(script_state, document, options,
exception_state);
}
BrowsingTopicsDocumentSupplement::BrowsingTopicsDocumentSupplement(
Document& document)
: Supplement<Document>(document),
document_host_(document.GetExecutionContext()) {}
ScriptPromise<IDLSequence<BrowsingTopic>>
BrowsingTopicsDocumentSupplement::GetBrowsingTopics(
ScriptState* script_state,
Document& document,
const BrowsingTopicsOptions* options,
ExceptionState& exception_state) {
if (!document.GetFrame()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidAccessError,
"A browsing context is required when "
"calling document.browsingTopics().");
RecordInvalidRequestingContextUkmMetrics(document);
return ScriptPromise<IDLSequence<BrowsingTopic>>();
}
UseCounter::Count(document, mojom::blink::WebFeature::kPrivacySandboxAdsAPIs);
UseCounter::Count(document, mojom::blink::WebFeature::kTopicsAPIAll);
auto* resolver =
MakeGarbageCollected<ScriptPromiseResolver<IDLSequence<BrowsingTopic>>>(
script_state, exception_state.GetContext());
auto promise = resolver->Promise();
// See https://github.com/jkarlin/topics#specific-details for the restrictions
// on the context.
if (document.GetExecutionContext()->GetSecurityOrigin()->IsOpaque()) {
resolver->Reject(V8ThrowDOMException::CreateOrEmpty(
script_state->GetIsolate(), DOMExceptionCode::kInvalidAccessError,
"document.browsingTopics() is not allowed in an opaque origin "
"context."));
RecordInvalidRequestingContextUkmMetrics(document);
return promise;
}
// Fenced frames disallow all permissions policies which would deny this call
// regardless, but adding this check to make the error more explicit.
if (document.GetFrame()->IsInFencedFrameTree()) {
resolver->Reject(V8ThrowDOMException::CreateOrEmpty(
script_state->GetIsolate(), DOMExceptionCode::kInvalidAccessError,
"document.browsingTopics() is not allowed in a fenced frame."));
RecordInvalidRequestingContextUkmMetrics(document);
return promise;
}
// The Mojo requests on a prerendered page will be canceled by default. Adding
// this check to make the error more explicit.
if (document.GetFrame()->GetPage()->IsPrerendering()) {
resolver->Reject(V8ThrowDOMException::CreateOrEmpty(
script_state->GetIsolate(), DOMExceptionCode::kInvalidAccessError,
"document.browsingTopics() is not allowed when the page is being "
"prerendered."));
RecordInvalidRequestingContextUkmMetrics(document);
return promise;
}
if (!document.GetExecutionContext()->IsFeatureEnabled(
network::mojom::PermissionsPolicyFeature::kBrowsingTopics)) {
resolver->Reject(V8ThrowDOMException::CreateOrEmpty(
script_state->GetIsolate(), DOMExceptionCode::kInvalidAccessError,
"The \"browsing-topics\" Permissions Policy denied the use of "
"document.browsingTopics()."));
RecordInvalidRequestingContextUkmMetrics(document);
return promise;
}
if (!document.GetExecutionContext()->IsFeatureEnabled(
network::mojom::PermissionsPolicyFeature::
kBrowsingTopicsBackwardCompatible)) {
resolver->Reject(V8ThrowDOMException::CreateOrEmpty(
script_state->GetIsolate(), DOMExceptionCode::kInvalidAccessError,
"The \"interest-cohort\" Permissions Policy denied the use of "
"document.browsingTopics()."));
RecordInvalidRequestingContextUkmMetrics(document);
return promise;
}
ExecutionContext* execution_context = ExecutionContext::From(script_state);
if (!document_host_.is_bound()) {
execution_context->GetBrowserInterfaceBroker().GetInterface(
document_host_.BindNewPipeAndPassReceiver(
execution_context->GetTaskRunner(TaskType::kMiscPlatformAPI)));
}
document_host_->GetBrowsingTopics(
/*observe=*/!options->skipObservation(),
WTF::BindOnce(
[](ScriptPromiseResolver<IDLSequence<BrowsingTopic>>* resolver,
BrowsingTopicsDocumentSupplement* supplement,
base::TimeTicks start_time,
mojom::blink::GetBrowsingTopicsResultPtr result) {
DCHECK(resolver);
DCHECK(supplement);
if (result->is_error_message()) {
ScriptState* script_state = resolver->GetScriptState();
ScriptState::Scope scope(script_state);
resolver->Reject(V8ThrowDOMException::CreateOrEmpty(
script_state->GetIsolate(),
DOMExceptionCode::kInvalidAccessError,
result->get_error_message()));
return;
}
DCHECK(result->is_browsing_topics());
HeapVector<Member<BrowsingTopic>> result_array;
for (const auto& topic : result->get_browsing_topics()) {
BrowsingTopic* result_topic = BrowsingTopic::Create();
result_topic->setTopic(topic->topic);
result_topic->setVersion(topic->version);
result_topic->setConfigVersion(topic->config_version);
result_topic->setModelVersion(topic->model_version);
result_topic->setTaxonomyVersion(topic->taxonomy_version);
result_array.push_back(result_topic);
}
base::TimeDelta time_to_resolve =
base::TimeTicks::Now() - start_time;
base::UmaHistogramTimes(
"BrowsingTopics.JavaScriptAPI.TimeToResolve", time_to_resolve);
resolver->Resolve(result_array);
},
WrapPersistent(resolver), WrapPersistent(this),
base::TimeTicks::Now()));
return promise;
}
void BrowsingTopicsDocumentSupplement::Trace(Visitor* visitor) const {
visitor->Trace(document_host_);
Supplement<Document>::Trace(visitor);
}
} // namespace blink
|