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
|
// Copyright 2020 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/font_access/font_access.h"
#include <algorithm>
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/numerics/safe_conversions.h"
#include "services/network/public/mojom/permissions_policy/permissions_policy_feature.mojom-blink.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/font_access/font_enumeration_table.pb.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/public/platform/platform.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_query_options.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/modules/font_access/font_metadata.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
namespace blink {
using mojom::blink::FontEnumerationStatus;
namespace {
const char kFeaturePolicyBlocked[] =
"Access to the feature \"local-fonts\" is disallowed by Permissions Policy";
}
// static
const char FontAccess::kSupplementName[] = "FontAccess";
FontAccess::FontAccess(LocalDOMWindow* window)
: Supplement<LocalDOMWindow>(*window), remote_(window) {}
void FontAccess::Trace(blink::Visitor* visitor) const {
visitor->Trace(remote_);
Supplement<LocalDOMWindow>::Trace(visitor);
}
// static
ScriptPromise<IDLSequence<FontMetadata>> FontAccess::queryLocalFonts(
ScriptState* script_state,
LocalDOMWindow& window,
const QueryOptions* options,
ExceptionState& exception_state) {
DCHECK(ExecutionContext::From(script_state)->IsContextThread());
return From(&window)->QueryLocalFontsImpl(script_state, options,
exception_state);
}
// static
FontAccess* FontAccess::From(LocalDOMWindow* window) {
auto* supplement = Supplement<LocalDOMWindow>::From<FontAccess>(window);
if (!supplement) {
supplement = MakeGarbageCollected<FontAccess>(window);
Supplement<LocalDOMWindow>::ProvideTo(*window, supplement);
}
return supplement;
}
ScriptPromise<IDLSequence<FontMetadata>> FontAccess::QueryLocalFontsImpl(
ScriptState* script_state,
const QueryOptions* options,
ExceptionState& exception_state) {
if (!base::FeatureList::IsEnabled(blink::features::kFontAccess)) {
exception_state.ThrowDOMException(DOMExceptionCode::kNotSupportedError,
"Font Access feature is not supported.");
return ScriptPromise<IDLSequence<FontMetadata>>();
}
if (!script_state->ContextIsValid()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"The execution context is not valid.");
return ScriptPromise<IDLSequence<FontMetadata>>();
}
ExecutionContext* context = ExecutionContext::From(script_state);
if (!context->IsFeatureEnabled(
network::mojom::PermissionsPolicyFeature::kLocalFonts,
ReportOptions::kReportOnFailure)) {
exception_state.ThrowSecurityError(kFeaturePolicyBlocked);
return ScriptPromise<IDLSequence<FontMetadata>>();
}
// Connect to font access manager remote if not bound already.
if (!remote_.is_bound()) {
context->GetBrowserInterfaceBroker().GetInterface(
remote_.BindNewPipeAndPassReceiver(
context->GetTaskRunner(TaskType::kFontLoading)));
remote_.set_disconnect_handler(
WTF::BindOnce(&FontAccess::OnDisconnect, WrapWeakPersistent(this)));
}
DCHECK(remote_.is_bound());
auto* resolver =
MakeGarbageCollected<ScriptPromiseResolver<IDLSequence<FontMetadata>>>(
script_state, exception_state.GetContext());
auto promise = resolver->Promise();
remote_->EnumerateLocalFonts(resolver->WrapCallbackInScriptScope(
WTF::BindOnce(&FontAccess::DidGetEnumerationResponse,
WrapWeakPersistent(this), WrapPersistent(options))));
return promise;
}
void FontAccess::DidGetEnumerationResponse(
const QueryOptions* options,
ScriptPromiseResolver<IDLSequence<FontMetadata>>* resolver,
FontEnumerationStatus status,
base::ReadOnlySharedMemoryRegion region) {
if (!resolver->GetScriptState()->ContextIsValid())
return;
if (RejectPromiseIfNecessary(status, resolver))
return;
// Return an empty font list if user has denied the permission request.
if (status == FontEnumerationStatus::kPermissionDenied) {
HeapVector<Member<FontMetadata>> entries;
resolver->Resolve(std::move(entries));
return;
}
// Font data exists; process and fill in the data.
base::ReadOnlySharedMemoryMapping mapping = region.Map();
FontEnumerationTable table;
if (mapping.size() > INT_MAX) {
// Cannot deserialize without overflow.
resolver->Reject(V8ThrowDOMException::CreateOrDie(
resolver->GetScriptState()->GetIsolate(), DOMExceptionCode::kDataError,
"Font data exceeds memory limit."));
return;
}
// Used to compare with data coming from the browser to avoid conversions.
const bool hasPostscriptNameFilter = options->hasPostscriptNames();
std::set<std::string> selection_utf8;
if (hasPostscriptNameFilter) {
for (const String& postscriptName : options->postscriptNames()) {
// While postscript names are encoded in a subset of ASCII, we convert the
// input into UTF8. This will still allow exact matches to occur.
selection_utf8.insert(postscriptName.Utf8());
}
}
HeapVector<Member<FontMetadata>> entries;
base::span<const uint8_t> mapped_mem(mapping);
table.ParseFromArray(mapped_mem.data(),
base::checked_cast<int>(mapped_mem.size()));
for (const auto& element : table.fonts()) {
// If the optional postscript name filter is set in QueryOptions,
// only allow items that match.
if (hasPostscriptNameFilter &&
!base::Contains(selection_utf8, element.postscript_name().c_str())) {
continue;
}
auto entry = FontEnumerationEntry{
.postscript_name = String::FromUTF8(element.postscript_name()),
.full_name = String::FromUTF8(element.full_name()),
.family = String::FromUTF8(element.family()),
.style = String::FromUTF8(element.style()),
};
entries.push_back(FontMetadata::Create(std::move(entry)));
}
resolver->Resolve(std::move(entries));
}
bool FontAccess::RejectPromiseIfNecessary(const FontEnumerationStatus& status,
ScriptPromiseResolverBase* resolver) {
switch (status) {
case FontEnumerationStatus::kOk:
case FontEnumerationStatus::kPermissionDenied:
break;
case FontEnumerationStatus::kUnimplemented:
resolver->Reject(V8ThrowDOMException::CreateOrDie(
resolver->GetScriptState()->GetIsolate(),
DOMExceptionCode::kNotSupportedError,
"Not yet supported on this platform."));
return true;
case FontEnumerationStatus::kNeedsUserActivation:
resolver->Reject(V8ThrowDOMException::CreateOrDie(
resolver->GetScriptState()->GetIsolate(),
DOMExceptionCode::kSecurityError, "User activation is required."));
return true;
case FontEnumerationStatus::kNotVisible:
resolver->Reject(V8ThrowDOMException::CreateOrDie(
resolver->GetScriptState()->GetIsolate(),
DOMExceptionCode::kSecurityError, "Page needs to be visible."));
return true;
case FontEnumerationStatus::kUnexpectedError:
default:
resolver->Reject(V8ThrowDOMException::CreateOrDie(
resolver->GetScriptState()->GetIsolate(),
DOMExceptionCode::kUnknownError, "An unexpected error occured."));
return true;
}
return false;
}
void FontAccess::OnDisconnect() {
remote_.reset();
}
} // namespace blink
|