| 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 "base/metrics/histogram_macros.h" | |
| 15 #include "chrome/browser/chromeos/arc/arc_navigation_throttle.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 "components/arc/intent_helper/page_transition_util.h" | |
| 24 #include "content/public/browser/browser_context.h" | |
| 25 #include "content/public/browser/browser_thread.h" | |
| 26 #include "content/public/browser/web_contents.h" | |
| 27 #include "ui/gfx/image/image.h" | |
| 28 #include "url/gurl.h" | |
| 29 | |
| 30 using content::WebContents; | |
| 31 | |
| 32 namespace arc { | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 constexpr uint32_t kMinVersionForHandleUrl = 2; | |
| 37 constexpr uint32_t kMinVersionForRequestUrlHandlerList = 2; | |
| 38 constexpr uint32_t kMinVersionForAddPreferredPackage = 7; | |
| 39 | |
| 40 void RecordUma(ArcNavigationThrottle::CloseReason close_reason) { | |
| 41 UMA_HISTOGRAM_ENUMERATION( | |
| 42 "Arc.IntentHandlerAction", static_cast<int>(close_reason), | |
| 43 static_cast<int>(ArcNavigationThrottle::CloseReason::SIZE)); | |
| 44 } | |
| 45 | |
| 46 // Shows the Chrome OS' original external protocol dialog as a fallback. | |
| 47 void ShowFallbackExternalProtocolDialog(int render_process_host_id, | |
| 48 int routing_id, | |
| 49 const GURL& url) { | |
| 50 WebContents* web_contents = | |
| 51 tab_util::GetWebContentsByID(render_process_host_id, routing_id); | |
| 52 new ExternalProtocolDialog(web_contents, url); | |
| 53 } | |
| 54 | |
| 55 scoped_refptr<ActivityIconLoader> GetIconLoader() { | |
| 56 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 57 ArcServiceManager* arc_service_manager = ArcServiceManager::Get(); | |
| 58 return arc_service_manager ? arc_service_manager->icon_loader() : nullptr; | |
| 59 } | |
| 60 | |
| 61 void CloseTabIfNeeded(int render_process_host_id, int routing_id) { | |
| 62 WebContents* web_contents = | |
| 63 tab_util::GetWebContentsByID(render_process_host_id, routing_id); | |
| 64 if (web_contents && web_contents->GetController().IsInitialNavigation()) | |
| 65 web_contents->Close(); | |
| 66 } | |
| 67 | |
| 68 // Called when the dialog is closed. | |
| 69 void OnIntentPickerClosed(int render_process_host_id, | |
| 70 int routing_id, | |
| 71 const GURL& url, | |
| 72 mojo::Array<mojom::IntentHandlerInfoPtr> handlers, | |
| 73 size_t selected_app_index, | |
| 74 ArcNavigationThrottle::CloseReason close_reason) { | |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 76 | |
| 77 // Make sure that the instance at least supports HandleUrl. | |
| 78 auto* instance = ArcIntentHelperBridge::GetIntentHelperInstance( | |
| 79 "HandleUrl", kMinVersionForHandleUrl); | |
| 80 if (!instance || selected_app_index >= handlers.size()) | |
| 81 close_reason = ArcNavigationThrottle::CloseReason::ERROR; | |
| 82 | |
| 83 switch (close_reason) { | |
| 84 case ArcNavigationThrottle::CloseReason::ALWAYS_PRESSED: { | |
| 85 if (ArcIntentHelperBridge::GetIntentHelperInstance( | |
| 86 "AddPreferredPackage", kMinVersionForAddPreferredPackage)) { | |
| 87 instance->AddPreferredPackage( | |
| 88 handlers[selected_app_index]->package_name); | |
| 89 } | |
| 90 // fall through. | |
| 91 } | |
| 92 case ArcNavigationThrottle::CloseReason::JUST_ONCE_PRESSED: { | |
| 93 // Launch the selected app. | |
| 94 instance->HandleUrl(url.spec(), | |
| 95 handlers[selected_app_index]->package_name); | |
| 96 CloseTabIfNeeded(render_process_host_id, routing_id); | |
| 97 break; | |
| 98 } | |
| 99 case ArcNavigationThrottle::CloseReason::PREFERRED_ACTIVITY_FOUND: | |
| 100 case ArcNavigationThrottle::CloseReason::INVALID: { | |
| 101 NOTREACHED(); | |
| 102 return; // no UMA recording. | |
| 103 } | |
| 104 case ArcNavigationThrottle::CloseReason::ERROR: { | |
| 105 LOG(ERROR) << "IntentPickerBubbleView returned CloseReason::ERROR: " | |
| 106 << "instance=" << instance | |
| 107 << ", selected_app_index=" << selected_app_index | |
| 108 << ", handlers.size=" << handlers.size(); | |
| 109 // fall through. | |
| 110 } | |
| 111 case ArcNavigationThrottle::CloseReason::DIALOG_DEACTIVATED: { | |
| 112 // The user didn't select any ARC activity. Show the Chrome OS dialog. | |
| 113 ShowFallbackExternalProtocolDialog(render_process_host_id, routing_id, | |
| 114 url); | |
| 115 break; | |
| 116 } | |
| 117 } | |
| 118 RecordUma(close_reason); | |
| 119 } | |
| 120 | |
| 121 // Called when ARC returned activity icons for the |handlers|. | |
| 122 void OnAppIconsReceived( | |
| 123 int render_process_host_id, | |
| 124 int routing_id, | |
| 125 const GURL& url, | |
| 126 mojo::Array<mojom::IntentHandlerInfoPtr> handlers, | |
| 127 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> icons) { | |
| 128 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 129 | |
| 130 using NameAndIcon = std::pair<std::string, gfx::Image>; | |
| 131 std::vector<NameAndIcon> app_info; | |
| 132 | |
| 133 for (const auto& handler : handlers) { | |
| 134 const ActivityIconLoader::ActivityName activity(handler->package_name, | |
| 135 handler->activity_name); | |
| 136 const auto it = icons->find(activity); | |
| 137 app_info.emplace_back( | |
| 138 handler->name, it != icons->end() ? it->second.icon20 : gfx::Image()); | |
| 139 } | |
| 140 | |
| 141 auto show_bubble_cb = base::Bind(ShowIntentPickerBubble()); | |
| 142 WebContents* web_contents = | |
| 143 tab_util::GetWebContentsByID(render_process_host_id, routing_id); | |
| 144 show_bubble_cb.Run(web_contents, app_info, | |
| 145 base::Bind(OnIntentPickerClosed, render_process_host_id, | |
| 146 routing_id, url, base::Passed(&handlers))); | |
| 147 } | |
| 148 | |
| 149 // Called when ARC returned a handler list for the |url|. | |
| 150 void OnUrlHandlerList(int render_process_host_id, | |
| 151 int routing_id, | |
| 152 const GURL& url, | |
| 153 mojo::Array<mojom::IntentHandlerInfoPtr> handlers) { | |
| 154 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 155 | |
| 156 auto* instance = ArcIntentHelperBridge::GetIntentHelperInstance( | |
| 157 "HandleUrl", kMinVersionForHandleUrl); | |
| 158 scoped_refptr<ActivityIconLoader> icon_loader = GetIconLoader(); | |
| 159 | |
| 160 if (!instance || !icon_loader || !handlers.size()) { | |
| 161 // No handler is available on ARC side. Show the Chrome OS dialog. | |
| 162 ShowFallbackExternalProtocolDialog(render_process_host_id, routing_id, url); | |
| 163 return; | |
| 164 } | |
| 165 | |
| 166 // If one of the apps is marked as preferred, use it right away without | |
| 167 // showing the UI. |is_preferred| will never be true unless the user | |
| 168 // explicitly makes it the default with the "always" button. | |
| 169 for (size_t i = 0; i < handlers.size(); ++i) { | |
| 170 if (!handlers[i]->is_preferred) | |
| 171 continue; | |
| 172 instance->HandleUrl(url.spec(), handlers[i]->package_name); | |
| 173 CloseTabIfNeeded(render_process_host_id, routing_id); | |
| 174 RecordUma(ArcNavigationThrottle::CloseReason::PREFERRED_ACTIVITY_FOUND); | |
| 175 return; | |
| 176 } | |
| 177 | |
| 178 // Otherwise, retrieve icons of the activities. | |
| 179 std::vector<ActivityIconLoader::ActivityName> activities; | |
| 180 for (const auto& handler : handlers) { | |
| 181 activities.emplace_back(handler->package_name, handler->activity_name); | |
| 182 } | |
| 183 icon_loader->GetActivityIcons( | |
| 184 activities, base::Bind(OnAppIconsReceived, render_process_host_id, | |
| 185 routing_id, url, base::Passed(&handlers))); | |
| 186 } | |
| 187 | |
| 188 } // namespace | |
| 189 | |
| 190 bool RunArcExternalProtocolDialog(const GURL& url, | |
| 191 int render_process_host_id, | |
| 192 int routing_id, | |
| 193 ui::PageTransition page_transition, | |
| 194 bool has_user_gesture) { | |
| 195 // Try to forward <form> submissions to ARC when possible. | |
| 196 constexpr bool kAllowFormSubmit = true; | |
| 197 if (ShouldIgnoreNavigation(page_transition, kAllowFormSubmit)) | |
| 198 return false; | |
| 199 | |
| 200 auto* instance = ArcIntentHelperBridge::GetIntentHelperInstance( | |
| 201 "RequestUrlHandlerList", kMinVersionForRequestUrlHandlerList); | |
| 202 if (!instance) | |
| 203 return false; // ARC is either not supported or not yet ready. | |
| 204 | |
| 205 WebContents* web_contents = | |
| 206 tab_util::GetWebContentsByID(render_process_host_id, routing_id); | |
| 207 if (!web_contents || !web_contents->GetBrowserContext() || | |
| 208 web_contents->GetBrowserContext()->IsOffTheRecord()) { | |
| 209 return false; | |
| 210 } | |
| 211 | |
| 212 // Show ARC version of the dialog, which is IntentPickerBubbleView. To show | |
| 213 // the bubble view, we need to ask ARC for a handler list first. | |
| 214 instance->RequestUrlHandlerList( | |
| 215 url.spec(), | |
| 216 base::Bind(OnUrlHandlerList, render_process_host_id, routing_id, url)); | |
| 217 return true; | |
| 218 } | |
| 219 | |
| 220 } // namespace arc | |
| OLD | NEW |