OLD | NEW |
(Empty) | |
| 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 "mojo/common/environment_data.h" |
| 6 |
| 7 #include "base/stl_util.h" |
| 8 |
| 9 namespace mojo { |
| 10 namespace common { |
| 11 |
| 12 // static |
| 13 EnvironmentData* EnvironmentData::instance_ = NULL; |
| 14 |
| 15 EnvironmentData::EnvironmentData() { |
| 16 DCHECK(!instance_); |
| 17 instance_ = this; |
| 18 } |
| 19 |
| 20 EnvironmentData::~EnvironmentData() { |
| 21 instance_ = NULL; |
| 22 DataMap data_map; |
| 23 data_map.swap(data_map_); |
| 24 STLDeleteContainerPairSecondPointers(data_map.begin(), data_map.end()); |
| 25 } |
| 26 |
| 27 // static |
| 28 EnvironmentData* EnvironmentData::GetInstance() { |
| 29 return instance_; |
| 30 } |
| 31 |
| 32 void EnvironmentData::SetData(const void* key, scoped_ptr<Data> data) { |
| 33 Data* old = NULL; |
| 34 { |
| 35 base::AutoLock auto_lock(data_lock_); |
| 36 old = data_map_[key]; |
| 37 if (data) |
| 38 data_map_[key] = data.release(); |
| 39 else |
| 40 data_map_.erase(key); |
| 41 } |
| 42 delete old; |
| 43 } |
| 44 |
| 45 EnvironmentData::Data* EnvironmentData::GetData(const void* key) { |
| 46 base::AutoLock auto_lock(data_lock_); |
| 47 return data_map_.count(key) > 0 ? data_map_[key] : NULL; |
| 48 } |
| 49 |
| 50 } // namespace common |
| 51 } // namespace mojo |
OLD | NEW |