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 #ifndef CHROMECAST_BASE_DEVICE_CAPABILITIES_IMPL_H_ | 5 #ifndef CHROMECAST_BASE_DEVICE_CAPABILITIES_IMPL_H_ |
6 #define CHROMECAST_BASE_DEVICE_CAPABILITIES_IMPL_H_ | 6 #define CHROMECAST_BASE_DEVICE_CAPABILITIES_IMPL_H_ |
7 | 7 |
8 #include "base/containers/hash_tables.h" | 8 #include "base/containers/scoped_ptr_hash_map.h" |
9 #include "base/observer_list.h" | 9 #include "base/macros.h" |
10 #include "base/threading/thread_checker.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/observer_list_threadsafe.h" |
| 12 #include "base/synchronization/lock.h" |
11 #include "chromecast/base/device_capabilities.h" | 13 #include "chromecast/base/device_capabilities.h" |
12 | 14 |
| 15 namespace base { |
| 16 class SingleThreadTaskRunner; |
| 17 } |
| 18 |
13 namespace chromecast { | 19 namespace chromecast { |
14 | 20 |
15 class DeviceCapabilitiesImpl : public DeviceCapabilities { | 21 class DeviceCapabilitiesImpl : public DeviceCapabilities { |
16 public: | 22 public: |
17 ~DeviceCapabilitiesImpl() override; | 23 ~DeviceCapabilitiesImpl() override; |
18 | 24 |
19 // DeviceCapabilities implementation: | 25 // DeviceCapabilities implementation: |
20 void Register(const std::string& key, Validator* validator) override; | 26 void Register(const std::string& key, Validator* validator) override; |
21 void Unregister(const std::string& key, const Validator* validator) override; | 27 void Unregister(const std::string& key, const Validator* validator) override; |
22 Validator* GetValidator(const std::string& key) const override; | 28 Validator* GetValidator(const std::string& key) const override; |
23 bool BluetoothSupported() const override; | 29 bool BluetoothSupported() const override; |
24 bool DisplaySupported() const override; | 30 bool DisplaySupported() const override; |
25 bool GetCapability(const std::string& path, | 31 scoped_ptr<base::Value> GetCapability(const std::string& path) const override; |
26 const base::Value** out_value) const override; | 32 scoped_refptr<Data> GetData() const override; |
27 const std::string& GetCapabilitiesString() const override; | |
28 const base::DictionaryValue* GetCapabilities() const override; | |
29 void SetCapability(const std::string& path, | 33 void SetCapability(const std::string& path, |
30 scoped_ptr<base::Value> proposed_value) override; | 34 scoped_ptr<base::Value> proposed_value) override; |
31 void MergeDictionary(const base::DictionaryValue& dict_value) override; | 35 void MergeDictionary(const base::DictionaryValue& dict_value) override; |
32 void AddCapabilitiesObserver(Observer* observer) override; | 36 void AddCapabilitiesObserver(Observer* observer) override; |
33 void RemoveCapabilitiesObserver(Observer* observer) override; | 37 void RemoveCapabilitiesObserver(Observer* observer) override; |
34 | 38 |
35 private: | 39 private: |
| 40 class ValidatorInfo : public base::SupportsWeakPtr<ValidatorInfo> { |
| 41 public: |
| 42 explicit ValidatorInfo(Validator* validator); |
| 43 ~ValidatorInfo(); |
| 44 |
| 45 Validator* validator() const { return validator_; } |
| 46 |
| 47 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const { |
| 48 return task_runner_; |
| 49 } |
| 50 |
| 51 void Validate(const std::string& path, |
| 52 scoped_ptr<base::Value> proposed_value) const; |
| 53 |
| 54 private: |
| 55 Validator* const validator_; |
| 56 // TaskRunner of thread that validator_ was registered on |
| 57 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(ValidatorInfo); |
| 60 }; |
| 61 |
36 // For DeviceCapabilitiesImpl() | 62 // For DeviceCapabilitiesImpl() |
37 friend class DeviceCapabilities; | 63 friend class DeviceCapabilities; |
38 // For SetValidatedValueInternal() | 64 // For SetValidatedValueInternal() |
39 friend class DeviceCapabilities::Validator; | 65 friend class DeviceCapabilities::Validator; |
40 | 66 |
41 // Map from capability key to corresponding Validator. Gets updated | 67 // Map from capability key to corresponding ValidatorInfo. Gets updated |
42 // in Register()/Unregister(). | 68 // in Register()/Unregister(). |
43 typedef base::hash_map<std::string, Validator*> ValidatorMap; | 69 typedef base::ScopedPtrHashMap<std::string, scoped_ptr<ValidatorInfo>> |
| 70 ValidatorMap; |
44 | 71 |
45 // Internal constructor used by static DeviceCapabilities::Create*() methods. | 72 // Internal constructor used by static DeviceCapabilities::Create*() methods. |
46 DeviceCapabilitiesImpl(); | 73 DeviceCapabilitiesImpl(); |
47 | 74 |
48 void SetValidatedValueInternal(const std::string& path, | 75 void SetValidatedValue(const std::string& path, |
49 scoped_ptr<base::Value> new_value) override; | 76 scoped_ptr<base::Value> new_value) override; |
50 void UpdateStrAndNotifyChanged(const std::string& path); | |
51 | 77 |
52 scoped_ptr<base::DictionaryValue> capabilities_; | 78 // Lock for reading/writing data_ pointer |
53 scoped_ptr<const std::string> capabilities_str_; | 79 mutable base::Lock data_lock_; |
| 80 // Lock for reading/writing validator_map_ |
| 81 mutable base::Lock validation_lock_; |
| 82 |
| 83 scoped_refptr<Data> data_; |
| 84 // TaskRunner for capability writes. All internal writes to data_ must occur |
| 85 // on task_runner_for_writes_'s thread. |
| 86 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_for_writes_; |
54 | 87 |
55 ValidatorMap validator_map_; | 88 ValidatorMap validator_map_; |
56 base::ObserverList<Observer> observer_list_; | 89 const scoped_refptr<base::ObserverListThreadSafe<Observer>> observer_list_; |
57 base::ThreadChecker thread_checker_; | |
58 | 90 |
59 DISALLOW_COPY_AND_ASSIGN(DeviceCapabilitiesImpl); | 91 DISALLOW_COPY_AND_ASSIGN(DeviceCapabilitiesImpl); |
60 }; | 92 }; |
61 | 93 |
62 } // namespace chromecast | 94 } // namespace chromecast |
63 | 95 |
64 #endif // CHROMECAST_BASE_DEVICE_CAPABILITIES_IMPL_H_ | 96 #endif // CHROMECAST_BASE_DEVICE_CAPABILITIES_IMPL_H_ |
OLD | NEW |