Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: ios/web/navigation/crw_session_entry.mm

Issue 1132563002: CRWSessionEntry: Replaced hardcoded strings with constants. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Resolved Stuart's review comments Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "ios/web/navigation/crw_session_entry.h" 5 #import "ios/web/navigation/crw_session_entry.h"
6 6
7 #include "base/mac/objc_property_releaser.h" 7 #include "base/mac/objc_property_releaser.h"
8 #include "base/mac/scoped_nsobject.h" 8 #include "base/mac/scoped_nsobject.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/sys_string_conversions.h" 10 #include "base/strings/sys_string_conversions.h"
11 #include "ios/web/navigation/navigation_item_impl.h" 11 #include "ios/web/navigation/navigation_item_impl.h"
12 #include "ios/web/navigation/nscoder_util.h" 12 #include "ios/web/navigation/nscoder_util.h"
13 #include "ios/web/public/navigation_item.h" 13 #include "ios/web/public/navigation_item.h"
14 #include "ios/web/public/web_state/page_scroll_state.h" 14 #include "ios/web/public/web_state/page_scroll_state.h"
15 #import "net/base/mac/url_conversions.h" 15 #import "net/base/mac/url_conversions.h"
16 16
17 namespace { 17 namespace web {
18 // Keys used to serialize web::PageScrollState properties. 18 // Keys used to serialize web::PageScrollState properties.
19 NSString* const kScrollOffsetXKey = @"scrollX"; 19 NSString* const kSessionEntryPageScrollStateKey = @"state";
20 NSString* const kScrollOffsetYKey = @"scrollY"; 20 NSString* const kSessionEntryScrollOffsetXKey = @"scrollX";
21 NSString* const kMinimumZoomScaleKey = @"minZoom"; 21 NSString* const kSessionEntryScrollOffsetYKey = @"scrollY";
22 NSString* const kMaximumZoomScaleKey = @"maxZoom"; 22 NSString* const kSessionEntryMinimumZoomScaleKey = @"minZoom";
23 NSString* const kZoomScaleKey = @"zoom"; 23 NSString* const kSessionEntryMaximumZoomScaleKey = @"maxZoom";
24 NSString* const kSessionEntryZoomScaleKey = @"zoom";
25
26 // Keys used to serialize navigation properties.
27 NSString* const kSessionEntryURLKey = @"virtualUrlString";
28 NSString* const kSessionEntryURLDeperecatedKey = @"virtualUrl";
29 NSString* const kSessionEntryReferrerURLKey = @"referrerUrlString";
30 NSString* const kSessionEntryReferrerURLDeprecatedKey = @"referrer";
31 NSString* const kSessionEntryReferrerPolicyKey = @"referrerPolicy";
32 NSString* const kSessionEntryTimestampKey = @"timestamp";
33 NSString* const kSessionEntryTitleKey = @"title";
34 NSString* const kSessionEntryPOSTDataKey = @"POSTData";
35 NSString* const kSessionEntryHTTPRequestHeadersKey = @"httpHeaders";
36 NSString* const kSessionEntrySkipResubmitConfirmationKey =
37 @"skipResubmitDataConfirmation";
38 NSString* const kSessionEntryUseDesktopUserAgentKey = @"useDesktopUserAgent";
24 } 39 }
25 40
26 @interface CRWSessionEntry () { 41 @interface CRWSessionEntry () {
27 // The original URL of the page. In cases where a redirect occurred, |url_| 42 // The original URL of the page. In cases where a redirect occurred, |url_|
28 // will contain the final post-redirect URL, and |originalUrl_| will contain 43 // will contain the final post-redirect URL, and |originalUrl_| will contain
29 // the pre-redirect URL. This field is not persisted to disk. 44 // the pre-redirect URL. This field is not persisted to disk.
30 GURL _originalUrl; 45 GURL _originalUrl;
31 46
32 // The NavigationItemImpl corresponding to this CRWSessionEntry. 47 // The NavigationItemImpl corresponding to this CRWSessionEntry.
33 // TODO(stuartmorgan): Move ownership to NavigationManagerImpl. 48 // TODO(stuartmorgan): Move ownership to NavigationManagerImpl.
(...skipping 30 matching lines...) Expand all
64 79
65 - (instancetype)initWithCoder:(NSCoder*)aDecoder { 80 - (instancetype)initWithCoder:(NSCoder*)aDecoder {
66 self = [super init]; 81 self = [super init];
67 if (self) { 82 if (self) {
68 _propertyReleaser_CRWSessionEntry.Init(self, [CRWSessionEntry class]); 83 _propertyReleaser_CRWSessionEntry.Init(self, [CRWSessionEntry class]);
69 _navigationItem.reset(new web::NavigationItemImpl()); 84 _navigationItem.reset(new web::NavigationItemImpl());
70 85
71 // Desktop chrome only persists virtualUrl_ and uses it to feed the url 86 // Desktop chrome only persists virtualUrl_ and uses it to feed the url
72 // when creating a NavigationEntry. 87 // when creating a NavigationEntry.
73 GURL url; 88 GURL url;
74 if ([aDecoder containsValueForKey:@"virtualUrlString"]) { 89 if ([aDecoder containsValueForKey:web::kSessionEntryURLKey]) {
75 url = GURL( 90 url = GURL(
76 web::nscoder_util::DecodeString(aDecoder, @"virtualUrlString")); 91 web::nscoder_util::DecodeString(aDecoder, web::kSessionEntryURLKey));
77 } else { 92 } else {
78 // Backward compatibility. 93 // Backward compatibility.
79 url = net::GURLWithNSURL([aDecoder decodeObjectForKey:@"virtualUrl"]); 94 url = net::GURLWithNSURL(
95 [aDecoder decodeObjectForKey:web::kSessionEntryURLDeperecatedKey]);
80 } 96 }
81 _navigationItem->SetURL(url); 97 _navigationItem->SetURL(url);
82 self.originalUrl = url; 98 self.originalUrl = url;
83 99
84 if ([aDecoder containsValueForKey:@"referrerUrlString"]) { 100 if ([aDecoder containsValueForKey:web::kSessionEntryReferrerURLKey]) {
85 const std::string referrerString(web::nscoder_util::DecodeString( 101 const std::string referrerString(web::nscoder_util::DecodeString(
86 aDecoder, @"referrerUrlString")); 102 aDecoder, web::kSessionEntryReferrerURLKey));
87 web::ReferrerPolicy referrerPolicy = 103 web::ReferrerPolicy referrerPolicy = static_cast<web::ReferrerPolicy>(
88 static_cast<web::ReferrerPolicy>( 104 [aDecoder decodeIntForKey:web::kSessionEntryReferrerPolicyKey]);
89 [aDecoder decodeIntForKey:@"referrerPolicy"]);
90 _navigationItem->SetReferrer( 105 _navigationItem->SetReferrer(
91 web::Referrer(GURL(referrerString), referrerPolicy)); 106 web::Referrer(GURL(referrerString), referrerPolicy));
92 } else { 107 } else {
93 // Backward compatibility. 108 // Backward compatibility.
94 NSURL* referrer = [aDecoder decodeObjectForKey:@"referrer"]; 109 NSURL* referrer = [aDecoder
110 decodeObjectForKey:web::kSessionEntryReferrerURLDeprecatedKey];
95 _navigationItem->SetReferrer(web::Referrer( 111 _navigationItem->SetReferrer(web::Referrer(
96 net::GURLWithNSURL(referrer), web::ReferrerPolicyDefault)); 112 net::GURLWithNSURL(referrer), web::ReferrerPolicyDefault));
97 } 113 }
98 114
99 if ([aDecoder containsValueForKey:@"timestamp"]) { 115 if ([aDecoder containsValueForKey:web::kSessionEntryTimestampKey]) {
100 int64 us = [aDecoder decodeInt64ForKey:@"timestamp"]; 116 int64 us = [aDecoder decodeInt64ForKey:web::kSessionEntryTimestampKey];
101 _navigationItem->SetTimestamp(base::Time::FromInternalValue(us)); 117 _navigationItem->SetTimestamp(base::Time::FromInternalValue(us));
102 } 118 }
103 119
104 NSString* title = [aDecoder decodeObjectForKey:@"title"]; 120 NSString* title = [aDecoder decodeObjectForKey:web::kSessionEntryTitleKey];
105 // Use a transition type of reload so that we don't incorrectly increase 121 // Use a transition type of reload so that we don't incorrectly increase
106 // the typed count. This is what desktop chrome does. 122 // the typed count. This is what desktop chrome does.
107 _navigationItem->SetPageID(-1); 123 _navigationItem->SetPageID(-1);
108 _navigationItem->SetTitle(base::SysNSStringToUTF16(title)); 124 _navigationItem->SetTitle(base::SysNSStringToUTF16(title));
109 _navigationItem->SetTransitionType(ui::PAGE_TRANSITION_RELOAD); 125 _navigationItem->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
110 _navigationItem->SetPageScrollState([[self class] 126 _navigationItem->SetPageScrollState([[self class]
111 scrollStateFromDictionary:[aDecoder decodeObjectForKey:@"state"]]); 127 scrollStateFromDictionary:
112 _navigationItem->SetShouldSkipResubmitDataConfirmation( 128 [aDecoder
113 [aDecoder decodeBoolForKey:@"skipResubmitDataConfirmation"]); 129 decodeObjectForKey:web::kSessionEntryPageScrollStateKey]]);
130 _navigationItem->SetShouldSkipResubmitDataConfirmation([aDecoder
131 decodeBoolForKey:web::kSessionEntrySkipResubmitConfirmationKey]);
114 _navigationItem->SetIsOverridingUserAgent( 132 _navigationItem->SetIsOverridingUserAgent(
115 [aDecoder decodeBoolForKey:@"useDesktopUserAgent"]); 133 [aDecoder decodeBoolForKey:web::kSessionEntryUseDesktopUserAgentKey]);
116 _navigationItem->SetPostData([aDecoder decodeObjectForKey:@"POSTData"]); 134 _navigationItem->SetPostData(
135 [aDecoder decodeObjectForKey:web::kSessionEntryPOSTDataKey]);
117 _navigationItem->AddHttpRequestHeaders( 136 _navigationItem->AddHttpRequestHeaders(
118 [aDecoder decodeObjectForKey:@"httpHeaders"]); 137 [aDecoder decodeObjectForKey:web::kSessionEntryHTTPRequestHeadersKey]);
119 } 138 }
120 return self; 139 return self;
121 } 140 }
122 141
123 - (void)encodeWithCoder:(NSCoder*)aCoder { 142 - (void)encodeWithCoder:(NSCoder*)aCoder {
124 // Desktop Chrome doesn't persist |url_| or |originalUrl_|, only 143 // Desktop Chrome doesn't persist |url_| or |originalUrl_|, only
125 // |virtualUrl_|. 144 // |virtualUrl_|.
126 web::nscoder_util::EncodeString(aCoder, @"virtualUrlString", 145 web::nscoder_util::EncodeString(aCoder, web::kSessionEntryURLKey,
127 _navigationItem->GetVirtualURL().spec()); 146 _navigationItem->GetVirtualURL().spec());
128 web::nscoder_util::EncodeString(aCoder, @"referrerUrlString", 147 web::nscoder_util::EncodeString(aCoder, web::kSessionEntryReferrerURLKey,
129 _navigationItem->GetReferrer().url.spec()); 148 _navigationItem->GetReferrer().url.spec());
130 [aCoder encodeInt:_navigationItem->GetReferrer().policy 149 [aCoder encodeInt:_navigationItem->GetReferrer().policy
131 forKey:@"referrerPolicy"]; 150 forKey:web::kSessionEntryReferrerPolicyKey];
132 [aCoder encodeInt64:_navigationItem->GetTimestamp().ToInternalValue() 151 [aCoder encodeInt64:_navigationItem->GetTimestamp().ToInternalValue()
133 forKey:@"timestamp"]; 152 forKey:web::kSessionEntryTimestampKey];
134 153
135 [aCoder encodeObject:base::SysUTF16ToNSString(_navigationItem->GetTitle()) 154 [aCoder encodeObject:base::SysUTF16ToNSString(_navigationItem->GetTitle())
136 forKey:@"title"]; 155 forKey:web::kSessionEntryTitleKey];
137 [aCoder encodeObject:[[self class] dictionaryFromScrollState: 156 [aCoder encodeObject:[[self class] dictionaryFromScrollState:
138 _navigationItem->GetPageScrollState()] 157 _navigationItem->GetPageScrollState()]
139 forKey:@"state"]; 158 forKey:web::kSessionEntryPageScrollStateKey];
140 [aCoder encodeBool:_navigationItem->ShouldSkipResubmitDataConfirmation() 159 [aCoder encodeBool:_navigationItem->ShouldSkipResubmitDataConfirmation()
141 forKey:@"skipResubmitDataConfirmation"]; 160 forKey:web::kSessionEntrySkipResubmitConfirmationKey];
142 [aCoder encodeBool:_navigationItem->IsOverridingUserAgent() 161 [aCoder encodeBool:_navigationItem->IsOverridingUserAgent()
143 forKey:@"useDesktopUserAgent"]; 162 forKey:web::kSessionEntryUseDesktopUserAgentKey];
144 [aCoder encodeObject:_navigationItem->GetPostData() forKey:@"POSTData"]; 163 [aCoder encodeObject:_navigationItem->GetPostData()
164 forKey:web::kSessionEntryPOSTDataKey];
145 [aCoder encodeObject:_navigationItem->GetHttpRequestHeaders() 165 [aCoder encodeObject:_navigationItem->GetHttpRequestHeaders()
146 forKey:@"httpHeaders"]; 166 forKey:web::kSessionEntryHTTPRequestHeadersKey];
147 } 167 }
148 168
149 // TODO(ios): Shall we overwrite EqualTo:? 169 // TODO(ios): Shall we overwrite EqualTo:?
150 170
151 - (instancetype)copyWithZone:(NSZone*)zone { 171 - (instancetype)copyWithZone:(NSZone*)zone {
152 CRWSessionEntry* copy = [[[self class] alloc] init]; 172 CRWSessionEntry* copy = [[[self class] alloc] init];
153 copy->_propertyReleaser_CRWSessionEntry.Init(copy, [CRWSessionEntry class]); 173 copy->_propertyReleaser_CRWSessionEntry.Init(copy, [CRWSessionEntry class]);
154 copy->_navigationItem.reset( 174 copy->_navigationItem.reset(
155 new web::NavigationItemImpl(*_navigationItem.get())); 175 new web::NavigationItemImpl(*_navigationItem.get()));
156 copy->_originalUrl = _originalUrl; 176 copy->_originalUrl = _originalUrl;
(...skipping 20 matching lines...) Expand all
177 197
178 - (web::NavigationItemImpl*)navigationItemImpl { 198 - (web::NavigationItemImpl*)navigationItemImpl {
179 return _navigationItem.get(); 199 return _navigationItem.get();
180 } 200 }
181 201
182 #pragma mark - Serialization helpers 202 #pragma mark - Serialization helpers
183 203
184 + (web::PageScrollState)scrollStateFromDictionary:(NSDictionary*)dictionary { 204 + (web::PageScrollState)scrollStateFromDictionary:(NSDictionary*)dictionary {
185 web::PageScrollState scrollState; 205 web::PageScrollState scrollState;
186 NSNumber* serializedValue = nil; 206 NSNumber* serializedValue = nil;
187 if ((serializedValue = dictionary[kScrollOffsetXKey])) 207 if ((serializedValue = dictionary[web::kSessionEntryScrollOffsetXKey]))
188 scrollState.set_scroll_offset_x([serializedValue doubleValue]); 208 scrollState.set_scroll_offset_x([serializedValue doubleValue]);
189 if ((serializedValue = dictionary[kScrollOffsetYKey])) 209 if ((serializedValue = dictionary[web::kSessionEntryScrollOffsetYKey]))
190 scrollState.set_scroll_offset_y([serializedValue doubleValue]); 210 scrollState.set_scroll_offset_y([serializedValue doubleValue]);
191 if ((serializedValue = dictionary[kMinimumZoomScaleKey])) 211 if ((serializedValue = dictionary[web::kSessionEntryMinimumZoomScaleKey]))
192 scrollState.set_minimum_zoom_scale([serializedValue doubleValue]); 212 scrollState.set_minimum_zoom_scale([serializedValue doubleValue]);
193 if ((serializedValue = dictionary[kMaximumZoomScaleKey])) 213 if ((serializedValue = dictionary[web::kSessionEntryMaximumZoomScaleKey]))
194 scrollState.set_maximum_zoom_scale([serializedValue doubleValue]); 214 scrollState.set_maximum_zoom_scale([serializedValue doubleValue]);
195 if ((serializedValue = dictionary[kZoomScaleKey])) 215 if ((serializedValue = dictionary[web::kSessionEntryZoomScaleKey]))
196 scrollState.set_zoom_scale([serializedValue doubleValue]); 216 scrollState.set_zoom_scale([serializedValue doubleValue]);
197 return scrollState; 217 return scrollState;
198 } 218 }
199 219
200 + (NSDictionary*)dictionaryFromScrollState: 220 + (NSDictionary*)dictionaryFromScrollState:
201 (const web::PageScrollState&)scrollState { 221 (const web::PageScrollState&)scrollState {
202 return @{ 222 return @{
203 kScrollOffsetXKey : @(scrollState.scroll_offset_x()), 223 web::kSessionEntryScrollOffsetXKey : @(scrollState.scroll_offset_x()),
204 kScrollOffsetYKey : @(scrollState.scroll_offset_y()), 224 web::kSessionEntryScrollOffsetYKey : @(scrollState.scroll_offset_y()),
205 kMinimumZoomScaleKey : @(scrollState.minimum_zoom_scale()), 225 web::kSessionEntryMinimumZoomScaleKey : @(scrollState.minimum_zoom_scale()),
206 kMaximumZoomScaleKey : @(scrollState.maximum_zoom_scale()), 226 web::kSessionEntryMaximumZoomScaleKey : @(scrollState.maximum_zoom_scale()),
207 kZoomScaleKey : @(scrollState.zoom_scale()) 227 web::kSessionEntryZoomScaleKey : @(scrollState.zoom_scale()),
208 }; 228 };
209 } 229 }
210 230
211 + (NSString*)scrollStateDescription:(const web::PageScrollState&)scrollState { 231 + (NSString*)scrollStateDescription:(const web::PageScrollState&)scrollState {
212 NSString* const kPageScrollStateDescriptionFormat = 232 NSString* const kPageScrollStateDescriptionFormat =
213 @"{ scrollOffset:(%0.2f, %0.2f), zoomScaleRange:(%0.2f, %0.2f), " 233 @"{ scrollOffset:(%0.2f, %0.2f), zoomScaleRange:(%0.2f, %0.2f), "
214 @"zoomScale:%0.2f }"; 234 @"zoomScale:%0.2f }";
215 return [NSString stringWithFormat:kPageScrollStateDescriptionFormat, 235 return [NSString stringWithFormat:kPageScrollStateDescriptionFormat,
216 scrollState.scroll_offset_x(), 236 scrollState.scroll_offset_x(),
217 scrollState.scroll_offset_y(), 237 scrollState.scroll_offset_y(),
218 scrollState.minimum_zoom_scale(), 238 scrollState.minimum_zoom_scale(),
219 scrollState.maximum_zoom_scale(), 239 scrollState.maximum_zoom_scale(),
220 scrollState.zoom_scale()]; 240 scrollState.zoom_scale()];
221 } 241 }
222 242
223 @end 243 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698