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

Side by Side Diff: ios/web/navigation/crw_session_controller.mm

Issue 2698773002: [iOS] Refactoring web CRWSessionController user agent code. (Closed)
Patch Set: Update NavigationItemImpl to use NavigationInitiationType Created 3 years, 10 months 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 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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 #import "ios/web/navigation/crw_session_controller.h" 5 #import "ios/web/navigation/crw_session_controller.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <utility> 10 #include <utility>
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 107
108 - (NSString*)uniqueID; 108 - (NSString*)uniqueID;
109 // Removes all entries after currentNavigationIndex_. 109 // Removes all entries after currentNavigationIndex_.
110 - (void)clearForwardItems; 110 - (void)clearForwardItems;
111 // Discards the transient entry, if any. 111 // Discards the transient entry, if any.
112 - (void)discardTransientItem; 112 - (void)discardTransientItem;
113 // Create a new autoreleased session entry. 113 // Create a new autoreleased session entry.
114 - (CRWSessionEntry*)sessionEntryWithURL:(const GURL&)url 114 - (CRWSessionEntry*)sessionEntryWithURL:(const GURL&)url
115 referrer:(const web::Referrer&)referrer 115 referrer:(const web::Referrer&)referrer
116 transition:(ui::PageTransition)transition 116 transition:(ui::PageTransition)transition
117 useDesktopUserAgent:(BOOL)useDesktopUserAgent
118 rendererInitiated:(BOOL)rendererInitiated; 117 rendererInitiated:(BOOL)rendererInitiated;
kkhorimoto 2017/02/17 20:51:35 Let's update this API to use web::NavigationInitia
liaoyuke 2017/02/17 22:15:40 Done.
119 // Returns YES if the PageTransition for the underlying navigationItem at 118 // Returns YES if the PageTransition for the underlying navigationItem at
120 // |index| in |entries_| has ui::PAGE_TRANSITION_IS_REDIRECT_MASK. 119 // |index| in |entries_| has ui::PAGE_TRANSITION_IS_REDIRECT_MASK.
121 - (BOOL)isRedirectTransitionForItemAtIndex:(NSInteger)index; 120 - (BOOL)isRedirectTransitionForItemAtIndex:(NSInteger)index;
122 // Returns a NavigationItemList containing the NavigationItems from |entries|. 121 // Returns a NavigationItemList containing the NavigationItems from |entries|.
123 - (web::NavigationItemList)itemListForEntryList:(NSArray*)entries; 122 - (web::NavigationItemList)itemListForEntryList:(NSArray*)entries;
124 @end 123 @end
125 124
126 @implementation CRWSessionController 125 @implementation CRWSessionController
127 126
128 @synthesize tabId = _tabId; 127 @synthesize tabId = _tabId;
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 } 310 }
312 311
313 // See NavigationController::GetVisibleEntry for the motivation for this 312 // See NavigationController::GetVisibleEntry for the motivation for this
314 // distinction. 313 // distinction.
315 - (CRWSessionEntry*)visibleEntry { 314 - (CRWSessionEntry*)visibleEntry {
316 if (_transientEntry) 315 if (_transientEntry)
317 return _transientEntry.get(); 316 return _transientEntry.get();
318 // Only return the pending_entry for new (non-history), browser-initiated 317 // Only return the pending_entry for new (non-history), browser-initiated
319 // navigations in order to prevent URL spoof attacks. 318 // navigations in order to prevent URL spoof attacks.
320 web::NavigationItemImpl* pendingItem = [_pendingEntry navigationItemImpl]; 319 web::NavigationItemImpl* pendingItem = [_pendingEntry navigationItemImpl];
321 bool safeToShowPending = pendingItem && 320 bool safeToShowPending = pendingItem && !pendingItem->IsRendererInitiated() &&
322 !pendingItem->is_renderer_initiated() &&
323 _pendingItemIndex == -1; 321 _pendingItemIndex == -1;
324 if (safeToShowPending) { 322 if (safeToShowPending) {
325 return _pendingEntry.get(); 323 return _pendingEntry.get();
326 } 324 }
327 return [self lastCommittedEntry]; 325 return [self lastCommittedEntry];
328 } 326 }
329 327
330 - (CRWSessionEntry*)pendingEntry { 328 - (CRWSessionEntry*)pendingEntry {
331 return _pendingEntry.get(); 329 return _pendingEntry.get();
332 } 330 }
(...skipping 27 matching lines...) Expand all
360 // Note: CRWSessionController currently has the responsibility to distinguish 358 // Note: CRWSessionController currently has the responsibility to distinguish
361 // between new navigations and history stack navigation, hence the inclusion 359 // between new navigations and history stack navigation, hence the inclusion
362 // of specific transiton type logic here, in order to make it reliable with 360 // of specific transiton type logic here, in order to make it reliable with
363 // real-world observed behavior. 361 // real-world observed behavior.
364 // TODO(crbug.com/676129): Fix the way changes are detected/reported elsewhere 362 // TODO(crbug.com/676129): Fix the way changes are detected/reported elsewhere
365 // in the web layer so that this hack can be removed. 363 // in the web layer so that this hack can be removed.
366 // Remove the workaround code from -presentSafeBrowsingWarningForResource:. 364 // Remove the workaround code from -presentSafeBrowsingWarningForResource:.
367 CRWSessionEntry* currentEntry = self.currentEntry; 365 CRWSessionEntry* currentEntry = self.currentEntry;
368 if (currentEntry) { 366 if (currentEntry) {
369 web::NavigationItem* item = [currentEntry navigationItem]; 367 web::NavigationItem* item = [currentEntry navigationItem];
370 if (item->GetURL() == url && 368
371 (!PageTransitionCoreTypeIs(trans, ui::PAGE_TRANSITION_FORM_SUBMIT) || 369 BOOL hasSameURL = item->GetURL() == url;
372 PageTransitionCoreTypeIs(item->GetTransitionType(), 370 BOOL isPendingTransitionFormSubmit =
373 ui::PAGE_TRANSITION_FORM_SUBMIT))) { 371 PageTransitionCoreTypeIs(trans, ui::PAGE_TRANSITION_FORM_SUBMIT);
372 BOOL isCurrentTransitionFormSubmit = PageTransitionCoreTypeIs(
373 item->GetTransitionType(), ui::PAGE_TRANSITION_FORM_SUBMIT);
374 BOOL shouldCreatePendingItem =
375 !hasSameURL ||
376 (isPendingTransitionFormSubmit && !isCurrentTransitionFormSubmit);
377
378 if (!shouldCreatePendingItem) {
374 // Send the notification anyway, to preserve old behavior. It's unknown 379 // Send the notification anyway, to preserve old behavior. It's unknown
375 // whether anything currently relies on this, but since both this whole 380 // whether anything currently relies on this, but since both this whole
376 // hack and the content facade will both be going away, it's not worth 381 // hack and the content facade will both be going away, it's not worth
377 // trying to unwind. 382 // trying to unwind.
378 if (_navigationManager && _navigationManager->GetFacadeDelegate()) { 383 if (_navigationManager && _navigationManager->GetFacadeDelegate()) {
379 _navigationManager->GetFacadeDelegate()->OnNavigationItemPending(); 384 _navigationManager->GetFacadeDelegate()->OnNavigationItemPending();
380 } 385 }
381 return; 386 return;
382 } 387 }
383 } 388 }
384 389
385 BOOL useDesktopUserAgent =
386 _useDesktopUserAgentForNextPendingItem ||
387 (self.currentEntry.navigationItem &&
388 self.currentEntry.navigationItem->IsOverridingUserAgent());
389 _useDesktopUserAgentForNextPendingItem = NO;
390 _pendingEntry.reset([self sessionEntryWithURL:url 390 _pendingEntry.reset([self sessionEntryWithURL:url
391 referrer:ref 391 referrer:ref
392 transition:trans 392 transition:trans
393 useDesktopUserAgent:useDesktopUserAgent
394 rendererInitiated:rendererInitiated]); 393 rendererInitiated:rendererInitiated]);
395 394
396 if (_navigationManager && _navigationManager->GetFacadeDelegate()) { 395 if (_navigationManager && _navigationManager->GetFacadeDelegate()) {
397 _navigationManager->GetFacadeDelegate()->OnNavigationItemPending(); 396 _navigationManager->GetFacadeDelegate()->OnNavigationItemPending();
398 } 397 }
399 } 398 }
400 399
401 - (void)updatePendingItem:(const GURL&)url { 400 - (void)updatePendingItem:(const GURL&)url {
402 // If there is no pending entry, navigation is probably happening within the 401 // If there is no pending entry, navigation is probably happening within the
403 // session history. Don't modify the entry list. 402 // session history. Don't modify the entry list.
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 if (_navigationManager && item) 476 if (_navigationManager && item)
478 _navigationManager->OnNavigationItemCommitted(); 477 _navigationManager->OnNavigationItemCommitted();
479 DCHECK_EQ(_pendingItemIndex, -1); 478 DCHECK_EQ(_pendingItemIndex, -1);
480 } 479 }
481 480
482 - (void)addTransientItemWithURL:(const GURL&)URL { 481 - (void)addTransientItemWithURL:(const GURL&)URL {
483 _transientEntry.reset([self 482 _transientEntry.reset([self
484 sessionEntryWithURL:URL 483 sessionEntryWithURL:URL
485 referrer:web::Referrer() 484 referrer:web::Referrer()
486 transition:ui::PAGE_TRANSITION_CLIENT_REDIRECT 485 transition:ui::PAGE_TRANSITION_CLIENT_REDIRECT
487 useDesktopUserAgent:NO
488 rendererInitiated:NO]); 486 rendererInitiated:NO]);
489 487
490 web::NavigationItem* navigationItem = [_transientEntry navigationItem]; 488 web::NavigationItem* navigationItem = [_transientEntry navigationItem];
491 DCHECK(navigationItem); 489 DCHECK(navigationItem);
492 navigationItem->SetTimestamp( 490 navigationItem->SetTimestamp(
493 _timeSmoother.GetSmoothedTime(base::Time::Now())); 491 _timeSmoother.GetSmoothedTime(base::Time::Now()));
494 } 492 }
495 493
496 - (void)pushNewItemWithURL:(const GURL&)URL 494 - (void)pushNewItemWithURL:(const GURL&)URL
497 stateObject:(NSString*)stateObject 495 stateObject:(NSString*)stateObject
498 transition:(ui::PageTransition)transition { 496 transition:(ui::PageTransition)transition {
499 DCHECK(![self pendingEntry]); 497 DCHECK(![self pendingEntry]);
500 DCHECK([self currentEntry]); 498 DCHECK([self currentEntry]);
501 web::NavigationItem* item = [self currentEntry].navigationItem; 499 web::NavigationItem* currentItem = [self currentEntry].navigationItem;
502 CHECK( 500 CHECK(web::history_state_util::IsHistoryStateChangeValid(
503 web::history_state_util::IsHistoryStateChangeValid(item->GetURL(), URL)); 501 currentItem->GetURL(), URL));
504 web::Referrer referrer(item->GetURL(), web::ReferrerPolicyDefault); 502 web::Referrer referrer(currentItem->GetURL(), web::ReferrerPolicyDefault);
505 bool overrideUserAgent = 503
506 self.currentEntry.navigationItem->IsOverridingUserAgent();
507 base::scoped_nsobject<CRWSessionEntry> pushedEntry([self 504 base::scoped_nsobject<CRWSessionEntry> pushedEntry([self
508 sessionEntryWithURL:URL 505 sessionEntryWithURL:URL
509 referrer:referrer 506 referrer:referrer
510 transition:transition 507 transition:transition
511 useDesktopUserAgent:overrideUserAgent
512 rendererInitiated:NO]); 508 rendererInitiated:NO]);
509
513 web::NavigationItemImpl* pushedItem = [pushedEntry navigationItemImpl]; 510 web::NavigationItemImpl* pushedItem = [pushedEntry navigationItemImpl];
511 pushedItem->SetIsOverridingUserAgent(currentItem->IsOverridingUserAgent());
514 pushedItem->SetSerializedStateObject(stateObject); 512 pushedItem->SetSerializedStateObject(stateObject);
515 pushedItem->SetIsCreatedFromPushState(true); 513 pushedItem->SetIsCreatedFromPushState(true);
516 web::SSLStatus& sslStatus = [self currentEntry].navigationItem->GetSSL(); 514 web::SSLStatus& sslStatus = [self currentEntry].navigationItem->GetSSL();
517 pushedEntry.get().navigationItem->GetSSL() = sslStatus; 515 pushedEntry.get().navigationItem->GetSSL() = sslStatus;
518 516
519 [self clearForwardItems]; 517 [self clearForwardItems];
520 // Add the new entry at the end. 518 // Add the new entry at the end.
521 [_entries addObject:pushedEntry]; 519 [_entries addObject:pushedEntry];
522 _previousNavigationIndex = _currentNavigationIndex; 520 _previousNavigationIndex = _currentNavigationIndex;
523 self.currentNavigationIndex = [_entries count] - 1; 521 self.currentNavigationIndex = [_entries count] - 1;
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 670
673 NSInteger index = _currentNavigationIndex; 671 NSInteger index = _currentNavigationIndex;
674 // This will return the first session entry if all other entries are 672 // This will return the first session entry if all other entries are
675 // redirects, regardless of the transition state of the first entry. 673 // redirects, regardless of the transition state of the first entry.
676 while (index > 0 && [self isRedirectTransitionForItemAtIndex:index]) { 674 while (index > 0 && [self isRedirectTransitionForItemAtIndex:index]) {
677 --index; 675 --index;
678 } 676 }
679 return [_entries objectAtIndex:index]; 677 return [_entries objectAtIndex:index];
680 } 678 }
681 679
682 - (void)useDesktopUserAgentForNextPendingItem {
683 if (_pendingEntry)
684 [_pendingEntry navigationItem]->SetIsOverridingUserAgent(true);
685 else
686 _useDesktopUserAgentForNextPendingItem = YES;
687 }
688
689 - (NSInteger)indexOfItem:(const web::NavigationItem*)item { 680 - (NSInteger)indexOfItem:(const web::NavigationItem*)item {
690 web::NavigationItemList items = self.items; 681 web::NavigationItemList items = self.items;
691 for (NSInteger i = 0; i < static_cast<NSInteger>(items.size()); ++i) { 682 for (NSInteger i = 0; i < static_cast<NSInteger>(items.size()); ++i) {
692 if (items[i] == item) 683 if (items[i] == item)
693 return i; 684 return i;
694 } 685 }
695 return NSNotFound; 686 return NSNotFound;
696 } 687 }
697 688
698 #pragma mark - 689 #pragma mark -
699 #pragma mark Private methods 690 #pragma mark Private methods
700 691
701 - (NSString*)uniqueID { 692 - (NSString*)uniqueID {
702 CFUUIDRef uuidRef = CFUUIDCreate(NULL); 693 CFUUIDRef uuidRef = CFUUIDCreate(NULL);
703 CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef); 694 CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
704 CFRelease(uuidRef); 695 CFRelease(uuidRef);
705 696
706 NSString* uuid = 697 NSString* uuid =
707 [NSString stringWithString:base::mac::ObjCCastStrict<NSString>( 698 [NSString stringWithString:base::mac::ObjCCastStrict<NSString>(
708 CFBridgingRelease(uuidStringRef))]; 699 CFBridgingRelease(uuidStringRef))];
709 return uuid; 700 return uuid;
710 } 701 }
711 702
712 - (CRWSessionEntry*)sessionEntryWithURL:(const GURL&)url 703 - (CRWSessionEntry*)sessionEntryWithURL:(const GURL&)url
713 referrer:(const web::Referrer&)referrer 704 referrer:(const web::Referrer&)referrer
714 transition:(ui::PageTransition)transition 705 transition:(ui::PageTransition)transition
715 useDesktopUserAgent:(BOOL)useDesktopUserAgent
716 rendererInitiated:(BOOL)rendererInitiated { 706 rendererInitiated:(BOOL)rendererInitiated {
717 GURL loaded_url(url); 707 GURL loaded_url(url);
718 BOOL urlWasRewritten = NO; 708 BOOL urlWasRewritten = NO;
719 if (_navigationManager) { 709 if (_navigationManager) {
720 std::unique_ptr<std::vector<web::BrowserURLRewriter::URLRewriter>> 710 std::unique_ptr<std::vector<web::BrowserURLRewriter::URLRewriter>>
721 transientRewriters = _navigationManager->GetTransientURLRewriters(); 711 transientRewriters = _navigationManager->GetTransientURLRewriters();
722 if (transientRewriters) { 712 if (transientRewriters) {
723 urlWasRewritten = web::BrowserURLRewriter::RewriteURLWithWriters( 713 urlWasRewritten = web::BrowserURLRewriter::RewriteURLWithWriters(
724 &loaded_url, _browserState, *transientRewriters.get()); 714 &loaded_url, _browserState, *transientRewriters.get());
725 } 715 }
726 } 716 }
727 if (!urlWasRewritten) { 717 if (!urlWasRewritten) {
728 web::BrowserURLRewriter::GetInstance()->RewriteURLIfNecessary( 718 web::BrowserURLRewriter::GetInstance()->RewriteURLIfNecessary(
729 &loaded_url, _browserState); 719 &loaded_url, _browserState);
730 } 720 }
721
731 std::unique_ptr<web::NavigationItemImpl> item(new web::NavigationItemImpl()); 722 std::unique_ptr<web::NavigationItemImpl> item(new web::NavigationItemImpl());
732 item->SetOriginalRequestURL(loaded_url); 723 item->SetOriginalRequestURL(loaded_url);
733 item->SetURL(loaded_url); 724 item->SetURL(loaded_url);
734 item->SetReferrer(referrer); 725 item->SetReferrer(referrer);
735 item->SetTransitionType(transition); 726 item->SetTransitionType(transition);
736 item->SetIsOverridingUserAgent(useDesktopUserAgent); 727 item->SetIsRendererInitiated(rendererInitiated);
737 item->set_is_renderer_initiated(rendererInitiated);
738 return [[CRWSessionEntry alloc] initWithNavigationItem:std::move(item)]; 728 return [[CRWSessionEntry alloc] initWithNavigationItem:std::move(item)];
739 } 729 }
740 730
741 - (BOOL)isRedirectTransitionForItemAtIndex:(NSInteger)index { 731 - (BOOL)isRedirectTransitionForItemAtIndex:(NSInteger)index {
742 ui::PageTransition transition = 732 ui::PageTransition transition =
743 [_entries[index] navigationItem]->GetTransitionType(); 733 [_entries[index] navigationItem]->GetTransitionType();
744 return (transition & ui::PAGE_TRANSITION_IS_REDIRECT_MASK) ? YES : NO; 734 return (transition & ui::PAGE_TRANSITION_IS_REDIRECT_MASK) ? YES : NO;
745 } 735 }
746 736
747 - (web::NavigationItemList)itemListForEntryList:(NSArray*)entries { 737 - (web::NavigationItemList)itemListForEntryList:(NSArray*)entries {
748 web::NavigationItemList list(entries.count); 738 web::NavigationItemList list(entries.count);
749 for (size_t index = 0; index < entries.count; ++index) 739 for (size_t index = 0; index < entries.count; ++index)
750 list[index] = [entries[index] navigationItem]; 740 list[index] = [entries[index] navigationItem];
751 return list; 741 return list;
752 } 742 }
753 743
754 @end 744 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698