Chromium Code Reviews| Index: ios/web/navigation/serialized_user_data_manager_impl.mm |
| diff --git a/ios/web/navigation/serialized_user_data_manager_impl.mm b/ios/web/navigation/serialized_user_data_manager_impl.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..22bb578e58a28fc4e0bde21fb755d1e0e3b87c50 |
| --- /dev/null |
| +++ b/ios/web/navigation/serialized_user_data_manager_impl.mm |
| @@ -0,0 +1,93 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ios/web/navigation/serialized_user_data_manager_impl.h" |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "ios/web/public/navigation_manager.h" |
| + |
| +namespace { |
| + |
| +// Private key used to store BrowserContextUserDataWrappers in a |
| +// NavigationManager's user data. |
| +const void* const kSerializedUserDataManagerWrapperKey = |
| + &kSerializedUserDataManagerWrapperKey; |
| + |
| +// Wraps a SerializedUserDataManagerImpl in a SupportsUserData::Data. |
| +class SerializedUserDataManagerWrapper : public base::SupportsUserData::Data { |
| + public: |
| + // Returns the SerializedUserDataManagerWrapper for |manager|, lazily creating |
| + // one if necessary. |
| + static SerializedUserDataManagerWrapper* FromNavigationManager( |
| + web::NavigationManager* manager) { |
| + DCHECK(manager); |
| + SerializedUserDataManagerWrapper* user_data_manager_wrapper = |
| + static_cast<SerializedUserDataManagerWrapper*>( |
| + manager->GetUserData(kSerializedUserDataManagerWrapperKey)); |
| + if (!user_data_manager_wrapper) |
| + user_data_manager_wrapper = new SerializedUserDataManagerWrapper(manager); |
| + return user_data_manager_wrapper; |
| + } |
| + |
| + // Returns the SerializedUserDataManager for this wrapper. |
| + web::SerializedUserDataManager* GetUserDataManager() const { |
| + return user_data_manager_.get(); |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(SerializedUserDataManagerWrapper); |
| + |
| + // The SerializedUserDataManager owned by the wrapper. |
| + scoped_ptr<web::SerializedUserDataManagerImpl> user_data_manager_; |
| + |
| + // Private constructor used to create a new wrapper for |manager|. |
| + explicit SerializedUserDataManagerWrapper(web::NavigationManager* manager) |
| + : user_data_manager_(new web::SerializedUserDataManagerImpl()) { |
| + DCHECK(manager); |
| + manager->SetUserData(kSerializedUserDataManagerWrapperKey, this); |
| + } |
| +}; |
| + |
| +} // namespace |
| + |
| +namespace web { |
| + |
| +// static |
| +SerializedUserDataManager* SerializedUserDataManager::FromNavigationManager( |
| + NavigationManager* manager) { |
| + return SerializedUserDataManagerWrapper::FromNavigationManager(manager) |
| + ->GetUserDataManager(); |
| +} |
| + |
| +SerializedUserDataManagerImpl::SerializedUserDataManagerImpl() |
| + : dictionary_([[NSMutableDictionary alloc] init]) {} |
| + |
| +SerializedUserDataManagerImpl::~SerializedUserDataManagerImpl() {} |
| + |
| +void SerializedUserDataManagerImpl::SetValueForSerializationKey( |
| + SerializedUserDataType data, |
| + NSString* serialization_key) { |
| + DCHECK(serialization_key.length); |
| + if (data) |
| + [dictionary_ setObject:data forKey:serialization_key]; |
| + else |
| + [dictionary_ removeObjectForKey:serialization_key]; |
| +} |
| + |
| +SerializedUserDataType |
| +SerializedUserDataManagerImpl::GetValueForSerializationKey( |
| + NSString* serialization_key) const { |
| + return [dictionary_ objectForKey:serialization_key]; |
|
Eugene But (OOO till 7-30)
2015/09/24 17:05:07
DCHECK(serialization_key.length)
kkhorimoto
2015/09/24 21:24:29
Done.
|
| +} |
| + |
| +NSDictionary* SerializedUserDataManagerImpl::GetSerializationDictionary() |
| + const { |
| + return dictionary_; |
|
Eugene But (OOO till 7-30)
2015/09/24 17:05:07
I would return an autoreleased copy of this dict t
kkhorimoto
2015/09/24 21:24:29
Done.
|
| +} |
| + |
| +void SerializedUserDataManagerImpl::AddValuesFromDictionary( |
| + NSDictionary* dictionary) { |
| + [dictionary_ addEntriesFromDictionary:dictionary]; |
|
Eugene But (OOO till 7-30)
2015/09/24 17:05:07
DCHECK(dictionary)
kkhorimoto
2015/09/24 21:24:29
Done.
|
| +} |
| +} |