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/mac/foundation_util.h" |
| 8 #include "base/strings/sys_string_conversions.h" |
| 9 #import "ios/web/navigation/nscoder_util.h" |
| 10 #import "net/base/mac/url_conversions.h" |
| 11 |
| 12 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 13 #error "This file requires ARC support." |
| 14 #endif |
| 15 |
| 16 namespace web { |
| 17 |
| 18 // Keys used to serialize navigation properties. |
| 19 NSString* const kNavigationItemStorageURLKey = @"virtualUrlString"; |
| 20 NSString* const kNavigationItemStorageURLDeperecatedKey = @"virtualUrl"; |
| 21 NSString* const kNavigationItemStorageReferrerURLKey = @"referrerUrlString"; |
| 22 NSString* const kNavigationItemStorageReferrerURLDeprecatedKey = @"referrer"; |
| 23 NSString* const kNavigationItemStorageReferrerPolicyKey = @"referrerPolicy"; |
| 24 NSString* const kNavigationItemStorageTimestampKey = @"timestamp"; |
| 25 NSString* const kNavigationItemStorageTitleKey = @"title"; |
| 26 NSString* const kNavigationItemStoragePageDisplayStateKey = @"state"; |
| 27 NSString* const kNavigationItemStoragePOSTDataKey = @"POSTData"; |
| 28 NSString* const kNavigationItemStorageHTTPRequestHeadersKey = @"httpHeaders"; |
| 29 NSString* const kNavigationItemStorageSkipRepostFormConfirmationKey = |
| 30 @"skipResubmitDataConfirmation"; |
| 31 NSString* const kNavigationItemStorageUseDesktopUserAgentKey = |
| 32 @"useDesktopUserAgent"; |
| 33 |
| 34 } // namespace web |
| 35 |
| 36 @implementation CRWNavigationItemStorage |
| 37 |
| 38 @synthesize virtualURL = _virtualURL; |
| 39 @synthesize referrer = _referrer; |
| 40 @synthesize timestamp = _timestamp; |
| 41 @synthesize title = _title; |
| 42 @synthesize displayState = _displayState; |
| 43 @synthesize shouldSkipRepostFormConfirmation = |
| 44 _shouldSkipRepostFormConfirmation; |
| 45 @synthesize overridingUserAgent = _overridingUserAgent; |
| 46 @synthesize POSTData = _POSTData; |
| 47 @synthesize HTTPRequestHeaders = _HTTPRequestHeaders; |
| 48 |
| 49 #pragma mark - NSObject |
| 50 |
| 51 - (NSUInteger)hash { |
| 52 NSUInteger hash = 0; |
| 53 hash ^= [net::NSURLWithGURL(_virtualURL) hash]; |
| 54 hash ^= [net::NSURLWithGURL(_referrer.url) hash]; |
| 55 hash ^= _referrer.policy; |
| 56 hash ^= _timestamp.ToTimeT(); |
| 57 hash ^= [base::SysUTF16ToNSString(_title) hash]; |
| 58 hash ^= [_displayState.GetSerialization() hash]; |
| 59 hash ^= _shouldSkipRepostFormConfirmation; |
| 60 // Bit shift |_overridingUserAgent| so to differentiate it from |
| 61 // |_shouldSkipRepostFormConfirmation| in the hash. |
| 62 hash ^= _overridingUserAgent << 1; |
| 63 hash ^= [_POSTData hash]; |
| 64 hash ^= [_HTTPRequestHeaders hash]; |
| 65 return hash; |
| 66 } |
| 67 |
| 68 - (BOOL)isEqual:(id)object { |
| 69 CRWNavigationItemStorage* other = |
| 70 base::mac::ObjCCast<CRWNavigationItemStorage>(object); |
| 71 return self.virtualURL == other.virtualURL && |
| 72 self.referrer.url == other.referrer.url && |
| 73 self.referrer.policy == other.referrer.policy && |
| 74 self.timestamp == other.timestamp && self.title == other.title && |
| 75 self.displayState == other.displayState && |
| 76 self.shouldSkipRepostFormConfirmation == |
| 77 other.shouldSkipRepostFormConfirmation && |
| 78 self.overridingUserAgent == other.overridingUserAgent && |
| 79 [self.POSTData isEqualToData:other.POSTData] && |
| 80 [self.HTTPRequestHeaders isEqualToDictionary:other.HTTPRequestHeaders]; |
| 81 } |
| 82 |
| 83 - (NSString*)description { |
| 84 NSMutableString* description = |
| 85 [NSMutableString stringWithString:[super description]]; |
| 86 [description appendFormat:@"virtualURL : %s, ", _virtualURL.spec().c_str()]; |
| 87 [description appendFormat:@"referrer : %s, ", _referrer.url.spec().c_str()]; |
| 88 [description appendFormat:@"timestamp : %f, ", _timestamp.ToCFAbsoluteTime()]; |
| 89 [description appendFormat:@"title : %@, ", base::SysUTF16ToNSString(_title)]; |
| 90 [description |
| 91 appendFormat:@"displayState : %@", _displayState.GetDescription()]; |
| 92 [description appendFormat:@"skipRepostConfirmation : %@, ", |
| 93 @(_shouldSkipRepostFormConfirmation)]; |
| 94 [description |
| 95 appendFormat:@"overridingUserAgent : %@, ", @(_overridingUserAgent)]; |
| 96 [description appendFormat:@"POSTData : %@, ", _POSTData]; |
| 97 [description appendFormat:@"HTTPRequestHeaders : %@", _HTTPRequestHeaders]; |
| 98 return description; |
| 99 } |
| 100 |
| 101 #pragma mark - NSCoding |
| 102 |
| 103 - (instancetype)initWithCoder:(NSCoder*)aDecoder { |
| 104 self = [super init]; |
| 105 if (self) { |
| 106 // Desktop chrome only persists virtualUrl_ and uses it to feed the url |
| 107 // when creating a NavigationEntry. |
| 108 if ([aDecoder containsValueForKey:web::kNavigationItemStorageURLKey]) { |
| 109 _virtualURL = GURL(web::nscoder_util::DecodeString( |
| 110 aDecoder, web::kNavigationItemStorageURLKey)); |
| 111 } else { |
| 112 // Backward compatibility. |
| 113 _virtualURL = net::GURLWithNSURL([aDecoder |
| 114 decodeObjectForKey:web::kNavigationItemStorageURLDeperecatedKey]); |
| 115 } |
| 116 |
| 117 if ([aDecoder |
| 118 containsValueForKey:web::kNavigationItemStorageReferrerURLKey]) { |
| 119 const std::string referrerString(web::nscoder_util::DecodeString( |
| 120 aDecoder, web::kNavigationItemStorageReferrerURLKey)); |
| 121 web::ReferrerPolicy referrerPolicy = |
| 122 static_cast<web::ReferrerPolicy>([aDecoder |
| 123 decodeIntForKey:web::kNavigationItemStorageReferrerPolicyKey]); |
| 124 _referrer = web::Referrer(GURL(referrerString), referrerPolicy); |
| 125 } else { |
| 126 // Backward compatibility. |
| 127 NSURL* referrerURL = |
| 128 [aDecoder decodeObjectForKey: |
| 129 web::kNavigationItemStorageReferrerURLDeprecatedKey]; |
| 130 _referrer = web::Referrer(net::GURLWithNSURL(referrerURL), |
| 131 web::ReferrerPolicyDefault); |
| 132 } |
| 133 |
| 134 if ([aDecoder |
| 135 containsValueForKey:web::kNavigationItemStorageTimestampKey]) { |
| 136 int64_t us = |
| 137 [aDecoder decodeInt64ForKey:web::kNavigationItemStorageTimestampKey]; |
| 138 _timestamp = base::Time::FromInternalValue(us); |
| 139 } |
| 140 |
| 141 NSString* title = |
| 142 [aDecoder decodeObjectForKey:web::kNavigationItemStorageTitleKey]; |
| 143 // Use a transition type of reload so that we don't incorrectly increase |
| 144 // the typed count. This is what desktop chrome does. |
| 145 _title = base::SysNSStringToUTF16(title); |
| 146 NSDictionary* serializedDisplayState = [aDecoder |
| 147 decodeObjectForKey:web::kNavigationItemStoragePageDisplayStateKey]; |
| 148 _displayState = web::PageDisplayState(serializedDisplayState); |
| 149 _shouldSkipRepostFormConfirmation = |
| 150 [aDecoder decodeBoolForKey: |
| 151 web::kNavigationItemStorageSkipRepostFormConfirmationKey]; |
| 152 _overridingUserAgent = [aDecoder |
| 153 decodeBoolForKey:web::kNavigationItemStorageUseDesktopUserAgentKey]; |
| 154 _POSTData = |
| 155 [aDecoder decodeObjectForKey:web::kNavigationItemStoragePOSTDataKey]; |
| 156 _HTTPRequestHeaders = [aDecoder |
| 157 decodeObjectForKey:web::kNavigationItemStorageHTTPRequestHeadersKey]; |
| 158 } |
| 159 return self; |
| 160 } |
| 161 |
| 162 - (void)encodeWithCoder:(NSCoder*)aCoder { |
| 163 // Desktop Chrome doesn't persist |url_| or |originalUrl_|, only |
| 164 // |virtualUrl_|. |
| 165 web::nscoder_util::EncodeString(aCoder, web::kNavigationItemStorageURLKey, |
| 166 _virtualURL.spec()); |
| 167 web::nscoder_util::EncodeString( |
| 168 aCoder, web::kNavigationItemStorageReferrerURLKey, _referrer.url.spec()); |
| 169 [aCoder encodeInt:_referrer.policy |
| 170 forKey:web::kNavigationItemStorageReferrerPolicyKey]; |
| 171 [aCoder encodeInt64:_timestamp.ToInternalValue() |
| 172 forKey:web::kNavigationItemStorageTimestampKey]; |
| 173 |
| 174 [aCoder encodeObject:base::SysUTF16ToNSString(_title) |
| 175 forKey:web::kNavigationItemStorageTitleKey]; |
| 176 [aCoder encodeObject:_displayState.GetSerialization() |
| 177 forKey:web::kNavigationItemStoragePageDisplayStateKey]; |
| 178 [aCoder encodeBool:_shouldSkipRepostFormConfirmation |
| 179 forKey:web::kNavigationItemStorageSkipRepostFormConfirmationKey]; |
| 180 [aCoder encodeBool:_overridingUserAgent |
| 181 forKey:web::kNavigationItemStorageUseDesktopUserAgentKey]; |
| 182 [aCoder encodeObject:_POSTData forKey:web::kNavigationItemStoragePOSTDataKey]; |
| 183 [aCoder encodeObject:_HTTPRequestHeaders |
| 184 forKey:web::kNavigationItemStorageHTTPRequestHeadersKey]; |
| 185 } |
| 186 |
| 187 @end |
OLD | NEW |