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 namespace { | |
19 | |
20 // Wrapper around an AppInstalledCallbacks, applying a filter to the successful | |
21 // result before passing it to the inner callback's onSuccess method. | |
22 class CallbackFilter : public AppInstalledCallbacks { | |
23 public: | |
24 // The filter function takes a vector of related applications, applies some | |
25 // operation to it, and then forwards the result to the given callback. | |
26 using FilterFunction = | |
27 WTF::Function<void(const blink::WebVector<blink::WebRelatedApplication>&, | |
28 std::unique_ptr<blink::AppInstalledCallbacks>)>; | |
29 | |
30 CallbackFilter(std::unique_ptr<FilterFunction>, | |
31 std::unique_ptr<AppInstalledCallbacks>); | |
32 | |
33 // AppInstalledCallbacks overrides: | |
34 void onSuccess(const WebVector<WebRelatedApplication>& relatedApps) override; | |
35 void onError() override; | |
36 | |
37 private: | |
38 std::unique_ptr<FilterFunction> m_filter; | |
39 std::unique_ptr<AppInstalledCallbacks> m_callbacks; | |
40 }; | |
41 | |
42 CallbackFilter::CallbackFilter(std::unique_ptr<FilterFunction> filter, | |
43 std::unique_ptr<AppInstalledCallbacks> callbacks) | |
44 : m_filter(std::move(filter)), m_callbacks(std::move(callbacks)) {} | |
45 | |
46 void CallbackFilter::onSuccess( | |
esprehn
2017/02/08 04:40:33
lets just make this virtual and call the super met
Matt Giuca
2017/02/08 08:27:11
Sorry, I don't understand what you're asking for h
Matt Giuca
2017/02/13 06:34:50
sammc@ helped me to remove some of the abstraction
| |
47 const WebVector<WebRelatedApplication>& relatedApps) { | |
48 (*m_filter)(relatedApps, std::move(m_callbacks)); | |
49 } | |
50 | |
51 void CallbackFilter::onError() { | |
52 m_callbacks->onError(); | |
53 } | |
54 | |
55 } // namespace | |
56 | |
16 InstalledAppController::~InstalledAppController() {} | 57 InstalledAppController::~InstalledAppController() {} |
17 | 58 |
18 void InstalledAppController::provideTo(LocalFrame& frame, | 59 void InstalledAppController::getInstalledRelatedApps( |
19 WebInstalledAppClient* client) { | 60 const WebSecurityOrigin& url, |
20 ASSERT(RuntimeEnabledFeatures::installedAppEnabled()); | 61 std::unique_ptr<AppInstalledCallbacks> callbacks) { |
62 // When detached, the fetcher is no longer valid. | |
63 if (!m_relatedAppsFetcher) { | |
64 // TODO(mgiuca): AbortError rather than simply undefined. | |
65 // https://crbug.com/687846 | |
66 callbacks->onError(); | |
67 return; | |
68 } | |
69 | |
70 // Get the list of related applications from the manifest. This requires a | |
71 // request to the content layer (because the manifest is not a Blink concept). | |
72 // Upon returning, filter the result list to those apps that are installed. | |
73 // TODO(mgiuca): This roundtrip to content could be eliminated if the Manifest | |
74 // class was moved from content into Blink. | |
75 m_relatedAppsFetcher->getManifestRelatedApplications( | |
76 WTF::makeUnique<CallbackFilter>( | |
77 WTF::bind(&InstalledAppController::filterByInstalledApps, | |
78 wrapPersistent(this), url), | |
79 std::move(callbacks))); | |
80 } | |
81 | |
82 void InstalledAppController::provideTo( | |
83 LocalFrame& frame, | |
84 WebRelatedAppsFetcher* relatedAppsFetcher) { | |
85 DCHECK(RuntimeEnabledFeatures::installedAppEnabled()); | |
21 | 86 |
22 InstalledAppController* controller = | 87 InstalledAppController* controller = |
23 new InstalledAppController(frame, client); | 88 new InstalledAppController(frame, relatedAppsFetcher); |
24 Supplement<LocalFrame>::provideTo(frame, supplementName(), controller); | 89 Supplement<LocalFrame>::provideTo(frame, supplementName(), controller); |
25 } | 90 } |
26 | 91 |
27 InstalledAppController* InstalledAppController::from(LocalFrame& frame) { | 92 InstalledAppController* InstalledAppController::from(LocalFrame& frame) { |
28 InstalledAppController* controller = static_cast<InstalledAppController*>( | 93 InstalledAppController* controller = static_cast<InstalledAppController*>( |
29 Supplement<LocalFrame>::from(frame, supplementName())); | 94 Supplement<LocalFrame>::from(frame, supplementName())); |
30 ASSERT(controller); | 95 DCHECK(controller); |
31 return controller; | 96 return controller; |
32 } | 97 } |
33 | 98 |
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() { | 99 const char* InstalledAppController::supplementName() { |
41 return "InstalledAppController"; | 100 return "InstalledAppController"; |
42 } | 101 } |
43 | 102 |
44 void InstalledAppController::getInstalledApps( | 103 InstalledAppController::InstalledAppController( |
45 const WebSecurityOrigin& url, | 104 LocalFrame& frame, |
46 std::unique_ptr<AppInstalledCallbacks> callback) { | 105 WebRelatedAppsFetcher* relatedAppsFetcher) |
47 // When detached, the client is no longer valid. | 106 : Supplement<LocalFrame>(frame), |
48 if (!m_client) { | 107 ContextLifecycleObserver(frame.document()), |
49 callback.release()->onError(); | 108 m_relatedAppsFetcher(relatedAppsFetcher) {} |
50 return; | |
51 } | |
52 | 109 |
53 // Client is expected to take ownership of the callback | 110 void InstalledAppController::contextDestroyed(ExecutionContext*) { |
54 m_client->getInstalledRelatedApps(url, std::move(callback)); | 111 m_relatedAppsFetcher = nullptr; |
55 } | 112 } |
56 | 113 |
57 void InstalledAppController::contextDestroyed(ExecutionContext*) { | 114 void InstalledAppController::filterByInstalledApps( |
58 m_client = nullptr; | 115 const blink::WebSecurityOrigin& /*origin*/, |
116 const blink::WebVector<blink::WebRelatedApplication>& /*relatedApps*/, | |
117 std::unique_ptr<blink::AppInstalledCallbacks> callbacks) { | |
118 // TODO(mgiuca): Call through to the browser to filter the list of | |
119 // applications down to just those that are installed on the device. | |
120 // For now, just return the empty list (no applications are installed). | |
121 callbacks->onSuccess(blink::WebVector<blink::WebRelatedApplication>()); | |
59 } | 122 } |
60 | 123 |
61 DEFINE_TRACE(InstalledAppController) { | 124 DEFINE_TRACE(InstalledAppController) { |
62 Supplement<LocalFrame>::trace(visitor); | 125 Supplement<LocalFrame>::trace(visitor); |
63 ContextLifecycleObserver::trace(visitor); | 126 ContextLifecycleObserver::trace(visitor); |
64 } | 127 } |
65 | 128 |
66 } // namespace blink | 129 } // namespace blink |
OLD | NEW |