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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
// Copyright 2021 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/subapps/sub_apps.h"
#include <utility>
#include "base/check.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.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_sub_apps_add_params.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_sub_apps_list_result.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_sub_apps_result_code.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.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/core/frame/navigator.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/platform/bindings/exception_code.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
using mojom::blink::SubAppsService;
using mojom::blink::SubAppsServiceAddParameters;
using mojom::blink::SubAppsServiceAddParametersPtr;
using mojom::blink::SubAppsServiceAddResultPtr;
using mojom::blink::SubAppsServiceListResultEntryPtr;
using mojom::blink::SubAppsServiceListResultPtr;
using mojom::blink::SubAppsServiceRemoveResultPtr;
using mojom::blink::SubAppsServiceResultCode;
namespace {
const int kMaximumNumberOfSubappsPerAddCall = 7;
Vector<std::pair<String, V8SubAppsResultCode>> AddResultsFromMojo(
Vector<SubAppsServiceAddResultPtr> add_results_mojo) {
Vector<std::pair<String, V8SubAppsResultCode>> add_results_idl;
for (auto& add_result : add_results_mojo) {
auto result_code =
add_result->result_code == SubAppsServiceResultCode::kSuccess
? V8SubAppsResultCode(V8SubAppsResultCode::Enum::kSuccess)
: V8SubAppsResultCode(V8SubAppsResultCode::Enum::kFailure);
add_results_idl.emplace_back(add_result->manifest_id_path, result_code);
}
return add_results_idl;
}
Vector<std::pair<String, V8SubAppsResultCode>> RemoveResultsFromMojo(
Vector<SubAppsServiceRemoveResultPtr> remove_results_mojo) {
Vector<std::pair<String, V8SubAppsResultCode>> results;
for (auto& remove_result : remove_results_mojo) {
auto result_code =
remove_result->result_code == SubAppsServiceResultCode::kSuccess
? V8SubAppsResultCode(V8SubAppsResultCode::Enum::kSuccess)
: V8SubAppsResultCode(V8SubAppsResultCode::Enum::kFailure);
results.emplace_back(remove_result->manifest_id_path, result_code);
}
return results;
}
Vector<SubAppsServiceAddParametersPtr> AddOptionsToMojo(
HeapVector<std::pair<String, Member<SubAppsAddParams>>>
sub_apps_to_add_idl) {
Vector<SubAppsServiceAddParametersPtr> sub_apps_to_add_mojo;
for (auto& [manifest_id_path, add_params] : sub_apps_to_add_idl) {
sub_apps_to_add_mojo.emplace_back(SubAppsServiceAddParameters::New(
manifest_id_path, add_params->installURL()));
}
return sub_apps_to_add_mojo;
}
HeapVector<std::pair<String, Member<SubAppsListResult>>> ListResultsFromMojo(
Vector<SubAppsServiceListResultEntryPtr> sub_apps_list_mojo) {
HeapVector<std::pair<String, Member<SubAppsListResult>>> sub_apps_list_idl;
for (auto& sub_app_entry : sub_apps_list_mojo) {
SubAppsListResult* list_result = SubAppsListResult::Create();
list_result->setAppName(std::move(sub_app_entry->app_name));
sub_apps_list_idl.emplace_back(std::move(sub_app_entry->manifest_id_path),
list_result);
}
return sub_apps_list_idl;
}
} // namespace
// static
const char SubApps::kSupplementName[] = "SubApps";
SubApps::SubApps(Navigator& navigator)
: Supplement<Navigator>(navigator),
service_(navigator.GetExecutionContext()) {}
// static
SubApps* SubApps::subApps(Navigator& navigator) {
SubApps* subapps = Supplement<Navigator>::From<SubApps>(navigator);
if (!subapps) {
subapps = MakeGarbageCollected<SubApps>(navigator);
ProvideTo(navigator, subapps);
}
return subapps;
}
void SubApps::Trace(Visitor* visitor) const {
ScriptWrappable::Trace(visitor);
Supplement<Navigator>::Trace(visitor);
visitor->Trace(service_);
}
HeapMojoRemote<SubAppsService>& SubApps::GetService() {
if (!service_.is_bound()) {
auto* context = GetSupplementable()->GetExecutionContext();
context->GetBrowserInterfaceBroker().GetInterface(
service_.BindNewPipeAndPassReceiver(
context->GetTaskRunner(TaskType::kMiscPlatformAPI)));
// In case the other endpoint gets disconnected, we want to reset our end of
// the pipe as well so that we don't remain connected to a half-open pipe.
service_.set_disconnect_handler(
WTF::BindOnce(&SubApps::OnConnectionError, WrapWeakPersistent(this)));
}
return service_;
}
void SubApps::OnConnectionError() {
service_.reset();
}
ScriptPromise<IDLRecord<IDLString, V8SubAppsResultCode>> SubApps::add(
ScriptState* script_state,
const HeapVector<std::pair<String, Member<SubAppsAddParams>>>&
sub_apps_to_add,
ExceptionState& exception_state) {
// [SecureContext] from the IDL ensures this.
DCHECK(ExecutionContext::From(script_state)->IsSecureContext());
if (!CheckPreconditionsMaybeThrow(script_state, exception_state)) {
return ScriptPromise<IDLRecord<IDLString, V8SubAppsResultCode>>();
}
auto* frame = GetSupplementable()->DomWindow()->GetFrame();
bool needsUserActivation =
frame->GetSettings()
->GetRequireTransientActivationAndAuthorizationForSubAppsAPI();
// We don't need user activation if the right policy is set.
if (needsUserActivation &&
!LocalFrame::ConsumeTransientUserActivation(frame)) {
exception_state.ThrowDOMException(
DOMExceptionCode::kNotAllowedError,
"Unable to add sub-app. This API can only be called shortly after a "
"user activation.");
return ScriptPromise<IDLRecord<IDLString, V8SubAppsResultCode>>();
}
// We don't need to limit add() if the right policy is set, we mainly want to
// avoid overwhelming the user with a permissions prompt that lists dozens of
// apps to install.
if (needsUserActivation &&
sub_apps_to_add.size() > kMaximumNumberOfSubappsPerAddCall) {
exception_state.ThrowDOMException(
DOMExceptionCode::kDataError,
"Unable to add sub-apps. The maximum number of apps added per call "
"is " +
String::Number(kMaximumNumberOfSubappsPerAddCall) + ", but " +
String::Number(sub_apps_to_add.size()) + " were provided.");
return ScriptPromise<IDLRecord<IDLString, V8SubAppsResultCode>>();
}
// Check that the arguments are root-relative paths.
for (const auto& [manifest_id_path, add_params] : sub_apps_to_add) {
if (KURL(manifest_id_path).IsValid() ||
KURL(add_params->installURL()).IsValid()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kNotSupportedError,
"Arguments must be root-relative paths.");
return ScriptPromise<IDLRecord<IDLString, V8SubAppsResultCode>>();
}
}
auto* resolver = MakeGarbageCollected<
ScriptPromiseResolver<IDLRecord<IDLString, V8SubAppsResultCode>>>(
script_state);
GetService()->Add(
AddOptionsToMojo(std::move(sub_apps_to_add)),
resolver->WrapCallbackInScriptScope(WTF::BindOnce(
[](ScriptPromiseResolver<IDLRecord<IDLString, V8SubAppsResultCode>>*
resolver,
Vector<SubAppsServiceAddResultPtr> results_mojo) {
for (const auto& add_result : results_mojo) {
if (add_result->result_code ==
SubAppsServiceResultCode::kFailure) {
return resolver
->Reject<IDLRecord<IDLString, V8SubAppsResultCode>>(
AddResultsFromMojo(std::move(results_mojo)));
}
}
resolver->Resolve(AddResultsFromMojo(std::move(results_mojo)));
})));
return resolver->Promise();
}
ScriptPromise<IDLRecord<IDLString, SubAppsListResult>> SubApps::list(
ScriptState* script_state,
ExceptionState& exception_state) {
if (!CheckPreconditionsMaybeThrow(script_state, exception_state)) {
return ScriptPromise<IDLRecord<IDLString, SubAppsListResult>>();
}
auto* resolver = MakeGarbageCollected<
ScriptPromiseResolver<IDLRecord<IDLString, SubAppsListResult>>>(
script_state);
GetService()->List(resolver->WrapCallbackInScriptScope(WTF::BindOnce(
[](ScriptPromiseResolver<IDLRecord<IDLString, SubAppsListResult>>*
resolver,
SubAppsServiceListResultPtr result) {
if (result->result_code == SubAppsServiceResultCode::kSuccess) {
resolver->Resolve(
ListResultsFromMojo(std::move(result->sub_apps_list)));
} else {
resolver->Reject(V8ThrowDOMException::CreateOrDie(
resolver->GetScriptState()->GetIsolate(),
DOMExceptionCode::kOperationError,
"Unable to list sub-apps. Check whether the calling app is "
"installed."));
}
})));
return resolver->Promise();
}
ScriptPromise<IDLRecord<IDLString, V8SubAppsResultCode>> SubApps::remove(
ScriptState* script_state,
const Vector<String>& manifest_id_paths,
ExceptionState& exception_state) {
if (!CheckPreconditionsMaybeThrow(script_state, exception_state)) {
return ScriptPromise<IDLRecord<IDLString, V8SubAppsResultCode>>();
}
// Check that the arguments are root-relative paths.
for (const auto& manifest_id_path : manifest_id_paths) {
if (KURL(manifest_id_path).IsValid()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kNotSupportedError,
"Arguments must be root-relative paths.");
return ScriptPromise<IDLRecord<IDLString, V8SubAppsResultCode>>();
}
}
auto* resolver = MakeGarbageCollected<
ScriptPromiseResolver<IDLRecord<IDLString, V8SubAppsResultCode>>>(
script_state);
GetService()->Remove(
manifest_id_paths,
resolver->WrapCallbackInScriptScope(WTF::BindOnce(
[](ScriptPromiseResolver<IDLRecord<IDLString, V8SubAppsResultCode>>*
resolver,
Vector<SubAppsServiceRemoveResultPtr> results_mojo) {
for (const auto& remove_result : results_mojo) {
if (remove_result->result_code ==
SubAppsServiceResultCode::kFailure) {
return resolver
->Reject<IDLRecord<IDLString, V8SubAppsResultCode>>(
RemoveResultsFromMojo(std::move(results_mojo)));
}
}
resolver->Resolve(RemoveResultsFromMojo(std::move(results_mojo)));
})));
return resolver->Promise();
}
bool SubApps::CheckPreconditionsMaybeThrow(ScriptState* script_state,
ExceptionState& exception_state) {
if (!ExecutionContext::From(script_state)
->IsFeatureEnabled(
network::mojom::PermissionsPolicyFeature::kSubApps)) {
exception_state.ThrowSecurityError(
"The executing top-level browsing context is not granted the "
"\"sub-apps\" permissions policy.");
return false;
}
Navigator* const navigator = GetSupplementable();
if (!navigator->DomWindow()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kNotFoundError,
"The object is no longer associated to a document.");
return false;
}
if (!navigator->DomWindow()->GetFrame()->IsMainFrame() ||
navigator->DomWindow()->GetFrame()->GetPage()->IsPrerendering() ||
navigator->DomWindow()->GetFrame()->IsInFencedFrameTree()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kInvalidStateError,
"API is only supported in primary top-level browsing contexts.");
return false;
}
return true;
}
} // namespace blink
|