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

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

Issue 2779383002: implement user agent override option. (Closed)
Patch Set: 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 if (user_agent_override_option == UserAgentOverrideOption::DESKTOP) {
kkhorimoto 2017/03/30 02:30:28 NIT: Should we just use a switch statement here?
liaoyuke 2017/03/30 19:56:25 Done.
203 // If the flag is not set, propagate the last non-native item's 204 pending_item->SetUserAgentType(UserAgentType::DESKTOP);
204 // UserAgentType. 205 } else if (user_agent_override_option == UserAgentOverrideOption::MOBILE) {
205 NavigationItem* last_non_native_item = 206 pending_item->SetUserAgentType(UserAgentType::MOBILE);
206 GetLastCommittedNonAppSpecificItem(); 207 } else {
207 DCHECK(!last_non_native_item || 208 // If INHERIT, propagate the last non-native item's UserAgentType.
208 last_non_native_item->GetUserAgentType() != UserAgentType::NONE); 209 NavigationItem* last_non_native_item = GetLastCommittedNonAppSpecificItem();
209 use_desktop_user_agent = 210 DCHECK(!last_non_native_item ||
210 last_non_native_item && 211 last_non_native_item->GetUserAgentType() != UserAgentType::NONE);
211 last_non_native_item->GetUserAgentType() == UserAgentType::DESKTOP; 212 if (last_non_native_item)
212 } 213 pending_item->SetUserAgentType(last_non_native_item->GetUserAgentType());
213 if (use_desktop_user_agent)
214 pending_item->SetUserAgentType(UserAgentType::DESKTOP);
215 } 214 }
216 override_desktop_user_agent_for_next_pending_item_ = false;
217 } 215 }
218 216
219 NavigationItemList NavigationManagerImpl::GetItems() const { 217 NavigationItemList NavigationManagerImpl::GetItems() const {
220 return CreateNavigationItemList([session_controller_ items]); 218 return CreateNavigationItemList([session_controller_ items]);
221 } 219 }
222 220
223 BrowserState* NavigationManagerImpl::GetBrowserState() const { 221 BrowserState* NavigationManagerImpl::GetBrowserState() const {
224 return browser_state_; 222 return browser_state_;
225 } 223 }
226 224
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 // Result may be out of bounds, so stop trying to skip redirect items and 436 // Result may be out of bounds, so stop trying to skip redirect items and
439 // simply add the remainder. 437 // simply add the remainder.
440 result += offset; 438 result += offset;
441 if (result < 0 /* overflow */) 439 if (result < 0 /* overflow */)
442 result = INT_MAX; 440 result = INT_MAX;
443 } 441 }
444 442
445 return result; 443 return result;
446 } 444 }
447 445
448 void NavigationManagerImpl::OverrideDesktopUserAgentForNextPendingItem() {
449 NavigationItem* pending_item = GetPendingItem();
450 if (pending_item) {
451 // The desktop user agent cannot be used for a pending navigation to an
452 // app-specific URL.
453 DCHECK_NE(pending_item->GetUserAgentType(), UserAgentType::NONE);
454 pending_item->SetUserAgentType(UserAgentType::DESKTOP);
455 } else {
456 override_desktop_user_agent_for_next_pending_item_ = true;
457 }
458 }
459
460 bool NavigationManagerImpl::IsRedirectItemAtIndex(int index) const { 446 bool NavigationManagerImpl::IsRedirectItemAtIndex(int index) const {
461 DCHECK_GE(index, 0); 447 DCHECK_GE(index, 0);
462 DCHECK_LT(index, GetItemCount()); 448 DCHECK_LT(index, GetItemCount());
463 ui::PageTransition transition = GetItemAtIndex(index)->GetTransitionType(); 449 ui::PageTransition transition = GetItemAtIndex(index)->GetTransitionType();
464 return transition & ui::PAGE_TRANSITION_IS_REDIRECT_MASK; 450 return transition & ui::PAGE_TRANSITION_IS_REDIRECT_MASK;
465 } 451 }
466 452
467 NavigationItem* NavigationManagerImpl::GetLastCommittedNonAppSpecificItem() 453 NavigationItem* NavigationManagerImpl::GetLastCommittedNonAppSpecificItem()
468 const { 454 const {
469 int index = GetLastCommittedItemIndex(); 455 int index = GetLastCommittedItemIndex();
470 if (index == -1) 456 if (index == -1)
471 return nullptr; 457 return nullptr;
472 WebClient* client = GetWebClient(); 458 WebClient* client = GetWebClient();
473 const ScopedNavigationItemImplList& items = [session_controller_ items]; 459 const ScopedNavigationItemImplList& items = [session_controller_ items];
474 while (index >= 0) { 460 while (index >= 0) {
475 NavigationItem* item = items[index--].get(); 461 NavigationItem* item = items[index--].get();
476 if (!client->IsAppSpecificURL(item->GetVirtualURL())) 462 if (!client->IsAppSpecificURL(item->GetVirtualURL()))
477 return item; 463 return item;
478 } 464 }
479 return nullptr; 465 return nullptr;
480 } 466 }
481 467
482 } // namespace web 468 } // namespace web
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698