[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 | |
[email protected] | ce137a26 | 2013-02-11 18:40:07 | [diff] [blame] | 7 | #include "base/metrics/histogram.h" |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 8 | #include "base/string_util.h" |
| 9 | #include "base/utf_string_conversions.h" |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 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). |
| 17 | static int GetUniqueIDInConstructor() { |
| 18 | static int unique_id_counter = 0; |
| 19 | return ++unique_id_counter; |
| 20 | } |
| 21 | |
| 22 | namespace content { |
| 23 | |
[email protected] | b26de07 | 2013-02-23 02:33:44 | [diff] [blame] | 24 | int NavigationEntryImpl::kInvalidBindings = -1; |
| 25 | |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 26 | NavigationEntry* NavigationEntry::Create() { |
| 27 | return new NavigationEntryImpl(); |
| 28 | } |
| 29 | |
| 30 | NavigationEntry* NavigationEntry::Create(const NavigationEntry& copy) { |
| 31 | return new NavigationEntryImpl(static_cast<const NavigationEntryImpl&>(copy)); |
| 32 | } |
| 33 | |
| 34 | NavigationEntryImpl* NavigationEntryImpl::FromNavigationEntry( |
| 35 | NavigationEntry* entry) { |
| 36 | return static_cast<NavigationEntryImpl*>(entry); |
| 37 | } |
| 38 | |
| 39 | NavigationEntryImpl::NavigationEntryImpl() |
| 40 | : unique_id_(GetUniqueIDInConstructor()), |
| 41 | site_instance_(NULL), |
[email protected] | b26de07 | 2013-02-23 02:33:44 | [diff] [blame] | 42 | bindings_(kInvalidBindings), |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 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), |
[email protected] | 86cd947 | 2012-02-03 19:51:05 | [diff] [blame] | 48 | post_id_(-1), |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 49 | restore_type_(RESTORE_NONE), |
[email protected] | 86ef6a39 | 2012-05-11 22:03:11 | [diff] [blame] | 50 | is_overriding_user_agent_(false), |
[email protected] | bcd90448 | 2012-02-01 01:54:22 | [diff] [blame] | 51 | is_renderer_initiated_(false), |
[email protected] | e2caa03 | 2012-11-15 23:29:18 | [diff] [blame] | 52 | should_replace_entry_(false), |
[email protected] | 60d6cca | 2013-04-30 08:47:13 | [diff] [blame^] | 53 | should_clear_history_list_(false), |
[email protected] | 951a6483 | 2012-10-11 16:26:37 | [diff] [blame] | 54 | can_load_local_resources_(false) { |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 57 | NavigationEntryImpl::NavigationEntryImpl(SiteInstanceImpl* instance, |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 58 | int page_id, |
| 59 | const GURL& url, |
| 60 | const Referrer& referrer, |
| 61 | const string16& title, |
| 62 | PageTransition transition_type, |
| 63 | bool is_renderer_initiated) |
| 64 | : unique_id_(GetUniqueIDInConstructor()), |
| 65 | site_instance_(instance), |
[email protected] | b26de07 | 2013-02-23 02:33:44 | [diff] [blame] | 66 | bindings_(kInvalidBindings), |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 67 | page_type_(PAGE_TYPE_NORMAL), |
| 68 | url_(url), |
| 69 | referrer_(referrer), |
| 70 | update_virtual_url_with_url_(false), |
| 71 | title_(title), |
| 72 | page_id_(page_id), |
| 73 | transition_type_(transition_type), |
| 74 | has_post_data_(false), |
[email protected] | 86cd947 | 2012-02-03 19:51:05 | [diff] [blame] | 75 | post_id_(-1), |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 76 | restore_type_(RESTORE_NONE), |
[email protected] | 86ef6a39 | 2012-05-11 22:03:11 | [diff] [blame] | 77 | is_overriding_user_agent_(false), |
[email protected] | bcd90448 | 2012-02-01 01:54:22 | [diff] [blame] | 78 | is_renderer_initiated_(is_renderer_initiated), |
[email protected] | e2caa03 | 2012-11-15 23:29:18 | [diff] [blame] | 79 | should_replace_entry_(false), |
[email protected] | 60d6cca | 2013-04-30 08:47:13 | [diff] [blame^] | 80 | should_clear_history_list_(false), |
[email protected] | e38019e8 | 2012-10-19 01:31:22 | [diff] [blame] | 81 | can_load_local_resources_(false) { |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | NavigationEntryImpl::~NavigationEntryImpl() { |
| 85 | } |
| 86 | |
| 87 | int NavigationEntryImpl::GetUniqueID() const { |
| 88 | return unique_id_; |
| 89 | } |
| 90 | |
| 91 | PageType NavigationEntryImpl::GetPageType() const { |
| 92 | return page_type_; |
| 93 | } |
| 94 | |
| 95 | void NavigationEntryImpl::SetURL(const GURL& url) { |
| 96 | url_ = url; |
| 97 | cached_display_title_.clear(); |
| 98 | } |
| 99 | |
| 100 | const GURL& NavigationEntryImpl::GetURL() const { |
| 101 | return url_; |
| 102 | } |
| 103 | |
[email protected] | d1ef81d | 2012-07-24 11:39:36 | [diff] [blame] | 104 | void NavigationEntryImpl::SetBaseURLForDataURL(const GURL& url) { |
| 105 | base_url_for_data_url_ = url; |
| 106 | } |
| 107 | |
| 108 | const GURL& NavigationEntryImpl::GetBaseURLForDataURL() const { |
| 109 | return base_url_for_data_url_; |
| 110 | } |
| 111 | |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 112 | void NavigationEntryImpl::SetReferrer(const Referrer& referrer) { |
| 113 | referrer_ = referrer; |
| 114 | } |
| 115 | |
| 116 | const Referrer& NavigationEntryImpl::GetReferrer() const { |
| 117 | return referrer_; |
| 118 | } |
| 119 | |
| 120 | void NavigationEntryImpl::SetVirtualURL(const GURL& url) { |
| 121 | virtual_url_ = (url == url_) ? GURL() : url; |
| 122 | cached_display_title_.clear(); |
| 123 | } |
| 124 | |
| 125 | const GURL& NavigationEntryImpl::GetVirtualURL() const { |
| 126 | return virtual_url_.is_empty() ? url_ : virtual_url_; |
| 127 | } |
| 128 | |
| 129 | void NavigationEntryImpl::SetTitle(const string16& title) { |
| 130 | title_ = title; |
| 131 | cached_display_title_.clear(); |
| 132 | } |
| 133 | |
| 134 | const string16& NavigationEntryImpl::GetTitle() const { |
| 135 | return title_; |
| 136 | } |
| 137 | |
| 138 | void NavigationEntryImpl::SetContentState(const std::string& state) { |
| 139 | content_state_ = state; |
| 140 | } |
| 141 | |
| 142 | const std::string& NavigationEntryImpl::GetContentState() const { |
| 143 | return content_state_; |
| 144 | } |
| 145 | |
| 146 | void NavigationEntryImpl::SetPageID(int page_id) { |
| 147 | page_id_ = page_id; |
| 148 | } |
| 149 | |
| 150 | int32 NavigationEntryImpl::GetPageID() const { |
| 151 | return page_id_; |
| 152 | } |
| 153 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 154 | void NavigationEntryImpl::set_site_instance(SiteInstanceImpl* site_instance) { |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 155 | site_instance_ = site_instance; |
| 156 | } |
| 157 | |
[email protected] | b26de07 | 2013-02-23 02:33:44 | [diff] [blame] | 158 | void NavigationEntryImpl::SetBindings(int bindings) { |
| 159 | // Ensure this is set to a valid value, and that it stays the same once set. |
| 160 | CHECK_NE(bindings, kInvalidBindings); |
| 161 | CHECK(bindings_ == kInvalidBindings || bindings_ == bindings); |
| 162 | bindings_ = bindings; |
| 163 | } |
| 164 | |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 165 | const string16& NavigationEntryImpl::GetTitleForDisplay( |
| 166 | const std::string& languages) const { |
| 167 | // Most pages have real titles. Don't even bother caching anything if this is |
| 168 | // the case. |
| 169 | if (!title_.empty()) |
| 170 | return title_; |
| 171 | |
| 172 | // More complicated cases will use the URLs as the title. This result we will |
| 173 | // cache since it's more complicated to compute. |
| 174 | if (!cached_display_title_.empty()) |
| 175 | return cached_display_title_; |
| 176 | |
| 177 | // Use the virtual URL first if any, and fall back on using the real URL. |
| 178 | string16 title; |
| 179 | if (!virtual_url_.is_empty()) { |
| 180 | title = net::FormatUrl(virtual_url_, languages); |
| 181 | } else if (!url_.is_empty()) { |
| 182 | title = net::FormatUrl(url_, languages); |
| 183 | } |
| 184 | |
| 185 | // For file:// URLs use the filename as the title, not the full path. |
| 186 | if (url_.SchemeIsFile()) { |
| 187 | string16::size_type slashpos = title.rfind('/'); |
| 188 | if (slashpos != string16::npos) |
| 189 | title = title.substr(slashpos + 1); |
| 190 | } |
| 191 | |
| 192 | ui::ElideString(title, kMaxTitleChars, &cached_display_title_); |
| 193 | return cached_display_title_; |
| 194 | } |
| 195 | |
| 196 | bool NavigationEntryImpl::IsViewSourceMode() const { |
| 197 | return virtual_url_.SchemeIs(chrome::kViewSourceScheme); |
| 198 | } |
| 199 | |
| 200 | void NavigationEntryImpl::SetTransitionType( |
| 201 | PageTransition transition_type) { |
| 202 | transition_type_ = transition_type; |
| 203 | } |
| 204 | |
| 205 | PageTransition NavigationEntryImpl::GetTransitionType() const { |
| 206 | return transition_type_; |
| 207 | } |
| 208 | |
| 209 | const GURL& NavigationEntryImpl::GetUserTypedURL() const { |
| 210 | return user_typed_url_; |
| 211 | } |
| 212 | |
| 213 | void NavigationEntryImpl::SetHasPostData(bool has_post_data) { |
| 214 | has_post_data_ = has_post_data; |
| 215 | } |
| 216 | |
| 217 | bool NavigationEntryImpl::GetHasPostData() const { |
| 218 | return has_post_data_; |
| 219 | } |
| 220 | |
[email protected] | 86cd947 | 2012-02-03 19:51:05 | [diff] [blame] | 221 | void NavigationEntryImpl::SetPostID(int64 post_id) { |
| 222 | post_id_ = post_id; |
| 223 | } |
| 224 | |
| 225 | int64 NavigationEntryImpl::GetPostID() const { |
| 226 | return post_id_; |
| 227 | } |
| 228 | |
[email protected] | 132e281a | 2012-07-31 18:32:44 | [diff] [blame] | 229 | void NavigationEntryImpl::SetBrowserInitiatedPostData( |
| 230 | const base::RefCountedMemory* data) { |
| 231 | browser_initiated_post_data_ = data; |
| 232 | } |
| 233 | |
| 234 | const base::RefCountedMemory* |
| 235 | NavigationEntryImpl::GetBrowserInitiatedPostData() const { |
| 236 | return browser_initiated_post_data_.get(); |
| 237 | } |
| 238 | |
| 239 | |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 240 | const FaviconStatus& NavigationEntryImpl::GetFavicon() const { |
| 241 | return favicon_; |
| 242 | } |
| 243 | |
| 244 | FaviconStatus& NavigationEntryImpl::GetFavicon() { |
| 245 | return favicon_; |
| 246 | } |
| 247 | |
| 248 | const SSLStatus& NavigationEntryImpl::GetSSL() const { |
| 249 | return ssl_; |
| 250 | } |
| 251 | |
| 252 | SSLStatus& NavigationEntryImpl::GetSSL() { |
| 253 | return ssl_; |
| 254 | } |
| 255 | |
[email protected] | 074269f | 2012-04-17 21:12:42 | [diff] [blame] | 256 | void NavigationEntryImpl::SetOriginalRequestURL(const GURL& original_url) { |
| 257 | original_request_url_ = original_url; |
| 258 | } |
| 259 | |
| 260 | const GURL& NavigationEntryImpl::GetOriginalRequestURL() const { |
| 261 | return original_request_url_; |
| 262 | } |
| 263 | |
[email protected] | 86ef6a39 | 2012-05-11 22:03:11 | [diff] [blame] | 264 | void NavigationEntryImpl::SetIsOverridingUserAgent(bool override) { |
| 265 | is_overriding_user_agent_ = override; |
| 266 | } |
| 267 | |
| 268 | bool NavigationEntryImpl::GetIsOverridingUserAgent() const { |
| 269 | return is_overriding_user_agent_; |
| 270 | } |
| 271 | |
[email protected] | 688aa65c6 | 2012-09-28 04:32:22 | [diff] [blame] | 272 | void NavigationEntryImpl::SetTimestamp(base::Time timestamp) { |
| 273 | timestamp_ = timestamp; |
| 274 | } |
| 275 | |
| 276 | base::Time NavigationEntryImpl::GetTimestamp() const { |
| 277 | return timestamp_; |
| 278 | } |
| 279 | |
[email protected] | 951a6483 | 2012-10-11 16:26:37 | [diff] [blame] | 280 | void NavigationEntryImpl::SetCanLoadLocalResources(bool allow) { |
| 281 | can_load_local_resources_ = allow; |
| 282 | } |
| 283 | |
| 284 | bool NavigationEntryImpl::GetCanLoadLocalResources() const { |
| 285 | return can_load_local_resources_; |
| 286 | } |
| 287 | |
[email protected] | 3027cf0 | 2013-01-24 08:16:58 | [diff] [blame] | 288 | void NavigationEntryImpl::SetFrameToNavigate(const std::string& frame_name) { |
| 289 | frame_to_navigate_ = frame_name; |
| 290 | } |
| 291 | |
| 292 | const std::string& NavigationEntryImpl::GetFrameToNavigate() const { |
| 293 | return frame_to_navigate_; |
| 294 | } |
| 295 | |
[email protected] | 261cf89 | 2013-02-01 00:42:36 | [diff] [blame] | 296 | void NavigationEntryImpl::SetExtraData(const std::string& key, |
| 297 | const string16& data) { |
| 298 | extra_data_[key] = data; |
| 299 | } |
| 300 | |
| 301 | bool NavigationEntryImpl::GetExtraData(const std::string& key, |
| 302 | string16* data) const { |
| 303 | std::map<std::string, string16>::const_iterator iter = extra_data_.find(key); |
| 304 | if (iter == extra_data_.end()) |
| 305 | return false; |
| 306 | *data = iter->second; |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | void NavigationEntryImpl::ClearExtraData(const std::string& key) { |
| 311 | extra_data_.erase(key); |
| 312 | } |
| 313 | |
[email protected] | 9677a3c | 2012-12-22 04:18:58 | [diff] [blame] | 314 | void NavigationEntryImpl::SetScreenshotPNGData( |
[email protected] | e6961e7 | 2013-04-27 02:06:32 | [diff] [blame] | 315 | scoped_refptr<base::RefCountedBytes> png_data) { |
| 316 | screenshot_ = png_data; |
[email protected] | ce137a26 | 2013-02-11 18:40:07 | [diff] [blame] | 317 | if (screenshot_) |
| 318 | UMA_HISTOGRAM_MEMORY_KB("Overscroll.ScreenshotSize", screenshot_->size()); |
[email protected] | 9677a3c | 2012-12-22 04:18:58 | [diff] [blame] | 319 | } |
| 320 | |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 321 | } // namespace content |