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.
5 #include "content/browser/frame_host/navigation_entry_impl.h"
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"
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).
17 static int GetUniqueIDInConstructor() {
18 static int unique_id_counter
= 0;
19 return ++unique_id_counter
;
24 int NavigationEntryImpl::kInvalidBindings
= -1;
26 NavigationEntry
* NavigationEntry::Create() {
27 return new NavigationEntryImpl();
30 NavigationEntry
* NavigationEntry::Create(const NavigationEntry
& copy
) {
31 return new NavigationEntryImpl(static_cast<const NavigationEntryImpl
&>(copy
));
34 NavigationEntryImpl
* NavigationEntryImpl::FromNavigationEntry(
35 NavigationEntry
* entry
) {
36 return static_cast<NavigationEntryImpl
*>(entry
);
39 NavigationEntryImpl::NavigationEntryImpl()
40 : unique_id_(GetUniqueIDInConstructor()),
42 bindings_(kInvalidBindings
),
43 page_type_(PAGE_TYPE_NORMAL
),
44 update_virtual_url_with_url_(false),
46 transition_type_(PAGE_TRANSITION_LINK
),
47 has_post_data_(false),
49 restore_type_(RESTORE_NONE
),
50 is_overriding_user_agent_(false),
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) {
59 NavigationEntryImpl::NavigationEntryImpl(SiteInstanceImpl
* instance
,
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
),
72 update_virtual_url_with_url_(false),
75 transition_type_(transition_type
),
76 has_post_data_(false),
78 restore_type_(RESTORE_NONE
),
79 is_overriding_user_agent_(false),
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) {
88 NavigationEntryImpl::~NavigationEntryImpl() {
91 int NavigationEntryImpl::GetUniqueID() const {
95 PageType
NavigationEntryImpl::GetPageType() const {
99 void NavigationEntryImpl::SetURL(const GURL
& url
) {
101 cached_display_title_
.clear();
104 const GURL
& NavigationEntryImpl::GetURL() const {
108 void NavigationEntryImpl::SetBaseURLForDataURL(const GURL
& url
) {
109 base_url_for_data_url_
= url
;
112 const GURL
& NavigationEntryImpl::GetBaseURLForDataURL() const {
113 return base_url_for_data_url_
;
116 void NavigationEntryImpl::SetReferrer(const Referrer
& referrer
) {
117 referrer_
= referrer
;
120 const Referrer
& NavigationEntryImpl::GetReferrer() const {
124 void NavigationEntryImpl::SetVirtualURL(const GURL
& url
) {
125 virtual_url_
= (url
== url_
) ? GURL() : url
;
126 cached_display_title_
.clear();
129 const GURL
& NavigationEntryImpl::GetVirtualURL() const {
130 return virtual_url_
.is_empty() ? url_
: virtual_url_
;
133 void NavigationEntryImpl::SetTitle(const base::string16
& title
) {
135 cached_display_title_
.clear();
138 const base::string16
& NavigationEntryImpl::GetTitle() const {
142 void NavigationEntryImpl::SetPageState(const PageState
& state
) {
146 const PageState
& NavigationEntryImpl::GetPageState() const {
150 void NavigationEntryImpl::SetPageID(int page_id
) {
154 int32
NavigationEntryImpl::GetPageID() const {
158 void NavigationEntryImpl::set_site_instance(SiteInstanceImpl
* site_instance
) {
159 site_instance_
= site_instance
;
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
;
169 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
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_
;
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
);
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);
196 gfx::ElideString(title
, kMaxTitleChars
, &cached_display_title_
);
197 return cached_display_title_
;
200 bool NavigationEntryImpl::IsViewSourceMode() const {
201 return virtual_url_
.SchemeIs(kViewSourceScheme
);
204 void NavigationEntryImpl::SetTransitionType(
205 PageTransition transition_type
) {
206 transition_type_
= transition_type
;
209 PageTransition
NavigationEntryImpl::GetTransitionType() const {
210 return transition_type_
;
213 const GURL
& NavigationEntryImpl::GetUserTypedURL() const {
214 return user_typed_url_
;
217 void NavigationEntryImpl::SetHasPostData(bool has_post_data
) {
218 has_post_data_
= has_post_data
;
221 bool NavigationEntryImpl::GetHasPostData() const {
222 return has_post_data_
;
225 void NavigationEntryImpl::SetPostID(int64 post_id
) {
229 int64
NavigationEntryImpl::GetPostID() const {
233 void NavigationEntryImpl::SetBrowserInitiatedPostData(
234 const base::RefCountedMemory
* data
) {
235 browser_initiated_post_data_
= data
;
238 const base::RefCountedMemory
*
239 NavigationEntryImpl::GetBrowserInitiatedPostData() const {
240 return browser_initiated_post_data_
.get();
244 const FaviconStatus
& NavigationEntryImpl::GetFavicon() const {
248 FaviconStatus
& NavigationEntryImpl::GetFavicon() {
252 const SSLStatus
& NavigationEntryImpl::GetSSL() const {
256 SSLStatus
& NavigationEntryImpl::GetSSL() {
260 void NavigationEntryImpl::SetOriginalRequestURL(const GURL
& original_url
) {
261 original_request_url_
= original_url
;
264 const GURL
& NavigationEntryImpl::GetOriginalRequestURL() const {
265 return original_request_url_
;
268 void NavigationEntryImpl::SetIsOverridingUserAgent(bool override
) {
269 is_overriding_user_agent_
= override
;
272 bool NavigationEntryImpl::GetIsOverridingUserAgent() const {
273 return is_overriding_user_agent_
;
276 void NavigationEntryImpl::SetTimestamp(base::Time timestamp
) {
277 timestamp_
= timestamp
;
280 base::Time
NavigationEntryImpl::GetTimestamp() const {
284 void NavigationEntryImpl::SetHttpStatusCode(int http_status_code
) {
285 http_status_code_
= http_status_code
;
288 int NavigationEntryImpl::GetHttpStatusCode() const {
289 return http_status_code_
;
292 void NavigationEntryImpl::SetRedirectChain(
293 const std::vector
<GURL
>& redirect_chain
) {
294 redirect_chain_
= redirect_chain
;
297 const std::vector
<GURL
>& NavigationEntryImpl::GetRedirectChain() const {
298 return redirect_chain_
;
301 bool NavigationEntryImpl::IsRestored() const {
302 return restore_type_
!= RESTORE_NONE
;
305 void NavigationEntryImpl::SetCanLoadLocalResources(bool allow
) {
306 can_load_local_resources_
= allow
;
309 bool NavigationEntryImpl::GetCanLoadLocalResources() const {
310 return can_load_local_resources_
;
313 void NavigationEntryImpl::SetFrameToNavigate(const std::string
& frame_name
) {
314 frame_to_navigate_
= frame_name
;
317 const std::string
& NavigationEntryImpl::GetFrameToNavigate() const {
318 return frame_to_navigate_
;
321 void NavigationEntryImpl::SetExtraData(const std::string
& key
,
322 const base::string16
& data
) {
323 extra_data_
[key
] = data
;
326 bool NavigationEntryImpl::GetExtraData(const std::string
& key
,
327 base::string16
* data
) const {
328 std::map
<std::string
, base::string16
>::const_iterator iter
=
329 extra_data_
.find(key
);
330 if (iter
== extra_data_
.end())
332 *data
= iter
->second
;
336 void NavigationEntryImpl::ClearExtraData(const std::string
& key
) {
337 extra_data_
.erase(key
);
340 void NavigationEntryImpl::ResetForCommit() {
341 // Any state that only matters when a navigation entry is pending should be
343 SetBrowserInitiatedPostData(NULL
);
344 set_is_renderer_initiated(false);
345 set_transferred_global_request_id(GlobalRequestID());
346 set_should_replace_entry(false);
348 set_should_clear_history_list(false);
349 set_frame_tree_node_id(-1);
352 void NavigationEntryImpl::SetScreenshotPNGData(
353 scoped_refptr
<base::RefCountedBytes
> png_data
) {
354 screenshot_
= png_data
;
355 if (screenshot_
.get())
356 UMA_HISTOGRAM_MEMORY_KB("Overscroll.ScreenshotSize", screenshot_
->size());
359 } // namespace content