| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/web_state/ui/crw_wk_navigation_states.h" | 5 #import "ios/web/web_state/ui/crw_wk_navigation_states.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "ios/web/web_state/navigation_context_impl.h" |
| 8 | 9 |
| 9 #if !defined(__has_feature) || !__has_feature(objc_arc) | 10 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 10 #error "This file requires ARC support." | 11 #error "This file requires ARC support." |
| 11 #endif | 12 #endif |
| 12 | 13 |
| 13 // Holds a pair of state and creation order index. | 14 // Holds a pair of state and creation order index. |
| 14 @interface CRWWKNavigationsStateRecord : NSObject | 15 @interface CRWWKNavigationsStateRecord : NSObject { |
| 16 // Backs up |context| property. |
| 17 std::unique_ptr<web::NavigationContextImpl> _context; |
| 18 } |
| 15 // Navigation state. | 19 // Navigation state. |
| 16 @property(nonatomic, assign) web::WKNavigationState state; | 20 @property(nonatomic, assign) web::WKNavigationState state; |
| 17 // Numerical index representing creation order (smaller index denotes earlier | 21 // Numerical index representing creation order (smaller index denotes earlier |
| 18 // navigations). | 22 // navigations). |
| 19 @property(nonatomic, assign, readonly) NSUInteger index; | 23 @property(nonatomic, assign, readonly) NSUInteger index; |
| 20 | 24 |
| 21 - (instancetype)init NS_UNAVAILABLE; | 25 - (instancetype)init NS_UNAVAILABLE; |
| 22 | 26 |
| 23 // Initializes record with state and index values. | 27 // Initializes record with state and index values. |
| 24 - (instancetype)initWithState:(web::WKNavigationState)state | 28 - (instancetype)initWithState:(web::WKNavigationState)state |
| 25 index:(NSUInteger)index NS_DESIGNATED_INITIALIZER; | 29 index:(NSUInteger)index NS_DESIGNATED_INITIALIZER; |
| 26 | 30 |
| 31 // Initializes record with context and index values. |
| 32 - (instancetype)initWithContext: |
| 33 (std::unique_ptr<web::NavigationContextImpl>)context |
| 34 index:(NSUInteger)index NS_DESIGNATED_INITIALIZER; |
| 35 |
| 36 // web::NavigationContextImpl for this navigation. |
| 37 - (web::NavigationContextImpl*)context; |
| 38 - (void)setContext:(std::unique_ptr<web::NavigationContextImpl>)context; |
| 39 |
| 27 @end | 40 @end |
| 28 | 41 |
| 29 @implementation CRWWKNavigationsStateRecord | 42 @implementation CRWWKNavigationsStateRecord |
| 30 @synthesize state = _state; | 43 @synthesize state = _state; |
| 31 @synthesize index = _index; | 44 @synthesize index = _index; |
| 32 | 45 |
| 46 #ifndef NDEBUG |
| 33 - (NSString*)description { | 47 - (NSString*)description { |
| 34 return [NSString stringWithFormat:@"state: %d, index: %ld", _state, | 48 return [NSString stringWithFormat:@"state: %d, index: %ld, context: %@", |
| 35 static_cast<long>(_index)]; | 49 _state, static_cast<long>(_index), |
| 50 _context->GetDescription()]; |
| 36 } | 51 } |
| 52 #endif // NDEBUG |
| 37 | 53 |
| 38 - (instancetype)initWithState:(web::WKNavigationState)state | 54 - (instancetype)initWithState:(web::WKNavigationState)state |
| 39 index:(NSUInteger)index { | 55 index:(NSUInteger)index { |
| 40 if ((self = [super init])) { | 56 if ((self = [super init])) { |
| 41 _state = state; | 57 _state = state; |
| 42 _index = index; | 58 _index = index; |
| 43 } | 59 } |
| 44 return self; | 60 return self; |
| 45 } | 61 } |
| 46 | 62 |
| 63 - (instancetype)initWithContext: |
| 64 (std::unique_ptr<web::NavigationContextImpl>)context |
| 65 index:(NSUInteger)index { |
| 66 if ((self = [super init])) { |
| 67 _context = std::move(context); |
| 68 _index = index; |
| 69 } |
| 70 return self; |
| 71 } |
| 72 |
| 73 - (void)setContext:(std::unique_ptr<web::NavigationContextImpl>)context { |
| 74 _context = std::move(context); |
| 75 } |
| 76 |
| 77 - (web::NavigationContextImpl*)context { |
| 78 return _context.get(); |
| 79 } |
| 80 |
| 47 @end | 81 @end |
| 48 | 82 |
| 49 @interface CRWWKNavigationStates () { | 83 @interface CRWWKNavigationStates () { |
| 50 NSMapTable* _records; | 84 NSMapTable* _records; |
| 51 NSUInteger _lastStateIndex; | 85 NSUInteger _lastStateIndex; |
| 52 } | 86 } |
| 53 @end | 87 @end |
| 54 | 88 |
| 55 @implementation CRWWKNavigationStates | 89 @implementation CRWWKNavigationStates |
| 56 | 90 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 - (void)removeNavigation:(WKNavigation*)navigation { | 127 - (void)removeNavigation:(WKNavigation*)navigation { |
| 94 if (!navigation) { | 128 if (!navigation) { |
| 95 // WKWebView may call WKNavigationDelegate callbacks with nil. | 129 // WKWebView may call WKNavigationDelegate callbacks with nil. |
| 96 return; | 130 return; |
| 97 } | 131 } |
| 98 | 132 |
| 99 DCHECK([_records objectForKey:navigation]); | 133 DCHECK([_records objectForKey:navigation]); |
| 100 [_records removeObjectForKey:navigation]; | 134 [_records removeObjectForKey:navigation]; |
| 101 } | 135 } |
| 102 | 136 |
| 137 - (void)setContext:(std::unique_ptr<web::NavigationContextImpl>)context |
| 138 forNavigation:(WKNavigation*)navigation { |
| 139 if (!navigation) { |
| 140 // WKWebView may call WKNavigationDelegate callbacks with nil. |
| 141 return; |
| 142 } |
| 143 CRWWKNavigationsStateRecord* record = [_records objectForKey:navigation]; |
| 144 if (!record) { |
| 145 record = |
| 146 [[CRWWKNavigationsStateRecord alloc] initWithContext:std::move(context) |
| 147 index:++_lastStateIndex]; |
| 148 } else { |
| 149 [record setContext:std::move(context)]; |
| 150 } |
| 151 [_records setObject:record forKey:navigation]; |
| 152 } |
| 153 |
| 154 - (web::NavigationContextImpl*)contextForNavigation:(WKNavigation*)navigation { |
| 155 if (!navigation) { |
| 156 // WKWebView may call WKNavigationDelegate callbacks with nil. |
| 157 return nullptr; |
| 158 } |
| 159 CRWWKNavigationsStateRecord* record = [_records objectForKey:navigation]; |
| 160 return [record context]; |
| 161 } |
| 162 |
| 103 - (WKNavigation*)lastAddedNavigation { | 163 - (WKNavigation*)lastAddedNavigation { |
| 104 WKNavigation* result = nil; | 164 WKNavigation* result = nil; |
| 105 NSUInteger lastAddedIndex = 0; // record indices start with 1. | 165 NSUInteger lastAddedIndex = 0; // record indices start with 1. |
| 106 for (WKNavigation* navigation in _records) { | 166 for (WKNavigation* navigation in _records) { |
| 107 CRWWKNavigationsStateRecord* record = [_records objectForKey:navigation]; | 167 CRWWKNavigationsStateRecord* record = [_records objectForKey:navigation]; |
| 108 if (lastAddedIndex < record.index) { | 168 if (lastAddedIndex < record.index) { |
| 109 result = navigation; | 169 result = navigation; |
| 110 lastAddedIndex = record.index; | 170 lastAddedIndex = record.index; |
| 111 } | 171 } |
| 112 } | 172 } |
| 113 return result; | 173 return result; |
| 114 } | 174 } |
| 115 | 175 |
| 116 - (web::WKNavigationState)lastAddedNavigationState { | 176 - (web::WKNavigationState)lastAddedNavigationState { |
| 117 CRWWKNavigationsStateRecord* lastAddedRecord = nil; | 177 CRWWKNavigationsStateRecord* lastAddedRecord = nil; |
| 118 WKNavigation* lastAddedNavigation = [self lastAddedNavigation]; | 178 WKNavigation* lastAddedNavigation = [self lastAddedNavigation]; |
| 119 if (lastAddedNavigation) | 179 if (lastAddedNavigation) |
| 120 lastAddedRecord = [_records objectForKey:lastAddedNavigation]; | 180 lastAddedRecord = [_records objectForKey:lastAddedNavigation]; |
| 121 | 181 |
| 122 return lastAddedRecord.state; | 182 return lastAddedRecord.state; |
| 123 } | 183 } |
| 124 | 184 |
| 125 @end | 185 @end |
| OLD | NEW |