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

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

Issue 2779383002: implement user agent override option. (Closed)
Patch Set: Rebase Created 3 years, 8 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 2013 The Chromium Authors. All rights reserved. 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 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/navigation_manager_impl.h" 5 #import "ios/web/navigation/navigation_manager_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 26 matching lines...) Expand all
37 return existing_url.EqualsIgnoringRef(new_url); 37 return existing_url.EqualsIgnoringRef(new_url);
38 } 38 }
39 39
40 } // anonymous namespace 40 } // anonymous namespace
41 41
42 namespace web { 42 namespace web {
43 43
44 NavigationManager::WebLoadParams::WebLoadParams(const GURL& url) 44 NavigationManager::WebLoadParams::WebLoadParams(const GURL& url)
45 : url(url), 45 : url(url),
46 transition_type(ui::PAGE_TRANSITION_LINK), 46 transition_type(ui::PAGE_TRANSITION_LINK),
47 user_agent_override_option(UserAgentOverrideOption::INHERIT),
47 is_renderer_initiated(false), 48 is_renderer_initiated(false),
48 post_data(nil) {} 49 post_data(nil) {}
49 50
50 NavigationManager::WebLoadParams::~WebLoadParams() {} 51 NavigationManager::WebLoadParams::~WebLoadParams() {}
51 52
52 NavigationManager::WebLoadParams::WebLoadParams(const WebLoadParams& other) 53 NavigationManager::WebLoadParams::WebLoadParams(const WebLoadParams& other)
53 : url(other.url), 54 : url(other.url),
54 referrer(other.referrer), 55 referrer(other.referrer),
55 transition_type(other.transition_type), 56 transition_type(other.transition_type),
57 user_agent_override_option(other.user_agent_override_option),
56 is_renderer_initiated(other.is_renderer_initiated), 58 is_renderer_initiated(other.is_renderer_initiated),
57 extra_headers([other.extra_headers copy]), 59 extra_headers([other.extra_headers copy]),
58 post_data([other.post_data copy]) {} 60 post_data([other.post_data copy]) {}
59 61
60 NavigationManager::WebLoadParams& NavigationManager::WebLoadParams::operator=( 62 NavigationManager::WebLoadParams& NavigationManager::WebLoadParams::operator=(
61 const WebLoadParams& other) { 63 const WebLoadParams& other) {
62 url = other.url; 64 url = other.url;
63 referrer = other.referrer; 65 referrer = other.referrer;
64 is_renderer_initiated = other.is_renderer_initiated; 66 is_renderer_initiated = other.is_renderer_initiated;
65 transition_type = other.transition_type; 67 transition_type = other.transition_type;
68 user_agent_override_option = other.user_agent_override_option;
66 extra_headers.reset([other.extra_headers copy]); 69 extra_headers.reset([other.extra_headers copy]);
67 post_data.reset([other.post_data copy]); 70 post_data.reset([other.post_data copy]);
68 71
69 return *this; 72 return *this;
70 } 73 }
71 74
72 NavigationManagerImpl::NavigationManagerImpl() 75 NavigationManagerImpl::NavigationManagerImpl()
73 : override_desktop_user_agent_for_next_pending_item_(false), 76 : delegate_(nullptr), browser_state_(nullptr), facade_delegate_(nullptr) {}
74 delegate_(nullptr),
75 browser_state_(nullptr),
76 facade_delegate_(nullptr) {}
77 77
78 NavigationManagerImpl::~NavigationManagerImpl() { 78 NavigationManagerImpl::~NavigationManagerImpl() {
79 // The facade layer should be deleted before this object. 79 // The facade layer should be deleted before this object.
80 DCHECK(!facade_delegate_); 80 DCHECK(!facade_delegate_);
81 81
82 [session_controller_ setNavigationManager:nullptr]; 82 [session_controller_ setNavigationManager:nullptr];
83 } 83 }
84 84
85 void NavigationManagerImpl::SetDelegate(NavigationManagerDelegate* delegate) { 85 void NavigationManagerImpl::SetDelegate(NavigationManagerDelegate* delegate) {
86 delegate_ = delegate; 86 delegate_ = delegate;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 ui::PageTransition type) { 167 ui::PageTransition type) {
168 WebState::OpenURLParams params(url, referrer, 168 WebState::OpenURLParams params(url, referrer,
169 WindowOpenDisposition::CURRENT_TAB, type, NO); 169 WindowOpenDisposition::CURRENT_TAB, type, NO);
170 delegate_->GetWebState()->OpenURL(params); 170 delegate_->GetWebState()->OpenURL(params);
171 } 171 }
172 172
173 void NavigationManagerImpl::AddPendingItem( 173 void NavigationManagerImpl::AddPendingItem(
174 const GURL& url, 174 const GURL& url,
175 const web::Referrer& referrer, 175 const web::Referrer& referrer,
176 ui::PageTransition navigation_type, 176 ui::PageTransition navigation_type,
177 NavigationInitiationType initiation_type) { 177 NavigationInitiationType initiation_type,
178 UserAgentOverrideOption user_agent_override_option) {
178 [session_controller_ addPendingItem:url 179 [session_controller_ addPendingItem:url
179 referrer:referrer 180 referrer:referrer
180 transition:navigation_type 181 transition:navigation_type
181 initiationType:initiation_type]; 182 initiationType:initiation_type];
182 183
183 // Set the user agent type for web URLs. 184 // Set the user agent type for web URLs.
184 NavigationItem* pending_item = GetPendingItem(); 185 NavigationItem* pending_item = GetPendingItem();
185 if (!pending_item) 186 if (!pending_item)
186 return; 187 return;
187 188
188 // |override_desktop_user_agent_for_next_pending_item_| must be false if 189 // |user_agent_override_option| must be INHERIT if |pending_item|'s
189 // |pending_item|'s UserAgentType is NONE, as requesting a desktop user 190 // UserAgentType is NONE, as requesting a desktop or mobile user agent should
190 // agent should be disabled for app-specific URLs. 191 // be disabled for app-specific URLs.
191 DCHECK(pending_item->GetUserAgentType() != UserAgentType::NONE || 192 DCHECK(pending_item->GetUserAgentType() != UserAgentType::NONE ||
192 !override_desktop_user_agent_for_next_pending_item_); 193 user_agent_override_option == UserAgentOverrideOption::INHERIT);
193 194
194 // Newly created pending items are created with UserAgentType::NONE for native 195 // Newly created pending items are created with UserAgentType::NONE for native
195 // pages or UserAgentType::MOBILE for non-native pages. If the pending item's 196 // pages or UserAgentType::MOBILE for non-native pages. If the pending item's
196 // URL is non-native, check whether it should be created with 197 // URL is non-native, check which user agent type it should be created with
197 // UserAgentType::DESKTOP. 198 // based on |user_agent_override_option|.
198 DCHECK_NE(UserAgentType::DESKTOP, pending_item->GetUserAgentType()); 199 DCHECK_NE(UserAgentType::DESKTOP, pending_item->GetUserAgentType());
199 if (pending_item->GetUserAgentType() != UserAgentType::NONE) { 200 if (pending_item->GetUserAgentType() == UserAgentType::NONE)
200 bool use_desktop_user_agent = 201 return;
201 override_desktop_user_agent_for_next_pending_item_; 202
202 if (!use_desktop_user_agent) { 203 switch (user_agent_override_option) {
203 // If the flag is not set, propagate the last non-native item's 204 case UserAgentOverrideOption::DESKTOP:
204 // UserAgentType. 205 pending_item->SetUserAgentType(UserAgentType::DESKTOP);
206 break;
207 case UserAgentOverrideOption::MOBILE:
208 pending_item->SetUserAgentType(UserAgentType::MOBILE);
209 break;
210 case UserAgentOverrideOption::INHERIT: {
211 // Propagate the last committed non-native item's UserAgentType if there
212 // is one, otherwise keep the default value, which is mobile.
205 NavigationItem* last_non_native_item = 213 NavigationItem* last_non_native_item =
206 GetLastCommittedNonAppSpecificItem(); 214 GetLastCommittedNonAppSpecificItem();
207 DCHECK(!last_non_native_item || 215 DCHECK(!last_non_native_item ||
208 last_non_native_item->GetUserAgentType() != UserAgentType::NONE); 216 last_non_native_item->GetUserAgentType() != UserAgentType::NONE);
209 use_desktop_user_agent = 217 if (last_non_native_item) {
210 last_non_native_item && 218 pending_item->SetUserAgentType(
211 last_non_native_item->GetUserAgentType() == UserAgentType::DESKTOP; 219 last_non_native_item->GetUserAgentType());
220 }
221 break;
212 } 222 }
213 if (use_desktop_user_agent)
214 pending_item->SetUserAgentType(UserAgentType::DESKTOP);
215 } 223 }
216 override_desktop_user_agent_for_next_pending_item_ = false;
217 } 224 }
218 225
219 NavigationItemList NavigationManagerImpl::GetItems() const { 226 NavigationItemList NavigationManagerImpl::GetItems() const {
220 return CreateNavigationItemList([session_controller_ items]); 227 return CreateNavigationItemList([session_controller_ items]);
221 } 228 }
222 229
223 BrowserState* NavigationManagerImpl::GetBrowserState() const { 230 BrowserState* NavigationManagerImpl::GetBrowserState() const {
224 return browser_state_; 231 return browser_state_;
225 } 232 }
226 233
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 // Result may be out of bounds, so stop trying to skip redirect items and 435 // Result may be out of bounds, so stop trying to skip redirect items and
429 // simply add the remainder. 436 // simply add the remainder.
430 result += offset; 437 result += offset;
431 if (result < 0 /* overflow */) 438 if (result < 0 /* overflow */)
432 result = INT_MAX; 439 result = INT_MAX;
433 } 440 }
434 441
435 return result; 442 return result;
436 } 443 }
437 444
438 void NavigationManagerImpl::OverrideDesktopUserAgentForNextPendingItem() {
439 NavigationItem* pending_item = GetPendingItem();
440 if (pending_item) {
441 // The desktop user agent cannot be used for a pending navigation to an
442 // app-specific URL.
443 DCHECK_NE(pending_item->GetUserAgentType(), UserAgentType::NONE);
444 pending_item->SetUserAgentType(UserAgentType::DESKTOP);
445 } else {
446 override_desktop_user_agent_for_next_pending_item_ = true;
447 }
448 }
449
450 bool NavigationManagerImpl::IsRedirectItemAtIndex(int index) const { 445 bool NavigationManagerImpl::IsRedirectItemAtIndex(int index) const {
451 DCHECK_GE(index, 0); 446 DCHECK_GE(index, 0);
452 DCHECK_LT(index, GetItemCount()); 447 DCHECK_LT(index, GetItemCount());
453 ui::PageTransition transition = GetItemAtIndex(index)->GetTransitionType(); 448 ui::PageTransition transition = GetItemAtIndex(index)->GetTransitionType();
454 return transition & ui::PAGE_TRANSITION_IS_REDIRECT_MASK; 449 return transition & ui::PAGE_TRANSITION_IS_REDIRECT_MASK;
455 } 450 }
456 451
457 NavigationItem* NavigationManagerImpl::GetLastCommittedNonAppSpecificItem() 452 NavigationItem* NavigationManagerImpl::GetLastCommittedNonAppSpecificItem()
458 const { 453 const {
459 int index = GetLastCommittedItemIndex(); 454 int index = GetLastCommittedItemIndex();
460 if (index == -1) 455 if (index == -1)
461 return nullptr; 456 return nullptr;
462 WebClient* client = GetWebClient(); 457 WebClient* client = GetWebClient();
463 const ScopedNavigationItemImplList& items = [session_controller_ items]; 458 const ScopedNavigationItemImplList& items = [session_controller_ items];
464 while (index >= 0) { 459 while (index >= 0) {
465 NavigationItem* item = items[index--].get(); 460 NavigationItem* item = items[index--].get();
466 if (!client->IsAppSpecificURL(item->GetVirtualURL())) 461 if (!client->IsAppSpecificURL(item->GetVirtualURL()))
467 return item; 462 return item;
468 } 463 }
469 return nullptr; 464 return nullptr;
470 } 465 }
471 466
472 } // namespace web 467 } // namespace web
OLDNEW
« no previous file with comments | « ios/web/navigation/navigation_manager_impl.h ('k') | ios/web/navigation/navigation_manager_impl_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698