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

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: Addressed review comments. 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,
35 static_cast<long>(_index)];
36 }
37
38 - (instancetype)initWithState:(web::WKNavigationState)state
39 index:(NSUInteger)index {
40 if ((self = [super init])) {
41 _state = state;
42 _index = index;
43 }
44 return self;
45 }
46
47 @end
48
49 @interface CRWWKNavigationStates () {
50 NSMapTable* _records;
51 NSUInteger _lastStateIndex;
52 }
53 @end
54
55 @implementation CRWWKNavigationStates
56
57 - (instancetype)init {
58 if ((self = [super init])) {
59 _records = [NSMapTable weakToStrongObjectsMapTable];
60 }
61 return self;
62 }
63
64 - (NSString*)description {
65 return [NSString stringWithFormat:@"records: %@, lastAddedNavigation: %@",
66 _records, self.lastAddedNavigation];
67 }
68
69 - (void)setState:(web::WKNavigationState)state
70 forNavigation:(WKNavigation*)navigation {
71 if (!navigation) {
72 // WKWebView may call WKNavigationDelegate callbacks with nil
73 // |WKNavigation|. TODO(crbug.com/677354): Remove this once WKWebView bug
74 // is fixed.
75 return;
76 }
77
78 CRWWKNavigationsStateRecord* record = [_records objectForKey:navigation];
79 if (!record) {
80 DCHECK(state == web::WKNavigationState::REQUESTED ||
81 state == web::WKNavigationState::STARTED ||
82 state == web::WKNavigationState::COMMITTED);
83 record =
84 [[CRWWKNavigationsStateRecord alloc] initWithState:state
85 index:++_lastStateIndex];
86 } else {
87 DCHECK(
88 record.state < state ||
89 (record.state == state && state == web::WKNavigationState::REDIRECTED));
90 record.state = state;
91 }
92 [_records setObject:record forKey:navigation];
93 }
94
95 - (WKNavigation*)lastAddedNavigation {
96 WKNavigation* result = nil;
97 NSUInteger lastAddedIndex = 0; // record indices start with 1.
98 for (WKNavigation* navigation in _records) {
99 CRWWKNavigationsStateRecord* record = [_records objectForKey:navigation];
100 if (lastAddedIndex < record.index) {
101 result = navigation;
102 lastAddedIndex = record.index;
103 }
104 }
105 return result;
106 }
107
108 - (web::WKNavigationState)stateForNavigation:(WKNavigation*)navigation {
109 CRWWKNavigationsStateRecord* record = [_records objectForKey:navigation];
110 return record.state;
111 }
112
113 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698