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) {
37 NavigationItemImpl::~NavigationItemImpl() {
40 int NavigationItemImpl::GetUniqueID() const {
44 void NavigationItemImpl::SetURL(const GURL& url) {
46 cached_display_title_.clear();
49 const GURL& NavigationItemImpl::GetURL() const {
53 void NavigationItemImpl::SetReferrer(const web::Referrer& referrer) {
57 const web::Referrer& NavigationItemImpl::GetReferrer() const {
61 void NavigationItemImpl::SetVirtualURL(const GURL& url) {
62 virtual_url_ = (url == url_) ? GURL() : url;
63 cached_display_title_.clear();
66 const GURL& NavigationItemImpl::GetVirtualURL() const {
67 return virtual_url_.is_empty() ? url_ : virtual_url_;
70 void NavigationItemImpl::SetTitle(const base::string16& title) {
72 cached_display_title_.clear();
75 const base::string16& NavigationItemImpl::GetTitle() const {
79 void NavigationItemImpl::SetPageID(int page_id) {
83 int32 NavigationItemImpl::GetPageID() const {
87 const base::string16& NavigationItemImpl::GetTitleForDisplay(
88 const std::string& languages) const {
89 // Most pages have real titles. Don't even bother caching anything if this is
94 // More complicated cases will use the URLs as the title. This result we will
95 // cache since it's more complicated to compute.
96 if (!cached_display_title_.empty())
97 return cached_display_title_;
99 // Use the virtual URL first if any, and fall back on using the real URL.
100 base::string16 title;
101 if (!virtual_url_.is_empty()) {
102 title = net::FormatUrl(virtual_url_, languages);
103 } else if (!url_.is_empty()) {
104 title = net::FormatUrl(url_, languages);
107 // For file:// URLs use the filename as the title, not the full path.
108 if (url_.SchemeIsFile()) {
109 base::string16::size_type slashpos = title.rfind('/');
110 if (slashpos != base::string16::npos)
111 title = title.substr(slashpos + 1);
114 const int kMaxTitleChars = 4 * 1024;
115 gfx::ElideString(title, kMaxTitleChars, &cached_display_title_);
116 return cached_display_title_;
119 void NavigationItemImpl::SetTransitionType(ui::PageTransition transition_type) {
120 transition_type_ = transition_type;
123 ui::PageTransition NavigationItemImpl::GetTransitionType() const {
124 return transition_type_;
127 const FaviconStatus& NavigationItemImpl::GetFavicon() const {
131 FaviconStatus& NavigationItemImpl::GetFavicon() {
135 const SSLStatus& NavigationItemImpl::GetSSL() const {
139 SSLStatus& NavigationItemImpl::GetSSL() {
143 void NavigationItemImpl::SetTimestamp(base::Time timestamp) {
144 timestamp_ = timestamp;
147 base::Time NavigationItemImpl::GetTimestamp() const {