Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_STATES_STATE_CHANGE_QUEUE_H_ |
| 6 | #define LIBWEAVE_SRC_STATES_STATE_CHANGE_QUEUE_H_ |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 7 | |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 8 | #include <map> |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 9 | #include <memory> |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
| 12 | #include <base/macros.h> |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 13 | #include <base/time/time.h> |
| 14 | #include <base/values.h> |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 15 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 16 | namespace weave { |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 17 | |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 18 | // A simple notification record event to track device state changes. |
| 19 | // The |timestamp| records the time of the state change. |
| 20 | // |changed_properties| contains a property set with the new property values |
| 21 | // which were updated at the time the event was recorded. |
| 22 | struct StateChange { |
| 23 | StateChange(base::Time time, |
| 24 | std::unique_ptr<base::DictionaryValue> properties) |
| 25 | : timestamp{time}, changed_properties{std::move(properties)} {} |
| 26 | base::Time timestamp; |
| 27 | std::unique_ptr<base::DictionaryValue> changed_properties; |
| 28 | }; |
| 29 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 30 | // An object to record and retrieve device state change notification events. |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 31 | class StateChangeQueue { |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 32 | public: |
| 33 | explicit StateChangeQueue(size_t max_queue_size); |
| 34 | |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 35 | bool NotifyPropertiesUpdated( |
| 36 | base::Time timestamp, |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 37 | const base::DictionaryValue& changed_properties); |
| 38 | std::vector<StateChange> GetAndClearRecordedStateChanges(); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 39 | |
| 40 | private: |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 41 | // Maximum queue size. If it is full, the oldest state update records are |
| 42 | // merged together until the queue size is within the size limit. |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 43 | const size_t max_queue_size_; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 44 | |
| 45 | // Accumulated list of device state change notifications. |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 46 | std::map<base::Time, std::unique_ptr<base::DictionaryValue>> state_changes_; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 47 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 48 | DISALLOW_COPY_AND_ASSIGN(StateChangeQueue); |
| 49 | }; |
| 50 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 51 | } // namespace weave |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 52 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 53 | #endif // LIBWEAVE_SRC_STATES_STATE_CHANGE_QUEUE_H_ |