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
3 // found in the LICENSE file.
4
5 #include "content/browser/frame_host/navigation_entry_impl.h"
6
7 #include "base/metrics/histogram.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.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/gfx/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).
GetUniqueIDInConstructor()17 static int GetUniqueIDInConstructor() {
18 static int unique_id_counter = 0;
19 return ++unique_id_counter;
20 }
21
22 namespace content {
23
24 int NavigationEntryImpl::kInvalidBindings = -1;
25
Create()26 NavigationEntry* NavigationEntry::Create() {
27 return new NavigationEntryImpl();
28 }
29
Create(const NavigationEntry & copy)30 NavigationEntry* NavigationEntry::Create(const NavigationEntry& copy) {
31 return new NavigationEntryImpl(static_cast<const NavigationEntryImpl&>(copy));
32 }
33
FromNavigationEntry(NavigationEntry * entry)34 NavigationEntryImpl* NavigationEntryImpl::FromNavigationEntry(
35 NavigationEntry* entry) {
36 return static_cast<NavigationEntryImpl*>(entry);
37 }
38
NavigationEntryImpl()39 NavigationEntryImpl::NavigationEntryImpl()
40 : unique_id_(GetUniqueIDInConstructor()),
41 site_instance_(NULL),
42 bindings_(kInvalidBindings),
43 page_type_(PAGE_TYPE_NORMAL),
44 update_virtual_url_with_url_(false),
45 page_id_(-1),
46 transition_type_(PAGE_TRANSITION_LINK),
47 has_post_data_(false),
48 post_id_(-1),
49 restore_type_(RESTORE_NONE),
50 is_overriding_user_agent_(false),
51 http_status_code_(0),
52 is_renderer_initiated_(false),
53 should_replace_entry_(false),
54 should_clear_history_list_(false),
55 can_load_local_resources_(false),
56 frame_tree_node_id_(-1) {
57 }
58
NavigationEntryImpl(SiteInstanceImpl * instance,int page_id,const GURL & url,const Referrer & referrer,const base::string16 & title,PageTransition transition_type,bool is_renderer_initiated)59 NavigationEntryImpl::NavigationEntryImpl(SiteInstanceImpl* instance,
60 int page_id,
61 const GURL& url,
62 const Referrer& referrer,
63 const base::string16& title,
64 PageTransition transition_type,
65 bool is_renderer_initiated)
66 : unique_id_(GetUniqueIDInConstructor()),
67 site_instance_(instance),
68 bindings_(kInvalidBindings),
69 page_type_(PAGE_TYPE_NORMAL),
70 url_(url),
71 referrer_(referrer),
72 update_virtual_url_with_url_(false),
73 title_(title),
74 page_id_(page_id),
75 transition_type_(transition_type),
76 has_post_data_(false),
77 post_id_(-1),
78 restore_type_(RESTORE_NONE),
79 is_overriding_user_agent_(false),
80 http_status_code_(0),
81 is_renderer_initiated_(is_renderer_initiated),
82 should_replace_entry_(false),
83 should_clear_history_list_(false),
84 can_load_local_resources_(false),
85 frame_tree_node_id_(-1) {
86 }
87
~NavigationEntryImpl()88 NavigationEntryImpl::~NavigationEntryImpl() {
89 }
90
GetUniqueID() const91 int NavigationEntryImpl::GetUniqueID() const {
92 return unique_id_;
93 }
94
GetPageType() const95 PageType NavigationEntryImpl::GetPageType() const {
96 return page_type_;
97 }
98
SetURL(const GURL & url)99 void NavigationEntryImpl::SetURL(const GURL& url) {
100 url_ = url;
101 cached_display_title_.clear();
102 }
103
GetURL() const104 const GURL& NavigationEntryImpl::GetURL() const {
105 return url_;
106 }
107
SetBaseURLForDataURL(const GURL & url)108 void NavigationEntryImpl::SetBaseURLForDataURL(const GURL& url) {
109 base_url_for_data_url_ = url;
110 }
111
GetBaseURLForDataURL() const112 const GURL& NavigationEntryImpl::GetBaseURLForDataURL() const {
113 return base_url_for_data_url_;
114 }
115
SetReferrer(const Referrer & referrer)116 void NavigationEntryImpl::SetReferrer(const Referrer& referrer) {
117 referrer_ = referrer;
118 }
119
GetReferrer() const120 const Referrer& NavigationEntryImpl::GetReferrer() const {
121 return referrer_;
122 }
123
SetVirtualURL(const GURL & url)124 void NavigationEntryImpl::SetVirtualURL(const GURL& url) {
125 virtual_url_ = (url == url_) ? GURL() : url;
126 cached_display_title_.clear();
127 }
128
GetVirtualURL() const129 const GURL& NavigationEntryImpl::GetVirtualURL() const {
130 return virtual_url_.is_empty() ? url_ : virtual_url_;
131 }
132
SetTitle(const base::string16 & title)133 void NavigationEntryImpl::SetTitle(const base::string16& title) {
134 title_ = title;
135 cached_display_title_.clear();
136 }
137
GetTitle() const138 const base::string16& NavigationEntryImpl::GetTitle() const {
139 return title_;
140 }
141
SetPageState(const PageState & state)142 void NavigationEntryImpl::SetPageState(const PageState& state) {
143 page_state_ = state;
144 }
145
GetPageState() const146 const PageState& NavigationEntryImpl::GetPageState() const {
147 return page_state_;
148 }
149
SetPageID(int page_id)150 void NavigationEntryImpl::SetPageID(int page_id) {
151 page_id_ = page_id;
152 }
153
GetPageID() const154 int32 NavigationEntryImpl::GetPageID() const {
155 return page_id_;
156 }
157
set_site_instance(SiteInstanceImpl * site_instance)158 void NavigationEntryImpl::set_site_instance(SiteInstanceImpl* site_instance) {
159 site_instance_ = site_instance;
160 }
161
SetBindings(int bindings)162 void NavigationEntryImpl::SetBindings(int bindings) {
163 // Ensure this is set to a valid value, and that it stays the same once set.
164 CHECK_NE(bindings, kInvalidBindings);
165 CHECK(bindings_ == kInvalidBindings || bindings_ == bindings);
166 bindings_ = bindings;
167 }
168
GetTitleForDisplay(const std::string & languages) const169 const base::string16& NavigationEntryImpl::GetTitleForDisplay(
170 const std::string& languages) const {
171 // Most pages have real titles. Don't even bother caching anything if this is
172 // the case.
173 if (!title_.empty())
174 return title_;
175
176 // More complicated cases will use the URLs as the title. This result we will
177 // cache since it's more complicated to compute.
178 if (!cached_display_title_.empty())
179 return cached_display_title_;
180
181 // Use the virtual URL first if any, and fall back on using the real URL.
182 base::string16 title;
183 if (!virtual_url_.is_empty()) {
184 title = net::FormatUrl(virtual_url_, languages);
185 } else if (!url_.is_empty()) {
186 title = net::FormatUrl(url_, languages);
187 }
188
189 // For file:// URLs use the filename as the title, not the full path.
190 if (url_.SchemeIsFile()) {
191 base::string16::size_type slashpos = title.rfind('/');
192 if (slashpos != base::string16::npos)
193 title = title.substr(slashpos + 1);
194 }
195
196 gfx::ElideString(title, kMaxTitleChars, &cached_display_title_);
197 return cached_display_title_;
198 }
199
IsViewSourceMode() const200 bool NavigationEntryImpl::IsViewSourceMode() const {
201 return virtual_url_.SchemeIs(kViewSourceScheme);
202 }
203
SetTransitionType(PageTransition transition_type)204 void NavigationEntryImpl::SetTransitionType(
205 PageTransition transition_type) {
206 transition_type_ = transition_type;
207 }
208
GetTransitionType() const209 PageTransition NavigationEntryImpl::GetTransitionType() const {
210 return transition_type_;
211 }
212
GetUserTypedURL() const213 const GURL& NavigationEntryImpl::GetUserTypedURL() const {
214 return user_typed_url_;
215 }
216
SetHasPostData(bool has_post_data)217 void NavigationEntryImpl::SetHasPostData(bool has_post_data) {
218 has_post_data_ = has_post_data;
219 }
220
GetHasPostData() const221 bool NavigationEntryImpl::GetHasPostData() const {
222 return has_post_data_;
223 }
224
SetPostID(int64 post_id)225 void NavigationEntryImpl::SetPostID(int64 post_id) {
226 post_id_ = post_id;
227 }
228
GetPostID() const229 int64 NavigationEntryImpl::GetPostID() const {
230 return post_id_;
231 }
232
SetBrowserInitiatedPostData(const base::RefCountedMemory * data)233 void NavigationEntryImpl::SetBrowserInitiatedPostData(
234 const base::RefCountedMemory* data) {
235 browser_initiated_post_data_ = data;
236 }
237
238 const base::RefCountedMemory*
GetBrowserInitiatedPostData() const239 NavigationEntryImpl::GetBrowserInitiatedPostData() const {
240 return browser_initiated_post_data_.get();
241 }
242
243
GetFavicon() const244 const FaviconStatus& NavigationEntryImpl::GetFavicon() const {
245 return favicon_;
246 }
247
GetFavicon()248 FaviconStatus& NavigationEntryImpl::GetFavicon() {
249 return favicon_;
250 }
251
GetSSL() const252 const SSLStatus& NavigationEntryImpl::GetSSL() const {
253 return ssl_;
254 }
255
GetSSL()256 SSLStatus& NavigationEntryImpl::GetSSL() {
257 return ssl_;
258 }
259
SetOriginalRequestURL(const GURL & original_url)260 void NavigationEntryImpl::SetOriginalRequestURL(const GURL& original_url) {
261 original_request_url_ = original_url;
262 }
263
GetOriginalRequestURL() const264 const GURL& NavigationEntryImpl::GetOriginalRequestURL() const {
265 return original_request_url_;
266 }
267
SetIsOverridingUserAgent(bool override)268 void NavigationEntryImpl::SetIsOverridingUserAgent(bool override) {
269 is_overriding_user_agent_ = override;
270 }
271
GetIsOverridingUserAgent() const272 bool NavigationEntryImpl::GetIsOverridingUserAgent() const {
273 return is_overriding_user_agent_;
274 }
275
SetTimestamp(base::Time timestamp)276 void NavigationEntryImpl::SetTimestamp(base::Time timestamp) {
277 timestamp_ = timestamp;
278 }
279
GetTimestamp() const280 base::Time NavigationEntryImpl::GetTimestamp() const {
281 return timestamp_;
282 }
283
SetHttpStatusCode(int http_status_code)284 void NavigationEntryImpl::SetHttpStatusCode(int http_status_code) {
285 http_status_code_ = http_status_code;
286 }
287
GetHttpStatusCode() const288 int NavigationEntryImpl::GetHttpStatusCode() const {
289 return http_status_code_;
290 }
291
SetCanLoadLocalResources(bool allow)292 void NavigationEntryImpl::SetCanLoadLocalResources(bool allow) {
293 can_load_local_resources_ = allow;
294 }
295
GetCanLoadLocalResources() const296 bool NavigationEntryImpl::GetCanLoadLocalResources() const {
297 return can_load_local_resources_;
298 }
299
SetFrameToNavigate(const std::string & frame_name)300 void NavigationEntryImpl::SetFrameToNavigate(const std::string& frame_name) {
301 frame_to_navigate_ = frame_name;
302 }
303
GetFrameToNavigate() const304 const std::string& NavigationEntryImpl::GetFrameToNavigate() const {
305 return frame_to_navigate_;
306 }
307
SetExtraData(const std::string & key,const base::string16 & data)308 void NavigationEntryImpl::SetExtraData(const std::string& key,
309 const base::string16& data) {
310 extra_data_[key] = data;
311 }
312
GetExtraData(const std::string & key,base::string16 * data) const313 bool NavigationEntryImpl::GetExtraData(const std::string& key,
314 base::string16* data) const {
315 std::map<std::string, base::string16>::const_iterator iter =
316 extra_data_.find(key);
317 if (iter == extra_data_.end())
318 return false;
319 *data = iter->second;
320 return true;
321 }
322
ClearExtraData(const std::string & key)323 void NavigationEntryImpl::ClearExtraData(const std::string& key) {
324 extra_data_.erase(key);
325 }
326
ResetForCommit()327 void NavigationEntryImpl::ResetForCommit() {
328 // Any state that only matters when a navigation entry is pending should be
329 // cleared here.
330 SetBrowserInitiatedPostData(NULL);
331 set_is_renderer_initiated(false);
332 set_transferred_global_request_id(GlobalRequestID());
333 set_should_replace_entry(false);
334 redirect_chain_.clear();
335 set_should_clear_history_list(false);
336 set_frame_tree_node_id(-1);
337 }
338
SetScreenshotPNGData(scoped_refptr<base::RefCountedBytes> png_data)339 void NavigationEntryImpl::SetScreenshotPNGData(
340 scoped_refptr<base::RefCountedBytes> png_data) {
341 screenshot_ = png_data;
342 if (screenshot_.get())
343 UMA_HISTOGRAM_MEMORY_KB("Overscroll.ScreenshotSize", screenshot_->size());
344 }
345
346 } // namespace content
347