| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/installedapp/NavigatorInstalledApp.h" | 5 #include "modules/installedapp/NavigatorInstalledApp.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 8 #include "bindings/core/v8/ScriptPromise.h" | 8 #include "bindings/core/v8/ScriptPromise.h" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 10 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
| 11 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
| 12 #include "core/dom/Document.h" | 12 #include "core/dom/Document.h" |
| 13 #include "core/dom/ExceptionCode.h" | 13 #include "core/dom/ExceptionCode.h" |
| 14 #include "core/frame/LocalDOMWindow.h" | 14 #include "core/frame/LocalDOMWindow.h" |
| 15 #include "core/frame/LocalFrame.h" | 15 #include "core/frame/LocalFrame.h" |
| 16 #include "core/frame/Navigator.h" | 16 #include "core/frame/Navigator.h" |
| 17 #include "modules/installedapp/InstalledAppController.h" | 17 #include "modules/installedapp/InstalledAppController.h" |
| 18 #include "modules/installedapp/RelatedApplication.h" | 18 #include "modules/installedapp/RelatedApplication.h" |
| 19 #include "public/platform/modules/installedapp/WebInstalledAppClient.h" | 19 #include "public/platform/modules/installedapp/WebInstalledAppClient.h" |
| 20 #include "public/platform/modules/installedapp/WebRelatedApplication.h" | 20 #include "public/platform/modules/installedapp/WebRelatedApplication.h" |
| 21 #include "wtf/PtrUtil.h" | 21 #include "wtf/PtrUtil.h" |
| 22 | 22 |
| 23 namespace blink { | 23 namespace blink { |
| 24 | 24 |
| 25 NavigatorInstalledApp::NavigatorInstalledApp(LocalFrame* frame) | 25 NavigatorInstalledApp::NavigatorInstalledApp(Navigator& navigator) |
| 26 : ContextClient(frame) {} | 26 : Supplement<Navigator>(navigator) {} |
| 27 | 27 |
| 28 NavigatorInstalledApp* NavigatorInstalledApp::from(Document& document) { | 28 NavigatorInstalledApp* NavigatorInstalledApp::from(Document& document) { |
| 29 if (!document.frame() || !document.frame()->domWindow()) | 29 if (!document.frame() || !document.frame()->domWindow()) |
| 30 return nullptr; | 30 return nullptr; |
| 31 Navigator& navigator = *document.frame()->domWindow()->navigator(); | 31 Navigator& navigator = *document.frame()->domWindow()->navigator(); |
| 32 return &from(navigator); | 32 return &from(navigator); |
| 33 } | 33 } |
| 34 | 34 |
| 35 NavigatorInstalledApp& NavigatorInstalledApp::from(Navigator& navigator) { | 35 NavigatorInstalledApp& NavigatorInstalledApp::from(Navigator& navigator) { |
| 36 NavigatorInstalledApp* supplement = static_cast<NavigatorInstalledApp*>( | 36 NavigatorInstalledApp* supplement = static_cast<NavigatorInstalledApp*>( |
| 37 Supplement<Navigator>::from(navigator, supplementName())); | 37 Supplement<Navigator>::from(navigator, supplementName())); |
| 38 if (!supplement) { | 38 if (!supplement) { |
| 39 supplement = new NavigatorInstalledApp(navigator.frame()); | 39 supplement = new NavigatorInstalledApp(navigator); |
| 40 provideTo(navigator, supplementName(), supplement); | 40 provideTo(navigator, supplementName(), supplement); |
| 41 } | 41 } |
| 42 return *supplement; | 42 return *supplement; |
| 43 } | 43 } |
| 44 | 44 |
| 45 ScriptPromise NavigatorInstalledApp::getInstalledRelatedApps( | 45 ScriptPromise NavigatorInstalledApp::getInstalledRelatedApps( |
| 46 ScriptState* scriptState, | 46 ScriptState* scriptState, |
| 47 Navigator& navigator) { | 47 Navigator& navigator) { |
| 48 return NavigatorInstalledApp::from(navigator).getInstalledRelatedApps( | 48 return NavigatorInstalledApp::from(navigator).getInstalledRelatedApps( |
| 49 scriptState); | 49 scriptState); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 64 webApplication.platform, webApplication.url, webApplication.id)); | 64 webApplication.platform, webApplication.url, webApplication.id)); |
| 65 return applications; | 65 return applications; |
| 66 } | 66 } |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 ScriptPromise NavigatorInstalledApp::getInstalledRelatedApps( | 69 ScriptPromise NavigatorInstalledApp::getInstalledRelatedApps( |
| 70 ScriptState* scriptState) { | 70 ScriptState* scriptState) { |
| 71 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 71 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 72 ScriptPromise promise = resolver->promise(); | 72 ScriptPromise promise = resolver->promise(); |
| 73 | 73 |
| 74 // Don't crash when called and unattached to document. | 74 InstalledAppController* appController = controller(); |
| 75 Document* document = frame() ? frame()->document() : 0; | 75 if (!appController) { // If the associated frame is detached |
| 76 | |
| 77 if (!document || !controller()) { | |
| 78 DOMException* exception = DOMException::create( | 76 DOMException* exception = DOMException::create( |
| 79 InvalidStateError, "The object is no longer associated to a document."); | 77 InvalidStateError, "The object is no longer associated to a document."); |
| 80 resolver->reject(exception); | 78 resolver->reject(exception); |
| 81 return promise; | 79 return promise; |
| 82 } | 80 } |
| 83 | 81 |
| 84 controller()->getInstalledApps( | 82 appController->getInstalledApps( |
| 85 WebSecurityOrigin( | 83 WebSecurityOrigin( |
| 86 scriptState->getExecutionContext()->getSecurityOrigin()), | 84 scriptState->getExecutionContext()->getSecurityOrigin()), |
| 87 WTF::wrapUnique( | 85 WTF::wrapUnique( |
| 88 new CallbackPromiseAdapter<RelatedAppArray, void>(resolver))); | 86 new CallbackPromiseAdapter<RelatedAppArray, void>(resolver))); |
| 89 return promise; | 87 return promise; |
| 90 } | 88 } |
| 91 | 89 |
| 92 InstalledAppController* NavigatorInstalledApp::controller() { | 90 InstalledAppController* NavigatorInstalledApp::controller() { |
| 93 if (!frame()) | 91 if (!host()->frame()) |
| 94 return nullptr; | 92 return nullptr; |
| 95 | 93 |
| 96 return InstalledAppController::from(*frame()); | 94 return InstalledAppController::from(*host()->frame()); |
| 97 } | 95 } |
| 98 | 96 |
| 99 const char* NavigatorInstalledApp::supplementName() { | 97 const char* NavigatorInstalledApp::supplementName() { |
| 100 return "NavigatorInstalledApp"; | 98 return "NavigatorInstalledApp"; |
| 101 } | 99 } |
| 102 | 100 |
| 103 DEFINE_TRACE(NavigatorInstalledApp) { | 101 DEFINE_TRACE(NavigatorInstalledApp) { |
| 104 Supplement<Navigator>::trace(visitor); | 102 Supplement<Navigator>::trace(visitor); |
| 105 ContextClient::trace(visitor); | |
| 106 } | 103 } |
| 107 | 104 |
| 108 } // namespace blink | 105 } // namespace blink |
| OLD | NEW |