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
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "modules/installedapp/NavigatorInstalledApp.h"
#include "bindings/core/v8/CallbackPromiseAdapter.h"
#include "bindings/core/v8/ScriptPromise.h"
#include "bindings/core/v8/ScriptPromiseResolver.h"
#include "bindings/core/v8/ScriptState.h"
#include "core/dom/DOMException.h"
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "core/frame/LocalDOMWindow.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/Navigator.h"
#include "modules/installedapp/InstalledAppController.h"
#include "modules/installedapp/RelatedApplication.h"
#include "public/platform/modules/installedapp/WebInstalledAppClient.h"
#include "public/platform/modules/installedapp/WebRelatedApplication.h"
#include "wtf/PtrUtil.h"
namespace blink {
NavigatorInstalledApp::NavigatorInstalledApp(Navigator& navigator)
: Supplement<Navigator>(navigator) {}
NavigatorInstalledApp* NavigatorInstalledApp::from(Document& document) {
if (!document.frame() || !document.frame()->domWindow())
return nullptr;
Navigator& navigator = *document.frame()->domWindow()->navigator();
return &from(navigator);
}
NavigatorInstalledApp& NavigatorInstalledApp::from(Navigator& navigator) {
NavigatorInstalledApp* supplement = static_cast<NavigatorInstalledApp*>(
Supplement<Navigator>::from(navigator, supplementName()));
if (!supplement) {
supplement = new NavigatorInstalledApp(navigator);
provideTo(navigator, supplementName(), supplement);
}
return *supplement;
}
ScriptPromise NavigatorInstalledApp::getInstalledRelatedApps(
ScriptState* scriptState,
Navigator& navigator) {
return NavigatorInstalledApp::from(navigator).getInstalledRelatedApps(
scriptState);
}
class RelatedAppArray {
STATIC_ONLY(RelatedAppArray);
public:
using WebType = const WebVector<WebRelatedApplication>&;
static HeapVector<Member<RelatedApplication>> take(
ScriptPromiseResolver*,
const WebVector<WebRelatedApplication>& webInfo) {
HeapVector<Member<RelatedApplication>> applications;
for (const auto& webApplication : webInfo)
applications.push_back(RelatedApplication::create(
webApplication.platform, webApplication.url, webApplication.id));
return applications;
}
};
ScriptPromise NavigatorInstalledApp::getInstalledRelatedApps(
ScriptState* scriptState) {
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
InstalledAppController* appController = controller();
if (!appController) { // If the associated frame is detached
DOMException* exception = DOMException::create(
InvalidStateError, "The object is no longer associated to a document.");
resolver->reject(exception);
return promise;
}
appController->getInstalledApps(
WebSecurityOrigin(
scriptState->getExecutionContext()->getSecurityOrigin()),
WTF::wrapUnique(
new CallbackPromiseAdapter<RelatedAppArray, void>(resolver)));
return promise;
}
InstalledAppController* NavigatorInstalledApp::controller() {
if (!supplementable()->frame())
return nullptr;
return InstalledAppController::from(*supplementable()->frame());
}
const char* NavigatorInstalledApp::supplementName() {
return "NavigatorInstalledApp";
}
DEFINE_TRACE(NavigatorInstalledApp) {
Supplement<Navigator>::trace(visitor);
}
} // namespace blink
|