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; |
| 29 | |
| 30 | private: |
| 31 | // To make sure we do not call NotifyPropertiesUpdated() and |
| 32 | // GetAndClearRecordedStateChanges() on different threads, |thread_checker_| |
| 33 | // is here to help us with verifying the single-threaded operation. |
| 34 | base::ThreadChecker thread_checker_; |
| 35 | |
| 36 | // Maximum queue size. If it is full, the oldest state update records are |
| 37 | // merged together until the queue size is within the size limit. |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 38 | const size_t max_queue_size_; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 39 | |
| 40 | // Accumulated list of device state change notifications. |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 41 | std::map<base::Time, native_types::Object> state_changes_; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 42 | |
| 43 | DISALLOW_COPY_AND_ASSIGN(StateChangeQueue); |
| 44 | }; |
| 45 | |
| 46 | } // namespace buffet |
| 47 | |
| 48 | #endif // BUFFET_STATES_STATE_CHANGE_QUEUE_H_ |