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

Side by Side Diff: ios/web/navigation/serializable_user_data_manager_impl.mm

Issue 2687353003: Created SerializableUserDataManager. (Closed)
Patch Set: Eugene's comments Created 3 years, 10 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 2017 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/navigation/serializable_user_data_manager_impl.h"
6
7 #import "base/mac/foundation_util.h"
8 #import "ios/web/public/web_state/web_state.h"
9
10 #if !defined(__has_feature) || !__has_feature(objc_arc)
11 #error "This file requires ARC support."
12 #endif
13
14 namespace web {
15 namespace {
16 // The key under which SerializableUserDataMangerWrapper are stored in the
17 // WebState's user data.
18 const void* const kSerializableUserDataManagerKey =
19 &kSerializableUserDataManagerKey;
20 // The key under which SerializableUserDataImpl's data is encoded.
21 NSString* const kSerializedUserDataKey = @"serializedUserData";
22
23 // Wrapper class used to associate SerializableUserDataManagerImpls with its
24 // associated WebState.
25 class SerializableUserDataManagerWrapper : base::SupportsUserData::Data {
26 public:
27 // Returns the SerializableUserDataMangerWrapper associated with |web_state|,
28 // creating one if necessary.
29 static SerializableUserDataManagerWrapper* FromWebState(
30 web::WebState* web_state) {
31 DCHECK(web_state);
32 SerializableUserDataManagerWrapper* wrapper =
33 static_cast<SerializableUserDataManagerWrapper*>(
34 web_state->GetUserData(kSerializableUserDataManagerKey));
35 if (!wrapper)
36 wrapper = new SerializableUserDataManagerWrapper(web_state);
37 return wrapper;
38 }
39
40 // Returns the manager owned by this wrapper.
41 SerializableUserDataManagerImpl* manager() { return &manager_; }
42
43 private:
44 // The SerializableUserDataMangerWrapper owned by this object.
45 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 };
54 } // namespace
55
56 // static
57 std::unique_ptr<SerializableUserData> SerializableUserData::Create() {
58 return std::unique_ptr<SerializableUserData>(new SerializableUserDataImpl());
59 }
60
61 SerializableUserDataImpl::SerializableUserDataImpl()
62 : data_([[NSDictionary alloc] init]) {}
63
64 SerializableUserDataImpl::~SerializableUserDataImpl() {}
65
66 SerializableUserDataImpl::SerializableUserDataImpl(NSDictionary* data)
67 : data_([data copy]) {}
68
69 void SerializableUserDataImpl::Encode(NSCoder* coder) {
70 [coder encodeObject:data_ forKey:kSerializedUserDataKey];
71 }
72
73 void SerializableUserDataImpl::Decode(NSCoder* coder) {
74 NSDictionary* data = base::mac::ObjCCastStrict<NSDictionary>(
75 [coder decodeObjectForKey:kSerializedUserDataKey]);
76 data_.reset([data mutableCopy]);
77 }
78
79 // static
80 SerializableUserDataManager* SerializableUserDataManager::FromWebState(
81 web::WebState* web_state) {
82 DCHECK(web_state);
83 return SerializableUserDataManagerWrapper::FromWebState(web_state)->manager();
84 }
85
86 SerializableUserDataManagerImpl::SerializableUserDataManagerImpl()
87 : data_([[NSMutableDictionary alloc] init]) {}
88
89 SerializableUserDataManagerImpl::~SerializableUserDataManagerImpl() {}
90
91 void SerializableUserDataManagerImpl::AddSerializableData(id<NSCoding> data,
92 NSString* key) {
93 DCHECK(data);
94 DCHECK(key.length);
95 [data_ setObject:data forKey:key];
96 }
97
98 id<NSCoding> SerializableUserDataManagerImpl::GetValueForSerializationKey(
99 NSString* key) {
100 return [data_ objectForKey:key];
101 }
102
103 std::unique_ptr<SerializableUserData>
104 SerializableUserDataManagerImpl::CreateSerializableUserData() const {
105 return std::unique_ptr<SerializableUserData>(
106 new SerializableUserDataImpl(data_));
107 }
108
109 void SerializableUserDataManagerImpl::AddSerializableUserData(
110 SerializableUserData* data) {
111 DCHECK(data);
112 SerializableUserDataImpl* data_impl =
113 static_cast<SerializableUserDataImpl*>(data);
114 data_.reset([data_impl->data() mutableCopy]);
115 }
116
117 } // namespace web
OLDNEW
« no previous file with comments | « ios/web/navigation/serializable_user_data_manager_impl.h ('k') | ios/web/navigation/session_storage_builder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698