Chromium Code Reviews| Index: chrome/browser/policy/remote_commands/testing_remote_commands_server.cc |
| diff --git a/chrome/browser/policy/remote_commands/testing_remote_commands_server.cc b/chrome/browser/policy/remote_commands/testing_remote_commands_server.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..83ba8c96e0ee50e4bd582558471b2c5cebdc62ce |
| --- /dev/null |
| +++ b/chrome/browser/policy/remote_commands/testing_remote_commands_server.cc |
| @@ -0,0 +1,110 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/policy/remote_commands/testing_remote_commands_server.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/callback.h" |
| +#include "base/location.h" |
| +#include "base/logging.h" |
| +#include "base/message_loop/message_loop_proxy.h" |
| +#include "base/run_loop.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/time/clock.h" |
| +#include "base/time/default_clock.h" |
| +#include "base/time/time.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace em = enterprise_management; |
|
bartfab (slow)
2015/02/12 14:29:18
Nit: No need to alias the namespace if you only us
binjin
2015/02/16 22:46:23
Acknowledged.
|
| + |
| +namespace policy { |
| + |
| +TestingRemoteCommandsServer::TestingRemoteCommandsServer() |
| + : last_acknowledged_id_(0), |
|
bartfab (slow)
2015/02/12 14:29:19
Nit: In C++ 11, you can initialize ast_acknowledge
binjin
2015/02/16 22:46:23
Done.
|
| + last_generated_unique_id_(1), |
|
bartfab (slow)
2015/02/12 14:29:18
Nit: 0 is sufficient.
binjin
2015/02/16 22:46:23
Done.
|
| + clock_(new base::DefaultClock()), |
| + task_runner_(base::MessageLoopProxy::current()) { |
|
bartfab (slow)
2015/02/12 14:29:18
Nit: base::MessageLoopProxy is deprecated. Use bas
binjin
2015/02/16 22:46:23
Done.
|
| +} |
| + |
| +TestingRemoteCommandsServer::~TestingRemoteCommandsServer() { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
|
bartfab (slow)
2015/02/12 14:29:19
Nit: Use a base::ThreadChecker for this.
binjin
2015/02/16 22:46:23
Done.
|
| +} |
| + |
| +void TestingRemoteCommandsServer::IssueCommand( |
| + enterprise_management::RemoteCommand_Type type, |
| + const std::string& payload) { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + |
| + base::AutoLock auto_lock(lock_); |
| + |
| + em::RemoteCommand command; |
| + command.set_type(type); |
| + command.set_unique_id(last_generated_unique_id_++); |
|
bartfab (slow)
2015/02/12 14:29:18
Nit: If this really is the last generated ID, you
binjin
2015/02/16 22:46:23
Done.
|
| + if (!payload.empty()) |
| + command.set_payload(payload); |
| + command.set_timestamp( |
| + (clock_->Now() - base::Time::UnixEpoch()).InMilliseconds()); |
| + |
| + commands_.push_back(command); |
| +} |
| + |
| +void TestingRemoteCommandsServer::SetClockForTesting( |
| + scoped_ptr<base::Clock> clock) { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + clock_ = clock.Pass(); |
| +} |
| + |
| +void TestingRemoteCommandsServer::FetchCommandsFromIO( |
| + RemoteCommandJob::UniqueIDType last_command_id, |
| + const std::vector<enterprise_management::RemoteCommandResult>& |
| + previous_job_results, |
| + std::vector<enterprise_management::RemoteCommand>* fetched_commands) { |
| + DCHECK(!task_runner_->BelongsToCurrentThread()); |
| + |
| + base::AutoLock auto_lock(lock_); |
| + |
| + DCHECK(fetched_commands); |
| + fetched_commands->clear(); |
| + |
| + // Verify that acknowledged ID from client is non-decreasing. |
| + EXPECT_GE(last_command_id, last_acknowledged_id_); |
|
bartfab (slow)
2015/02/12 14:29:18
Nit: Expected value first, actual second.
binjin
2015/02/16 22:46:23
Done.
|
| + last_acknowledged_id_ = last_command_id; |
| + |
| + for (const auto& job_result : previous_job_results) { |
| + DCHECK(job_result.has_unique_id()); |
| + DCHECK(job_result.has_result()); |
| + |
| + EXPECT_LE(job_result.has_unique_id(), last_command_id); |
|
bartfab (slow)
2015/02/12 14:29:19
Nit: Expected value first, actual second.
binjin
2015/02/16 22:46:22
Done.
|
| + |
| + bool found_command = false; |
| + for (auto it = commands_.begin(); it != commands_.end(); ++it) { |
| + if (it->unique_id() == job_result.unique_id()) { |
| + commands_.erase(it); |
| + found_command = true; |
| + break; |
| + } |
| + } |
| + |
| + EXPECT_TRUE(found_command); |
| + |
| + task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&TestingRemoteCommandsServer::JobResultReported, |
| + base::Unretained(this), job_result)); |
|
bartfab (slow)
2015/02/12 14:29:18
Do not use base::Unretained when posting across th
binjin
2015/02/16 22:46:23
Done.
|
| + } |
| + |
| + for (const auto& command : commands_) { |
| + if (command.unique_id() > last_command_id) |
| + fetched_commands->push_back(command); |
| + } |
| +} |
| + |
| +size_t TestingRemoteCommandsServer::GetUnreportedCommandCount() const { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + return commands_.size(); |
| +} |
| + |
| +} // namespace policy |