Chromium Code Reviews| 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 int kMinInstanceVersion = 3; // RequestActivityIcons' MinVersion. | |
|
Luis Héctor Chávez
2016/09/15 22:42:11
nit: uint32_t.
Yusuke Sato
2016/09/15 23:08:24
Done.
| |
| 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 gfx::Image icon; | |
|
Luis Héctor Chávez
2016/09/15 22:42:11
This doesn't seem to be used.
Yusuke Sato
2016/09/15 23:08:24
Done.
| |
| 111 const ActivityIconLoader::ActivityName activity(handler->package_name, | |
| 112 handler->activity_name); | |
| 113 const auto it = icons->find(activity); | |
| 114 app_info.emplace_back( | |
| 115 handler->name, it != icons->end() ? it->second.icon20 : gfx::Image()); | |
| 116 } | |
| 117 | |
| 118 auto show_bubble_cb = base::Bind(ShowIntentPickerBubble()); | |
| 119 WebContents* web_contents = | |
| 120 tab_util::GetWebContentsByID(render_process_host_id, routing_id); | |
| 121 show_bubble_cb.Run(web_contents, app_info, | |
| 122 base::Bind(OnIntentPickerClosed, render_process_host_id, | |
| 123 routing_id, url, base::Passed(&handlers))); | |
| 124 } | |
| 125 | |
| 126 // Called when ARC returned a handler list for the |url|. | |
| 127 void OnUrlHandlerList(int render_process_host_id, | |
| 128 int routing_id, | |
| 129 const GURL& url, | |
| 130 mojo::Array<mojom::UrlHandlerInfoPtr> handlers) { | |
| 131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 132 | |
| 133 mojom::IntentHelperInstance* intent_helper = GetIntentHelper(); | |
| 134 scoped_refptr<ActivityIconLoader> icon_loader = GetIconLoader(); | |
| 135 | |
| 136 if (!intent_helper || !icon_loader || !handlers.size()) { | |
| 137 // No handler is available on ARC side. Show the Chrome OS dialog. | |
| 138 ShowFallbackExternalProtocolDialog(render_process_host_id, routing_id, url); | |
| 139 return; | |
| 140 } | |
| 141 | |
| 142 if (handlers.size() == 1) { | |
| 143 // Special case. When ARC has only one activity for the |url|, silently | |
| 144 // launch the app to be consistent with Android's behavior. No UI needed | |
| 145 // in this case. | |
| 146 intent_helper->HandleUrl(url.spec(), handlers[0]->package_name); | |
| 147 return; | |
| 148 } | |
| 149 | |
| 150 // Otherwise, retrieve icons of the activities. | |
| 151 std::vector<ActivityIconLoader::ActivityName> activities; | |
| 152 for (const auto& handler : handlers) { | |
| 153 activities.emplace_back(handler->package_name, handler->activity_name); | |
| 154 } | |
| 155 icon_loader->GetActivityIcons( | |
| 156 activities, base::Bind(OnAppIconsReceived, render_process_host_id, | |
| 157 routing_id, url, base::Passed(&handlers))); | |
| 158 } | |
| 159 | |
| 160 } // namespace | |
| 161 | |
| 162 // static | |
| 163 bool RunArcExternalProtocolDialog(const GURL& url, | |
| 164 int render_process_host_id, | |
| 165 int routing_id, | |
| 166 ui::PageTransition page_transition, | |
| 167 bool has_user_gesture) { | |
| 168 if (ShouldIgnoreNavigation(page_transition)) | |
| 169 return false; | |
| 170 | |
| 171 mojom::IntentHelperInstance* intent_helper = GetIntentHelper(); | |
| 172 if (!intent_helper) | |
| 173 return false; // ARC is either not supported or not yet ready. | |
| 174 | |
| 175 WebContents* web_contents = | |
| 176 tab_util::GetWebContentsByID(render_process_host_id, routing_id); | |
| 177 if (!web_contents || !web_contents->GetBrowserContext() || | |
| 178 web_contents->GetBrowserContext()->IsOffTheRecord()) { | |
| 179 return false; | |
| 180 } | |
| 181 | |
| 182 // Show ARC version of the dialog, which is IntentPickerBubbleView. To show | |
| 183 // the bubble view, we need to ask ARC for a handler list first. | |
| 184 intent_helper->RequestUrlHandlerList( | |
|
Luis Héctor Chávez
2016/09/15 22:42:11
What would happen if ARC were to completely crash
Yusuke Sato
2016/09/15 23:08:24
In that case, no dialog will be shown for the user
| |
| 185 url.spec(), | |
| 186 base::Bind(OnUrlHandlerList, render_process_host_id, routing_id, url)); | |
| 187 return true; | |
| 188 } | |
| 189 | |
| 190 } // namespace arc | |
| OLD | NEW |