Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 | #ifndef BUFFET_STATES_STATE_CHANGE_QUEUE_H_ |
| 6 | #define BUFFET_STATES_STATE_CHANGE_QUEUE_H_ |
| 7 | |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 8 | #include <map> |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
| 11 | #include <base/macros.h> |
| 12 | #include <base/threading/thread_checker.h> |
| 13 | |
| 14 | #include "buffet/states/state_change_queue_interface.h" |
| 15 | |
| 16 | namespace buffet { |
| 17 | |
| 18 | // An object to record and retrieve device state change notification events. |
| 19 | class StateChangeQueue : public StateChangeQueueInterface { |
| 20 | public: |
| 21 | explicit StateChangeQueue(size_t max_queue_size); |
| 22 | |
| 23 | // Overrides from StateChangeQueueInterface. |
| 24 | bool IsEmpty() const override { return state_changes_.empty(); } |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 25 | bool NotifyPropertiesUpdated( |
| 26 | base::Time timestamp, |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 27 | native_types::Object changed_properties) override; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 28 | std::vector<StateChange> GetAndClearRecordedStateChanges() override; |
Alex Vakulenko | e7a7007 | 2015-06-25 11:36:07 -0700 | [diff] [blame] | 29 | UpdateID GetLastStateChangeId() const override { return last_change_id_; } |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 30 | |
| 31 | private: |
| 32 | // To make sure we do not call NotifyPropertiesUpdated() and |
| 33 | // GetAndClearRecordedStateChanges() on different threads, |thread_checker_| |
| 34 | // is here to help us with verifying the single-threaded operation. |
| 35 | base::ThreadChecker thread_checker_; |
| 36 | |
| 37 | // Maximum queue size. If it is full, the oldest state update records are |
| 38 | // merged together until the queue size is within the size limit. |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 39 | const size_t max_queue_size_; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 40 | |
| 41 | // Accumulated list of device state change notifications. |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 42 | std::map<base::Time, native_types::Object> state_changes_; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 43 | |
Alex Vakulenko | e7a7007 | 2015-06-25 11:36:07 -0700 | [diff] [blame] | 44 | // An ID of last state change update. Each NotifyPropertiesUpdated() |
| 45 | // invocation increments this value by 1. |
| 46 | UpdateID last_change_id_{0}; |
| 47 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 48 | DISALLOW_COPY_AND_ASSIGN(StateChangeQueue); |
| 49 | }; |
| 50 | |
| 51 | } // namespace buffet |
| 52 | |
| 53 | #endif // BUFFET_STATES_STATE_CHANGE_QUEUE_H_ |