OLD | NEW |
---|---|
(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)addNavigation:(WKNavigation*)navigation | |
70 forState:(web::WKNavigationState)state { | |
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::WK_NAVIGATION_STATE_REQUESTED || | |
81 state == web::WK_NAVIGATION_STATE_STARTED || | |
82 state == web::WK_NAVIGATION_STATE_COMMITTED); | |
83 record = | |
84 [[CRWWKNavigationsStateRecord alloc] initWithState:state | |
85 index:++_lastStateIndex]; | |
86 } else { | |
87 DCHECK(record.state < state || | |
88 (record.state == state && | |
89 state == web::WK_NAVIGATION_STATE_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 srart with 1. | |
kkhorimoto
2017/01/03 22:29:31
s/srart/start
Eugene But (OOO till 7-30)
2017/01/04 00:09:27
Done.
| |
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 | |
OLD | NEW |