OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/navigation/serializable_user_data_manager_impl.h" | 5 #import "ios/web/navigation/serializable_user_data_manager_impl.h" |
6 | 6 |
7 #import "base/mac/foundation_util.h" | 7 #import "base/mac/foundation_util.h" |
| 8 #include "base/memory/ptr_util.h" |
8 #import "ios/web/public/web_state/web_state.h" | 9 #import "ios/web/public/web_state/web_state.h" |
9 | 10 |
10 #if !defined(__has_feature) || !__has_feature(objc_arc) | 11 #if !defined(__has_feature) || !__has_feature(objc_arc) |
11 #error "This file requires ARC support." | 12 #error "This file requires ARC support." |
12 #endif | 13 #endif |
13 | 14 |
14 namespace web { | 15 namespace web { |
15 namespace { | 16 namespace { |
16 // The key under which SerializableUserDataMangerWrapper are stored in the | 17 // The key under which SerializableUserDataManagerWrapper are stored in the |
17 // WebState's user data. | 18 // WebState's user data. |
18 const void* const kSerializableUserDataManagerKey = | 19 const void* const kSerializableUserDataManagerKey = |
19 &kSerializableUserDataManagerKey; | 20 &kSerializableUserDataManagerKey; |
20 // The key under which SerializableUserDataImpl's data is encoded. | 21 // The key under which SerializableUserDataImpl's data is encoded. |
21 NSString* const kSerializedUserDataKey = @"serializedUserData"; | 22 NSString* const kSerializedUserDataKey = @"serializedUserData"; |
22 | 23 |
23 // Wrapper class used to associate SerializableUserDataManagerImpls with its | 24 // Wrapper class used to associate SerializableUserDataManagerImpls with its |
24 // associated WebState. | 25 // associated WebState. |
25 class SerializableUserDataManagerWrapper : base::SupportsUserData::Data { | 26 class SerializableUserDataManagerWrapper : public base::SupportsUserData::Data { |
26 public: | 27 public: |
27 // Returns the SerializableUserDataMangerWrapper associated with |web_state|, | 28 // Returns the SerializableUserDataManagerWrapper associated with |web_state|, |
28 // creating one if necessary. | 29 // creating one if necessary. |
29 static SerializableUserDataManagerWrapper* FromWebState( | 30 static SerializableUserDataManagerWrapper* FromWebState( |
30 web::WebState* web_state) { | 31 web::WebState* web_state) { |
31 DCHECK(web_state); | 32 DCHECK(web_state); |
32 SerializableUserDataManagerWrapper* wrapper = | 33 SerializableUserDataManagerWrapper* wrapper = |
33 static_cast<SerializableUserDataManagerWrapper*>( | 34 static_cast<SerializableUserDataManagerWrapper*>( |
34 web_state->GetUserData(kSerializableUserDataManagerKey)); | 35 web_state->GetUserData(kSerializableUserDataManagerKey)); |
35 if (!wrapper) | 36 if (wrapper) |
36 wrapper = new SerializableUserDataManagerWrapper(web_state); | 37 return wrapper; |
37 return wrapper; | 38 |
| 39 web_state->SetUserData( |
| 40 kSerializableUserDataManagerKey, |
| 41 base::MakeUnique<SerializableUserDataManagerWrapper>()); |
| 42 return static_cast<SerializableUserDataManagerWrapper*>( |
| 43 web_state->GetUserData(kSerializableUserDataManagerKey)); |
38 } | 44 } |
39 | 45 |
40 // Returns the manager owned by this wrapper. | 46 // Returns the manager owned by this wrapper. |
41 SerializableUserDataManagerImpl* manager() { return &manager_; } | 47 SerializableUserDataManagerImpl* manager() { return &manager_; } |
42 | 48 |
43 private: | 49 private: |
44 // The SerializableUserDataMangerWrapper owned by this object. | 50 // The SerializableUserDataManagerWrapper owned by this object. |
45 SerializableUserDataManagerImpl manager_; | 51 SerializableUserDataManagerImpl manager_; |
46 | |
47 // Private constructor. The created object will be added to |web_state|'s | |
48 // user data. | |
49 SerializableUserDataManagerWrapper(web::WebState* web_state) { | |
50 DCHECK(web_state); | |
51 web_state->SetUserData(kSerializableUserDataManagerKey, this); | |
52 } | |
53 }; | 52 }; |
54 } // namespace | 53 } // namespace |
55 | 54 |
56 // static | 55 // static |
57 std::unique_ptr<SerializableUserData> SerializableUserData::Create() { | 56 std::unique_ptr<SerializableUserData> SerializableUserData::Create() { |
58 return std::unique_ptr<SerializableUserData>(new SerializableUserDataImpl()); | 57 return base::MakeUnique<SerializableUserDataImpl>(); |
59 } | 58 } |
60 | 59 |
61 SerializableUserDataImpl::SerializableUserDataImpl() | 60 SerializableUserDataImpl::SerializableUserDataImpl() |
62 : data_(@{}), legacy_key_conversions_(@{ | 61 : data_(@{}), legacy_key_conversions_(@{ |
63 @"tabId" : @"TabID", | 62 @"tabId" : @"TabID", |
64 @"openerId" : @"OpenerID", | 63 @"openerId" : @"OpenerID", |
65 @"openerNavigationIndex" : @"OpenerNavigationIndex", | 64 @"openerNavigationIndex" : @"OpenerNavigationIndex", |
66 @"lastVisitedTimestamp" : @"LastVisitedTimestamp", | 65 @"lastVisitedTimestamp" : @"LastVisitedTimestamp", |
67 }) {} | 66 }) {} |
68 | 67 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 [data_ setObject:data forKey:key]; | 110 [data_ setObject:data forKey:key]; |
112 } | 111 } |
113 | 112 |
114 id<NSCoding> SerializableUserDataManagerImpl::GetValueForSerializationKey( | 113 id<NSCoding> SerializableUserDataManagerImpl::GetValueForSerializationKey( |
115 NSString* key) { | 114 NSString* key) { |
116 return [data_ objectForKey:key]; | 115 return [data_ objectForKey:key]; |
117 } | 116 } |
118 | 117 |
119 std::unique_ptr<SerializableUserData> | 118 std::unique_ptr<SerializableUserData> |
120 SerializableUserDataManagerImpl::CreateSerializableUserData() const { | 119 SerializableUserDataManagerImpl::CreateSerializableUserData() const { |
121 return std::unique_ptr<SerializableUserData>( | 120 return base::MakeUnique<SerializableUserDataImpl>(data_); |
122 new SerializableUserDataImpl(data_)); | |
123 } | 121 } |
124 | 122 |
125 void SerializableUserDataManagerImpl::AddSerializableUserData( | 123 void SerializableUserDataManagerImpl::AddSerializableUserData( |
126 SerializableUserData* data) { | 124 SerializableUserData* data) { |
127 DCHECK(data); | 125 if (data) { |
128 SerializableUserDataImpl* data_impl = | 126 SerializableUserDataImpl* data_impl = |
129 static_cast<SerializableUserDataImpl*>(data); | 127 static_cast<SerializableUserDataImpl*>(data); |
130 data_.reset([data_impl->data() mutableCopy]); | 128 data_.reset([data_impl->data() mutableCopy]); |
| 129 } |
131 } | 130 } |
132 | 131 |
133 } // namespace web | 132 } // namespace web |
OLD | NEW |