blob: 09339d98ce046ede57c5a0314398b12b69b32aca [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>
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070013
Alex Vakulenko132617a2014-09-04 08:59:43 -070014#include <base/macros.h>
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070015
16#include "buffet/commands/command_instance.h"
17
18namespace buffet {
19
Alex Vakulenko515b42b2014-08-07 15:46:31 -070020class CommandDispachInterface;
21
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070022class CommandQueue final {
23 public:
24 CommandQueue() = default;
25
Alex Vakulenko515b42b2014-08-07 15:46:31 -070026 // Sets a command dispatch notifications for changes in command queue.
27 // |dispatch_interface| must outlive the CommandQueue object instance
28 // or be nullptr.
29 void SetCommandDispachInterface(CommandDispachInterface* dispatch_interface) {
30 dispatch_interface_ = dispatch_interface;
31 }
32
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
Alex Vakulenko515b42b2014-08-07 15:46:31 -070078 // Callback interface for command dispatch, if provided.
79 CommandDispachInterface* dispatch_interface_ = nullptr;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070080
81 DISALLOW_COPY_AND_ASSIGN(CommandQueue);
82};
83
84} // namespace buffet
85
86#endif // BUFFET_COMMANDS_COMMAND_QUEUE_H_