Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_STATES_STATE_CHANGE_QUEUE_INTERFACE_H_ |
| 6 | #define LIBWEAVE_SRC_STATES_STATE_CHANGE_QUEUE_INTERFACE_H_ |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 7 | |
| 8 | #include <vector> |
| 9 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 10 | #include <base/callback_list.h> |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 11 | #include <base/time/time.h> |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 12 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 13 | #include "src/commands/schema_utils.h" |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 14 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 15 | namespace weave { |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 16 | |
| 17 | // A simple notification record event to track device state changes. |
| 18 | // The |timestamp| records the time of the state change. |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 19 | // |changed_properties| contains a property set with the new property values |
| 20 | // which were updated at the time the event was recorded. |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 21 | struct StateChange { |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 22 | StateChange(base::Time time, ValueMap properties) |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 23 | : timestamp{time}, changed_properties{std::move(properties)} {} |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 24 | base::Time timestamp; |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 25 | ValueMap changed_properties; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | // An abstract interface to StateChangeQueue to record and retrieve state |
| 29 | // change notification events. |
| 30 | class StateChangeQueueInterface { |
| 31 | public: |
Alex Vakulenko | e7a7007 | 2015-06-25 11:36:07 -0700 | [diff] [blame] | 32 | using UpdateID = uint64_t; |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 33 | using Token = |
| 34 | std::unique_ptr<base::CallbackList<void(UpdateID)>::Subscription>; |
Alex Vakulenko | e7a7007 | 2015-06-25 11:36:07 -0700 | [diff] [blame] | 35 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 36 | // 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 Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 40 | virtual bool NotifyPropertiesUpdated(base::Time timestamp, |
| 41 | ValueMap changed_properties) = 0; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 42 | |
| 43 | // Returns the recorded state changes since last time this method was called. |
| 44 | virtual std::vector<StateChange> GetAndClearRecordedStateChanges() = 0; |
| 45 | |
Alex Vakulenko | e7a7007 | 2015-06-25 11:36:07 -0700 | [diff] [blame] | 46 | // 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 Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 50 | // 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 Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 60 | protected: |
| 61 | // No one should attempt do destroy the queue through the interface. |
Alex Vakulenko | e7a7007 | 2015-06-25 11:36:07 -0700 | [diff] [blame] | 62 | virtual ~StateChangeQueueInterface() {} |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 65 | } // namespace weave |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 66 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 67 | #endif // LIBWEAVE_SRC_STATES_STATE_CHANGE_QUEUE_INTERFACE_H_ |