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 | #include "buffet/commands/command_queue.h" |
| 6 | |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 7 | #include <base/bind.h> |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 8 | #include <base/time/time.h> |
| 9 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 10 | namespace buffet { |
| 11 | |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 12 | namespace { |
| 13 | const int kRemoveCommandDelayMin = 5; |
| 14 | } |
| 15 | |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 16 | void CommandQueue::AddOnCommandAddedCallback(const Callback& callback) { |
| 17 | on_command_added_.push_back(callback); |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 18 | // Send all pre-existed commands. |
| 19 | for (const auto& command : map_) |
| 20 | callback.Run(command.second.get()); |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | void CommandQueue::AddOnCommandRemovedCallback(const Callback& callback) { |
| 24 | on_command_removed_.push_back(callback); |
| 25 | } |
| 26 | |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 27 | void CommandQueue::Add(std::unique_ptr<CommandInstance> instance) { |
| 28 | std::string id = instance->GetID(); |
| 29 | LOG_IF(FATAL, id.empty()) << "Command has no ID"; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 30 | instance->SetCommandQueue(this); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 31 | auto pair = map_.insert(std::make_pair(id, std::move(instance))); |
| 32 | LOG_IF(FATAL, !pair.second) << "Command with ID '" << id |
| 33 | << "' is already in the queue"; |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 34 | for (const auto& cb : on_command_added_) |
| 35 | cb.Run(pair.first->second.get()); |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 36 | Cleanup(); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 39 | void CommandQueue::DelayedRemove(const std::string& id) { |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 40 | auto p = map_.find(id); |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 41 | if (p == map_.end()) |
| 42 | return; |
| 43 | remove_queue_.push(std::make_pair( |
| 44 | base::Time::Now() + base::TimeDelta::FromMinutes(kRemoveCommandDelayMin), |
| 45 | id)); |
| 46 | Cleanup(); |
| 47 | } |
| 48 | |
| 49 | bool CommandQueue::Remove(const std::string& id) { |
| 50 | auto p = map_.find(id); |
| 51 | if (p == map_.end()) |
| 52 | return false; |
| 53 | std::unique_ptr<CommandInstance> instance{std::move(p->second)}; |
| 54 | instance->SetCommandQueue(nullptr); |
| 55 | map_.erase(p); |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 56 | for (const auto& cb : on_command_removed_) |
| 57 | cb.Run(instance.get()); |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 58 | return true; |
| 59 | } |
| 60 | |
| 61 | void CommandQueue::Cleanup() { |
| 62 | while (!remove_queue_.empty() && remove_queue_.front().first < Now()) { |
| 63 | Remove(remove_queue_.front().second); |
| 64 | remove_queue_.pop(); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 65 | } |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void CommandQueue::SetNowForTest(base::Time now) { |
| 69 | test_now_ = now; |
| 70 | } |
| 71 | |
| 72 | base::Time CommandQueue::Now() const { |
| 73 | return test_now_.is_null() ? base::Time::Now() : test_now_; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 76 | CommandInstance* CommandQueue::Find(const std::string& id) const { |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 77 | auto p = map_.find(id); |
| 78 | return (p != map_.end()) ? p->second.get() : nullptr; |
| 79 | } |
| 80 | |
| 81 | } // namespace buffet |