1 // Copyright 2015 The Weave 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 LIBWEAVE_SRC_STATES_STATE_CHANGE_QUEUE_H_
6 #define LIBWEAVE_SRC_STATES_STATE_CHANGE_QUEUE_H_
7 
8 #include <map>
9 #include <memory>
10 #include <vector>
11 
12 #include <base/macros.h>
13 #include <base/time/time.h>
14 #include <base/values.h>
15 
16 namespace weave {
17 
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 {
StateChangeStateChange23   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 
30 // An object to record and retrieve device state change notification events.
31 class StateChangeQueue {
32  public:
33   explicit StateChangeQueue(size_t max_queue_size);
34 
35   bool NotifyPropertiesUpdated(base::Time timestamp,
36                                const base::DictionaryValue& changed_properties);
37   std::vector<StateChange> GetAndClearRecordedStateChanges();
38 
39  private:
40   // Maximum queue size. If it is full, the oldest state update records are
41   // merged together until the queue size is within the size limit.
42   const size_t max_queue_size_;
43 
44   // Accumulated list of device state change notifications.
45   std::map<base::Time, std::unique_ptr<base::DictionaryValue>> state_changes_;
46 
47   DISALLOW_COPY_AND_ASSIGN(StateChangeQueue);
48 };
49 
50 }  // namespace weave
51 
52 #endif  // LIBWEAVE_SRC_STATES_STATE_CHANGE_QUEUE_H_
53