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/InstalledAppController.h" | 5 #include "modules/installedapp/InstalledAppController.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/frame/LocalFrame.h" | 8 #include "core/frame/LocalFrame.h" |
9 #include "platform/RuntimeEnabledFeatures.h" | 9 #include "platform/RuntimeEnabledFeatures.h" |
10 #include "public/platform/WebSecurityOrigin.h" | 10 #include "platform/weborigin/SecurityOrigin.h" |
| 11 #include "public/platform/InterfaceProvider.h" |
| 12 #include "wtf/Functional.h" |
11 | 13 |
12 #include <utility> | 14 #include <utility> |
13 | 15 |
14 namespace blink { | 16 namespace blink { |
15 | 17 |
| 18 // Callbacks for the result of |
| 19 // WebRelatedAppsFetcher::getManifestRelatedApplications. Calls |
| 20 // filterByInstalledApps upon receiving the list of related applications. |
| 21 class InstalledAppController::GetRelatedAppsCallbacks |
| 22 : public AppInstalledCallbacks { |
| 23 public: |
| 24 GetRelatedAppsCallbacks(InstalledAppController* controller, |
| 25 WTF::RefPtr<SecurityOrigin> url, |
| 26 std::unique_ptr<AppInstalledCallbacks> callbacks) |
| 27 : m_controller(controller), |
| 28 m_url(url), |
| 29 m_callbacks(std::move(callbacks)) {} |
| 30 |
| 31 // AppInstalledCallbacks overrides: |
| 32 void onSuccess(const WebVector<WebRelatedApplication>& relatedApps) override { |
| 33 if (!m_controller) |
| 34 return; |
| 35 |
| 36 m_controller->filterByInstalledApps(m_url, relatedApps, |
| 37 std::move(m_callbacks)); |
| 38 } |
| 39 void onError() override { m_callbacks->onError(); } |
| 40 |
| 41 private: |
| 42 WeakPersistent<InstalledAppController> m_controller; |
| 43 WTF::RefPtr<SecurityOrigin> m_url; |
| 44 std::unique_ptr<AppInstalledCallbacks> m_callbacks; |
| 45 }; |
| 46 |
16 InstalledAppController::~InstalledAppController() {} | 47 InstalledAppController::~InstalledAppController() {} |
17 | 48 |
18 void InstalledAppController::provideTo(LocalFrame& frame, | 49 void InstalledAppController::getInstalledRelatedApps( |
19 WebInstalledAppClient* client) { | 50 WTF::RefPtr<SecurityOrigin> url, |
20 ASSERT(RuntimeEnabledFeatures::installedAppEnabled()); | 51 std::unique_ptr<AppInstalledCallbacks> callbacks) { |
| 52 // When detached, the fetcher is no longer valid. |
| 53 if (!m_relatedAppsFetcher) { |
| 54 // TODO(mgiuca): AbortError rather than simply undefined. |
| 55 // https://crbug.com/687846 |
| 56 callbacks->onError(); |
| 57 return; |
| 58 } |
| 59 |
| 60 // Get the list of related applications from the manifest. This requires a |
| 61 // request to the content layer (because the manifest is not a Blink concept). |
| 62 // Upon returning, filter the result list to those apps that are installed. |
| 63 // TODO(mgiuca): This roundtrip to content could be eliminated if the Manifest |
| 64 // class was moved from content into Blink. |
| 65 m_relatedAppsFetcher->getManifestRelatedApplications( |
| 66 WTF::makeUnique<GetRelatedAppsCallbacks>(this, url, |
| 67 std::move(callbacks))); |
| 68 } |
| 69 |
| 70 void InstalledAppController::provideTo( |
| 71 LocalFrame& frame, |
| 72 WebRelatedAppsFetcher* relatedAppsFetcher) { |
| 73 DCHECK(RuntimeEnabledFeatures::installedAppEnabled()); |
21 | 74 |
22 InstalledAppController* controller = | 75 InstalledAppController* controller = |
23 new InstalledAppController(frame, client); | 76 new InstalledAppController(frame, relatedAppsFetcher); |
24 Supplement<LocalFrame>::provideTo(frame, supplementName(), controller); | 77 Supplement<LocalFrame>::provideTo(frame, supplementName(), controller); |
25 } | 78 } |
26 | 79 |
27 InstalledAppController* InstalledAppController::from(LocalFrame& frame) { | 80 InstalledAppController* InstalledAppController::from(LocalFrame& frame) { |
28 InstalledAppController* controller = static_cast<InstalledAppController*>( | 81 InstalledAppController* controller = static_cast<InstalledAppController*>( |
29 Supplement<LocalFrame>::from(frame, supplementName())); | 82 Supplement<LocalFrame>::from(frame, supplementName())); |
30 ASSERT(controller); | 83 DCHECK(controller); |
31 return controller; | 84 return controller; |
32 } | 85 } |
33 | 86 |
34 InstalledAppController::InstalledAppController(LocalFrame& frame, | |
35 WebInstalledAppClient* client) | |
36 : Supplement<LocalFrame>(frame), | |
37 ContextLifecycleObserver(frame.document()), | |
38 m_client(client) {} | |
39 | |
40 const char* InstalledAppController::supplementName() { | 87 const char* InstalledAppController::supplementName() { |
41 return "InstalledAppController"; | 88 return "InstalledAppController"; |
42 } | 89 } |
43 | 90 |
44 void InstalledAppController::getInstalledApps( | 91 InstalledAppController::InstalledAppController( |
45 const WebSecurityOrigin& url, | 92 LocalFrame& frame, |
46 std::unique_ptr<AppInstalledCallbacks> callback) { | 93 WebRelatedAppsFetcher* relatedAppsFetcher) |
47 // When detached, the client is no longer valid. | 94 : Supplement<LocalFrame>(frame), |
48 if (!m_client) { | 95 ContextLifecycleObserver(frame.document()), |
49 callback.release()->onError(); | 96 m_relatedAppsFetcher(relatedAppsFetcher) {} |
50 return; | |
51 } | |
52 | 97 |
53 // Client is expected to take ownership of the callback | 98 void InstalledAppController::contextDestroyed(ExecutionContext*) { |
54 m_client->getInstalledRelatedApps(url, std::move(callback)); | 99 m_relatedAppsFetcher = nullptr; |
55 } | 100 } |
56 | 101 |
57 void InstalledAppController::contextDestroyed(ExecutionContext*) { | 102 void InstalledAppController::filterByInstalledApps( |
58 m_client = nullptr; | 103 WTF::RefPtr<SecurityOrigin> /*origin*/, |
| 104 const WebVector<WebRelatedApplication>& /*relatedApps*/, |
| 105 std::unique_ptr<AppInstalledCallbacks> callbacks) { |
| 106 // TODO(mgiuca): Call through to the browser to filter the list of |
| 107 // applications down to just those that are installed on the device. |
| 108 // For now, just return the empty list (no applications are installed). |
| 109 callbacks->onSuccess(WebVector<WebRelatedApplication>()); |
59 } | 110 } |
60 | 111 |
61 DEFINE_TRACE(InstalledAppController) { | 112 DEFINE_TRACE(InstalledAppController) { |
62 Supplement<LocalFrame>::trace(visitor); | 113 Supplement<LocalFrame>::trace(visitor); |
63 ContextLifecycleObserver::trace(visitor); | 114 ContextLifecycleObserver::trace(visitor); |
64 } | 115 } |
65 | 116 |
66 } // namespace blink | 117 } // namespace blink |
OLD | NEW |