blob: e0b924cf4d6bd2d63141c0a2dd9bb98ff930d312 [file] [log] [blame]
[email protected]10f417c52011-12-28 21:04:231// Copyright (c) 2011 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.
4
5#include "content/browser/tab_contents/navigation_entry_impl.h"
6
7#include "base/string_util.h"
8#include "base/utf_string_conversions.h"
9#include "content/browser/site_instance.h"
10#include "content/public/common/content_constants.h"
11#include "content/public/common/url_constants.h"
12#include "net/base/net_util.h"
13#include "ui/base/text/text_elider.h"
14
15// Use this to get a new unique ID for a NavigationEntry during construction.
16// The returned ID is guaranteed to be nonzero (which is the "no ID" indicator).
17static int GetUniqueIDInConstructor() {
18 static int unique_id_counter = 0;
19 return ++unique_id_counter;
20}
21
22namespace content {
23
24NavigationEntry* NavigationEntry::Create() {
25 return new NavigationEntryImpl();
26}
27
28NavigationEntry* NavigationEntry::Create(const NavigationEntry& copy) {
29 return new NavigationEntryImpl(static_cast<const NavigationEntryImpl&>(copy));
30}
31
32NavigationEntryImpl* NavigationEntryImpl::FromNavigationEntry(
33 NavigationEntry* entry) {
34 return static_cast<NavigationEntryImpl*>(entry);
35}
36
37NavigationEntryImpl::NavigationEntryImpl()
38 : unique_id_(GetUniqueIDInConstructor()),
39 site_instance_(NULL),
40 page_type_(PAGE_TYPE_NORMAL),
41 update_virtual_url_with_url_(false),
42 page_id_(-1),
43 transition_type_(PAGE_TRANSITION_LINK),
44 has_post_data_(false),
45 restore_type_(RESTORE_NONE),
46 is_renderer_initiated_(false) {
47}
48
49NavigationEntryImpl::NavigationEntryImpl(SiteInstance* instance,
50 int page_id,
51 const GURL& url,
52 const Referrer& referrer,
53 const string16& title,
54 PageTransition transition_type,
55 bool is_renderer_initiated)
56 : unique_id_(GetUniqueIDInConstructor()),
57 site_instance_(instance),
58 page_type_(PAGE_TYPE_NORMAL),
59 url_(url),
60 referrer_(referrer),
61 update_virtual_url_with_url_(false),
62 title_(title),
63 page_id_(page_id),
64 transition_type_(transition_type),
65 has_post_data_(false),
66 restore_type_(RESTORE_NONE),
67 is_renderer_initiated_(is_renderer_initiated) {
68}
69
70NavigationEntryImpl::~NavigationEntryImpl() {
71}
72
73int NavigationEntryImpl::GetUniqueID() const {
74 return unique_id_;
75}
76
77PageType NavigationEntryImpl::GetPageType() const {
78 return page_type_;
79}
80
81void NavigationEntryImpl::SetURL(const GURL& url) {
82 url_ = url;
83 cached_display_title_.clear();
84}
85
86const GURL& NavigationEntryImpl::GetURL() const {
87 return url_;
88}
89
90void NavigationEntryImpl::SetReferrer(const Referrer& referrer) {
91 referrer_ = referrer;
92}
93
94const Referrer& NavigationEntryImpl::GetReferrer() const {
95 return referrer_;
96}
97
98void NavigationEntryImpl::SetVirtualURL(const GURL& url) {
99 virtual_url_ = (url == url_) ? GURL() : url;
100 cached_display_title_.clear();
101}
102
103const GURL& NavigationEntryImpl::GetVirtualURL() const {
104 return virtual_url_.is_empty() ? url_ : virtual_url_;
105}
106
107void NavigationEntryImpl::SetTitle(const string16& title) {
108 title_ = title;
109 cached_display_title_.clear();
110}
111
112const string16& NavigationEntryImpl::GetTitle() const {
113 return title_;
114}
115
116void NavigationEntryImpl::SetContentState(const std::string& state) {
117 content_state_ = state;
118}
119
120const std::string& NavigationEntryImpl::GetContentState() const {
121 return content_state_;
122}
123
124void NavigationEntryImpl::SetPageID(int page_id) {
125 page_id_ = page_id;
126}
127
128int32 NavigationEntryImpl::GetPageID() const {
129 return page_id_;
130}
131
132void NavigationEntryImpl::set_site_instance(SiteInstance* site_instance) {
133 site_instance_ = site_instance;
134}
135
136const string16& NavigationEntryImpl::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_;
142
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_;
147
148 // Use the virtual URL first if any, and fall back on using the real URL.
149 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 }
155
156 // For file:// URLs use the filename as the title, not the full path.
157 if (url_.SchemeIsFile()) {
158 string16::size_type slashpos = title.rfind('/');
159 if (slashpos != string16::npos)
160 title = title.substr(slashpos + 1);
161 }
162
163 ui::ElideString(title, kMaxTitleChars, &cached_display_title_);
164 return cached_display_title_;
165}
166
167bool NavigationEntryImpl::IsViewSourceMode() const {
168 return virtual_url_.SchemeIs(chrome::kViewSourceScheme);
169}
170
171void NavigationEntryImpl::SetTransitionType(
172 PageTransition transition_type) {
173 transition_type_ = transition_type;
174}
175
176PageTransition NavigationEntryImpl::GetTransitionType() const {
177 return transition_type_;
178}
179
180const GURL& NavigationEntryImpl::GetUserTypedURL() const {
181 return user_typed_url_;
182}
183
184void NavigationEntryImpl::SetHasPostData(bool has_post_data) {
185 has_post_data_ = has_post_data;
186}
187
188bool NavigationEntryImpl::GetHasPostData() const {
189 return has_post_data_;
190}
191
192const FaviconStatus& NavigationEntryImpl::GetFavicon() const {
193 return favicon_;
194}
195
196FaviconStatus& NavigationEntryImpl::GetFavicon() {
197 return favicon_;
198}
199
200const SSLStatus& NavigationEntryImpl::GetSSL() const {
201 return ssl_;
202}
203
204SSLStatus& NavigationEntryImpl::GetSSL() {
205 return ssl_;
206}
207
208} // namespace content