| 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" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 } | 61 } |
| 62 | 62 |
| 63 return !net::registry_controlled_domains::SameDomainOrHost( | 63 return !net::registry_controlled_domains::SameDomainOrHost( |
| 64 current_url, previous_url, | 64 current_url, previous_url, |
| 65 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 65 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Returns true if |handlers| contain one or more apps. When this function is | 68 // Returns true if |handlers| contain one or more apps. When this function is |
| 69 // called from OnAppCandidatesReceived, |handlers| always contain Chrome (aka | 69 // called from OnAppCandidatesReceived, |handlers| always contain Chrome (aka |
| 70 // intent_helper), but the function doesn't treat it as an app. | 70 // intent_helper), but the function doesn't treat it as an app. |
| 71 bool IsAppAvailable(const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers) { | 71 bool IsAppAvailable(const std::vector<mojom::IntentHandlerInfoPtr>& handlers) { |
| 72 return handlers.size() > 1 || (handlers.size() == 1 && | 72 return handlers.size() > 1 || (handlers.size() == 1 && |
| 73 !ArcIntentHelperBridge::IsIntentHelperPackage( | 73 !ArcIntentHelperBridge::IsIntentHelperPackage( |
| 74 handlers[0]->package_name)); | 74 handlers[0]->package_name)); |
| 75 } | 75 } |
| 76 | 76 |
| 77 // Searches for a preferred app in |handlers| and returns its index. If not | 77 // Searches for a preferred app in |handlers| and returns its index. If not |
| 78 // found, returns |handlers.size()|. | 78 // found, returns |handlers.size()|. |
| 79 size_t FindPreferredApp( | 79 size_t FindPreferredApp( |
| 80 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers, | 80 const std::vector<mojom::IntentHandlerInfoPtr>& handlers, |
| 81 const GURL& url_for_logging) { | 81 const GURL& url_for_logging) { |
| 82 for (size_t i = 0; i < handlers.size(); ++i) { | 82 for (size_t i = 0; i < handlers.size(); ++i) { |
| 83 if (!handlers[i]->is_preferred) | 83 if (!handlers[i]->is_preferred) |
| 84 continue; | 84 continue; |
| 85 if (ArcIntentHelperBridge::IsIntentHelperPackage( | 85 if (ArcIntentHelperBridge::IsIntentHelperPackage( |
| 86 handlers[i]->package_name)) { | 86 handlers[i]->package_name)) { |
| 87 // If Chrome browser was selected as the preferred app, we shouldn't | 87 // If Chrome browser was selected as the preferred app, we shouldn't |
| 88 // create a throttle. | 88 // create a throttle. |
| 89 DVLOG(1) | 89 DVLOG(1) |
| 90 << "Chrome browser is selected as the preferred app for this URL: " | 90 << "Chrome browser is selected as the preferred app for this URL: " |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 navigation_handle()->GetWebContents()->GetLastCommittedURL(); | 212 navigation_handle()->GetWebContents()->GetLastCommittedURL(); |
| 213 if (last_committed_url.is_valid() && !last_committed_url.is_empty()) | 213 if (last_committed_url.is_valid() && !last_committed_url.is_empty()) |
| 214 return last_committed_url; | 214 return last_committed_url; |
| 215 | 215 |
| 216 return navigation_handle()->GetStartingSiteInstance()->GetSiteURL(); | 216 return navigation_handle()->GetStartingSiteInstance()->GetSiteURL(); |
| 217 } | 217 } |
| 218 | 218 |
| 219 // We received the array of app candidates to handle this URL (even the Chrome | 219 // We received the array of app candidates to handle this URL (even the Chrome |
| 220 // app is included). | 220 // app is included). |
| 221 void ArcNavigationThrottle::OnAppCandidatesReceived( | 221 void ArcNavigationThrottle::OnAppCandidatesReceived( |
| 222 mojo::Array<mojom::IntentHandlerInfoPtr> handlers) { | 222 std::vector<mojom::IntentHandlerInfoPtr> handlers) { |
| 223 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 223 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 224 if (!IsAppAvailable(handlers)) { | 224 if (!IsAppAvailable(handlers)) { |
| 225 // This scenario shouldn't be accesed as ArcNavigationThrottle is created | 225 // This scenario shouldn't be accesed as ArcNavigationThrottle is created |
| 226 // iff there are ARC apps which can actually handle the given URL. | 226 // iff there are ARC apps which can actually handle the given URL. |
| 227 DVLOG(1) << "There are no app candidates for this URL: " | 227 DVLOG(1) << "There are no app candidates for this URL: " |
| 228 << navigation_handle()->GetURL(); | 228 << navigation_handle()->GetURL(); |
| 229 navigation_handle()->Resume(); | 229 navigation_handle()->Resume(); |
| 230 return; | 230 return; |
| 231 } | 231 } |
| 232 | 232 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 254 std::vector<ActivityIconLoader::ActivityName> activities; | 254 std::vector<ActivityIconLoader::ActivityName> activities; |
| 255 for (const auto& handler : handlers) | 255 for (const auto& handler : handlers) |
| 256 activities.emplace_back(handler->package_name, handler->activity_name); | 256 activities.emplace_back(handler->package_name, handler->activity_name); |
| 257 icon_loader->GetActivityIcons( | 257 icon_loader->GetActivityIcons( |
| 258 activities, | 258 activities, |
| 259 base::Bind(&ArcNavigationThrottle::OnAppIconsReceived, | 259 base::Bind(&ArcNavigationThrottle::OnAppIconsReceived, |
| 260 weak_ptr_factory_.GetWeakPtr(), base::Passed(&handlers))); | 260 weak_ptr_factory_.GetWeakPtr(), base::Passed(&handlers))); |
| 261 } | 261 } |
| 262 | 262 |
| 263 void ArcNavigationThrottle::OnAppIconsReceived( | 263 void ArcNavigationThrottle::OnAppIconsReceived( |
| 264 mojo::Array<mojom::IntentHandlerInfoPtr> handlers, | 264 std::vector<mojom::IntentHandlerInfoPtr> handlers, |
| 265 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> icons) { | 265 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> icons) { |
| 266 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 266 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 267 std::vector<AppInfo> app_info; | 267 std::vector<AppInfo> app_info; |
| 268 | 268 |
| 269 for (const auto& handler : handlers) { | 269 for (const auto& handler : handlers) { |
| 270 gfx::Image icon; | 270 gfx::Image icon; |
| 271 const ActivityIconLoader::ActivityName activity(handler->package_name, | 271 const ActivityIconLoader::ActivityName activity(handler->package_name, |
| 272 handler->activity_name); | 272 handler->activity_name); |
| 273 const auto it = icons->find(activity); | 273 const auto it = icons->find(activity); |
| 274 | 274 |
| 275 app_info.emplace_back( | 275 app_info.emplace_back( |
| 276 AppInfo(it != icons->end() ? it->second.icon20 : gfx::Image(), | 276 AppInfo(it != icons->end() ? it->second.icon20 : gfx::Image(), |
| 277 handler->package_name, handler->name)); | 277 handler->package_name, handler->name)); |
| 278 } | 278 } |
| 279 | 279 |
| 280 show_intent_picker_callback_.Run( | 280 show_intent_picker_callback_.Run( |
| 281 navigation_handle()->GetWebContents(), app_info, | 281 navigation_handle()->GetWebContents(), app_info, |
| 282 base::Bind(&ArcNavigationThrottle::OnIntentPickerClosed, | 282 base::Bind(&ArcNavigationThrottle::OnIntentPickerClosed, |
| 283 weak_ptr_factory_.GetWeakPtr(), base::Passed(&handlers))); | 283 weak_ptr_factory_.GetWeakPtr(), base::Passed(&handlers))); |
| 284 } | 284 } |
| 285 | 285 |
| 286 void ArcNavigationThrottle::OnIntentPickerClosed( | 286 void ArcNavigationThrottle::OnIntentPickerClosed( |
| 287 mojo::Array<mojom::IntentHandlerInfoPtr> handlers, | 287 std::vector<mojom::IntentHandlerInfoPtr> handlers, |
| 288 const std::string& selected_app_package, | 288 const std::string& selected_app_package, |
| 289 CloseReason close_reason) { | 289 CloseReason close_reason) { |
| 290 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 290 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 291 const GURL& url = navigation_handle()->GetURL(); | 291 const GURL& url = navigation_handle()->GetURL(); |
| 292 content::NavigationHandle* handle = navigation_handle(); | 292 content::NavigationHandle* handle = navigation_handle(); |
| 293 previous_user_action_ = close_reason; | 293 previous_user_action_ = close_reason; |
| 294 | 294 |
| 295 // Make sure that the instance at least supports HandleUrl. | 295 // Make sure that the instance at least supports HandleUrl. |
| 296 auto* instance = ArcIntentHelperBridge::GetIntentHelperInstance( | 296 auto* instance = ArcIntentHelperBridge::GetIntentHelperInstance( |
| 297 "HandleUrl", kMinVersionForHandleUrl); | 297 "HandleUrl", kMinVersionForHandleUrl); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 } | 347 } |
| 348 } | 348 } |
| 349 | 349 |
| 350 Platform platform = | 350 Platform platform = |
| 351 GetDestinationPlatform(selected_app_package, close_reason); | 351 GetDestinationPlatform(selected_app_package, close_reason); |
| 352 RecordUma(close_reason, platform); | 352 RecordUma(close_reason, platform); |
| 353 } | 353 } |
| 354 | 354 |
| 355 // static | 355 // static |
| 356 size_t ArcNavigationThrottle::GetAppIndex( | 356 size_t ArcNavigationThrottle::GetAppIndex( |
| 357 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers, | 357 const std::vector<mojom::IntentHandlerInfoPtr>& handlers, |
| 358 const std::string& selected_app_package) { | 358 const std::string& selected_app_package) { |
| 359 for (size_t i = 0; i < handlers.size(); ++i) { | 359 for (size_t i = 0; i < handlers.size(); ++i) { |
| 360 if (handlers[i]->package_name == selected_app_package) | 360 if (handlers[i]->package_name == selected_app_package) |
| 361 return i; | 361 return i; |
| 362 } | 362 } |
| 363 return handlers.size(); | 363 return handlers.size(); |
| 364 } | 364 } |
| 365 | 365 |
| 366 // static | 366 // static |
| 367 ArcNavigationThrottle::Platform ArcNavigationThrottle::GetDestinationPlatform( | 367 ArcNavigationThrottle::Platform ArcNavigationThrottle::GetDestinationPlatform( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 388 | 388 |
| 389 // static | 389 // static |
| 390 bool ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( | 390 bool ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( |
| 391 const GURL& previous_url, | 391 const GURL& previous_url, |
| 392 const GURL& current_url) { | 392 const GURL& current_url) { |
| 393 return ShouldOverrideUrlLoading(previous_url, current_url); | 393 return ShouldOverrideUrlLoading(previous_url, current_url); |
| 394 } | 394 } |
| 395 | 395 |
| 396 // static | 396 // static |
| 397 bool ArcNavigationThrottle::IsAppAvailableForTesting( | 397 bool ArcNavigationThrottle::IsAppAvailableForTesting( |
| 398 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers) { | 398 const std::vector<mojom::IntentHandlerInfoPtr>& handlers) { |
| 399 return IsAppAvailable(handlers); | 399 return IsAppAvailable(handlers); |
| 400 } | 400 } |
| 401 | 401 |
| 402 // static | 402 // static |
| 403 size_t ArcNavigationThrottle::FindPreferredAppForTesting( | 403 size_t ArcNavigationThrottle::FindPreferredAppForTesting( |
| 404 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers) { | 404 const std::vector<mojom::IntentHandlerInfoPtr>& handlers) { |
| 405 return FindPreferredApp(handlers, GURL()); | 405 return FindPreferredApp(handlers, GURL()); |
| 406 } | 406 } |
| 407 | 407 |
| 408 // static | 408 // static |
| 409 bool ArcNavigationThrottle::IsSwapElementsNeeded( | 409 bool ArcNavigationThrottle::IsSwapElementsNeeded( |
| 410 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers, | 410 const std::vector<mojom::IntentHandlerInfoPtr>& handlers, |
| 411 std::pair<size_t, size_t>* out_indices) { | 411 std::pair<size_t, size_t>* out_indices) { |
| 412 size_t chrome_app_index = 0; | 412 size_t chrome_app_index = 0; |
| 413 for (size_t i = 0; i < handlers.size(); ++i) { | 413 for (size_t i = 0; i < handlers.size(); ++i) { |
| 414 if (ArcIntentHelperBridge::IsIntentHelperPackage( | 414 if (ArcIntentHelperBridge::IsIntentHelperPackage( |
| 415 handlers[i]->package_name)) { | 415 handlers[i]->package_name)) { |
| 416 chrome_app_index = i; | 416 chrome_app_index = i; |
| 417 break; | 417 break; |
| 418 } | 418 } |
| 419 } | 419 } |
| 420 if (chrome_app_index < ArcNavigationThrottle::kMaxAppResults) | 420 if (chrome_app_index < ArcNavigationThrottle::kMaxAppResults) |
| 421 return false; | 421 return false; |
| 422 | 422 |
| 423 *out_indices = std::make_pair(ArcNavigationThrottle::kMaxAppResults - 1, | 423 *out_indices = std::make_pair(ArcNavigationThrottle::kMaxAppResults - 1, |
| 424 chrome_app_index); | 424 chrome_app_index); |
| 425 return true; | 425 return true; |
| 426 } | 426 } |
| 427 | 427 |
| 428 } // namespace arc | 428 } // namespace arc |
| OLD | NEW |