blob: 9181ef4f06c115abeeebac7cbbf9ed162e6017a9 [file] [log] [blame]
Alex Vakulenko57123b22014-10-28 13:50:16 -07001// 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 Vakulenkoff73cf22014-10-29 09:53:52 -07008#include <map>
Alex Vakulenko57123b22014-10-28 13:50:16 -07009#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
16namespace buffet {
17
18// An object to record and retrieve device state change notification events.
19class 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 Vakulenkoff73cf22014-10-29 09:53:52 -070025 bool NotifyPropertiesUpdated(
26 base::Time timestamp,
Anton Muhin01829452014-11-21 02:16:04 +040027 native_types::Object changed_properties) override;
Alex Vakulenko57123b22014-10-28 13:50:16 -070028 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 Vakulenkoff73cf22014-10-29 09:53:52 -070038 const size_t max_queue_size_;
Alex Vakulenko57123b22014-10-28 13:50:16 -070039
40 // Accumulated list of device state change notifications.
Anton Muhin01829452014-11-21 02:16:04 +040041 std::map<base::Time, native_types::Object> state_changes_;
Alex Vakulenko57123b22014-10-28 13:50:16 -070042
43 DISALLOW_COPY_AND_ASSIGN(StateChangeQueue);
44};
45
46} // namespace buffet
47
48#endif // BUFFET_STATES_STATE_CHANGE_QUEUE_H_