Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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. | |
| 4 | |
| 5 #import "ios/web/public/crw_navigation_item_storage.h" | |
| 6 | |
| 7 #import "base/strings/sys_string_conversions.h" | |
|
Eugene But (OOO till 7-30)
2017/01/31 22:12:26
nit: s/import/include
kkhorimoto
2017/02/03 00:36:09
Done.
| |
| 8 #import "ios/web/navigation/nscoder_util.h" | |
| 9 #import "net/base/mac/url_conversions.h" | |
| 10 | |
| 11 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 12 #error "This file requires ARC support." | |
| 13 #endif | |
| 14 | |
| 15 namespace web { | |
| 16 | |
| 17 // Keys used to serialize web::PageScrollState properties. | |
| 18 NSString* const kNavigationItemStoragePageDisplayStateKey = @"state"; | |
| 19 NSString* const kNavigationItemStorageScrollOffsetXKey = @"scrollX"; | |
| 20 NSString* const kNavigationItemStorageScrollOffsetYKey = @"scrollY"; | |
| 21 NSString* const kNavigationItemStorageMinimumZoomScaleKey = @"minZoom"; | |
| 22 NSString* const kNavigationItemStorageMaximumZoomScaleKey = @"maxZoom"; | |
| 23 NSString* const kNavigationItemStorageZoomScaleKey = @"zoom"; | |
| 24 | |
| 25 // Keys used to serialize navigation properties. | |
| 26 NSString* const kNavigationItemStorageURLKey = @"virtualUrlString"; | |
| 27 NSString* const kNavigationItemStorageURLDeperecatedKey = @"virtualUrl"; | |
| 28 NSString* const kNavigationItemStorageReferrerURLKey = @"referrerUrlString"; | |
| 29 NSString* const kNavigationItemStorageReferrerURLDeprecatedKey = @"referrer"; | |
| 30 NSString* const kNavigationItemStorageReferrerPolicyKey = @"referrerPolicy"; | |
| 31 NSString* const kNavigationItemStorageTimestampKey = @"timestamp"; | |
| 32 NSString* const kNavigationItemStorageTitleKey = @"title"; | |
| 33 NSString* const kNavigationItemStoragePOSTDataKey = @"POSTData"; | |
| 34 NSString* const kNavigationItemStorageHTTPRequestHeadersKey = @"httpHeaders"; | |
| 35 NSString* const kNavigationItemStorageSkipRepostFormConfirmationKey = | |
| 36 @"skipResubmitDataConfirmation"; | |
| 37 NSString* const kNavigationItemStorageUseDesktopUserAgentKey = | |
| 38 @"useDesktopUserAgent"; | |
| 39 | |
| 40 } // namespace web | |
| 41 | |
| 42 namespace { | |
| 43 | |
| 44 // Converts a serialized NSDictionary to a web::PageDisplayState. | |
| 45 web::PageDisplayState DisplayStateFromDictionary(NSDictionary* dictionary) { | |
| 46 NSNumber* serializedValue = nil; | |
| 47 web::PageScrollState scrollState; | |
| 48 if ((serializedValue = | |
| 49 dictionary[web::kNavigationItemStorageScrollOffsetXKey])) | |
| 50 scrollState.set_offset_x([serializedValue doubleValue]); | |
| 51 if ((serializedValue = | |
| 52 dictionary[web::kNavigationItemStorageScrollOffsetYKey])) | |
| 53 scrollState.set_offset_y([serializedValue doubleValue]); | |
| 54 web::PageZoomState zoomState; | |
| 55 if ((serializedValue = | |
| 56 dictionary[web::kNavigationItemStorageMinimumZoomScaleKey])) | |
| 57 zoomState.set_minimum_zoom_scale([serializedValue doubleValue]); | |
| 58 if ((serializedValue = | |
| 59 dictionary[web::kNavigationItemStorageMaximumZoomScaleKey])) | |
| 60 zoomState.set_maximum_zoom_scale([serializedValue doubleValue]); | |
| 61 if ((serializedValue = dictionary[web::kNavigationItemStorageZoomScaleKey])) | |
| 62 zoomState.set_zoom_scale([serializedValue doubleValue]); | |
| 63 return web::PageDisplayState(scrollState, zoomState); | |
| 64 } | |
| 65 | |
| 66 // Serializes a web::PageDisplayState to an NSDictionary. | |
| 67 NSDictionary* DictionaryFromDisplayState(web::PageDisplayState display_state) { | |
| 68 return @{ | |
| 69 web::kNavigationItemStorageScrollOffsetXKey : | |
| 70 @(display_state.scroll_state().offset_x()), | |
| 71 web::kNavigationItemStorageScrollOffsetYKey : | |
| 72 @(display_state.scroll_state().offset_y()), | |
| 73 web::kNavigationItemStorageMinimumZoomScaleKey : | |
| 74 @(display_state.zoom_state().minimum_zoom_scale()), | |
| 75 web::kNavigationItemStorageMaximumZoomScaleKey : | |
| 76 @(display_state.zoom_state().maximum_zoom_scale()), | |
| 77 web::kNavigationItemStorageZoomScaleKey : | |
| 78 @(display_state.zoom_state().zoom_scale()) | |
| 79 }; | |
| 80 } | |
| 81 | |
| 82 } // namespace | |
| 83 | |
| 84 @implementation CRWNavigationItemStorage | |
| 85 | |
| 86 @synthesize virtualURL = _virtualURL; | |
| 87 @synthesize referrer = _referrer; | |
| 88 @synthesize timestamp = _timestamp; | |
| 89 @synthesize title = _title; | |
| 90 @synthesize displayState = _displayState; | |
| 91 @synthesize shouldSkipRepostFormConfirmation = | |
| 92 _shouldSkipRepostFormConfirmation; | |
| 93 @synthesize overridingUserAgent = _overridingUserAgent; | |
| 94 @synthesize POSTData = _POSTData; | |
| 95 @synthesize HTTPRequestHeaders = _HTTPRequestHeaders; | |
| 96 | |
| 97 #pragma mark - NSObject | |
| 98 | |
| 99 - (NSUInteger)hash { | |
| 100 NSUInteger hash = 0; | |
| 101 hash ^= [net::NSURLWithGURL(_virtualURL) hash]; | |
| 102 hash ^= [net::NSURLWithGURL(_referrer.url) hash]; | |
| 103 hash ^= _referrer.policy; | |
| 104 hash ^= _timestamp.ToTimeT(); | |
| 105 hash ^= [base::SysUTF16ToNSString(_title) hash]; | |
| 106 hash ^= [DictionaryFromDisplayState(_displayState) hash]; | |
| 107 hash ^= _shouldSkipRepostFormConfirmation; | |
| 108 // Bit shift |_overridingUserAgent| so to differentiate it from | |
| 109 // |_shouldSkipRepostFormConfirmation| in the hash. | |
| 110 hash ^= _overridingUserAgent << 1; | |
| 111 hash ^= [_POSTData hash]; | |
| 112 hash ^= [_HTTPRequestHeaders hash]; | |
| 113 return hash; | |
| 114 } | |
| 115 | |
| 116 - (BOOL)isEqual:(id)object { | |
| 117 return [object class] == [self class] && [object hash] == [self hash]; | |
|
Eugene But (OOO till 7-30)
2017/01/31 22:12:26
Equality of hashes does not mean objects equality,
kkhorimoto
2017/02/03 00:36:09
Fixed.
Eugene But (OOO till 7-30)
2017/02/03 01:52:34
So the other question was: "Is this method used ou
Eugene But (OOO till 7-30)
2017/02/03 01:57:16
The other reason for not using |hash| and |isEqual
kkhorimoto
2017/02/03 02:47:23
Done.
| |
| 118 } | |
| 119 | |
| 120 - (NSString*)description { | |
| 121 NSMutableString* description = | |
| 122 [NSMutableString stringWithString:[super description]]; | |
| 123 [description appendFormat:@"virtualURL : %s, ", _virtualURL.spec().c_str()]; | |
| 124 [description appendFormat:@"referrer : %s, ", _referrer.url.spec().c_str()]; | |
| 125 [description appendFormat:@"timestamp : %f, ", _timestamp.ToCFAbsoluteTime()]; | |
| 126 [description appendFormat:@"title : %@, ", base::SysUTF16ToNSString(_title)]; | |
| 127 [description appendFormat:@"displayState : %@", | |
| 128 DictionaryFromDisplayState(_displayState)]; | |
| 129 [description appendFormat:@"skipRepostConfirmation : %@, ", | |
| 130 @(_shouldSkipRepostFormConfirmation)]; | |
| 131 [description | |
| 132 appendFormat:@"overridingUserAgent : %@, ", @(_overridingUserAgent)]; | |
| 133 [description appendFormat:@"POSTData : %@, ", _POSTData]; | |
| 134 [description appendFormat:@"HTTPRequestHeaders : %@", _HTTPRequestHeaders]; | |
| 135 return description; | |
| 136 } | |
| 137 | |
| 138 #pragma mark - NSCoding | |
| 139 | |
| 140 - (instancetype)initWithCoder:(NSCoder*)aDecoder { | |
| 141 self = [super init]; | |
| 142 if (self) { | |
| 143 // Desktop chrome only persists virtualUrl_ and uses it to feed the url | |
| 144 // when creating a NavigationEntry. | |
| 145 if ([aDecoder containsValueForKey:web::kNavigationItemStorageURLKey]) { | |
| 146 _virtualURL = GURL(web::nscoder_util::DecodeString( | |
| 147 aDecoder, web::kNavigationItemStorageURLKey)); | |
| 148 } else { | |
| 149 // Backward compatibility. | |
| 150 _virtualURL = net::GURLWithNSURL([aDecoder | |
| 151 decodeObjectForKey:web::kNavigationItemStorageURLDeperecatedKey]); | |
| 152 } | |
| 153 | |
| 154 if ([aDecoder | |
| 155 containsValueForKey:web::kNavigationItemStorageReferrerURLKey]) { | |
| 156 const std::string referrerString(web::nscoder_util::DecodeString( | |
| 157 aDecoder, web::kNavigationItemStorageReferrerURLKey)); | |
| 158 web::ReferrerPolicy referrerPolicy = | |
| 159 static_cast<web::ReferrerPolicy>([aDecoder | |
| 160 decodeIntForKey:web::kNavigationItemStorageReferrerPolicyKey]); | |
| 161 _referrer = web::Referrer(GURL(referrerString), referrerPolicy); | |
| 162 } else { | |
| 163 // Backward compatibility. | |
| 164 NSURL* referrerURL = | |
| 165 [aDecoder decodeObjectForKey: | |
| 166 web::kNavigationItemStorageReferrerURLDeprecatedKey]; | |
| 167 _referrer = web::Referrer(net::GURLWithNSURL(referrerURL), | |
| 168 web::ReferrerPolicyDefault); | |
| 169 } | |
| 170 | |
| 171 if ([aDecoder | |
| 172 containsValueForKey:web::kNavigationItemStorageTimestampKey]) { | |
| 173 int64_t us = | |
| 174 [aDecoder decodeInt64ForKey:web::kNavigationItemStorageTimestampKey]; | |
| 175 _timestamp = base::Time::FromInternalValue(us); | |
| 176 } | |
| 177 | |
| 178 NSString* title = | |
| 179 [aDecoder decodeObjectForKey:web::kNavigationItemStorageTitleKey]; | |
| 180 // Use a transition type of reload so that we don't incorrectly increase | |
| 181 // the typed count. This is what desktop chrome does. | |
| 182 _title = base::SysNSStringToUTF16(title); | |
| 183 NSDictionary* serializedDisplayState = [aDecoder | |
| 184 decodeObjectForKey:web::kNavigationItemStoragePageDisplayStateKey]; | |
| 185 _displayState = DisplayStateFromDictionary(serializedDisplayState); | |
| 186 _shouldSkipRepostFormConfirmation = | |
| 187 [aDecoder decodeBoolForKey: | |
| 188 web::kNavigationItemStorageSkipRepostFormConfirmationKey]; | |
| 189 _overridingUserAgent = [aDecoder | |
| 190 decodeBoolForKey:web::kNavigationItemStorageUseDesktopUserAgentKey]; | |
| 191 _POSTData = | |
| 192 [aDecoder decodeObjectForKey:web::kNavigationItemStoragePOSTDataKey]; | |
| 193 _HTTPRequestHeaders = [aDecoder | |
| 194 decodeObjectForKey:web::kNavigationItemStorageHTTPRequestHeadersKey]; | |
| 195 } | |
| 196 return self; | |
| 197 } | |
| 198 | |
| 199 - (void)encodeWithCoder:(NSCoder*)aCoder { | |
| 200 // Desktop Chrome doesn't persist |url_| or |originalUrl_|, only | |
| 201 // |virtualUrl_|. | |
| 202 web::nscoder_util::EncodeString(aCoder, web::kNavigationItemStorageURLKey, | |
| 203 _virtualURL.spec()); | |
| 204 web::nscoder_util::EncodeString( | |
| 205 aCoder, web::kNavigationItemStorageReferrerURLKey, _referrer.url.spec()); | |
| 206 [aCoder encodeInt:_referrer.policy | |
| 207 forKey:web::kNavigationItemStorageReferrerPolicyKey]; | |
| 208 [aCoder encodeInt64:_timestamp.ToInternalValue() | |
| 209 forKey:web::kNavigationItemStorageTimestampKey]; | |
| 210 | |
| 211 [aCoder encodeObject:base::SysUTF16ToNSString(_title) | |
| 212 forKey:web::kNavigationItemStorageTitleKey]; | |
| 213 [aCoder encodeObject:DictionaryFromDisplayState(_displayState) | |
| 214 forKey:web::kNavigationItemStoragePageDisplayStateKey]; | |
| 215 [aCoder encodeBool:_shouldSkipRepostFormConfirmation | |
| 216 forKey:web::kNavigationItemStorageSkipRepostFormConfirmationKey]; | |
| 217 [aCoder encodeBool:_overridingUserAgent | |
| 218 forKey:web::kNavigationItemStorageUseDesktopUserAgentKey]; | |
| 219 [aCoder encodeObject:_POSTData forKey:web::kNavigationItemStoragePOSTDataKey]; | |
| 220 [aCoder encodeObject:_HTTPRequestHeaders | |
| 221 forKey:web::kNavigationItemStorageHTTPRequestHeadersKey]; | |
| 222 } | |
| 223 | |
| 224 @end | |
| 225 | |
| 226 @implementation CRWNavigationItemStorage (Testing) | |
| 227 | |
| 228 + (web::PageDisplayState)displayStateFromDictionary:(NSDictionary*)dictionary { | |
| 229 return DisplayStateFromDictionary(dictionary); | |
| 230 } | |
| 231 | |
| 232 + (NSDictionary*)dictionaryFromDisplayState: | |
| 233 (web::PageDisplayState)displayState { | |
| 234 return DictionaryFromDisplayState(displayState); | |
| 235 } | |
| 236 | |
| 237 @end | |
| OLD | NEW |