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 "platform/weborigin/SecurityOrigin.h" | |
11 #include "public/platform/InterfaceProvider.h" | 10 #include "public/platform/InterfaceProvider.h" |
12 #include "wtf/Functional.h" | 11 #include "wtf/Functional.h" |
13 | 12 |
14 #include <utility> | 13 #include <utility> |
15 | 14 |
16 namespace blink { | 15 namespace blink { |
17 | 16 |
18 // Callbacks for the result of | 17 // Callbacks for the result of |
19 // WebRelatedAppsFetcher::getManifestRelatedApplications. Calls | 18 // WebRelatedAppsFetcher::getManifestRelatedApplications. Calls |
20 // filterByInstalledApps upon receiving the list of related applications. | 19 // filterByInstalledApps upon receiving the list of related applications. |
21 class InstalledAppController::GetRelatedAppsCallbacks | 20 class InstalledAppController::GetRelatedAppsCallbacks |
22 : public AppInstalledCallbacks { | 21 : public AppInstalledCallbacks { |
23 public: | 22 public: |
24 GetRelatedAppsCallbacks(InstalledAppController* controller, | 23 GetRelatedAppsCallbacks(InstalledAppController* controller, |
25 WTF::RefPtr<SecurityOrigin> url, | |
26 std::unique_ptr<AppInstalledCallbacks> callbacks) | 24 std::unique_ptr<AppInstalledCallbacks> callbacks) |
27 : m_controller(controller), | 25 : m_controller(controller), |
28 m_url(url), | |
29 m_callbacks(std::move(callbacks)) {} | 26 m_callbacks(std::move(callbacks)) {} |
30 | 27 |
31 // AppInstalledCallbacks overrides: | 28 // AppInstalledCallbacks overrides: |
32 void onSuccess(const WebVector<WebRelatedApplication>& relatedApps) override { | 29 void onSuccess(const WebVector<WebRelatedApplication>& relatedApps) override { |
33 if (!m_controller) | 30 if (!m_controller) |
34 return; | 31 return; |
35 | 32 |
36 m_controller->filterByInstalledApps(m_url, relatedApps, | 33 m_controller->filterByInstalledApps(relatedApps, std::move(m_callbacks)); |
37 std::move(m_callbacks)); | |
38 } | 34 } |
39 void onError() override { m_callbacks->onError(); } | 35 void onError() override { m_callbacks->onError(); } |
40 | 36 |
41 private: | 37 private: |
42 WeakPersistent<InstalledAppController> m_controller; | 38 WeakPersistent<InstalledAppController> m_controller; |
43 WTF::RefPtr<SecurityOrigin> m_url; | |
44 std::unique_ptr<AppInstalledCallbacks> m_callbacks; | 39 std::unique_ptr<AppInstalledCallbacks> m_callbacks; |
45 }; | 40 }; |
46 | 41 |
47 InstalledAppController::~InstalledAppController() {} | 42 InstalledAppController::~InstalledAppController() {} |
48 | 43 |
49 void InstalledAppController::getInstalledRelatedApps( | 44 void InstalledAppController::getInstalledRelatedApps( |
50 WTF::RefPtr<SecurityOrigin> url, | |
51 std::unique_ptr<AppInstalledCallbacks> callbacks) { | 45 std::unique_ptr<AppInstalledCallbacks> callbacks) { |
52 // When detached, the fetcher is no longer valid. | 46 // When detached, the fetcher is no longer valid. |
53 if (!m_relatedAppsFetcher) { | 47 if (!m_relatedAppsFetcher) { |
54 // TODO(mgiuca): AbortError rather than simply undefined. | 48 // TODO(mgiuca): AbortError rather than simply undefined. |
55 // https://crbug.com/687846 | 49 // https://crbug.com/687846 |
56 callbacks->onError(); | 50 callbacks->onError(); |
57 return; | 51 return; |
58 } | 52 } |
59 | 53 |
60 // Get the list of related applications from the manifest. This requires a | 54 // 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). | 55 // 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. | 56 // 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 | 57 // TODO(mgiuca): This roundtrip to content could be eliminated if the Manifest |
64 // class was moved from content into Blink. | 58 // class was moved from content into Blink. |
65 m_relatedAppsFetcher->getManifestRelatedApplications( | 59 m_relatedAppsFetcher->getManifestRelatedApplications( |
66 WTF::makeUnique<GetRelatedAppsCallbacks>(this, url, | 60 WTF::makeUnique<GetRelatedAppsCallbacks>(this, std::move(callbacks))); |
67 std::move(callbacks))); | |
68 } | 61 } |
69 | 62 |
70 void InstalledAppController::provideTo( | 63 void InstalledAppController::provideTo( |
71 LocalFrame& frame, | 64 LocalFrame& frame, |
72 WebRelatedAppsFetcher* relatedAppsFetcher) { | 65 WebRelatedAppsFetcher* relatedAppsFetcher) { |
73 DCHECK(RuntimeEnabledFeatures::installedAppEnabled()); | 66 DCHECK(RuntimeEnabledFeatures::installedAppEnabled()); |
74 | 67 |
75 InstalledAppController* controller = | 68 InstalledAppController* controller = |
76 new InstalledAppController(frame, relatedAppsFetcher); | 69 new InstalledAppController(frame, relatedAppsFetcher); |
77 Supplement<LocalFrame>::provideTo(frame, supplementName(), controller); | 70 Supplement<LocalFrame>::provideTo(frame, supplementName(), controller); |
(...skipping 11 matching lines...) Expand all Loading... | |
89 } | 82 } |
90 | 83 |
91 InstalledAppController::InstalledAppController( | 84 InstalledAppController::InstalledAppController( |
92 LocalFrame& frame, | 85 LocalFrame& frame, |
93 WebRelatedAppsFetcher* relatedAppsFetcher) | 86 WebRelatedAppsFetcher* relatedAppsFetcher) |
94 : Supplement<LocalFrame>(frame), | 87 : Supplement<LocalFrame>(frame), |
95 ContextLifecycleObserver(frame.document()), | 88 ContextLifecycleObserver(frame.document()), |
96 m_relatedAppsFetcher(relatedAppsFetcher) {} | 89 m_relatedAppsFetcher(relatedAppsFetcher) {} |
97 | 90 |
98 void InstalledAppController::contextDestroyed(ExecutionContext*) { | 91 void InstalledAppController::contextDestroyed(ExecutionContext*) { |
92 m_provider.reset(); | |
99 m_relatedAppsFetcher = nullptr; | 93 m_relatedAppsFetcher = nullptr; |
100 } | 94 } |
101 | 95 |
102 void InstalledAppController::filterByInstalledApps( | 96 void InstalledAppController::filterByInstalledApps( |
103 WTF::RefPtr<SecurityOrigin> /*origin*/, | 97 const blink::WebVector<blink::WebRelatedApplication>& relatedApps, |
104 const WebVector<WebRelatedApplication>& /*relatedApps*/, | 98 std::unique_ptr<blink::AppInstalledCallbacks> callbacks) { |
105 std::unique_ptr<AppInstalledCallbacks> callbacks) { | 99 WTF::Vector<mojom::blink::RelatedApplicationPtr> mojoRelatedApps; |
106 // TODO(mgiuca): Call through to the browser to filter the list of | 100 for (const auto& relatedApplication : relatedApps) { |
107 // applications down to just those that are installed on the device. | 101 mojom::blink::RelatedApplicationPtr convertedApplication( |
108 // For now, just return the empty list (no applications are installed). | 102 mojom::blink::RelatedApplication::New()); |
109 callbacks->onSuccess(WebVector<WebRelatedApplication>()); | 103 DCHECK(!relatedApplication.platform.isEmpty()); |
104 convertedApplication->platform = relatedApplication.platform; | |
105 convertedApplication->id = relatedApplication.id; | |
106 convertedApplication->url = relatedApplication.url; | |
haraken
2017/03/14 08:58:55
Would it be possible to type-map between RelatedAp
Matt Giuca
2017/03/14 23:21:03
This was discussed above (see #17 to #23). I think
| |
107 mojoRelatedApps.push_back(std::move(convertedApplication)); | |
108 } | |
109 | |
110 if (!m_provider) { | |
111 supplementable()->interfaceProvider()->getInterface( | |
112 mojo::MakeRequest(&m_provider)); | |
113 // TODO(mgiuca): Set a connection error handler. This requires a refactor to | |
114 // work like NavigatorShare.cpp (retain a persistent list of clients to | |
115 // reject all of their promises). | |
116 DCHECK(m_provider); | |
117 } | |
118 | |
119 m_provider->FilterInstalledApps( | |
120 std::move(mojoRelatedApps), | |
121 convertToBaseCallback( | |
122 WTF::bind(&InstalledAppController::OnFilterInstalledApps, | |
123 wrapPersistent(this), WTF::passed(std::move(callbacks))))); | |
124 } | |
125 | |
126 void InstalledAppController::OnFilterInstalledApps( | |
127 std::unique_ptr<blink::AppInstalledCallbacks> callbacks, | |
128 WTF::Vector<mojom::blink::RelatedApplicationPtr> result) { | |
129 std::vector<blink::WebRelatedApplication> applications; | |
130 for (const auto& res : result) { | |
131 blink::WebRelatedApplication app; | |
132 app.platform = res->platform; | |
133 app.url = res->url; | |
134 app.id = res->id; | |
haraken
2017/03/14 08:58:55
Ditto.
| |
135 applications.push_back(app); | |
136 } | |
137 callbacks->onSuccess( | |
138 blink::WebVector<blink::WebRelatedApplication>(applications)); | |
110 } | 139 } |
111 | 140 |
112 DEFINE_TRACE(InstalledAppController) { | 141 DEFINE_TRACE(InstalledAppController) { |
113 Supplement<LocalFrame>::trace(visitor); | 142 Supplement<LocalFrame>::trace(visitor); |
114 ContextLifecycleObserver::trace(visitor); | 143 ContextLifecycleObserver::trace(visitor); |
115 } | 144 } |
116 | 145 |
117 } // namespace blink | 146 } // namespace blink |
OLD | NEW |