1 // Copyright 2013 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 #include "ios/public/consumer/base/supports_user_data.h" 6 7 #include "base/memory/scoped_ptr.h" 8 #include "base/supports_user_data.h" 9 10 namespace ios { 11 12 // Class that wraps a ios::SupportsUserData::Data object in a 13 // base::SupportsUserData::Data object. The wrapper object takes ownership of 14 // the wrapped object and will delete it on destruction. 15 class DataAdaptor : public base::SupportsUserData::Data { 16 public: 17 DataAdaptor(SupportsUserData::Data* data); 18 virtual ~DataAdaptor(); 19 20 SupportsUserData::Data* data() { return data_.get(); } 21 22 private: 23 scoped_ptr<SupportsUserData::Data> data_; 24 }; 25 26 DataAdaptor::DataAdaptor(SupportsUserData::Data* data) 27 : data_(data) {} 28 29 DataAdaptor::~DataAdaptor() {} 30 31 // Class that subclasses base::SupportsUserData in order to enable it to 32 // support ios::SupportsUserData::Data objects. It accomplishes this by 33 // wrapping these objects internally in ios::DataAdaptor objects. 34 class SupportsUserDataInternal : public base::SupportsUserData { 35 public: 36 // Returns the data that is associated with |key|, or NULL if there is no 37 // such associated data. 38 ios::SupportsUserData::Data* GetIOSUserData(const void* key); 39 40 // Associates |data| with |key|. Takes ownership of |data| and will delete it 41 // on either a call to |RemoveUserData(key)| or otherwise on destruction. 42 void SetIOSUserData(const void* key, ios::SupportsUserData::Data* data); 43 44 private: 45 SupportsUserDataInternal() {} 46 virtual ~SupportsUserDataInternal() {} 47 48 friend class ios::SupportsUserData; 49 }; 50 51 ios::SupportsUserData::Data* SupportsUserDataInternal::GetIOSUserData( 52 const void* key) { 53 DataAdaptor* adaptor = static_cast<DataAdaptor*>( 54 base::SupportsUserData::GetUserData(key)); 55 if (!adaptor) 56 return NULL; 57 return adaptor->data(); 58 } 59 60 void SupportsUserDataInternal::SetIOSUserData( 61 const void* key, ios::SupportsUserData::Data* data) { 62 base::SupportsUserData::SetUserData(key, new DataAdaptor(data)); 63 } 64 65 // ios::SupportsUserData implementation. 66 SupportsUserData::SupportsUserData() 67 : internal_helper_(new SupportsUserDataInternal()) { 68 } 69 70 SupportsUserData::~SupportsUserData() { 71 delete internal_helper_; 72 } 73 74 SupportsUserData::Data* SupportsUserData::GetUserData(const void* key) const { 75 return internal_helper_->GetIOSUserData(key); 76 } 77 78 void SupportsUserData::SetUserData(const void* key, Data* data) { 79 internal_helper_->SetIOSUserData(key, data); 80 } 81 82 void SupportsUserData::RemoveUserData(const void* key) { 83 internal_helper_->RemoveUserData(key); 84 } 85 86 void SupportsUserData::DetachUserDataThread() { 87 internal_helper_->DetachUserDataThread(); 88 } 89 90 } // namespace ios 91