OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "chromecast/base/device_capabilities_impl.h" | 5 #include "chromecast/base/device_capabilities_impl.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chromecast/base/serializers.h" | 9 #include "chromecast/base/serializers.h" |
10 | 10 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 DCHECK(capabilities_str_.get()); | 76 DCHECK(capabilities_str_.get()); |
77 } | 77 } |
78 | 78 |
79 DeviceCapabilitiesImpl::~DeviceCapabilitiesImpl() { | 79 DeviceCapabilitiesImpl::~DeviceCapabilitiesImpl() { |
80 DCHECK(thread_checker_.CalledOnValidThread()); | 80 DCHECK(thread_checker_.CalledOnValidThread()); |
81 // Make sure that any registered Validators have unregistered at this point | 81 // Make sure that any registered Validators have unregistered at this point |
82 DCHECK(validator_map_.empty()); | 82 DCHECK(validator_map_.empty()); |
83 } | 83 } |
84 | 84 |
85 void DeviceCapabilitiesImpl::Register(const std::string& key, | 85 void DeviceCapabilitiesImpl::Register(const std::string& key, |
86 scoped_ptr<base::Value> init_value, | |
87 Validator* validator) { | 86 Validator* validator) { |
88 DCHECK(thread_checker_.CalledOnValidThread()); | 87 DCHECK(thread_checker_.CalledOnValidThread()); |
89 DCHECK(IsValidRegisterKey(key)); | 88 DCHECK(IsValidRegisterKey(key)); |
90 DCHECK(init_value.get()); | |
91 DCHECK(validator); | 89 DCHECK(validator); |
92 | 90 |
93 AddValidator(key, validator); | 91 bool added = validator_map_.insert(std::make_pair(key, validator)).second; |
94 | 92 // Check that a validator has not already been registered for this key |
95 capabilities_->Set(key, init_value.Pass()); | 93 DCHECK(added); |
96 UpdateStrAndNotifyChanged(key); | |
97 } | 94 } |
98 | 95 |
99 void DeviceCapabilitiesImpl::Unregister(const std::string& key, | 96 void DeviceCapabilitiesImpl::Unregister(const std::string& key, |
100 const Validator* validator) { | 97 const Validator* validator) { |
101 DCHECK(thread_checker_.CalledOnValidThread()); | 98 DCHECK(thread_checker_.CalledOnValidThread()); |
102 auto validator_it = validator_map_.find(key); | |
103 DCHECK(validator_it != validator_map_.end()); | |
104 // Check that validator being unregistered matches the original for |key|. | 99 // Check that validator being unregistered matches the original for |key|. |
105 // This prevents managers from accidentally unregistering incorrect | 100 // This prevents managers from accidentally unregistering incorrect |
106 // validators. | 101 // validators. |
107 DCHECK_EQ(validator, validator_it->second); | 102 DCHECK_EQ(validator, GetValidator(key)); |
108 validator_map_.erase(validator_it); | 103 bool erased = validator_map_.erase(key); |
| 104 DCHECK(erased); |
| 105 } |
109 | 106 |
110 bool removed = capabilities_->Remove(key, nullptr); | 107 DeviceCapabilities::Validator* DeviceCapabilitiesImpl::GetValidator( |
111 DCHECK(removed); | 108 const std::string& key) const { |
112 UpdateStrAndNotifyChanged(key); | 109 auto validator_it = validator_map_.find(key); |
| 110 return validator_it == validator_map_.end() ? nullptr : validator_it->second; |
113 } | 111 } |
114 | 112 |
115 bool DeviceCapabilitiesImpl::BluetoothSupported() const { | 113 bool DeviceCapabilitiesImpl::BluetoothSupported() const { |
116 DCHECK(thread_checker_.CalledOnValidThread()); | 114 DCHECK(thread_checker_.CalledOnValidThread()); |
117 bool bluetooth_supported = false; | 115 bool bluetooth_supported = false; |
118 bool found_key = | 116 bool found_key = |
119 capabilities_->GetBoolean(kKeyBluetoothSupported, &bluetooth_supported); | 117 capabilities_->GetBoolean(kKeyBluetoothSupported, &bluetooth_supported); |
120 DCHECK(found_key); | 118 DCHECK(found_key); |
121 return bluetooth_supported; | 119 return bluetooth_supported; |
122 } | 120 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 GetCapability(path, &cur_value) && cur_value->Equals(new_value.get()); | 199 GetCapability(path, &cur_value) && cur_value->Equals(new_value.get()); |
202 if (capability_unchaged) { | 200 if (capability_unchaged) { |
203 VLOG(1) << "Ignoring unchanged capability: " << path; | 201 VLOG(1) << "Ignoring unchanged capability: " << path; |
204 return; | 202 return; |
205 } | 203 } |
206 | 204 |
207 capabilities_->Set(path, new_value.Pass()); | 205 capabilities_->Set(path, new_value.Pass()); |
208 UpdateStrAndNotifyChanged(path); | 206 UpdateStrAndNotifyChanged(path); |
209 } | 207 } |
210 | 208 |
211 void DeviceCapabilitiesImpl::AddValidator(const std::string& key, | |
212 Validator* validator) { | |
213 DCHECK(validator); | |
214 bool added = validator_map_.insert(std::make_pair(key, validator)).second; | |
215 // Check that a validator has not already been registered for this key | |
216 DCHECK(added); | |
217 } | |
218 | |
219 void DeviceCapabilitiesImpl::UpdateStrAndNotifyChanged( | 209 void DeviceCapabilitiesImpl::UpdateStrAndNotifyChanged( |
220 const std::string& path) { | 210 const std::string& path) { |
221 // Update capabilities string here since all updates to capabilities must | 211 // Update capabilities string here since all updates to capabilities must |
222 // ultimately call this method no matter where the update originated from. | 212 // ultimately call this method no matter where the update originated from. |
223 capabilities_str_ = SerializeToJson(*capabilities_); | 213 capabilities_str_ = SerializeToJson(*capabilities_); |
224 DCHECK(capabilities_str_.get()); | 214 DCHECK(capabilities_str_.get()); |
225 FOR_EACH_OBSERVER(Observer, observer_list_, OnCapabilitiesChanged(path)); | 215 FOR_EACH_OBSERVER(Observer, observer_list_, OnCapabilitiesChanged(path)); |
226 } | 216 } |
227 | 217 |
228 } // namespace chromecast | 218 } // namespace chromecast |
OLD | NEW |