blob: 3aef8d5edbba02bb5ff44a3912ae094a1f56d850 [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Alex Vakulenko57123b22014-10-28 13:50:16 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Vitaly Buka912b6982015-07-06 11:13:03 -07005#ifndef LIBWEAVE_SRC_STATES_STATE_CHANGE_QUEUE_H_
6#define LIBWEAVE_SRC_STATES_STATE_CHANGE_QUEUE_H_
Alex Vakulenko57123b22014-10-28 13:50:16 -07007
Alex Vakulenkoff73cf22014-10-29 09:53:52 -07008#include <map>
Alex Vakulenkod91d6252015-12-05 17:14:39 -08009#include <memory>
Alex Vakulenko57123b22014-10-28 13:50:16 -070010#include <vector>
11
12#include <base/macros.h>
Alex Vakulenkod91d6252015-12-05 17:14:39 -080013#include <base/time/time.h>
14#include <base/values.h>
Alex Vakulenko57123b22014-10-28 13:50:16 -070015
Vitaly Bukab6f015a2015-07-09 14:59:23 -070016namespace weave {
Alex Vakulenko57123b22014-10-28 13:50:16 -070017
Alex Vakulenkod91d6252015-12-05 17:14:39 -080018// 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.
22struct 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 Vakulenko57123b22014-10-28 13:50:16 -070030// An object to record and retrieve device state change notification events.
Alex Vakulenkod91d6252015-12-05 17:14:39 -080031class StateChangeQueue {
Alex Vakulenko57123b22014-10-28 13:50:16 -070032 public:
33 explicit StateChangeQueue(size_t max_queue_size);
34
Alex Vakulenko7d669212015-11-23 16:05:24 -080035 bool NotifyPropertiesUpdated(
36 base::Time timestamp,
Alex Vakulenkod91d6252015-12-05 17:14:39 -080037 const base::DictionaryValue& changed_properties);
38 std::vector<StateChange> GetAndClearRecordedStateChanges();
Alex Vakulenko57123b22014-10-28 13:50:16 -070039
40 private:
Alex Vakulenko57123b22014-10-28 13:50:16 -070041 // 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 Vakulenkoff73cf22014-10-29 09:53:52 -070043 const size_t max_queue_size_;
Alex Vakulenko57123b22014-10-28 13:50:16 -070044
45 // Accumulated list of device state change notifications.
Alex Vakulenko7d669212015-11-23 16:05:24 -080046 std::map<base::Time, std::unique_ptr<base::DictionaryValue>> state_changes_;
Alex Vakulenko57123b22014-10-28 13:50:16 -070047
Alex Vakulenko57123b22014-10-28 13:50:16 -070048 DISALLOW_COPY_AND_ASSIGN(StateChangeQueue);
49};
50
Vitaly Bukab6f015a2015-07-09 14:59:23 -070051} // namespace weave
Alex Vakulenko57123b22014-10-28 13:50:16 -070052
Vitaly Buka912b6982015-07-06 11:13:03 -070053#endif // LIBWEAVE_SRC_STATES_STATE_CHANGE_QUEUE_H_