| 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 const std::string& selected_app_package, | |
| 74 ArcNavigationThrottle::CloseReason close_reason) { | |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 76 | |
| 77 // If the user selected an app to continue the navigation, confirm that the | |
| 78 // |package_name| matches a valid option and return the index. | |
| 79 const size_t selected_app_index = | |
| 80 ArcNavigationThrottle::GetAppIndex(handlers, selected_app_package); | |
| 81 // Make sure that the instance at least supports HandleUrl. | |
| 82 auto* instance = ArcIntentHelperBridge::GetIntentHelperInstance( | |
| 83 "HandleUrl", kMinVersionForHandleUrl); | |
| 84 if (!instance) { | |
| 85 close_reason = ArcNavigationThrottle::CloseReason::ERROR; | |
| 86 } else if (close_reason == | |
| 87 ArcNavigationThrottle::CloseReason::JUST_ONCE_PRESSED || | |
| 88 close_reason == | |
| 89 ArcNavigationThrottle::CloseReason::ALWAYS_PRESSED) { | |
| 90 if (selected_app_index == handlers.size()) | |
| 91 close_reason = ArcNavigationThrottle::CloseReason::ERROR; | |
| 92 } | |
| 93 | |
| 94 switch (close_reason) { | |
| 95 case ArcNavigationThrottle::CloseReason::ALWAYS_PRESSED: { | |
| 96 if (ArcIntentHelperBridge::GetIntentHelperInstance( | |
| 97 "AddPreferredPackage", kMinVersionForAddPreferredPackage)) { | |
| 98 instance->AddPreferredPackage( | |
| 99 handlers[selected_app_index]->package_name); | |
| 100 } | |
| 101 // fall through. | |
| 102 } | |
| 103 case ArcNavigationThrottle::CloseReason::JUST_ONCE_PRESSED: { | |
| 104 // Launch the selected app. | |
| 105 instance->HandleUrl(url.spec(), | |
| 106 handlers[selected_app_index]->package_name); | |
| 107 CloseTabIfNeeded(render_process_host_id, routing_id); | |
| 108 break; | |
| 109 } | |
| 110 case ArcNavigationThrottle::CloseReason::PREFERRED_ACTIVITY_FOUND: | |
| 111 case ArcNavigationThrottle::CloseReason::INVALID: { | |
| 112 NOTREACHED(); | |
| 113 return; // no UMA recording. | |
| 114 } | |
| 115 case ArcNavigationThrottle::CloseReason::ERROR: { | |
| 116 LOG(ERROR) << "IntentPickerBubbleView returned CloseReason::ERROR: " | |
| 117 << "instance=" << instance | |
| 118 << ", selected_app_index=" << selected_app_index | |
| 119 << ", handlers.size=" << handlers.size(); | |
| 120 // fall through. | |
| 121 } | |
| 122 case ArcNavigationThrottle::CloseReason::DIALOG_DEACTIVATED: { | |
| 123 // The user didn't select any ARC activity. Show the Chrome OS dialog. | |
| 124 ShowFallbackExternalProtocolDialog(render_process_host_id, routing_id, | |
| 125 url); | |
| 126 break; | |
| 127 } | |
| 128 } | |
| 129 RecordUma(close_reason); | |
| 130 } | |
| 131 | |
| 132 // Called when ARC returned activity icons for the |handlers|. | |
| 133 void OnAppIconsReceived( | |
| 134 int render_process_host_id, | |
| 135 int routing_id, | |
| 136 const GURL& url, | |
| 137 mojo::Array<mojom::IntentHandlerInfoPtr> handlers, | |
| 138 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> icons) { | |
| 139 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 140 | |
| 141 using AppInfo = arc::ArcNavigationThrottle::AppInfo; | |
| 142 std::vector<AppInfo> app_info; | |
| 143 | |
| 144 for (const auto& handler : handlers) { | |
| 145 const ActivityIconLoader::ActivityName activity(handler->package_name, | |
| 146 handler->activity_name); | |
| 147 const auto it = icons->find(activity); | |
| 148 app_info.emplace_back( | |
| 149 AppInfo(it != icons->end() ? it->second.icon20 : gfx::Image(), | |
| 150 handler->package_name, handler->name)); | |
| 151 } | |
| 152 | |
| 153 auto show_bubble_cb = base::Bind(ShowIntentPickerBubble()); | |
| 154 WebContents* web_contents = | |
| 155 tab_util::GetWebContentsByID(render_process_host_id, routing_id); | |
| 156 show_bubble_cb.Run(web_contents, app_info, | |
| 157 base::Bind(OnIntentPickerClosed, render_process_host_id, | |
| 158 routing_id, url, base::Passed(&handlers))); | |
| 159 } | |
| 160 | |
| 161 // Called when ARC returned a handler list for the |url|. | |
| 162 void OnUrlHandlerList(int render_process_host_id, | |
| 163 int routing_id, | |
| 164 const GURL& url, | |
| 165 mojo::Array<mojom::IntentHandlerInfoPtr> handlers) { | |
| 166 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 167 | |
| 168 auto* instance = ArcIntentHelperBridge::GetIntentHelperInstance( | |
| 169 "HandleUrl", kMinVersionForHandleUrl); | |
| 170 scoped_refptr<ActivityIconLoader> icon_loader = GetIconLoader(); | |
| 171 | |
| 172 if (!instance || !icon_loader || !handlers.size()) { | |
| 173 // No handler is available on ARC side. Show the Chrome OS dialog. | |
| 174 ShowFallbackExternalProtocolDialog(render_process_host_id, routing_id, url); | |
| 175 return; | |
| 176 } | |
| 177 | |
| 178 // If one of the apps is marked as preferred, use it right away without | |
| 179 // showing the UI. |is_preferred| will never be true unless the user | |
| 180 // explicitly makes it the default with the "always" button. | |
| 181 for (size_t i = 0; i < handlers.size(); ++i) { | |
| 182 if (!handlers[i]->is_preferred) | |
| 183 continue; | |
| 184 instance->HandleUrl(url.spec(), handlers[i]->package_name); | |
| 185 CloseTabIfNeeded(render_process_host_id, routing_id); | |
| 186 RecordUma(ArcNavigationThrottle::CloseReason::PREFERRED_ACTIVITY_FOUND); | |
| 187 return; | |
| 188 } | |
| 189 | |
| 190 // Otherwise, retrieve icons of the activities. | |
| 191 std::vector<ActivityIconLoader::ActivityName> activities; | |
| 192 for (const auto& handler : handlers) { | |
| 193 activities.emplace_back(handler->package_name, handler->activity_name); | |
| 194 } | |
| 195 icon_loader->GetActivityIcons( | |
| 196 activities, base::Bind(OnAppIconsReceived, render_process_host_id, | |
| 197 routing_id, url, base::Passed(&handlers))); | |
| 198 } | |
| 199 | |
| 200 } // namespace | |
| 201 | |
| 202 bool RunArcExternalProtocolDialog(const GURL& url, | |
| 203 int render_process_host_id, | |
| 204 int routing_id, | |
| 205 ui::PageTransition page_transition, | |
| 206 bool has_user_gesture) { | |
| 207 // Try to forward <form> submissions to ARC when possible. | |
| 208 constexpr bool kAllowFormSubmit = true; | |
| 209 if (ShouldIgnoreNavigation(page_transition, kAllowFormSubmit)) | |
| 210 return false; | |
| 211 | |
| 212 auto* instance = ArcIntentHelperBridge::GetIntentHelperInstance( | |
| 213 "RequestUrlHandlerList", kMinVersionForRequestUrlHandlerList); | |
| 214 if (!instance) | |
| 215 return false; // ARC is either not supported or not yet ready. | |
| 216 | |
| 217 WebContents* web_contents = | |
| 218 tab_util::GetWebContentsByID(render_process_host_id, routing_id); | |
| 219 if (!web_contents || !web_contents->GetBrowserContext() || | |
| 220 web_contents->GetBrowserContext()->IsOffTheRecord()) { | |
| 221 return false; | |
| 222 } | |
| 223 | |
| 224 // Show ARC version of the dialog, which is IntentPickerBubbleView. To show | |
| 225 // the bubble view, we need to ask ARC for a handler list first. | |
| 226 instance->RequestUrlHandlerList( | |
| 227 url.spec(), | |
| 228 base::Bind(OnUrlHandlerList, render_process_host_id, routing_id, url)); | |
| 229 return true; | |
| 230 } | |
| 231 | |
| 232 } // namespace arc | |
| OLD | NEW |