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_H_ | 5 #ifndef CHROMECAST_BASE_DEVICE_CAPABILITIES_H_ |
6 #define CHROMECAST_BASE_DEVICE_CAPABILITIES_H_ | 6 #define CHROMECAST_BASE_DEVICE_CAPABILITIES_H_ |
7 | 7 |
| 8 #include <memory> |
8 #include <string> | 9 #include <string> |
9 | 10 |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 class DictionaryValue; | 15 class DictionaryValue; |
16 class Value; | 16 class Value; |
17 } | 17 } |
18 | 18 |
19 namespace chromecast { | 19 namespace chromecast { |
20 | 20 |
21 // Device capabilities are a set of features used to determine what operations | 21 // Device capabilities are a set of features used to determine what operations |
22 // are available on the device. They are identified by a key (string) and a | 22 // are available on the device. They are identified by a key (string) and a |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 public: | 68 public: |
69 // |path| is full path to capability, which could include paths expanded on | 69 // |path| is full path to capability, which could include paths expanded on |
70 // the capability key that gets registered through the Register() method. | 70 // the capability key that gets registered through the Register() method. |
71 // For example, if a key of "foo" is registered for a Validator, |path| | 71 // For example, if a key of "foo" is registered for a Validator, |path| |
72 // could be "foo", "foo.bar", "foo.bar.what", etc. |proposed_value| is new | 72 // could be "foo", "foo.bar", "foo.bar.what", etc. |proposed_value| is new |
73 // value being proposed for |path|. Determines if |proposed_value| is valid | 73 // value being proposed for |path|. Determines if |proposed_value| is valid |
74 // change for |path|. This method may be asynchronous, but multiple calls | 74 // change for |path|. This method may be asynchronous, but multiple calls |
75 // to it must be handled serially. Returns response through | 75 // to it must be handled serially. Returns response through |
76 // SetValidatedValue(). | 76 // SetValidatedValue(). |
77 virtual void Validate(const std::string& path, | 77 virtual void Validate(const std::string& path, |
78 scoped_ptr<base::Value> proposed_value) = 0; | 78 std::unique_ptr<base::Value> proposed_value) = 0; |
79 | 79 |
80 protected: | 80 protected: |
81 explicit Validator(DeviceCapabilities* capabilities); | 81 explicit Validator(DeviceCapabilities* capabilities); |
82 virtual ~Validator() {} | 82 virtual ~Validator() {} |
83 | 83 |
84 DeviceCapabilities* capabilities() const { return capabilities_; } | 84 DeviceCapabilities* capabilities() const { return capabilities_; } |
85 | 85 |
86 // Meant to be called when Validate() has finished. |path| is full path to | 86 // Meant to be called when Validate() has finished. |path| is full path to |
87 // capability. |new_value| is new validated value to be used in | 87 // capability. |new_value| is new validated value to be used in |
88 // DeviceCapabilities. This method passes these parameters to | 88 // DeviceCapabilities. This method passes these parameters to |
89 // DeviceCapabilities, where |path| is updated internally to |new_value|. | 89 // DeviceCapabilities, where |path| is updated internally to |new_value|. |
90 void SetValidatedValue(const std::string& path, | 90 void SetValidatedValue(const std::string& path, |
91 scoped_ptr<base::Value> new_value) const; | 91 std::unique_ptr<base::Value> new_value) const; |
92 | 92 |
93 private: | 93 private: |
94 DeviceCapabilities* const capabilities_; | 94 DeviceCapabilities* const capabilities_; |
95 | 95 |
96 DISALLOW_COPY_AND_ASSIGN(Validator); | 96 DISALLOW_COPY_AND_ASSIGN(Validator); |
97 }; | 97 }; |
98 | 98 |
99 // Class used to store/own capabilities-related data. It is immutable and | 99 // Class used to store/own capabilities-related data. It is immutable and |
100 // RefCountedThreadSafe, so client code can freely query it throughout its | 100 // RefCountedThreadSafe, so client code can freely query it throughout its |
101 // lifetime without worrying about the data getting invalidated in any way. | 101 // lifetime without worrying about the data getting invalidated in any way. |
102 class Data : public base::RefCountedThreadSafe<Data> { | 102 class Data : public base::RefCountedThreadSafe<Data> { |
103 public: | 103 public: |
104 // Accessor for complete capabilities in dictionary format. | 104 // Accessor for complete capabilities in dictionary format. |
105 const base::DictionaryValue& dictionary() const { | 105 const base::DictionaryValue& dictionary() const { |
106 return *dictionary_.get(); | 106 return *dictionary_.get(); |
107 } | 107 } |
108 | 108 |
109 // Accessor for complete capabilities string in JSON format. | 109 // Accessor for complete capabilities string in JSON format. |
110 const std::string& json_string() const { return *json_string_.get(); } | 110 const std::string& json_string() const { return *json_string_.get(); } |
111 | 111 |
112 private: | 112 private: |
113 friend class base::RefCountedThreadSafe<Data>; | 113 friend class base::RefCountedThreadSafe<Data>; |
114 // DeviceCapabilities should be the only one responsible for Data | 114 // DeviceCapabilities should be the only one responsible for Data |
115 // construction. See CreateData() methods. | 115 // construction. See CreateData() methods. |
116 friend class DeviceCapabilities; | 116 friend class DeviceCapabilities; |
117 | 117 |
118 // Constructs empty dictionary with no capabilities. | 118 // Constructs empty dictionary with no capabilities. |
119 Data(); | 119 Data(); |
120 // Uses |dictionary| as capabilities dictionary. | 120 // Uses |dictionary| as capabilities dictionary. |
121 explicit Data(scoped_ptr<const base::DictionaryValue> dictionary); | 121 explicit Data(std::unique_ptr<const base::DictionaryValue> dictionary); |
122 ~Data(); | 122 ~Data(); |
123 | 123 |
124 const scoped_ptr<const base::DictionaryValue> dictionary_; | 124 const std::unique_ptr<const base::DictionaryValue> dictionary_; |
125 const scoped_ptr<const std::string> json_string_; | 125 const std::unique_ptr<const std::string> json_string_; |
126 | 126 |
127 DISALLOW_COPY_AND_ASSIGN(Data); | 127 DISALLOW_COPY_AND_ASSIGN(Data); |
128 }; | 128 }; |
129 | 129 |
130 // Default Capability keys | 130 // Default Capability keys |
131 static const char kKeyBluetoothSupported[]; | 131 static const char kKeyBluetoothSupported[]; |
132 static const char kKeyDisplaySupported[]; | 132 static const char kKeyDisplaySupported[]; |
133 static const char kKeyHiResAudioSupported[]; | 133 static const char kKeyHiResAudioSupported[]; |
134 | 134 |
135 // This class should get destroyed after all Validators have been | 135 // This class should get destroyed after all Validators have been |
136 // unregistered, all Observers have been removed, and the class is no longer | 136 // unregistered, all Observers have been removed, and the class is no longer |
137 // being accessed. | 137 // being accessed. |
138 virtual ~DeviceCapabilities() {} | 138 virtual ~DeviceCapabilities() {} |
139 | 139 |
140 // Create empty instance with no capabilities. Although the class is not | 140 // Create empty instance with no capabilities. Although the class is not |
141 // singleton, there is meant to be a single instance owned by another module. | 141 // singleton, there is meant to be a single instance owned by another module. |
142 // The instance should be created early enough for all managers to register | 142 // The instance should be created early enough for all managers to register |
143 // themselves, and then live long enough for all managers to unregister. | 143 // themselves, and then live long enough for all managers to unregister. |
144 static scoped_ptr<DeviceCapabilities> Create(); | 144 static std::unique_ptr<DeviceCapabilities> Create(); |
145 // Creates an instance where all the default capabilities are initialized | 145 // Creates an instance where all the default capabilities are initialized |
146 // to a predefined default value, and no Validators are registered. For use | 146 // to a predefined default value, and no Validators are registered. For use |
147 // only in unit tests. | 147 // only in unit tests. |
148 static scoped_ptr<DeviceCapabilities> CreateForTesting(); | 148 static std::unique_ptr<DeviceCapabilities> CreateForTesting(); |
149 | 149 |
150 // Registers a Validator for a capability. A given key must only be | 150 // Registers a Validator for a capability. A given key must only be |
151 // registered once, and must be unregistered before calling Register() again. | 151 // registered once, and must be unregistered before calling Register() again. |
152 // If the capability has a value of Dictionary type, |key| must be just | 152 // If the capability has a value of Dictionary type, |key| must be just |
153 // the capability's top-level key and not include path expansions to levels | 153 // the capability's top-level key and not include path expansions to levels |
154 // farther down. For example, "foo" is a valid value for |key|, but "foo.bar" | 154 // farther down. For example, "foo" is a valid value for |key|, but "foo.bar" |
155 // is not. Note that if "foo.bar" is updated in SetCapability(), the | 155 // is not. Note that if "foo.bar" is updated in SetCapability(), the |
156 // Validate() method for "foo"'s Validator will be called, with a |path| of | 156 // Validate() method for "foo"'s Validator will be called, with a |path| of |
157 // "foo.bar". Note that this method does not add or modify the capability. | 157 // "foo.bar". Note that this method does not add or modify the capability. |
158 // To do this, SetCapability() should be called, or Validators can call | 158 // To do this, SetCapability() should be called, or Validators can call |
(...skipping 14 matching lines...) Expand all Loading... |
173 | 173 |
174 // Accessors for default capabilities. Note that the capability must be added | 174 // Accessors for default capabilities. Note that the capability must be added |
175 // through SetCapability() or SetValidatedValue() (for Validators) before | 175 // through SetCapability() or SetValidatedValue() (for Validators) before |
176 // accessors are called. | 176 // accessors are called. |
177 virtual bool BluetoothSupported() const = 0; | 177 virtual bool BluetoothSupported() const = 0; |
178 virtual bool DisplaySupported() const = 0; | 178 virtual bool DisplaySupported() const = 0; |
179 virtual bool HiResAudioSupported() const = 0; | 179 virtual bool HiResAudioSupported() const = 0; |
180 | 180 |
181 // Returns a deep copy of the value at |path|. If the capability at |path| | 181 // Returns a deep copy of the value at |path|. If the capability at |path| |
182 // does not exist, a null scoped_ptr is returned. | 182 // does not exist, a null scoped_ptr is returned. |
183 virtual scoped_ptr<base::Value> GetCapability( | 183 virtual std::unique_ptr<base::Value> GetCapability( |
184 const std::string& path) const = 0; | 184 const std::string& path) const = 0; |
185 | 185 |
186 // Use this method to access dictionary and JSON string. No deep copying is | 186 // Use this method to access dictionary and JSON string. No deep copying is |
187 // performed, so this method is inexpensive. Note that any capability updates | 187 // performed, so this method is inexpensive. Note that any capability updates |
188 // that occur after GetData() has been called will not be reflected in the | 188 // that occur after GetData() has been called will not be reflected in the |
189 // returned scoped_refptr. You can think of this method as taking a snapshot | 189 // returned scoped_refptr. You can think of this method as taking a snapshot |
190 // of the capabilities when it gets called. | 190 // of the capabilities when it gets called. |
191 virtual scoped_refptr<Data> GetData() const = 0; | 191 virtual scoped_refptr<Data> GetData() const = 0; |
192 | 192 |
193 // Updates the value at |path| to |proposed_value| if |path| already exists | 193 // Updates the value at |path| to |proposed_value| if |path| already exists |
194 // and adds new capability if |path| doesn't. Note that if a key has been | 194 // and adds new capability if |path| doesn't. Note that if a key has been |
195 // registered that is at the beginning of |path|, then the Validator will be | 195 // registered that is at the beginning of |path|, then the Validator will be |
196 // used to determine if |proposed_value| is accepted. | 196 // used to determine if |proposed_value| is accepted. |
197 // Ex: If "foo" has a Validator registered, a |path| of "foo.bar" | 197 // Ex: If "foo" has a Validator registered, a |path| of "foo.bar" |
198 // will cause |proposed_value| to go through the Validator's Validate() | 198 // will cause |proposed_value| to go through the Validator's Validate() |
199 // method. Client code may use the Observer interface to determine the | 199 // method. Client code may use the Observer interface to determine the |
200 // ultimate value used. This method is asynchronous. | 200 // ultimate value used. This method is asynchronous. |
201 virtual void SetCapability(const std::string& path, | 201 virtual void SetCapability(const std::string& path, |
202 scoped_ptr<base::Value> proposed_value) = 0; | 202 std::unique_ptr<base::Value> proposed_value) = 0; |
203 // Iterates through entries in |dict_value| and calls SetCapability() for | 203 // Iterates through entries in |dict_value| and calls SetCapability() for |
204 // each one. This method is asynchronous. | 204 // each one. This method is asynchronous. |
205 virtual void MergeDictionary(const base::DictionaryValue& dict_value) = 0; | 205 virtual void MergeDictionary(const base::DictionaryValue& dict_value) = 0; |
206 | 206 |
207 // Adds/removes an observer. It doesn't take the ownership of |observer|. | 207 // Adds/removes an observer. It doesn't take the ownership of |observer|. |
208 virtual void AddCapabilitiesObserver(Observer* observer) = 0; | 208 virtual void AddCapabilitiesObserver(Observer* observer) = 0; |
209 virtual void RemoveCapabilitiesObserver(Observer* observer) = 0; | 209 virtual void RemoveCapabilitiesObserver(Observer* observer) = 0; |
210 | 210 |
211 protected: | 211 protected: |
212 DeviceCapabilities() {} | 212 DeviceCapabilities() {} |
213 | 213 |
214 // For derived implementation classes to create Data instances since they do | 214 // For derived implementation classes to create Data instances since they do |
215 // not have access to Data constructors. | 215 // not have access to Data constructors. |
216 // Creates empty dictionary with no capabilities. | 216 // Creates empty dictionary with no capabilities. |
217 static scoped_refptr<Data> CreateData(); | 217 static scoped_refptr<Data> CreateData(); |
218 // Uses |dictionary| as capabilities dictionary. | 218 // Uses |dictionary| as capabilities dictionary. |
219 static scoped_refptr<Data> CreateData( | 219 static scoped_refptr<Data> CreateData( |
220 scoped_ptr<const base::DictionaryValue> dictionary); | 220 std::unique_ptr<const base::DictionaryValue> dictionary); |
221 | 221 |
222 private: | 222 private: |
223 // Does actual internal update of |path| to |new_value|. | 223 // Does actual internal update of |path| to |new_value|. |
224 virtual void SetValidatedValue(const std::string& path, | 224 virtual void SetValidatedValue(const std::string& path, |
225 scoped_ptr<base::Value> new_value) = 0; | 225 std::unique_ptr<base::Value> new_value) = 0; |
226 | 226 |
227 DISALLOW_COPY_AND_ASSIGN(DeviceCapabilities); | 227 DISALLOW_COPY_AND_ASSIGN(DeviceCapabilities); |
228 }; | 228 }; |
229 | 229 |
230 } // namespace chromecast | 230 } // namespace chromecast |
231 | 231 |
232 #endif // CHROMECAST_BASE_DEVICE_CAPABILITIES_H_ | 232 #endif // CHROMECAST_BASE_DEVICE_CAPABILITIES_H_ |
OLD | NEW |