Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 1 | // 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 Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 10 | #include <queue> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 11 | #include <string> |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 12 | #include <utility> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 13 | |
Alex Vakulenko | 132617a | 2014-09-04 08:59:43 -0700 | [diff] [blame] | 14 | #include <base/macros.h> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 15 | |
| 16 | #include "buffet/commands/command_instance.h" |
| 17 | |
| 18 | namespace buffet { |
| 19 | |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 20 | class CommandDispachInterface; |
| 21 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 22 | class CommandQueue final { |
| 23 | public: |
| 24 | CommandQueue() = default; |
| 25 | |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 26 | // 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 Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 33 | // 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 Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 40 | // 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 Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 43 | |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 44 | // 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 Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 47 | |
| 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 Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 51 | CommandInstance* Find(const std::string& id) const; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 52 | |
| 53 | private: |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 54 | 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 Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 72 | // ID-to-CommandInstance map. |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 73 | std::map<std::string, std::unique_ptr<CommandInstance>> map_; |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 74 | |
| 75 | // Queue of commands to be removed. |
| 76 | std::queue<std::pair<base::Time, std::string>> remove_queue_; |
| 77 | |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 78 | // Callback interface for command dispatch, if provided. |
| 79 | CommandDispachInterface* dispatch_interface_ = nullptr; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 80 | |
| 81 | DISALLOW_COPY_AND_ASSIGN(CommandQueue); |
| 82 | }; |
| 83 | |
| 84 | } // namespace buffet |
| 85 | |
| 86 | #endif // BUFFET_COMMANDS_COMMAND_QUEUE_H_ |