Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: chrome/browser/chromeos/arc/arc_navigation_throttle.cc

Issue 2443313002: Add more tests to arc_navigation_throttle.cc (Closed)
Patch Set: fix one more typo Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/arc_navigation_throttle.h" 5 #include "chrome/browser/chromeos/arc/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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 if (!current_url.is_valid() || current_url.is_empty()) { 51 if (!current_url.is_valid() || current_url.is_empty()) {
52 DVLOG(1) << "Unexpected URL: " << current_url << ", opening it in Chrome."; 52 DVLOG(1) << "Unexpected URL: " << current_url << ", opening it in Chrome.";
53 return false; 53 return false;
54 } 54 }
55 55
56 return !net::registry_controlled_domains::SameDomainOrHost( 56 return !net::registry_controlled_domains::SameDomainOrHost(
57 current_url, previous_url, 57 current_url, previous_url,
58 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); 58 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
59 } 59 }
60 60
61 // Returns true if |handlers| contain one or more apps.
Luis Héctor Chávez 2016/10/24 22:59:54 Re: the assumption about production always contain
Yusuke Sato 2016/10/24 23:25:17 Done.
62 bool IsAppAvailable(const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers) {
63 return handlers.size() > 1 || (handlers.size() == 1 &&
64 !ArcIntentHelperBridge::IsIntentHelperPackage(
65 handlers[0]->package_name));
66 }
67
68 // Searches for a preferred app in |handlers| and returns its index. If not
69 // found, returns |handlers.size()|.
70 size_t FindPreferredApp(
71 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers,
72 const GURL& url_for_logging) {
73 for (size_t i = 0; i < handlers.size(); ++i) {
74 if (!handlers[i]->is_preferred)
75 continue;
76 if (ArcIntentHelperBridge::IsIntentHelperPackage(
77 handlers[i]->package_name)) {
78 // If Chrome browser was selected as the preferred app, we shouldn't
79 // create a throttle.
80 DVLOG(1)
81 << "Chrome browser is selected as the preferred app for this URL: "
82 << url_for_logging;
83 }
84 return i;
85 }
86 return handlers.size(); // not found
87 }
88
89 // Swaps Chrome app with any app in row |kMaxAppResults-1| iff its index is
90 // bigger, thus ensuring the user can always see Chrome without scrolling.
91 // When swap is needed, fills |out_indices| and returns true. If |handlers|
92 // do not have Chrome, returns false.
93 bool IsSwapElementsNeeded(
94 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers,
95 std::pair<size_t, size_t>* out_indices) {
96 size_t chrome_app_index = 0;
97 for (size_t i = 0; i < handlers.size(); ++i) {
98 if (ArcIntentHelperBridge::IsIntentHelperPackage(
99 handlers[i]->package_name)) {
100 chrome_app_index = i;
101 break;
102 }
103 }
104 if (chrome_app_index < ArcNavigationThrottle::kMaxAppResults)
105 return false;
106
107 *out_indices = std::make_pair(ArcNavigationThrottle::kMaxAppResults - 1,
108 chrome_app_index);
109 return true;
110 }
111
61 } // namespace 112 } // namespace
62 113
63 ArcNavigationThrottle::ArcNavigationThrottle( 114 ArcNavigationThrottle::ArcNavigationThrottle(
64 content::NavigationHandle* navigation_handle, 115 content::NavigationHandle* navigation_handle,
65 const ShowIntentPickerCallback& show_intent_picker_cb) 116 const ShowIntentPickerCallback& show_intent_picker_cb)
66 : content::NavigationThrottle(navigation_handle), 117 : content::NavigationThrottle(navigation_handle),
67 show_intent_picker_callback_(show_intent_picker_cb), 118 show_intent_picker_callback_(show_intent_picker_cb),
68 previous_user_action_(CloseReason::INVALID), 119 previous_user_action_(CloseReason::INVALID),
69 weak_ptr_factory_(this) {} 120 weak_ptr_factory_(this) {}
70 121
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 url.spec(), base::Bind(&ArcNavigationThrottle::OnAppCandidatesReceived, 207 url.spec(), base::Bind(&ArcNavigationThrottle::OnAppCandidatesReceived,
157 weak_ptr_factory_.GetWeakPtr())); 208 weak_ptr_factory_.GetWeakPtr()));
158 return content::NavigationThrottle::DEFER; 209 return content::NavigationThrottle::DEFER;
159 } 210 }
160 211
161 // We received the array of app candidates to handle this URL (even the Chrome 212 // We received the array of app candidates to handle this URL (even the Chrome
162 // app is included). 213 // app is included).
163 void ArcNavigationThrottle::OnAppCandidatesReceived( 214 void ArcNavigationThrottle::OnAppCandidatesReceived(
164 mojo::Array<mojom::IntentHandlerInfoPtr> handlers) { 215 mojo::Array<mojom::IntentHandlerInfoPtr> handlers) {
165 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 216 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
166 if (handlers.empty() || 217 if (!IsAppAvailable(handlers)) {
167 (handlers.size() == 1 && ArcIntentHelperBridge::IsIntentHelperPackage(
168 handlers[0]->package_name))) {
169 // This scenario shouldn't be accesed as ArcNavigationThrottle is created 218 // This scenario shouldn't be accesed as ArcNavigationThrottle is created
170 // iff there are ARC apps which can actually handle the given URL. 219 // iff there are ARC apps which can actually handle the given URL.
171 DVLOG(1) << "There are no app candidates for this URL: " 220 DVLOG(1) << "There are no app candidates for this URL: "
172 << navigation_handle()->GetURL().spec(); 221 << navigation_handle()->GetURL().spec();
173 navigation_handle()->Resume(); 222 navigation_handle()->Resume();
174 return; 223 return;
175 } 224 }
176 225
177 // If one of the apps is marked as preferred, use it right away without 226 // If one of the apps is marked as preferred, use it right away without
178 // showing the UI. 227 // showing the UI.
179 for (size_t i = 0; i < handlers.size(); ++i) { 228 const size_t index =
180 if (!handlers[i]->is_preferred) 229 FindPreferredApp(handlers, navigation_handle()->GetURL());
181 continue; 230 if (index != handlers.size()) {
182 if (ArcIntentHelperBridge::IsIntentHelperPackage( 231 const std::string package_name = handlers[index]->package_name;
183 handlers[i]->package_name)) {
184 // If Chrome browser was selected as the preferred app, we should't
185 // create a throttle.
186 DVLOG(1)
187 << "Chrome browser is selected as the preferred app for this URL: "
188 << navigation_handle()->GetURL().spec();
189 }
190 std::string package_name = handlers[i]->package_name;
191 OnIntentPickerClosed(std::move(handlers), package_name, 232 OnIntentPickerClosed(std::move(handlers), package_name,
192 CloseReason::PREFERRED_ACTIVITY_FOUND); 233 CloseReason::PREFERRED_ACTIVITY_FOUND);
193 return; 234 return;
194 } 235 }
195 236
196 // Swap Chrome app with any app in row |kMaxAppResults-1| iff its index is 237 std::pair<size_t, size_t> indices;
197 // bigger, thus ensuring the user can always see Chrome without scrolling. 238 if (IsSwapElementsNeeded(handlers, &indices))
198 size_t chrome_app_index = 0; 239 std::swap(handlers[indices.first], handlers[indices.second]);
199 for (size_t i = 0; i < handlers.size(); ++i) {
200 if (ArcIntentHelperBridge::IsIntentHelperPackage(
201 handlers[i]->package_name)) {
202 chrome_app_index = i;
203 break;
204 }
205 }
206
207 if (chrome_app_index >= kMaxAppResults)
208 std::swap(handlers[kMaxAppResults - 1], handlers[chrome_app_index]);
209 240
210 scoped_refptr<ActivityIconLoader> icon_loader = GetIconLoader(); 241 scoped_refptr<ActivityIconLoader> icon_loader = GetIconLoader();
211 if (!icon_loader) { 242 if (!icon_loader) {
212 LOG(ERROR) << "Cannot get an instance of ActivityIconLoader"; 243 LOG(ERROR) << "Cannot get an instance of ActivityIconLoader";
213 navigation_handle()->Resume(); 244 navigation_handle()->Resume();
214 return; 245 return;
215 } 246 }
216 std::vector<ActivityIconLoader::ActivityName> activities; 247 std::vector<ActivityIconLoader::ActivityName> activities;
217 for (const auto& handler : handlers) { 248 for (const auto& handler : handlers)
218 activities.emplace_back(handler->package_name, handler->activity_name); 249 activities.emplace_back(handler->package_name, handler->activity_name);
219 }
220 icon_loader->GetActivityIcons( 250 icon_loader->GetActivityIcons(
221 activities, 251 activities,
222 base::Bind(&ArcNavigationThrottle::OnAppIconsReceived, 252 base::Bind(&ArcNavigationThrottle::OnAppIconsReceived,
223 weak_ptr_factory_.GetWeakPtr(), base::Passed(&handlers))); 253 weak_ptr_factory_.GetWeakPtr(), base::Passed(&handlers)));
224 } 254 }
225 255
226 void ArcNavigationThrottle::OnAppIconsReceived( 256 void ArcNavigationThrottle::OnAppIconsReceived(
227 mojo::Array<mojom::IntentHandlerInfoPtr> handlers, 257 mojo::Array<mojom::IntentHandlerInfoPtr> handlers,
228 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> icons) { 258 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> icons) {
229 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 259 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
(...skipping 21 matching lines...) Expand all
251 std::string selected_app_package, 281 std::string selected_app_package,
252 CloseReason close_reason) { 282 CloseReason close_reason) {
253 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 283 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
254 const GURL& url = navigation_handle()->GetURL(); 284 const GURL& url = navigation_handle()->GetURL();
255 content::NavigationHandle* handle = navigation_handle(); 285 content::NavigationHandle* handle = navigation_handle();
256 previous_user_action_ = close_reason; 286 previous_user_action_ = close_reason;
257 287
258 // Make sure that the instance at least supports HandleUrl. 288 // Make sure that the instance at least supports HandleUrl.
259 auto* instance = ArcIntentHelperBridge::GetIntentHelperInstance( 289 auto* instance = ArcIntentHelperBridge::GetIntentHelperInstance(
260 "HandleUrl", kMinVersionForHandleUrl); 290 "HandleUrl", kMinVersionForHandleUrl);
261 size_t selected_app_index = handlers.size(); 291 // Since we are selecting an app by its package name, we need to locate it
292 // on the |handlers| structure before sending the IPC to ARC.
293 const size_t selected_app_index = GetAppIndex(handlers, selected_app_package);
262 if (!instance) { 294 if (!instance) {
263 close_reason = CloseReason::ERROR; 295 close_reason = CloseReason::ERROR;
264 } else if (close_reason == CloseReason::JUST_ONCE_PRESSED || 296 } else if (close_reason == CloseReason::JUST_ONCE_PRESSED ||
265 close_reason == CloseReason::ALWAYS_PRESSED || 297 close_reason == CloseReason::ALWAYS_PRESSED ||
266 close_reason == CloseReason::PREFERRED_ACTIVITY_FOUND) { 298 close_reason == CloseReason::PREFERRED_ACTIVITY_FOUND) {
267 // Since we are selecting an app by its package name, we need to locate it
268 // on the |handlers| structure before sending the IPC to ARC.
269 for (size_t i = 0; i < handlers.size(); ++i) {
270 if (handlers[i]->package_name == selected_app_package) {
271 selected_app_index = i;
272 break;
273 }
274 }
275
276 if (selected_app_index == handlers.size()) 299 if (selected_app_index == handlers.size())
277 close_reason = CloseReason::ERROR; 300 close_reason = CloseReason::ERROR;
278 } 301 }
279 302
280 switch (close_reason) { 303 switch (close_reason) {
281 case CloseReason::ERROR: 304 case CloseReason::ERROR:
282 case CloseReason::DIALOG_DEACTIVATED: { 305 case CloseReason::DIALOG_DEACTIVATED: {
283 // If the user fails to select an option from the list, or the UI returned 306 // If the user fails to select an option from the list, or the UI returned
284 // an error or if |selected_app_index| is not a valid index, then resume 307 // an error or if |selected_app_index| is not a valid index, then resume
285 // the navigation in Chrome. 308 // the navigation in Chrome.
(...skipping 30 matching lines...) Expand all
316 return; 339 return;
317 } 340 }
318 } 341 }
319 342
320 UMA_HISTOGRAM_ENUMERATION("Arc.IntentHandlerAction", 343 UMA_HISTOGRAM_ENUMERATION("Arc.IntentHandlerAction",
321 static_cast<int>(close_reason), 344 static_cast<int>(close_reason),
322 static_cast<int>(CloseReason::SIZE)); 345 static_cast<int>(CloseReason::SIZE));
323 } 346 }
324 347
325 // static 348 // static
349 size_t ArcNavigationThrottle::GetAppIndex(
350 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers,
351 const std::string& selected_app_package) {
352 for (size_t i = 0; i < handlers.size(); ++i) {
353 if (handlers[i]->package_name == selected_app_package)
354 return i;
355 }
356 return handlers.size();
357 }
358
359 // static
326 bool ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( 360 bool ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting(
327 const GURL& previous_url, 361 const GURL& previous_url,
328 const GURL& current_url) { 362 const GURL& current_url) {
329 return ShouldOverrideUrlLoading(previous_url, current_url); 363 return ShouldOverrideUrlLoading(previous_url, current_url);
330 } 364 }
331 365
366 // static
367 bool ArcNavigationThrottle::IsAppAvailableForTesting(
368 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers) {
369 return IsAppAvailable(handlers);
370 }
371
372 // static
373 size_t ArcNavigationThrottle::FindPreferredAppForTesting(
374 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers) {
375 return FindPreferredApp(handlers, GURL());
376 }
377
378 // static
379 bool ArcNavigationThrottle::IsSwapElementsNeededForTesting(
380 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers,
381 std::pair<size_t, size_t>* out_indices) {
382 return IsSwapElementsNeeded(handlers, out_indices);
383 }
384
332 } // namespace arc 385 } // namespace arc
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/arc/arc_navigation_throttle.h ('k') | chrome/browser/chromeos/arc/arc_navigation_throttle_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698