| 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 "chrome/browser/chromeos/arc/intent_helper/arc_navigation_throttle.h" | 5 #include "chrome/browser/chromeos/arc/intent_helper/arc_navigation_throttle.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "components/arc/arc_bridge_service.h" | 13 #include "components/arc/arc_bridge_service.h" |
| 14 #include "components/arc/arc_service_manager.h" | 14 #include "components/arc/arc_service_manager.h" |
| 15 #include "components/arc/intent_helper/arc_intent_helper_bridge.h" | 15 #include "components/arc/intent_helper/arc_intent_helper_bridge.h" |
| 16 #include "components/arc/intent_helper/local_activity_resolver.h" | 16 #include "components/arc/intent_helper/local_activity_resolver.h" |
| 17 #include "components/arc/intent_helper/page_transition_util.h" | 17 #include "components/arc/intent_helper/page_transition_util.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/navigation_controller.h" | 19 #include "content/public/browser/navigation_controller.h" |
| 20 #include "content/public/browser/navigation_handle.h" | 20 #include "content/public/browser/navigation_handle.h" |
| 21 #include "content/public/browser/site_instance.h" | 21 #include "content/public/browser/site_instance.h" |
| 22 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 23 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 23 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 24 #include "ui/base/page_transition_types.h" | 24 #include "ui/base/page_transition_types.h" |
| 25 | 25 |
| 26 namespace arc { | 26 namespace arc { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 constexpr uint32_t kMinVersionForHandleUrl = 2; | |
| 31 constexpr uint32_t kMinVersionForRequestUrlHandlerList = 2; | |
| 32 constexpr uint32_t kMinVersionForAddPreferredPackage = 7; | |
| 33 | |
| 34 scoped_refptr<ActivityIconLoader> GetIconLoader() { | 30 scoped_refptr<ActivityIconLoader> GetIconLoader() { |
| 35 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 31 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 36 ArcServiceManager* arc_service_manager = ArcServiceManager::Get(); | 32 ArcServiceManager* arc_service_manager = ArcServiceManager::Get(); |
| 37 return arc_service_manager ? arc_service_manager->icon_loader() : nullptr; | 33 return arc_service_manager ? arc_service_manager->icon_loader() : nullptr; |
| 38 } | 34 } |
| 39 | 35 |
| 40 // Compares the host name of the referrer and target URL to decide whether | 36 // Compares the host name of the referrer and target URL to decide whether |
| 41 // the navigation needs to be overriden. | 37 // the navigation needs to be overriden. |
| 42 bool ShouldOverrideUrlLoading(const GURL& previous_url, | 38 bool ShouldOverrideUrlLoading(const GURL& previous_url, |
| 43 const GURL& current_url) { | 39 const GURL& current_url) { |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 return content::NavigationThrottle::PROCEED; | 171 return content::NavigationThrottle::PROCEED; |
| 176 | 172 |
| 177 if (ShouldIgnoreNavigation(navigation_handle()->GetPageTransition(), | 173 if (ShouldIgnoreNavigation(navigation_handle()->GetPageTransition(), |
| 178 kAllowFormSubmit, kAllowClientRedirect)) | 174 kAllowFormSubmit, kAllowClientRedirect)) |
| 179 return content::NavigationThrottle::PROCEED; | 175 return content::NavigationThrottle::PROCEED; |
| 180 | 176 |
| 181 if (!ShouldOverrideUrlLoading(starting_gurl_, url)) | 177 if (!ShouldOverrideUrlLoading(starting_gurl_, url)) |
| 182 return content::NavigationThrottle::PROCEED; | 178 return content::NavigationThrottle::PROCEED; |
| 183 | 179 |
| 184 ArcServiceManager* arc_service_manager = ArcServiceManager::Get(); | 180 ArcServiceManager* arc_service_manager = ArcServiceManager::Get(); |
| 185 DCHECK(arc_service_manager); | 181 if (!arc_service_manager) |
| 182 return content::NavigationThrottle::PROCEED; |
| 183 |
| 186 scoped_refptr<LocalActivityResolver> local_resolver = | 184 scoped_refptr<LocalActivityResolver> local_resolver = |
| 187 arc_service_manager->activity_resolver(); | 185 arc_service_manager->activity_resolver(); |
| 188 if (local_resolver->ShouldChromeHandleUrl(url)) { | 186 if (local_resolver->ShouldChromeHandleUrl(url)) { |
| 189 // Allow navigation to proceed if there isn't an android app that handles | 187 // Allow navigation to proceed if there isn't an android app that handles |
| 190 // the given URL. | 188 // the given URL. |
| 191 return content::NavigationThrottle::PROCEED; | 189 return content::NavigationThrottle::PROCEED; |
| 192 } | 190 } |
| 193 | 191 |
| 194 auto* instance = ArcIntentHelperBridge::GetIntentHelperInstance( | 192 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( |
| 195 "RequestUrlHandlerList", kMinVersionForRequestUrlHandlerList); | 193 arc_service_manager->arc_bridge_service()->intent_helper(), |
| 194 RequestUrlHandlerList); |
| 196 if (!instance) | 195 if (!instance) |
| 197 return content::NavigationThrottle::PROCEED; | 196 return content::NavigationThrottle::PROCEED; |
| 198 instance->RequestUrlHandlerList( | 197 instance->RequestUrlHandlerList( |
| 199 url.spec(), base::Bind(&ArcNavigationThrottle::OnAppCandidatesReceived, | 198 url.spec(), base::Bind(&ArcNavigationThrottle::OnAppCandidatesReceived, |
| 200 weak_ptr_factory_.GetWeakPtr())); | 199 weak_ptr_factory_.GetWeakPtr())); |
| 201 return content::NavigationThrottle::DEFER; | 200 return content::NavigationThrottle::DEFER; |
| 202 } | 201 } |
| 203 | 202 |
| 204 GURL ArcNavigationThrottle::GetStartingGURL() const { | 203 GURL ArcNavigationThrottle::GetStartingGURL() const { |
| 205 // This helps us determine a reference GURL for the current NavigationHandle. | 204 // This helps us determine a reference GURL for the current NavigationHandle. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 void ArcNavigationThrottle::OnIntentPickerClosed( | 288 void ArcNavigationThrottle::OnIntentPickerClosed( |
| 290 std::vector<mojom::IntentHandlerInfoPtr> handlers, | 289 std::vector<mojom::IntentHandlerInfoPtr> handlers, |
| 291 const std::string& selected_app_package, | 290 const std::string& selected_app_package, |
| 292 CloseReason close_reason) { | 291 CloseReason close_reason) { |
| 293 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 292 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 294 const GURL& url = navigation_handle()->GetURL(); | 293 const GURL& url = navigation_handle()->GetURL(); |
| 295 content::NavigationHandle* handle = navigation_handle(); | 294 content::NavigationHandle* handle = navigation_handle(); |
| 296 previous_user_action_ = close_reason; | 295 previous_user_action_ = close_reason; |
| 297 | 296 |
| 298 // Make sure that the instance at least supports HandleUrl. | 297 // Make sure that the instance at least supports HandleUrl. |
| 299 auto* instance = ArcIntentHelperBridge::GetIntentHelperInstance( | 298 auto* arc_service_manager = ArcServiceManager::Get(); |
| 300 "HandleUrl", kMinVersionForHandleUrl); | 299 mojom::IntentHelperInstance* instance = nullptr; |
| 300 if (arc_service_manager) { |
| 301 instance = ARC_GET_INSTANCE_FOR_METHOD( |
| 302 arc_service_manager->arc_bridge_service()->intent_helper(), HandleUrl); |
| 303 } |
| 304 |
| 301 // Since we are selecting an app by its package name, we need to locate it | 305 // Since we are selecting an app by its package name, we need to locate it |
| 302 // on the |handlers| structure before sending the IPC to ARC. | 306 // on the |handlers| structure before sending the IPC to ARC. |
| 303 const size_t selected_app_index = GetAppIndex(handlers, selected_app_package); | 307 const size_t selected_app_index = GetAppIndex(handlers, selected_app_package); |
| 304 if (!instance) { | 308 if (!instance) { |
| 305 close_reason = CloseReason::ERROR; | 309 close_reason = CloseReason::ERROR; |
| 306 } else if (close_reason == CloseReason::JUST_ONCE_PRESSED || | 310 } else if (close_reason == CloseReason::JUST_ONCE_PRESSED || |
| 307 close_reason == CloseReason::ALWAYS_PRESSED || | 311 close_reason == CloseReason::ALWAYS_PRESSED || |
| 308 close_reason == CloseReason::PREFERRED_ACTIVITY_FOUND) { | 312 close_reason == CloseReason::PREFERRED_ACTIVITY_FOUND) { |
| 309 if (selected_app_index == handlers.size()) | 313 if (selected_app_index == handlers.size()) |
| 310 close_reason = CloseReason::ERROR; | 314 close_reason = CloseReason::ERROR; |
| 311 } | 315 } |
| 312 | 316 |
| 313 switch (close_reason) { | 317 switch (close_reason) { |
| 314 case CloseReason::ERROR: | 318 case CloseReason::ERROR: |
| 315 case CloseReason::DIALOG_DEACTIVATED: { | 319 case CloseReason::DIALOG_DEACTIVATED: { |
| 316 // If the user fails to select an option from the list, or the UI returned | 320 // If the user fails to select an option from the list, or the UI returned |
| 317 // an error or if |selected_app_index| is not a valid index, then resume | 321 // an error or if |selected_app_index| is not a valid index, then resume |
| 318 // the navigation in Chrome. | 322 // the navigation in Chrome. |
| 319 DVLOG(1) << "User didn't select a valid option, resuming navigation."; | 323 DVLOG(1) << "User didn't select a valid option, resuming navigation."; |
| 320 handle->Resume(); | 324 handle->Resume(); |
| 321 break; | 325 break; |
| 322 } | 326 } |
| 323 case CloseReason::ALWAYS_PRESSED: { | 327 case CloseReason::ALWAYS_PRESSED: { |
| 324 // Call AddPreferredPackage if it is supported. Reusing the same | 328 // Call AddPreferredPackage if it is supported. Reusing the same |
| 325 // |instance| is okay. | 329 // |instance| is okay. |
| 326 if (ArcIntentHelperBridge::GetIntentHelperInstance( | 330 DCHECK(arc_service_manager); |
| 327 "AddPreferredPackage", kMinVersionForAddPreferredPackage)) { | 331 if (ARC_GET_INSTANCE_FOR_METHOD( |
| 332 arc_service_manager->arc_bridge_service()->intent_helper(), |
| 333 AddPreferredPackage)) { |
| 328 instance->AddPreferredPackage( | 334 instance->AddPreferredPackage( |
| 329 handlers[selected_app_index]->package_name); | 335 handlers[selected_app_index]->package_name); |
| 330 } | 336 } |
| 331 // fall through. | 337 // fall through. |
| 332 } | 338 } |
| 333 case CloseReason::JUST_ONCE_PRESSED: | 339 case CloseReason::JUST_ONCE_PRESSED: |
| 334 case CloseReason::PREFERRED_ACTIVITY_FOUND: { | 340 case CloseReason::PREFERRED_ACTIVITY_FOUND: { |
| 335 if (ArcIntentHelperBridge::IsIntentHelperPackage( | 341 if (ArcIntentHelperBridge::IsIntentHelperPackage( |
| 336 handlers[selected_app_index]->package_name)) { | 342 handlers[selected_app_index]->package_name)) { |
| 337 handle->Resume(); | 343 handle->Resume(); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 } | 428 } |
| 423 if (chrome_app_index < ArcNavigationThrottle::kMaxAppResults) | 429 if (chrome_app_index < ArcNavigationThrottle::kMaxAppResults) |
| 424 return false; | 430 return false; |
| 425 | 431 |
| 426 *out_indices = std::make_pair(ArcNavigationThrottle::kMaxAppResults - 1, | 432 *out_indices = std::make_pair(ArcNavigationThrottle::kMaxAppResults - 1, |
| 427 chrome_app_index); | 433 chrome_app_index); |
| 428 return true; | 434 return true; |
| 429 } | 435 } |
| 430 | 436 |
| 431 } // namespace arc | 437 } // namespace arc |
| OLD | NEW |