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

Side by Side Diff: ios/web/web_state/ui/crw_wk_navigation_states.mm

Issue 2835463002: Store NavigationIntext in CRWWKNavigationStates. (Closed)
Patch Set: Rebased Created 3 years, 8 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 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
15 // Navigation state. 16 // Navigation state.
16 @property(nonatomic, assign) web::WKNavigationState state; 17 @property(nonatomic, assign) web::WKNavigationState state;
17 // Numerical index representing creation order (smaller index denotes earlier 18 // Numerical index representing creation order (smaller index denotes earlier
18 // navigations). 19 // navigations).
19 @property(nonatomic, assign, readonly) NSUInteger index; 20 @property(nonatomic, assign, readonly) NSUInteger index;
20 21
21 - (instancetype)init NS_UNAVAILABLE; 22 - (instancetype)init NS_UNAVAILABLE;
22 23
23 // Initializes record with state and index values. 24 // Initializes record with state and index values.
24 - (instancetype)initWithState:(web::WKNavigationState)state 25 - (instancetype)initWithState:(web::WKNavigationState)state
25 index:(NSUInteger)index NS_DESIGNATED_INITIALIZER; 26 index:(NSUInteger)index NS_DESIGNATED_INITIALIZER;
26 27
28 // Initializes record with context and index values.
29 - (instancetype)initWithContext:
30 (std::unique_ptr<web::NavigationContextImpl>)context
31 index:(NSUInteger)index NS_DESIGNATED_INITIALIZER;
32
33 // |_context| setter and getter.
34 - (void)setContext:(std::unique_ptr<web::NavigationContextImpl>)context;
35 - (web::NavigationContextImpl*)context;
kkhorimoto 2017/04/25 13:29:55 Optional: Personally I prefer classes have readonl
Eugene But (OOO till 7-30) 2017/04/25 21:02:58 Done.
Eugene But (OOO till 7-30) 2017/05/08 18:30:51 Turns out property does not work on bots: ../../
36
27 @end 37 @end
28 38
29 @implementation CRWWKNavigationsStateRecord 39 @implementation CRWWKNavigationsStateRecord {
40 // web::NavigationContextImpl for this navigation.
41 std::unique_ptr<web::NavigationContextImpl> _context;
42 }
kkhorimoto 2017/04/25 13:29:55 Can you move the ivars to the @interface?
Eugene But (OOO till 7-30) 2017/04/25 21:02:58 Done.
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698