[email protected] | bcd90448 | 2012-02-01 01:54:22 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f9e4dae | 2012-04-10 21:26:37 | [diff] [blame] | 5 | #include "content/browser/web_contents/navigation_entry_impl.h" |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 6 | |
| 7 | #include "base/string_util.h" |
| 8 | #include "base/utf_string_conversions.h" |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 9 | #include "content/public/common/content_constants.h" |
| 10 | #include "content/public/common/url_constants.h" |
| 11 | #include "net/base/net_util.h" |
| 12 | #include "ui/base/text/text_elider.h" |
| 13 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 14 | using content::SiteInstance; |
| 15 | |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 16 | // Use this to get a new unique ID for a NavigationEntry during construction. |
| 17 | // The returned ID is guaranteed to be nonzero (which is the "no ID" indicator). |
| 18 | static int GetUniqueIDInConstructor() { |
| 19 | static int unique_id_counter = 0; |
| 20 | return ++unique_id_counter; |
| 21 | } |
| 22 | |
| 23 | namespace content { |
| 24 | |
| 25 | NavigationEntry* NavigationEntry::Create() { |
| 26 | return new NavigationEntryImpl(); |
| 27 | } |
| 28 | |
| 29 | NavigationEntry* NavigationEntry::Create(const NavigationEntry& copy) { |
| 30 | return new NavigationEntryImpl(static_cast<const NavigationEntryImpl&>(copy)); |
| 31 | } |
| 32 | |
| 33 | NavigationEntryImpl* NavigationEntryImpl::FromNavigationEntry( |
| 34 | NavigationEntry* entry) { |
| 35 | return static_cast<NavigationEntryImpl*>(entry); |
| 36 | } |
| 37 | |
| 38 | NavigationEntryImpl::NavigationEntryImpl() |
| 39 | : unique_id_(GetUniqueIDInConstructor()), |
| 40 | site_instance_(NULL), |
| 41 | page_type_(PAGE_TYPE_NORMAL), |
| 42 | update_virtual_url_with_url_(false), |
| 43 | page_id_(-1), |
| 44 | transition_type_(PAGE_TRANSITION_LINK), |
| 45 | has_post_data_(false), |
[email protected] | 86cd947 | 2012-02-03 19:51:05 | [diff] [blame] | 46 | post_id_(-1), |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 47 | restore_type_(RESTORE_NONE), |
[email protected] | 86ef6a39 | 2012-05-11 22:03:11 | [diff] [blame^] | 48 | is_overriding_user_agent_(false), |
[email protected] | bcd90448 | 2012-02-01 01:54:22 | [diff] [blame] | 49 | is_renderer_initiated_(false), |
| 50 | is_cross_site_reload_(false) { |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 53 | NavigationEntryImpl::NavigationEntryImpl(SiteInstanceImpl* instance, |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 54 | int page_id, |
| 55 | const GURL& url, |
| 56 | const Referrer& referrer, |
| 57 | const string16& title, |
| 58 | PageTransition transition_type, |
| 59 | bool is_renderer_initiated) |
| 60 | : unique_id_(GetUniqueIDInConstructor()), |
| 61 | site_instance_(instance), |
| 62 | page_type_(PAGE_TYPE_NORMAL), |
| 63 | url_(url), |
| 64 | referrer_(referrer), |
| 65 | update_virtual_url_with_url_(false), |
| 66 | title_(title), |
| 67 | page_id_(page_id), |
| 68 | transition_type_(transition_type), |
| 69 | has_post_data_(false), |
[email protected] | 86cd947 | 2012-02-03 19:51:05 | [diff] [blame] | 70 | post_id_(-1), |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 71 | restore_type_(RESTORE_NONE), |
[email protected] | 86ef6a39 | 2012-05-11 22:03:11 | [diff] [blame^] | 72 | is_overriding_user_agent_(false), |
[email protected] | bcd90448 | 2012-02-01 01:54:22 | [diff] [blame] | 73 | is_renderer_initiated_(is_renderer_initiated), |
| 74 | is_cross_site_reload_(false) { |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | NavigationEntryImpl::~NavigationEntryImpl() { |
| 78 | } |
| 79 | |
| 80 | int NavigationEntryImpl::GetUniqueID() const { |
| 81 | return unique_id_; |
| 82 | } |
| 83 | |
| 84 | PageType NavigationEntryImpl::GetPageType() const { |
| 85 | return page_type_; |
| 86 | } |
| 87 | |
| 88 | void NavigationEntryImpl::SetURL(const GURL& url) { |
| 89 | url_ = url; |
| 90 | cached_display_title_.clear(); |
| 91 | } |
| 92 | |
| 93 | const GURL& NavigationEntryImpl::GetURL() const { |
| 94 | return url_; |
| 95 | } |
| 96 | |
| 97 | void NavigationEntryImpl::SetReferrer(const Referrer& referrer) { |
| 98 | referrer_ = referrer; |
| 99 | } |
| 100 | |
| 101 | const Referrer& NavigationEntryImpl::GetReferrer() const { |
| 102 | return referrer_; |
| 103 | } |
| 104 | |
| 105 | void NavigationEntryImpl::SetVirtualURL(const GURL& url) { |
| 106 | virtual_url_ = (url == url_) ? GURL() : url; |
| 107 | cached_display_title_.clear(); |
| 108 | } |
| 109 | |
| 110 | const GURL& NavigationEntryImpl::GetVirtualURL() const { |
| 111 | return virtual_url_.is_empty() ? url_ : virtual_url_; |
| 112 | } |
| 113 | |
| 114 | void NavigationEntryImpl::SetTitle(const string16& title) { |
| 115 | title_ = title; |
| 116 | cached_display_title_.clear(); |
| 117 | } |
| 118 | |
| 119 | const string16& NavigationEntryImpl::GetTitle() const { |
| 120 | return title_; |
| 121 | } |
| 122 | |
| 123 | void NavigationEntryImpl::SetContentState(const std::string& state) { |
| 124 | content_state_ = state; |
| 125 | } |
| 126 | |
| 127 | const std::string& NavigationEntryImpl::GetContentState() const { |
| 128 | return content_state_; |
| 129 | } |
| 130 | |
| 131 | void NavigationEntryImpl::SetPageID(int page_id) { |
| 132 | page_id_ = page_id; |
| 133 | } |
| 134 | |
| 135 | int32 NavigationEntryImpl::GetPageID() const { |
| 136 | return page_id_; |
| 137 | } |
| 138 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 139 | void NavigationEntryImpl::set_site_instance(SiteInstanceImpl* site_instance) { |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 140 | site_instance_ = site_instance; |
| 141 | } |
| 142 | |
| 143 | const string16& NavigationEntryImpl::GetTitleForDisplay( |
| 144 | const std::string& languages) const { |
| 145 | // Most pages have real titles. Don't even bother caching anything if this is |
| 146 | // the case. |
| 147 | if (!title_.empty()) |
| 148 | return title_; |
| 149 | |
| 150 | // More complicated cases will use the URLs as the title. This result we will |
| 151 | // cache since it's more complicated to compute. |
| 152 | if (!cached_display_title_.empty()) |
| 153 | return cached_display_title_; |
| 154 | |
| 155 | // Use the virtual URL first if any, and fall back on using the real URL. |
| 156 | string16 title; |
| 157 | if (!virtual_url_.is_empty()) { |
| 158 | title = net::FormatUrl(virtual_url_, languages); |
| 159 | } else if (!url_.is_empty()) { |
| 160 | title = net::FormatUrl(url_, languages); |
| 161 | } |
| 162 | |
| 163 | // For file:// URLs use the filename as the title, not the full path. |
| 164 | if (url_.SchemeIsFile()) { |
| 165 | string16::size_type slashpos = title.rfind('/'); |
| 166 | if (slashpos != string16::npos) |
| 167 | title = title.substr(slashpos + 1); |
| 168 | } |
| 169 | |
| 170 | ui::ElideString(title, kMaxTitleChars, &cached_display_title_); |
| 171 | return cached_display_title_; |
| 172 | } |
| 173 | |
| 174 | bool NavigationEntryImpl::IsViewSourceMode() const { |
| 175 | return virtual_url_.SchemeIs(chrome::kViewSourceScheme); |
| 176 | } |
| 177 | |
| 178 | void NavigationEntryImpl::SetTransitionType( |
| 179 | PageTransition transition_type) { |
| 180 | transition_type_ = transition_type; |
| 181 | } |
| 182 | |
| 183 | PageTransition NavigationEntryImpl::GetTransitionType() const { |
| 184 | return transition_type_; |
| 185 | } |
| 186 | |
| 187 | const GURL& NavigationEntryImpl::GetUserTypedURL() const { |
| 188 | return user_typed_url_; |
| 189 | } |
| 190 | |
| 191 | void NavigationEntryImpl::SetHasPostData(bool has_post_data) { |
| 192 | has_post_data_ = has_post_data; |
| 193 | } |
| 194 | |
| 195 | bool NavigationEntryImpl::GetHasPostData() const { |
| 196 | return has_post_data_; |
| 197 | } |
| 198 | |
[email protected] | 86cd947 | 2012-02-03 19:51:05 | [diff] [blame] | 199 | void NavigationEntryImpl::SetPostID(int64 post_id) { |
| 200 | post_id_ = post_id; |
| 201 | } |
| 202 | |
| 203 | int64 NavigationEntryImpl::GetPostID() const { |
| 204 | return post_id_; |
| 205 | } |
| 206 | |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 207 | const FaviconStatus& NavigationEntryImpl::GetFavicon() const { |
| 208 | return favicon_; |
| 209 | } |
| 210 | |
| 211 | FaviconStatus& NavigationEntryImpl::GetFavicon() { |
| 212 | return favicon_; |
| 213 | } |
| 214 | |
| 215 | const SSLStatus& NavigationEntryImpl::GetSSL() const { |
| 216 | return ssl_; |
| 217 | } |
| 218 | |
| 219 | SSLStatus& NavigationEntryImpl::GetSSL() { |
| 220 | return ssl_; |
| 221 | } |
| 222 | |
[email protected] | 074269f | 2012-04-17 21:12:42 | [diff] [blame] | 223 | void NavigationEntryImpl::SetOriginalRequestURL(const GURL& original_url) { |
| 224 | original_request_url_ = original_url; |
| 225 | } |
| 226 | |
| 227 | const GURL& NavigationEntryImpl::GetOriginalRequestURL() const { |
| 228 | return original_request_url_; |
| 229 | } |
| 230 | |
[email protected] | 86ef6a39 | 2012-05-11 22:03:11 | [diff] [blame^] | 231 | void NavigationEntryImpl::SetIsOverridingUserAgent(bool override) { |
| 232 | is_overriding_user_agent_ = override; |
| 233 | } |
| 234 | |
| 235 | bool NavigationEntryImpl::GetIsOverridingUserAgent() const { |
| 236 | return is_overriding_user_agent_; |
| 237 | } |
| 238 | |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 239 | } // namespace content |