OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "ios/web/navigation/navigation_manager_impl.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #import "ios/web/navigation/crw_session_controller+private_constructors.h" |
| 9 #import "ios/web/navigation/crw_session_controller.h" |
| 10 #import "ios/web/navigation/crw_session_entry.h" |
| 11 #include "ios/web/navigation/navigation_item_impl.h" |
| 12 #include "ios/web/navigation/navigation_manager_delegate.h" |
| 13 #import "ios/web/navigation/navigation_manager_facade_delegate.h" |
| 14 #include "ios/web/public/load_committed_details.h" |
| 15 #include "ios/web/public/navigation_item.h" |
| 16 #include "ios/web/public/web_state/web_state.h" |
| 17 #include "ui/base/page_transition_types.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Checks whether or not two URL are an in-page navigation (differing only |
| 22 // in the fragment). |
| 23 bool AreURLsInPageNavigation(const GURL& existing_url, const GURL& new_url) { |
| 24 if (existing_url == new_url || !new_url.has_ref()) |
| 25 return false; |
| 26 |
| 27 url::Replacements<char> replacements; |
| 28 replacements.ClearRef(); |
| 29 return existing_url.ReplaceComponents(replacements) == |
| 30 new_url.ReplaceComponents(replacements); |
| 31 } |
| 32 |
| 33 } // anonymous namespace |
| 34 |
| 35 namespace web { |
| 36 |
| 37 NavigationManagerImpl::NavigationManagerImpl( |
| 38 NavigationManagerDelegate* delegate, |
| 39 BrowserState* browser_state) |
| 40 : delegate_(delegate), |
| 41 browser_state_(browser_state), |
| 42 facade_delegate_(nullptr) { |
| 43 DCHECK(browser_state_); |
| 44 } |
| 45 |
| 46 NavigationManagerImpl::~NavigationManagerImpl() { |
| 47 // The facade layer should be deleted before this object. |
| 48 DCHECK(!facade_delegate_); |
| 49 |
| 50 [session_controller_ setNavigationManager:nullptr]; |
| 51 } |
| 52 |
| 53 CRWSessionController* NavigationManagerImpl::GetSessionController() { |
| 54 return session_controller_; |
| 55 } |
| 56 |
| 57 void NavigationManagerImpl::SetSessionController( |
| 58 CRWSessionController* session_controller) { |
| 59 session_controller_.reset([session_controller retain]); |
| 60 [session_controller_ setNavigationManager:this]; |
| 61 } |
| 62 |
| 63 void NavigationManagerImpl::InitializeSession(NSString* window_name, |
| 64 NSString* opener_id, |
| 65 BOOL opened_by_dom, |
| 66 int opener_navigation_index) { |
| 67 SetSessionController([[[CRWSessionController alloc] |
| 68 initWithWindowName:window_name |
| 69 openerId:opener_id |
| 70 openedByDOM:opened_by_dom |
| 71 openerNavigationIndex:opener_navigation_index |
| 72 browserState:browser_state_] autorelease]); |
| 73 } |
| 74 |
| 75 void NavigationManagerImpl::ReplaceSessionHistory( |
| 76 ScopedVector<web::NavigationItem> items, |
| 77 int current_index) { |
| 78 SetSessionController([[[CRWSessionController alloc] |
| 79 initWithNavigationItems:items.Pass() |
| 80 currentIndex:current_index |
| 81 browserState:browser_state_] autorelease]); |
| 82 } |
| 83 |
| 84 void NavigationManagerImpl::SetFacadeDelegate( |
| 85 NavigationManagerFacadeDelegate* facade_delegate) { |
| 86 facade_delegate_ = facade_delegate; |
| 87 } |
| 88 |
| 89 NavigationManagerFacadeDelegate* NavigationManagerImpl::GetFacadeDelegate() |
| 90 const { |
| 91 return facade_delegate_; |
| 92 } |
| 93 |
| 94 |
| 95 void NavigationManagerImpl::OnNavigationItemChanged() { |
| 96 if (facade_delegate_) |
| 97 facade_delegate_->OnNavigationItemChanged(); |
| 98 } |
| 99 |
| 100 void NavigationManagerImpl::OnNavigationItemCommitted() { |
| 101 LoadCommittedDetails details; |
| 102 details.item = GetLastCommittedItem(); |
| 103 DCHECK(details.item); |
| 104 details.previous_item_index = [session_controller_ previousNavigationIndex]; |
| 105 if (details.previous_item_index >= 0) { |
| 106 DCHECK([session_controller_ previousEntry]); |
| 107 details.previous_url = |
| 108 [session_controller_ previousEntry].navigationItem->GetURL(); |
| 109 details.is_in_page = |
| 110 AreURLsInPageNavigation(details.previous_url, details.item->GetURL()); |
| 111 } else { |
| 112 details.previous_url = GURL(); |
| 113 details.is_in_page = NO; |
| 114 } |
| 115 |
| 116 delegate_->OnNavigationItemCommitted(details); |
| 117 |
| 118 if (facade_delegate_) { |
| 119 facade_delegate_->OnNavigationItemCommitted(details.previous_item_index, |
| 120 details.is_in_page); |
| 121 } |
| 122 } |
| 123 |
| 124 NavigationItem* NavigationManagerImpl::GetVisibleItem() const { |
| 125 CRWSessionEntry* entry = [session_controller_ visibleEntry]; |
| 126 return [entry navigationItem]; |
| 127 } |
| 128 |
| 129 NavigationItem* NavigationManagerImpl::GetPendingItem() const { |
| 130 return [[session_controller_ pendingEntry] navigationItem]; |
| 131 } |
| 132 |
| 133 NavigationItem* NavigationManagerImpl::GetTransientItem() const { |
| 134 return [[session_controller_ transientEntry] navigationItem]; |
| 135 } |
| 136 |
| 137 NavigationItem* NavigationManagerImpl::GetLastCommittedItem() const { |
| 138 CRWSessionEntry* entry = [session_controller_ lastCommittedEntry]; |
| 139 return [entry navigationItem]; |
| 140 } |
| 141 |
| 142 NavigationItem* NavigationManagerImpl::GetItemAtIndex(size_t index) const { |
| 143 NSArray* entries = [session_controller_ entries]; |
| 144 return index < entries.count ? [entries[index] navigationItem] : nullptr; |
| 145 } |
| 146 |
| 147 int NavigationManagerImpl::GetCurrentEntryIndex() const { |
| 148 return [session_controller_ currentNavigationIndex]; |
| 149 } |
| 150 |
| 151 int NavigationManagerImpl::GetLastCommittedEntryIndex() const { |
| 152 if (![[session_controller_ entries] count]) |
| 153 return -1; |
| 154 return [session_controller_ currentNavigationIndex]; |
| 155 } |
| 156 |
| 157 int NavigationManagerImpl::GetEntryCount() const { |
| 158 return [[session_controller_ entries] count]; |
| 159 } |
| 160 |
| 161 bool NavigationManagerImpl::RemoveEntryAtIndex(int index) { |
| 162 if (index == GetLastCommittedEntryIndex() || |
| 163 index == GetPendingEntryIndex()) |
| 164 return false; |
| 165 |
| 166 NSUInteger idx = static_cast<NSUInteger>(index); |
| 167 NSArray* entries = [session_controller_ entries]; |
| 168 if (idx >= entries.count) |
| 169 return false; |
| 170 |
| 171 [session_controller_ removeEntryAtIndex:index]; |
| 172 return true; |
| 173 } |
| 174 |
| 175 void NavigationManagerImpl::DiscardNonCommittedEntries() { |
| 176 [session_controller_ discardNonCommittedEntries]; |
| 177 } |
| 178 |
| 179 NavigationItem* NavigationManagerImpl::GetLastUserItem() const { |
| 180 CRWSessionEntry* entry = [session_controller_ lastUserEntry]; |
| 181 return [entry navigationItem]; |
| 182 } |
| 183 |
| 184 NavigationItem* NavigationManagerImpl::GetPreviousItem() const { |
| 185 CRWSessionEntry* entry = [session_controller_ previousEntry]; |
| 186 return [entry navigationItem]; |
| 187 } |
| 188 |
| 189 int NavigationManagerImpl::GetPendingEntryIndex() const { |
| 190 if ([session_controller_ hasPendingEntry]) |
| 191 return GetCurrentEntryIndex(); |
| 192 return -1; |
| 193 } |
| 194 |
| 195 void NavigationManagerImpl::LoadURL(const GURL& url, |
| 196 const web::Referrer& referrer, |
| 197 ui::PageTransition type) { |
| 198 WebState::OpenURLParams params(url, referrer, CURRENT_TAB, type, NO); |
| 199 delegate_->GetWebState()->OpenURL(params); |
| 200 } |
| 201 |
| 202 bool NavigationManagerImpl::CanGoBack() const { |
| 203 return [session_controller_ canGoBack]; |
| 204 } |
| 205 |
| 206 bool NavigationManagerImpl::CanGoForward() const { |
| 207 return [session_controller_ canGoForward]; |
| 208 } |
| 209 |
| 210 void NavigationManagerImpl::GoBack() { |
| 211 if (CanGoBack()) { |
| 212 [session_controller_ goBack]; |
| 213 // Signal the delegate to load the old page. |
| 214 delegate_->NavigateToPendingEntry(); |
| 215 } |
| 216 } |
| 217 |
| 218 void NavigationManagerImpl::GoForward() { |
| 219 if (CanGoForward()) { |
| 220 [session_controller_ goForward]; |
| 221 // Signal the delegate to load the new page. |
| 222 delegate_->NavigateToPendingEntry(); |
| 223 } |
| 224 } |
| 225 |
| 226 std::vector<NavigationItem*> NavigationManagerImpl::GetItems() { |
| 227 std::vector<NavigationItem*> items; |
| 228 size_t i = 0; |
| 229 items.resize([session_controller_ entries].count); |
| 230 for (CRWSessionEntry* entry in [session_controller_ entries]) { |
| 231 items[i++] = entry.navigationItem; |
| 232 } |
| 233 return items; |
| 234 } |
| 235 |
| 236 BrowserState* NavigationManagerImpl::GetBrowserState() const { |
| 237 return browser_state_; |
| 238 } |
| 239 |
| 240 WebState* NavigationManagerImpl::GetWebState() const { |
| 241 return delegate_->GetWebState(); |
| 242 } |
| 243 |
| 244 void NavigationManagerImpl::CopyState( |
| 245 NavigationManagerImpl* navigation_manager) { |
| 246 SetSessionController( |
| 247 [[navigation_manager->GetSessionController() copy] autorelease]); |
| 248 } |
| 249 |
| 250 } // namespace web |
| 251 |
OLD | NEW |