OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/arc/arc_external_protocol_dialog.h" | |
6 | |
7 #include <memory> | |
8 #include <string> | |
9 #include <utility> | |
10 #include <vector> | |
11 | |
12 #include "base/bind.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "chrome/browser/chromeos/arc/arc_navigation_throttle.h" | |
15 #include "chrome/browser/chromeos/arc/page_transition_util.h" | |
16 #include "chrome/browser/chromeos/external_protocol_dialog.h" | |
17 #include "chrome/browser/tab_contents/tab_util.h" | |
18 #include "chrome/browser/ui/browser_dialogs.h" | |
19 #include "components/arc/arc_bridge_service.h" | |
20 #include "components/arc/arc_service_manager.h" | |
21 #include "components/arc/intent_helper/activity_icon_loader.h" | |
22 #include "components/arc/intent_helper/arc_intent_helper_bridge.h" | |
23 #include "content/public/browser/browser_context.h" | |
24 #include "content/public/browser/browser_thread.h" | |
25 #include "content/public/browser/web_contents.h" | |
26 #include "ui/gfx/image/image.h" | |
27 #include "url/gurl.h" | |
28 | |
29 using content::WebContents; | |
30 | |
31 namespace arc { | |
32 | |
33 namespace { | |
34 | |
35 constexpr uint32_t kMinInstanceVersion = 3; // RequestActivityIcons' MinVersion | |
36 | |
37 // Shows the Chrome OS' original external protocol dialog as a fallback. | |
38 void ShowFallbackExternalProtocolDialog(int render_process_host_id, | |
39 int routing_id, | |
40 const GURL& url) { | |
41 WebContents* web_contents = | |
42 tab_util::GetWebContentsByID(render_process_host_id, routing_id); | |
43 new ExternalProtocolDialog(web_contents, url); | |
44 } | |
45 | |
46 mojom::IntentHelperInstance* GetIntentHelper() { | |
47 return ArcIntentHelperBridge::GetIntentHelperInstance(kMinInstanceVersion); | |
48 } | |
49 | |
50 scoped_refptr<ActivityIconLoader> GetIconLoader() { | |
51 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
52 ArcServiceManager* arc_service_manager = ArcServiceManager::Get(); | |
53 return arc_service_manager ? arc_service_manager->icon_loader() : nullptr; | |
54 } | |
55 | |
56 // Called when the dialog is closed. | |
57 void OnIntentPickerClosed(int render_process_host_id, | |
58 int routing_id, | |
59 const GURL& url, | |
60 mojo::Array<mojom::UrlHandlerInfoPtr> handlers, | |
61 size_t selected_app_index, | |
62 ArcNavigationThrottle::CloseReason close_reason) { | |
63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
64 | |
65 mojom::IntentHelperInstance* intent_helper = GetIntentHelper(); | |
66 if (!intent_helper || selected_app_index >= handlers.size()) | |
67 close_reason = ArcNavigationThrottle::CloseReason::ERROR; | |
68 | |
69 switch (close_reason) { | |
70 case ArcNavigationThrottle::CloseReason::ALWAYS_PRESSED: { | |
71 intent_helper->AddPreferredPackage( | |
72 handlers[selected_app_index]->package_name); | |
73 // fall through. | |
74 } | |
75 case ArcNavigationThrottle::CloseReason::JUST_ONCE_PRESSED: | |
76 case ArcNavigationThrottle::CloseReason::PREFERRED_ACTIVITY_FOUND: { | |
77 // Launch the selected app. | |
78 intent_helper->HandleUrl(url.spec(), | |
79 handlers[selected_app_index]->package_name); | |
80 break; | |
81 } | |
82 case ArcNavigationThrottle::CloseReason::ERROR: | |
83 case ArcNavigationThrottle::CloseReason::INVALID: { | |
84 LOG(ERROR) << "IntentPickerBubbleView returned unexpected close_reason: " | |
85 << static_cast<int>(close_reason); | |
86 // fall through. | |
87 } | |
88 case ArcNavigationThrottle::CloseReason::DIALOG_DEACTIVATED: { | |
89 // The user didn't select any ARC activity. Show the Chrome OS dialog. | |
90 ShowFallbackExternalProtocolDialog(render_process_host_id, routing_id, | |
91 url); | |
92 break; | |
93 } | |
94 } | |
95 } | |
96 | |
97 // Called when ARC returned activity icons for the |handlers|. | |
98 void OnAppIconsReceived( | |
99 int render_process_host_id, | |
100 int routing_id, | |
101 const GURL& url, | |
102 mojo::Array<mojom::UrlHandlerInfoPtr> handlers, | |
103 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> icons) { | |
104 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
105 | |
106 using NameAndIcon = std::pair<std::string, gfx::Image>; | |
107 std::vector<NameAndIcon> app_info; | |
108 | |
109 for (const auto& handler : handlers) { | |
110 const ActivityIconLoader::ActivityName activity(handler->package_name, | |
111 handler->activity_name); | |
112 const auto it = icons->find(activity); | |
113 app_info.emplace_back( | |
114 handler->name, it != icons->end() ? it->second.icon20 : gfx::Image()); | |
115 } | |
116 | |
117 auto show_bubble_cb = base::Bind(ShowIntentPickerBubble()); | |
118 WebContents* web_contents = | |
119 tab_util::GetWebContentsByID(render_process_host_id, routing_id); | |
120 show_bubble_cb.Run(web_contents, app_info, | |
121 base::Bind(OnIntentPickerClosed, render_process_host_id, | |
122 routing_id, url, base::Passed(&handlers))); | |
123 } | |
124 | |
125 // Called when ARC returned a handler list for the |url|. | |
126 void OnUrlHandlerList(int render_process_host_id, | |
127 int routing_id, | |
128 const GURL& url, | |
129 mojo::Array<mojom::UrlHandlerInfoPtr> handlers) { | |
130 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
131 | |
132 mojom::IntentHelperInstance* intent_helper = GetIntentHelper(); | |
133 scoped_refptr<ActivityIconLoader> icon_loader = GetIconLoader(); | |
134 | |
135 if (!intent_helper || !icon_loader || !handlers.size()) { | |
136 // No handler is available on ARC side. Show the Chrome OS dialog. | |
137 ShowFallbackExternalProtocolDialog(render_process_host_id, routing_id, url); | |
138 return; | |
139 } | |
140 | |
141 if (handlers.size() == 1) { | |
142 // Special case. When ARC has only one activity for the |url|, silently | |
143 // launch the app to be consistent with Android's behavior. No UI needed | |
144 // in this case. | |
145 intent_helper->HandleUrl(url.spec(), handlers[0]->package_name); | |
146 return; | |
147 } | |
148 | |
149 // Otherwise, retrieve icons of the activities. | |
150 std::vector<ActivityIconLoader::ActivityName> activities; | |
151 for (const auto& handler : handlers) { | |
152 activities.emplace_back(handler->package_name, handler->activity_name); | |
153 } | |
154 icon_loader->GetActivityIcons( | |
155 activities, base::Bind(OnAppIconsReceived, render_process_host_id, | |
156 routing_id, url, base::Passed(&handlers))); | |
157 } | |
158 | |
159 } // namespace | |
160 | |
161 // static | |
xiyuan
2016/09/16 20:38:35
nit: remove since it is not really a static method
Yusuke Sato
2016/09/16 20:55:12
Done.
| |
162 bool RunArcExternalProtocolDialog(const GURL& url, | |
163 int render_process_host_id, | |
164 int routing_id, | |
165 ui::PageTransition page_transition, | |
166 bool has_user_gesture) { | |
167 if (ShouldIgnoreNavigation(page_transition)) | |
168 return false; | |
169 | |
170 mojom::IntentHelperInstance* intent_helper = GetIntentHelper(); | |
171 if (!intent_helper) | |
172 return false; // ARC is either not supported or not yet ready. | |
173 | |
174 WebContents* web_contents = | |
175 tab_util::GetWebContentsByID(render_process_host_id, routing_id); | |
176 if (!web_contents || !web_contents->GetBrowserContext() || | |
177 web_contents->GetBrowserContext()->IsOffTheRecord()) { | |
178 return false; | |
179 } | |
180 | |
181 // Show ARC version of the dialog, which is IntentPickerBubbleView. To show | |
182 // the bubble view, we need to ask ARC for a handler list first. | |
183 intent_helper->RequestUrlHandlerList( | |
184 url.spec(), | |
185 base::Bind(OnUrlHandlerList, render_process_host_id, routing_id, url)); | |
186 return true; | |
187 } | |
188 | |
189 } // namespace arc | |
OLD | NEW |