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

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

Issue 2601083003: [ios] Correctly determine last seen WKNavigation object. (Closed)
Patch Set: Self review Created 3 years, 11 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #import "ios/web/web_state/ui/crw_wk_navigation_states.h"
6
7 #include "base/logging.h"
8
9 #if !defined(__has_feature) || !__has_feature(objc_arc)
10 #error "This file requires ARC support."
11 #endif
12
13 // Holds a pair of state and creation order index.
14 @interface CRWWKNavigationsStateRecord : NSObject
15 // Navigation state.
16 @property(nonatomic, assign) web::WKNavigationState state;
17 // Numerical index representing creation order (smaller index denotes earlier
18 // navigations).
19 @property(nonatomic, assign, readonly) NSUInteger index;
20
21 - (instancetype)init NS_UNAVAILABLE;
22
23 // Initializes record with state and index values.
24 - (instancetype)initWithState:(web::WKNavigationState)state
25 index:(NSUInteger)index NS_DESIGNATED_INITIALIZER;
26
27 @end
28
29 @implementation CRWWKNavigationsStateRecord
30 @synthesize state = _state;
31 @synthesize index = _index;
32
33 - (NSString*)description {
34 return [NSString stringWithFormat:@"state: %d, index: %ld", _state, _index];
35 }
36
37 - (instancetype)initWithState:(web::WKNavigationState)state
38 index:(NSUInteger)index {
39 if ((self = [super init])) {
40 _state = state;
41 _index = index;
42 }
43 return self;
44 }
45
46 @end
47
48 @interface CRWWKNavigationStates () {
49 NSMapTable* _records;
50 NSUInteger _lastStateIndex;
51 }
52 @end
53
54 @implementation CRWWKNavigationStates
55
56 - (instancetype)init {
57 if ((self = [super init])) {
58 _records = [NSMapTable weakToStrongObjectsMapTable];
59 }
60 return self;
61 }
62
63 - (NSString*)description {
64 return [NSString stringWithFormat:@"records: %@, lastAddedNavigation: %@",
65 _records, self.lastAddedNavigation];
66 }
67
68 - (void)addNavigation:(WKNavigation*)navigation
69 forState:(web::WKNavigationState)state {
70 if (!navigation) {
71 // WKWebView may call WKNavigationDelegate callbacks with nil
72 // |WKNavigation|. TODO(crbug.com/677354): Remove this once WKWebView bug
73 // is fixed.
74 return;
75 }
76
77 CRWWKNavigationsStateRecord* record = [_records objectForKey:navigation];
78 if (!record) {
79 DCHECK(state == web::WK_NAVIGATION_STATE_REQUESTED ||
80 state == web::WK_NAVIGATION_STATE_STARTED ||
81 state == web::WK_NAVIGATION_STATE_COMMITTED);
82 record =
83 [[CRWWKNavigationsStateRecord alloc] initWithState:state
84 index:++_lastStateIndex];
85 } else {
86 DCHECK_GT(state, record.state);
87 record.state = state;
88 }
89 [_records setObject:record forKey:navigation];
90 }
91
92 - (WKNavigation*)lastAddedNavigation {
93 WKNavigation* result = nil;
94 NSUInteger lastAddedIndex = 0; // record indices srart with 1.
95 for (WKNavigation* navigation in _records) {
96 CRWWKNavigationsStateRecord* record = [_records objectForKey:navigation];
97 if (lastAddedIndex < record.index) {
98 result = navigation;
99 lastAddedIndex = record.index;
100 }
101 }
102 return result;
103 }
104
105 - (web::WKNavigationState)stateForNavigation:(WKNavigation*)navigation {
106 CRWWKNavigationsStateRecord* record = [_records objectForKey:navigation];
107 return record.state;
108 }
109
110 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698