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"
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;
27 scoped_ptr<NavigationItem> NavigationItem::Create() {
28 return scoped_ptr<NavigationItem>(new NavigationItemImpl());
31 NavigationItemImpl::NavigationItemImpl()
32 : unique_id_(GetUniqueIDInConstructor()),
34 transition_type_(ui::PAGE_TRANSITION_LINK),
35 is_renderer_initiated_(false),
37 facade_delegate_(nullptr) {
40 NavigationItemImpl::~NavigationItemImpl() {
43 NavigationItemImpl::NavigationItemImpl(const NavigationItemImpl& item)
44 : unique_id_(item.unique_id_),
46 referrer_(item.referrer_),
47 virtual_url_(item.virtual_url_),
49 page_id_(item.page_id_),
50 page_scroll_state_(item.page_scroll_state_),
51 transition_type_(item.transition_type_),
52 favicon_(item.favicon_),
54 timestamp_(item.timestamp_),
55 is_renderer_initiated_(item.is_renderer_initiated_),
56 is_unsafe_(item.is_unsafe_),
57 cached_display_title_(item.cached_display_title_),
58 facade_delegate_(nullptr) {
61 void NavigationItemImpl::SetFacadeDelegate(
62 scoped_ptr<NavigationItemFacadeDelegate> facade_delegate) {
63 facade_delegate_ = facade_delegate.Pass();
66 NavigationItemFacadeDelegate* NavigationItemImpl::GetFacadeDelegate() const {
67 return facade_delegate_.get();
70 int NavigationItemImpl::GetUniqueID() const {
74 void NavigationItemImpl::SetURL(const GURL& url) {
76 cached_display_title_.clear();
79 const GURL& NavigationItemImpl::GetURL() const {
83 void NavigationItemImpl::SetReferrer(const web::Referrer& referrer) {
87 const web::Referrer& NavigationItemImpl::GetReferrer() const {
91 void NavigationItemImpl::SetVirtualURL(const GURL& url) {
92 virtual_url_ = (url == url_) ? GURL() : url;
93 cached_display_title_.clear();
96 const GURL& NavigationItemImpl::GetVirtualURL() const {
97 return virtual_url_.is_empty() ? url_ : virtual_url_;
100 void NavigationItemImpl::SetTitle(const base::string16& title) {
102 cached_display_title_.clear();
105 const base::string16& NavigationItemImpl::GetTitle() const {
109 void NavigationItemImpl::SetPageID(int page_id) {
113 int32 NavigationItemImpl::GetPageID() const {
117 void NavigationItemImpl::SetPageScrollState(
118 const web::PageScrollState& scroll_state) {
119 page_scroll_state_ = scroll_state;
122 const PageScrollState& NavigationItemImpl::GetPageScrollState() const {
123 return page_scroll_state_;
126 const base::string16& NavigationItemImpl::GetTitleForDisplay(
127 const std::string& languages) const {
128 // Most pages have real titles. Don't even bother caching anything if this is
133 // More complicated cases will use the URLs as the title. This result we will
134 // cache since it's more complicated to compute.
135 if (!cached_display_title_.empty())
136 return cached_display_title_;
138 // Use the virtual URL first if any, and fall back on using the real URL.
139 base::string16 title;
140 if (!virtual_url_.is_empty()) {
141 title = net::FormatUrl(virtual_url_, languages);
142 } else if (!url_.is_empty()) {
143 title = net::FormatUrl(url_, languages);
146 // For file:// URLs use the filename as the title, not the full path.
147 if (url_.SchemeIsFile()) {
148 base::string16::size_type slashpos = title.rfind('/');
149 if (slashpos != base::string16::npos)
150 title = title.substr(slashpos + 1);
153 const int kMaxTitleChars = 4 * 1024;
154 gfx::ElideString(title, kMaxTitleChars, &cached_display_title_);
155 return cached_display_title_;
158 void NavigationItemImpl::SetTransitionType(ui::PageTransition transition_type) {
159 transition_type_ = transition_type;
162 ui::PageTransition NavigationItemImpl::GetTransitionType() const {
163 return transition_type_;
166 const FaviconStatus& NavigationItemImpl::GetFavicon() const {
170 FaviconStatus& NavigationItemImpl::GetFavicon() {
174 const SSLStatus& NavigationItemImpl::GetSSL() const {
178 SSLStatus& NavigationItemImpl::GetSSL() {
182 void NavigationItemImpl::SetTimestamp(base::Time timestamp) {
183 timestamp_ = timestamp;
186 base::Time NavigationItemImpl::GetTimestamp() const {
190 void NavigationItemImpl::ResetForCommit() {
191 // Any state that only matters when a navigation item is pending should be
193 set_is_renderer_initiated(false);
196 void NavigationItemImpl::SetUnsafe(bool is_unsafe) {
197 is_unsafe_ = is_unsafe;
200 bool NavigationItemImpl::IsUnsafe() const {