OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/installedapp/NavigatorInstalledApp.h" |
| 6 |
| 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 8 #include "bindings/core/v8/ScriptPromise.h" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 10 #include "bindings/core/v8/ScriptState.h" |
| 11 #include "core/dom/DOMException.h" |
| 12 #include "core/dom/Document.h" |
| 13 #include "core/dom/ExceptionCode.h" |
| 14 #include "core/frame/LocalDOMWindow.h" |
| 15 #include "core/frame/LocalFrame.h" |
| 16 #include "core/frame/Navigator.h" |
| 17 #include "modules/installedapp/InstalledAppController.h" |
| 18 #include "modules/installedapp/RelatedApplication.h" |
| 19 #include "public/platform/modules/installedapp/WebInstalledAppClient.h" |
| 20 #include "public/platform/modules/installedapp/WebRelatedApplication.h" |
| 21 |
| 22 namespace blink { |
| 23 |
| 24 NavigatorInstalledApp::NavigatorInstalledApp(LocalFrame* frame) |
| 25 : DOMWindowProperty(frame) |
| 26 { |
| 27 } |
| 28 |
| 29 NavigatorInstalledApp* NavigatorInstalledApp::from(Document& document) |
| 30 { |
| 31 if (!document.frame() || !document.frame()->domWindow()) |
| 32 return nullptr; |
| 33 Navigator& navigator = *document.frame()->domWindow()->navigator(); |
| 34 return &from(navigator); |
| 35 } |
| 36 |
| 37 NavigatorInstalledApp& NavigatorInstalledApp::from(Navigator& navigator) |
| 38 { |
| 39 NavigatorInstalledApp* supplement = static_cast<NavigatorInstalledApp*>(Heap
Supplement<Navigator>::from(navigator, supplementName())); |
| 40 if (!supplement) { |
| 41 supplement = new NavigatorInstalledApp(navigator.frame()); |
| 42 provideTo(navigator, supplementName(), supplement); |
| 43 } |
| 44 return *supplement; |
| 45 } |
| 46 |
| 47 ScriptPromise NavigatorInstalledApp::getInstalledRelatedApps(ScriptState* script
State, Navigator& navigator) |
| 48 { |
| 49 return NavigatorInstalledApp::from(navigator).getInstalledRelatedApps(script
State); |
| 50 } |
| 51 |
| 52 class RelatedAppArray { |
| 53 STATIC_ONLY(RelatedAppArray); |
| 54 |
| 55 public: |
| 56 using WebType = const WebVector<WebRelatedApplication>&; |
| 57 |
| 58 static HeapVector<Member<RelatedApplication>> take(ScriptPromiseResolver*, c
onst WebVector<WebRelatedApplication>& webInfo) |
| 59 { |
| 60 HeapVector<Member<RelatedApplication>> applications; |
| 61 for (const auto& webApplication : webInfo) |
| 62 applications.append(RelatedApplication::create(webApplication.platfo
rm, webApplication.url, webApplication.id)); |
| 63 return applications; |
| 64 } |
| 65 }; |
| 66 |
| 67 ScriptPromise NavigatorInstalledApp::getInstalledRelatedApps(ScriptState* script
State) |
| 68 { |
| 69 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 70 ScriptPromise promise = resolver->promise(); |
| 71 |
| 72 // Don't crash when called and unattached to document. |
| 73 Document* document = m_frame ? m_frame->document() : 0; |
| 74 |
| 75 if (!document || !controller()) { |
| 76 DOMException* exception = DOMException::create(InvalidStateError, "The o
bject is no longer associated to a document."); |
| 77 resolver->reject(exception); |
| 78 return promise; |
| 79 } |
| 80 |
| 81 controller()->getInstalledApps( |
| 82 WebSecurityOrigin(scriptState->executionContext()->securityOrigin()), |
| 83 adoptWebPtr(new CallbackPromiseAdapter<RelatedAppArray, void>(resolver))
); |
| 84 return promise; |
| 85 } |
| 86 |
| 87 InstalledAppController* NavigatorInstalledApp::controller() |
| 88 { |
| 89 if (!frame()) |
| 90 return nullptr; |
| 91 |
| 92 return InstalledAppController::from(*frame()); |
| 93 } |
| 94 |
| 95 const char* NavigatorInstalledApp::supplementName() |
| 96 { |
| 97 return "NavigatorInstalledApp"; |
| 98 } |
| 99 |
| 100 DEFINE_TRACE(NavigatorInstalledApp) |
| 101 { |
| 102 HeapSupplement<Navigator>::trace(visitor); |
| 103 DOMWindowProperty::trace(visitor); |
| 104 } |
| 105 |
| 106 } // namespace blink |
OLD | NEW |