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