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 : NavigationEntryImpl(nullptr, -1, GURL(), Referrer(), base::string16(),
41 ui::PAGE_TRANSITION_LINK
, false) {
44 NavigationEntryImpl::NavigationEntryImpl(SiteInstanceImpl
* instance
,
47 const Referrer
& referrer
,
48 const base::string16
& title
,
49 ui::PageTransition transition_type
,
50 bool is_renderer_initiated
)
51 : frame_entry_(instance
, url
, referrer
),
52 unique_id_(GetUniqueIDInConstructor()),
53 bindings_(kInvalidBindings
),
54 page_type_(PAGE_TYPE_NORMAL
),
55 update_virtual_url_with_url_(false),
58 transition_type_(transition_type
),
59 has_post_data_(false),
61 restore_type_(RESTORE_NONE
),
62 is_overriding_user_agent_(false),
64 is_renderer_initiated_(is_renderer_initiated
),
65 should_replace_entry_(false),
66 should_clear_history_list_(false),
67 can_load_local_resources_(false),
68 frame_tree_node_id_(-1) {
71 NavigationEntryImpl::~NavigationEntryImpl() {
74 int NavigationEntryImpl::GetUniqueID() const {
78 PageType
NavigationEntryImpl::GetPageType() const {
82 void NavigationEntryImpl::SetURL(const GURL
& url
) {
83 frame_entry_
.set_url(url
);
84 cached_display_title_
.clear();
87 const GURL
& NavigationEntryImpl::GetURL() const {
88 return frame_entry_
.url();
91 void NavigationEntryImpl::SetBaseURLForDataURL(const GURL
& url
) {
92 base_url_for_data_url_
= url
;
95 const GURL
& NavigationEntryImpl::GetBaseURLForDataURL() const {
96 return base_url_for_data_url_
;
99 void NavigationEntryImpl::SetReferrer(const Referrer
& referrer
) {
100 frame_entry_
.set_referrer(referrer
);
103 const Referrer
& NavigationEntryImpl::GetReferrer() const {
104 return frame_entry_
.referrer();
107 void NavigationEntryImpl::SetVirtualURL(const GURL
& url
) {
108 virtual_url_
= (url
== GetURL()) ? GURL() : url
;
109 cached_display_title_
.clear();
112 const GURL
& NavigationEntryImpl::GetVirtualURL() const {
113 return virtual_url_
.is_empty() ? GetURL() : virtual_url_
;
116 void NavigationEntryImpl::SetTitle(const base::string16
& title
) {
118 cached_display_title_
.clear();
121 const base::string16
& NavigationEntryImpl::GetTitle() const {
125 void NavigationEntryImpl::SetPageState(const PageState
& state
) {
129 const PageState
& NavigationEntryImpl::GetPageState() const {
133 void NavigationEntryImpl::SetPageID(int page_id
) {
137 int32
NavigationEntryImpl::GetPageID() const {
141 void NavigationEntryImpl::set_site_instance(SiteInstanceImpl
* site_instance
) {
142 frame_entry_
.set_site_instance(site_instance
);
145 void NavigationEntryImpl::set_source_site_instance(
146 SiteInstanceImpl
* source_site_instance
) {
147 source_site_instance_
= source_site_instance
;
150 void NavigationEntryImpl::SetBindings(int bindings
) {
151 // Ensure this is set to a valid value, and that it stays the same once set.
152 CHECK_NE(bindings
, kInvalidBindings
);
153 CHECK(bindings_
== kInvalidBindings
|| bindings_
== bindings
);
154 bindings_
= bindings
;
157 const base::string16
& NavigationEntryImpl::GetTitleForDisplay(
158 const std::string
& languages
) const {
159 // Most pages have real titles. Don't even bother caching anything if this is
164 // More complicated cases will use the URLs as the title. This result we will
165 // cache since it's more complicated to compute.
166 if (!cached_display_title_
.empty())
167 return cached_display_title_
;
169 // Use the virtual URL first if any, and fall back on using the real URL.
170 base::string16 title
;
171 if (!virtual_url_
.is_empty()) {
172 title
= net::FormatUrl(virtual_url_
, languages
);
173 } else if (!GetURL().is_empty()) {
174 title
= net::FormatUrl(GetURL(), languages
);
177 // For file:// URLs use the filename as the title, not the full path.
178 if (GetURL().SchemeIsFile()) {
179 base::string16::size_type slashpos
= title
.rfind('/');
180 if (slashpos
!= base::string16::npos
)
181 title
= title
.substr(slashpos
+ 1);
184 gfx::ElideString(title
, kMaxTitleChars
, &cached_display_title_
);
185 return cached_display_title_
;
188 bool NavigationEntryImpl::IsViewSourceMode() const {
189 return virtual_url_
.SchemeIs(kViewSourceScheme
);
192 void NavigationEntryImpl::SetTransitionType(
193 ui::PageTransition transition_type
) {
194 transition_type_
= transition_type
;
197 ui::PageTransition
NavigationEntryImpl::GetTransitionType() const {
198 return transition_type_
;
201 const GURL
& NavigationEntryImpl::GetUserTypedURL() const {
202 return user_typed_url_
;
205 void NavigationEntryImpl::SetHasPostData(bool has_post_data
) {
206 has_post_data_
= has_post_data
;
209 bool NavigationEntryImpl::GetHasPostData() const {
210 return has_post_data_
;
213 void NavigationEntryImpl::SetPostID(int64 post_id
) {
217 int64
NavigationEntryImpl::GetPostID() const {
221 void NavigationEntryImpl::SetBrowserInitiatedPostData(
222 const base::RefCountedMemory
* data
) {
223 browser_initiated_post_data_
= data
;
226 const base::RefCountedMemory
*
227 NavigationEntryImpl::GetBrowserInitiatedPostData() const {
228 return browser_initiated_post_data_
.get();
232 const FaviconStatus
& NavigationEntryImpl::GetFavicon() const {
236 FaviconStatus
& NavigationEntryImpl::GetFavicon() {
240 const SSLStatus
& NavigationEntryImpl::GetSSL() const {
244 SSLStatus
& NavigationEntryImpl::GetSSL() {
248 void NavigationEntryImpl::SetOriginalRequestURL(const GURL
& original_url
) {
249 original_request_url_
= original_url
;
252 const GURL
& NavigationEntryImpl::GetOriginalRequestURL() const {
253 return original_request_url_
;
256 void NavigationEntryImpl::SetIsOverridingUserAgent(bool override
) {
257 is_overriding_user_agent_
= override
;
260 bool NavigationEntryImpl::GetIsOverridingUserAgent() const {
261 return is_overriding_user_agent_
;
264 void NavigationEntryImpl::SetTimestamp(base::Time timestamp
) {
265 timestamp_
= timestamp
;
268 base::Time
NavigationEntryImpl::GetTimestamp() const {
272 void NavigationEntryImpl::SetHttpStatusCode(int http_status_code
) {
273 http_status_code_
= http_status_code
;
276 int NavigationEntryImpl::GetHttpStatusCode() const {
277 return http_status_code_
;
280 void NavigationEntryImpl::SetRedirectChain(
281 const std::vector
<GURL
>& redirect_chain
) {
282 redirect_chain_
= redirect_chain
;
285 const std::vector
<GURL
>& NavigationEntryImpl::GetRedirectChain() const {
286 return redirect_chain_
;
289 bool NavigationEntryImpl::IsRestored() const {
290 return restore_type_
!= RESTORE_NONE
;
293 void NavigationEntryImpl::SetCanLoadLocalResources(bool allow
) {
294 can_load_local_resources_
= allow
;
297 bool NavigationEntryImpl::GetCanLoadLocalResources() const {
298 return can_load_local_resources_
;
301 void NavigationEntryImpl::SetFrameToNavigate(const std::string
& frame_name
) {
302 frame_to_navigate_
= frame_name
;
305 const std::string
& NavigationEntryImpl::GetFrameToNavigate() const {
306 return frame_to_navigate_
;
309 void NavigationEntryImpl::SetExtraData(const std::string
& key
,
310 const base::string16
& data
) {
311 extra_data_
[key
] = data
;
314 bool NavigationEntryImpl::GetExtraData(const std::string
& key
,
315 base::string16
* data
) const {
316 std::map
<std::string
, base::string16
>::const_iterator iter
=
317 extra_data_
.find(key
);
318 if (iter
== extra_data_
.end())
320 *data
= iter
->second
;
324 void NavigationEntryImpl::ClearExtraData(const std::string
& key
) {
325 extra_data_
.erase(key
);
328 void NavigationEntryImpl::ResetForCommit() {
329 // Any state that only matters when a navigation entry is pending should be
331 SetBrowserInitiatedPostData(nullptr);
332 set_is_renderer_initiated(false);
333 set_transferred_global_request_id(GlobalRequestID());
334 set_should_replace_entry(false);
336 set_should_clear_history_list(false);
337 set_frame_tree_node_id(-1);
338 set_source_site_instance(nullptr);
340 #if defined(OS_ANDROID)
341 // Reset the time stamp so that the metrics are not reported if this entry is
342 // loaded again in the future.
343 set_intent_received_timestamp(base::TimeTicks());
347 void NavigationEntryImpl::SetScreenshotPNGData(
348 scoped_refptr
<base::RefCountedBytes
> png_data
) {
349 screenshot_
= png_data
;
350 if (screenshot_
.get())
351 UMA_HISTOGRAM_MEMORY_KB("Overscroll.ScreenshotSize", screenshot_
->size());
354 GURL
NavigationEntryImpl::GetHistoryURLForDataURL() const {
355 return GetBaseURLForDataURL().is_empty() ? GURL() : GetVirtualURL();
358 } // namespace content