Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / ios / web / navigation / navigation_item_impl.mm
blob5609aabd346513a2a54b84689ef433e55294a988
1 // Copyright 2014 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.
5 #include "ios/web/navigation/navigation_item_impl.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "net/base/net_util.h"
10 #include "ui/base/page_transition_types.h"
11 #include "ui/gfx/text_elider.h"
13 namespace {
15 // Returns a new unique ID for use in NavigationItem during construction.  The
16 // returned ID is guaranteed to be nonzero (which is the "no ID" indicator).
17 static int GetUniqueIDInConstructor() {
18   static int unique_id_counter = 0;
19   return ++unique_id_counter;
22 }  // namespace
24 namespace web {
26 // static
27 scoped_ptr<NavigationItem> NavigationItem::Create() {
28   return scoped_ptr<NavigationItem>(new NavigationItemImpl());
31 NavigationItemImpl::NavigationItemImpl()
32     : unique_id_(GetUniqueIDInConstructor()),
33       page_id_(-1),
34       transition_type_(ui::PAGE_TRANSITION_LINK),
35       is_overriding_user_agent_(false),
36       is_created_from_push_state_(false),
37       should_skip_resubmit_data_confirmation_(false),
38       is_renderer_initiated_(false),
39       is_unsafe_(false),
40       facade_delegate_(nullptr) {
43 NavigationItemImpl::~NavigationItemImpl() {
46 NavigationItemImpl::NavigationItemImpl(const NavigationItemImpl& item)
47     : unique_id_(item.unique_id_),
48       url_(item.url_),
49       referrer_(item.referrer_),
50       virtual_url_(item.virtual_url_),
51       title_(item.title_),
52       page_id_(item.page_id_),
53       page_scroll_state_(item.page_scroll_state_),
54       transition_type_(item.transition_type_),
55       favicon_(item.favicon_),
56       ssl_(item.ssl_),
57       timestamp_(item.timestamp_),
58       is_overriding_user_agent_(item.is_overriding_user_agent_),
59       http_request_headers_([item.http_request_headers_ copy]),
60       serialized_state_object_([item.serialized_state_object_ copy]),
61       is_created_from_push_state_(item.is_created_from_push_state_),
62       should_skip_resubmit_data_confirmation_(
63           item.should_skip_resubmit_data_confirmation_),
64       post_data_([item.post_data_ copy]),
65       is_renderer_initiated_(item.is_renderer_initiated_),
66       is_unsafe_(item.is_unsafe_),
67       cached_display_title_(item.cached_display_title_),
68       facade_delegate_(nullptr) {
71 void NavigationItemImpl::SetFacadeDelegate(
72     scoped_ptr<NavigationItemFacadeDelegate> facade_delegate) {
73   facade_delegate_ = facade_delegate.Pass();
76 NavigationItemFacadeDelegate* NavigationItemImpl::GetFacadeDelegate() const {
77   return facade_delegate_.get();
80 int NavigationItemImpl::GetUniqueID() const {
81   return unique_id_;
84 void NavigationItemImpl::SetURL(const GURL& url) {
85   url_ = url;
86   cached_display_title_.clear();
89 const GURL& NavigationItemImpl::GetURL() const {
90   return url_;
93 void NavigationItemImpl::SetReferrer(const web::Referrer& referrer) {
94   referrer_ = referrer;
97 const web::Referrer& NavigationItemImpl::GetReferrer() const {
98   return referrer_;
101 void NavigationItemImpl::SetVirtualURL(const GURL& url) {
102   virtual_url_ = (url == url_) ? GURL() : url;
103   cached_display_title_.clear();
106 const GURL& NavigationItemImpl::GetVirtualURL() const {
107   return virtual_url_.is_empty() ? url_ : virtual_url_;
110 void NavigationItemImpl::SetTitle(const base::string16& title) {
111   title_ = title;
112   cached_display_title_.clear();
115 const base::string16& NavigationItemImpl::GetTitle() const {
116   return title_;
119 void NavigationItemImpl::SetPageID(int page_id) {
120   page_id_ = page_id;
123 int32 NavigationItemImpl::GetPageID() const {
124   return page_id_;
127 void NavigationItemImpl::SetPageScrollState(
128     const web::PageScrollState& scroll_state) {
129   page_scroll_state_ = scroll_state;
132 const PageScrollState& NavigationItemImpl::GetPageScrollState() const {
133   return page_scroll_state_;
136 const base::string16& NavigationItemImpl::GetTitleForDisplay(
137     const std::string& languages) const {
138   // Most pages have real titles. Don't even bother caching anything if this is
139   // the case.
140   if (!title_.empty())
141     return title_;
143   // More complicated cases will use the URLs as the title. This result we will
144   // cache since it's more complicated to compute.
145   if (!cached_display_title_.empty())
146     return cached_display_title_;
148   // Use the virtual URL first if any, and fall back on using the real URL.
149   base::string16 title;
150   if (!virtual_url_.is_empty()) {
151     title = net::FormatUrl(virtual_url_, languages);
152   } else if (!url_.is_empty()) {
153     title = net::FormatUrl(url_, languages);
154   }
156   // For file:// URLs use the filename as the title, not the full path.
157   if (url_.SchemeIsFile()) {
158     base::string16::size_type slashpos = title.rfind('/');
159     if (slashpos != base::string16::npos)
160       title = title.substr(slashpos + 1);
161   }
163   const int kMaxTitleChars = 4 * 1024;
164   gfx::ElideString(title, kMaxTitleChars, &cached_display_title_);
165   return cached_display_title_;
168 void NavigationItemImpl::SetTransitionType(ui::PageTransition transition_type) {
169   transition_type_ = transition_type;
172 ui::PageTransition NavigationItemImpl::GetTransitionType() const {
173   return transition_type_;
176 const FaviconStatus& NavigationItemImpl::GetFavicon() const {
177   return favicon_;
180 FaviconStatus& NavigationItemImpl::GetFavicon() {
181   return favicon_;
184 const SSLStatus& NavigationItemImpl::GetSSL() const {
185   return ssl_;
188 SSLStatus& NavigationItemImpl::GetSSL() {
189   return ssl_;
192 void NavigationItemImpl::SetTimestamp(base::Time timestamp) {
193   timestamp_ = timestamp;
196 base::Time NavigationItemImpl::GetTimestamp() const {
197   return timestamp_;
200 void NavigationItemImpl::SetUnsafe(bool is_unsafe) {
201   is_unsafe_ = is_unsafe;
204 bool NavigationItemImpl::IsUnsafe() const {
205   return is_unsafe_;
208 void NavigationItemImpl::SetIsOverridingUserAgent(
209     bool is_overriding_user_agent) {
210   is_overriding_user_agent_ = is_overriding_user_agent;
213 bool NavigationItemImpl::IsOverridingUserAgent() const {
214   return is_overriding_user_agent_;
217 bool NavigationItemImpl::HasPostData() const {
218   return post_data_.get() != nil;
221 NSDictionary* NavigationItemImpl::GetHttpRequestHeaders() const {
222   return [[http_request_headers_ copy] autorelease];
225 void NavigationItemImpl::AddHttpRequestHeaders(
226     NSDictionary* additional_headers) {
227   if (!additional_headers)
228     return;
230   if (http_request_headers_)
231     [http_request_headers_ addEntriesFromDictionary:additional_headers];
232   else
233     http_request_headers_.reset([additional_headers mutableCopy]);
236 void NavigationItemImpl::SetSerializedStateObject(
237     NSString* serialized_state_object) {
238   serialized_state_object_.reset([serialized_state_object retain]);
241 NSString* NavigationItemImpl::GetSerializedStateObject() const {
242   return serialized_state_object_.get();
245 void NavigationItemImpl::SetIsCreatedFromPushState(bool push_state) {
246   is_created_from_push_state_ = push_state;
249 bool NavigationItemImpl::IsCreatedFromPushState() const {
250   return is_created_from_push_state_;
253 void NavigationItemImpl::SetShouldSkipResubmitDataConfirmation(bool skip) {
254   should_skip_resubmit_data_confirmation_ = skip;
257 bool NavigationItemImpl::ShouldSkipResubmitDataConfirmation() const {
258   return should_skip_resubmit_data_confirmation_;
261 void NavigationItemImpl::SetPostData(NSData* post_data) {
262   post_data_.reset([post_data retain]);
265 NSData* NavigationItemImpl::GetPostData() const {
266   return post_data_.get();
269 void NavigationItemImpl::RemoveHttpRequestHeaderForKey(NSString* key) {
270   DCHECK(key);
271   [http_request_headers_ removeObjectForKey:key];
272   if (![http_request_headers_ count])
273     http_request_headers_.reset();
276 void NavigationItemImpl::ResetHttpRequestHeaders() {
277   http_request_headers_.reset();
280 void NavigationItemImpl::ResetForCommit() {
281   // Any state that only matters when a navigation item is pending should be
282   // cleared here.
283   set_is_renderer_initiated(false);
286 }  // namespace web