blob: e3b365010bc2806bce8f05c361e3c5aa457320a4 [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_INTERFACE_H_
6#define LIBWEAVE_SRC_STATES_STATE_CHANGE_QUEUE_INTERFACE_H_
Alex Vakulenko57123b22014-10-28 13:50:16 -07007
8#include <vector>
9
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070010#include <base/callback_list.h>
Alex Vakulenko57123b22014-10-28 13:50:16 -070011#include <base/time/time.h>
Alex Vakulenko57123b22014-10-28 13:50:16 -070012
Stefan Sauer2d16dfa2015-09-25 17:08:35 +020013#include "src/commands/schema_utils.h"
Anton Muhin01829452014-11-21 02:16:04 +040014
Vitaly Bukab6f015a2015-07-09 14:59:23 -070015namespace weave {
Alex Vakulenko57123b22014-10-28 13:50:16 -070016
17// A simple notification record event to track device state changes.
18// The |timestamp| records the time of the state change.
Alex Vakulenkoff73cf22014-10-29 09:53:52 -070019// |changed_properties| contains a property set with the new property values
20// which were updated at the time the event was recorded.
Alex Vakulenko57123b22014-10-28 13:50:16 -070021struct StateChange {
Vitaly Buka774cdf52015-07-21 13:55:00 -070022 StateChange(base::Time time, ValueMap properties)
Alex Vakulenko5ef75792015-03-19 15:50:44 -070023 : timestamp{time}, changed_properties{std::move(properties)} {}
Alex Vakulenko57123b22014-10-28 13:50:16 -070024 base::Time timestamp;
Vitaly Buka774cdf52015-07-21 13:55:00 -070025 ValueMap changed_properties;
Alex Vakulenko57123b22014-10-28 13:50:16 -070026};
27
28// An abstract interface to StateChangeQueue to record and retrieve state
29// change notification events.
30class StateChangeQueueInterface {
31 public:
Alex Vakulenkoe7a70072015-06-25 11:36:07 -070032 using UpdateID = uint64_t;
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070033 using Token =
34 std::unique_ptr<base::CallbackList<void(UpdateID)>::Subscription>;
Alex Vakulenkoe7a70072015-06-25 11:36:07 -070035
Alex Vakulenko57123b22014-10-28 13:50:16 -070036 // Returns true if the state change notification queue is empty.
37 virtual bool IsEmpty() const = 0;
38
39 // Called by StateManager when device state properties are updated.
Vitaly Buka774cdf52015-07-21 13:55:00 -070040 virtual bool NotifyPropertiesUpdated(base::Time timestamp,
41 ValueMap changed_properties) = 0;
Alex Vakulenko57123b22014-10-28 13:50:16 -070042
43 // Returns the recorded state changes since last time this method was called.
44 virtual std::vector<StateChange> GetAndClearRecordedStateChanges() = 0;
45
Alex Vakulenkoe7a70072015-06-25 11:36:07 -070046 // Returns an ID of last state change update. Each NotifyPropertiesUpdated()
47 // invocation increments this value by 1.
48 virtual UpdateID GetLastStateChangeId() const = 0;
49
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070050 // Subscribes for device state update notifications from cloud server.
51 // The |callback| will be called every time a state patch with given ID is
52 // successfully received and processed by GCD server.
53 // Returns a subscription token. As soon as this token is destroyed, the
54 // respective callback is removed from the callback list.
55 virtual Token AddOnStateUpdatedCallback(
56 const base::Callback<void(UpdateID)>& callback) WARN_UNUSED_RESULT = 0;
57
58 virtual void NotifyStateUpdatedOnServer(UpdateID update_id) = 0;
59
Alex Vakulenko57123b22014-10-28 13:50:16 -070060 protected:
61 // No one should attempt do destroy the queue through the interface.
Alex Vakulenkoe7a70072015-06-25 11:36:07 -070062 virtual ~StateChangeQueueInterface() {}
Alex Vakulenko57123b22014-10-28 13:50:16 -070063};
64
Vitaly Bukab6f015a2015-07-09 14:59:23 -070065} // namespace weave
Alex Vakulenko57123b22014-10-28 13:50:16 -070066
Vitaly Buka912b6982015-07-06 11:13:03 -070067#endif // LIBWEAVE_SRC_STATES_STATE_CHANGE_QUEUE_INTERFACE_H_