blob: 9178c8a17c72bf852056afb7585dbd181748e456 [file] [log] [blame]
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -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_COMMANDS_COMMAND_QUEUE_H_
6#define BUFFET_COMMANDS_COMMAND_QUEUE_H_
7
8#include <map>
9#include <memory>
Vitaly Buka2a9b30f2015-04-01 10:51:59 -070010#include <queue>
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070011#include <string>
Vitaly Buka2a9b30f2015-04-01 10:51:59 -070012#include <utility>
Vitaly Bukaae0f3a12015-05-11 16:27:30 -070013#include <vector>
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070014
Vitaly Bukaae0f3a12015-05-11 16:27:30 -070015#include <base/callback.h>
Alex Vakulenko132617a2014-09-04 08:59:43 -070016#include <base/macros.h>
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070017
18#include "buffet/commands/command_instance.h"
19
20namespace buffet {
21
22class CommandQueue final {
23 public:
Vitaly Bukaae0f3a12015-05-11 16:27:30 -070024 using Callback = base::Callback<void(CommandInstance*)>;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070025 CommandQueue() = default;
26
Vitaly Bukaae0f3a12015-05-11 16:27:30 -070027 // Adds notifications callback for a new command is added to the queue.
28 void AddOnCommandAddedCallback(const Callback& callback);
29
30 // Adds notifications callback for a command is removed from the queue.
31 void AddOnCommandRemovedCallback(const Callback& callback);
Alex Vakulenko515b42b2014-08-07 15:46:31 -070032
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070033 // Checks if the command queue is empty.
34 bool IsEmpty() const { return map_.empty(); }
35
36 // Returns the number of commands in the queue.
37 size_t GetCount() const { return map_.size(); }
38
39 // Adds a new command to the queue. Each command in the queue has a unique
Anton Muhin5191e812014-10-30 17:49:48 +040040 // ID that identifies that command instance in this queue.
41 // One shouldn't attempt to add a command with the same ID.
42 void Add(std::unique_ptr<CommandInstance> instance);
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070043
Vitaly Buka2a9b30f2015-04-01 10:51:59 -070044 // Selects command identified by |id| ready for removal. Command will actually
45 // be removed after some time.
46 void DelayedRemove(const std::string& id);
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070047
48 // Finds a command instance in the queue by the instance |id|. Returns
49 // nullptr if the command with the given |id| is not found. The returned
50 // pointer should not be persisted for a long period of time.
Alex Vakulenkofedc4872014-08-20 12:38:43 -070051 CommandInstance* Find(const std::string& id) const;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070052
53 private:
Vitaly Buka2a9b30f2015-04-01 10:51:59 -070054 friend class CommandQueueTest;
55 friend class DBusCommandDispacherTest;
56
57 // Removes a command identified by |id| from the queue.
58 bool Remove(const std::string& id);
59
60 // Removes old commands selected with DelayedRemove.
61 void Cleanup();
62
63 // Overrides CommandQueue::Now() for tests.
64 void SetNowForTest(base::Time now);
65
66 // Returns current time.
67 base::Time Now() const;
68
69 // Overrided value to be returned from Now().
70 base::Time test_now_;
71
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070072 // ID-to-CommandInstance map.
Alex Vakulenkofedc4872014-08-20 12:38:43 -070073 std::map<std::string, std::unique_ptr<CommandInstance>> map_;
Vitaly Buka2a9b30f2015-04-01 10:51:59 -070074
75 // Queue of commands to be removed.
76 std::queue<std::pair<base::Time, std::string>> remove_queue_;
77
Vitaly Bukaae0f3a12015-05-11 16:27:30 -070078 using CallbackList = std::vector<Callback>;
79 CallbackList on_command_added_;
80 CallbackList on_command_removed_;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070081
82 DISALLOW_COPY_AND_ASSIGN(CommandQueue);
83};
84
85} // namespace buffet
86
87#endif // BUFFET_COMMANDS_COMMAND_QUEUE_H_