Broke ContentSettingBubbleModelTest.Plugins on Android.
[chromium-blink-merge.git] / content / browser / web_contents / navigation_entry_impl.cc
blobeb43aaf420eeab8295371c3b0a9a5246d6f31e7e
1 // Copyright (c) 2012 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/web_contents/navigation_entry_impl.h"
7 #include "base/string_util.h"
8 #include "base/utf_string_conversions.h"
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"
14 // Use this to get a new unique ID for a NavigationEntry during construction.
15 // The returned ID is guaranteed to be nonzero (which is the "no ID" indicator).
16 static int GetUniqueIDInConstructor() {
17 static int unique_id_counter = 0;
18 return ++unique_id_counter;
21 namespace content {
23 NavigationEntry* NavigationEntry::Create() {
24 return new NavigationEntryImpl();
27 NavigationEntry* NavigationEntry::Create(const NavigationEntry& copy) {
28 return new NavigationEntryImpl(static_cast<const NavigationEntryImpl&>(copy));
31 NavigationEntryImpl* NavigationEntryImpl::FromNavigationEntry(
32 NavigationEntry* entry) {
33 return static_cast<NavigationEntryImpl*>(entry);
36 NavigationEntryImpl::NavigationEntryImpl()
37 : unique_id_(GetUniqueIDInConstructor()),
38 site_instance_(NULL),
39 page_type_(PAGE_TYPE_NORMAL),
40 update_virtual_url_with_url_(false),
41 page_id_(-1),
42 transition_type_(PAGE_TRANSITION_LINK),
43 has_post_data_(false),
44 post_id_(-1),
45 restore_type_(RESTORE_NONE),
46 is_overriding_user_agent_(false),
47 is_renderer_initiated_(false),
48 should_replace_entry_(false),
49 can_load_local_resources_(false) {
52 NavigationEntryImpl::NavigationEntryImpl(SiteInstanceImpl* instance,
53 int page_id,
54 const GURL& url,
55 const Referrer& referrer,
56 const string16& title,
57 PageTransition transition_type,
58 bool is_renderer_initiated)
59 : unique_id_(GetUniqueIDInConstructor()),
60 site_instance_(instance),
61 page_type_(PAGE_TYPE_NORMAL),
62 url_(url),
63 referrer_(referrer),
64 update_virtual_url_with_url_(false),
65 title_(title),
66 page_id_(page_id),
67 transition_type_(transition_type),
68 has_post_data_(false),
69 post_id_(-1),
70 restore_type_(RESTORE_NONE),
71 is_overriding_user_agent_(false),
72 is_renderer_initiated_(is_renderer_initiated),
73 should_replace_entry_(false),
74 can_load_local_resources_(false) {
77 NavigationEntryImpl::~NavigationEntryImpl() {
80 int NavigationEntryImpl::GetUniqueID() const {
81 return unique_id_;
84 PageType NavigationEntryImpl::GetPageType() const {
85 return page_type_;
88 void NavigationEntryImpl::SetURL(const GURL& url) {
89 url_ = url;
90 cached_display_title_.clear();
93 const GURL& NavigationEntryImpl::GetURL() const {
94 return url_;
97 void NavigationEntryImpl::SetBaseURLForDataURL(const GURL& url) {
98 base_url_for_data_url_ = url;
101 const GURL& NavigationEntryImpl::GetBaseURLForDataURL() const {
102 return base_url_for_data_url_;
105 void NavigationEntryImpl::SetReferrer(const Referrer& referrer) {
106 referrer_ = referrer;
109 const Referrer& NavigationEntryImpl::GetReferrer() const {
110 return referrer_;
113 void NavigationEntryImpl::SetVirtualURL(const GURL& url) {
114 virtual_url_ = (url == url_) ? GURL() : url;
115 cached_display_title_.clear();
118 const GURL& NavigationEntryImpl::GetVirtualURL() const {
119 return virtual_url_.is_empty() ? url_ : virtual_url_;
122 void NavigationEntryImpl::SetTitle(const string16& title) {
123 title_ = title;
124 cached_display_title_.clear();
127 const string16& NavigationEntryImpl::GetTitle() const {
128 return title_;
131 void NavigationEntryImpl::SetContentState(const std::string& state) {
132 content_state_ = state;
135 const std::string& NavigationEntryImpl::GetContentState() const {
136 return content_state_;
139 void NavigationEntryImpl::SetPageID(int page_id) {
140 page_id_ = page_id;
143 int32 NavigationEntryImpl::GetPageID() const {
144 return page_id_;
147 void NavigationEntryImpl::set_site_instance(SiteInstanceImpl* site_instance) {
148 site_instance_ = site_instance;
151 const string16& NavigationEntryImpl::GetTitleForDisplay(
152 const std::string& languages) const {
153 // Most pages have real titles. Don't even bother caching anything if this is
154 // the case.
155 if (!title_.empty())
156 return title_;
158 // More complicated cases will use the URLs as the title. This result we will
159 // cache since it's more complicated to compute.
160 if (!cached_display_title_.empty())
161 return cached_display_title_;
163 // Use the virtual URL first if any, and fall back on using the real URL.
164 string16 title;
165 if (!virtual_url_.is_empty()) {
166 title = net::FormatUrl(virtual_url_, languages);
167 } else if (!url_.is_empty()) {
168 title = net::FormatUrl(url_, languages);
171 // For file:// URLs use the filename as the title, not the full path.
172 if (url_.SchemeIsFile()) {
173 string16::size_type slashpos = title.rfind('/');
174 if (slashpos != string16::npos)
175 title = title.substr(slashpos + 1);
178 ui::ElideString(title, kMaxTitleChars, &cached_display_title_);
179 return cached_display_title_;
182 bool NavigationEntryImpl::IsViewSourceMode() const {
183 return virtual_url_.SchemeIs(chrome::kViewSourceScheme);
186 void NavigationEntryImpl::SetTransitionType(
187 PageTransition transition_type) {
188 transition_type_ = transition_type;
191 PageTransition NavigationEntryImpl::GetTransitionType() const {
192 return transition_type_;
195 const GURL& NavigationEntryImpl::GetUserTypedURL() const {
196 return user_typed_url_;
199 void NavigationEntryImpl::SetHasPostData(bool has_post_data) {
200 has_post_data_ = has_post_data;
203 bool NavigationEntryImpl::GetHasPostData() const {
204 return has_post_data_;
207 void NavigationEntryImpl::SetPostID(int64 post_id) {
208 post_id_ = post_id;
211 int64 NavigationEntryImpl::GetPostID() const {
212 return post_id_;
215 void NavigationEntryImpl::SetBrowserInitiatedPostData(
216 const base::RefCountedMemory* data) {
217 browser_initiated_post_data_ = data;
220 const base::RefCountedMemory*
221 NavigationEntryImpl::GetBrowserInitiatedPostData() const {
222 return browser_initiated_post_data_.get();
226 const FaviconStatus& NavigationEntryImpl::GetFavicon() const {
227 return favicon_;
230 FaviconStatus& NavigationEntryImpl::GetFavicon() {
231 return favicon_;
234 const SSLStatus& NavigationEntryImpl::GetSSL() const {
235 return ssl_;
238 SSLStatus& NavigationEntryImpl::GetSSL() {
239 return ssl_;
242 void NavigationEntryImpl::SetOriginalRequestURL(const GURL& original_url) {
243 original_request_url_ = original_url;
246 const GURL& NavigationEntryImpl::GetOriginalRequestURL() const {
247 return original_request_url_;
250 void NavigationEntryImpl::SetIsOverridingUserAgent(bool override) {
251 is_overriding_user_agent_ = override;
254 bool NavigationEntryImpl::GetIsOverridingUserAgent() const {
255 return is_overriding_user_agent_;
258 void NavigationEntryImpl::SetTimestamp(base::Time timestamp) {
259 timestamp_ = timestamp;
262 base::Time NavigationEntryImpl::GetTimestamp() const {
263 return timestamp_;
266 void NavigationEntryImpl::SetCanLoadLocalResources(bool allow) {
267 can_load_local_resources_ = allow;
270 bool NavigationEntryImpl::GetCanLoadLocalResources() const {
271 return can_load_local_resources_;
274 void NavigationEntryImpl::SetFrameToNavigate(const std::string& frame_name) {
275 frame_to_navigate_ = frame_name;
278 const std::string& NavigationEntryImpl::GetFrameToNavigate() const {
279 return frame_to_navigate_;
282 void NavigationEntryImpl::SetExtraData(const std::string& key,
283 const string16& data) {
284 extra_data_[key] = data;
287 bool NavigationEntryImpl::GetExtraData(const std::string& key,
288 string16* data) const {
289 std::map<std::string, string16>::const_iterator iter = extra_data_.find(key);
290 if (iter == extra_data_.end())
291 return false;
292 *data = iter->second;
293 return true;
296 void NavigationEntryImpl::ClearExtraData(const std::string& key) {
297 extra_data_.erase(key);
300 void NavigationEntryImpl::SetScreenshotPNGData(
301 const std::vector<unsigned char>& png_data) {
302 screenshot_ = png_data.empty() ? NULL : new base::RefCountedBytes(png_data);
305 } // namespace content