 Chromium Code Reviews
 Chromium Code Reviews Issue 879233003:
  Initial RemoteCommandService  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@remote-commands
    
  
    Issue 879233003:
  Initial RemoteCommandService  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@remote-commands| Index: components/policy/core/common/remote_commands/testing_remote_commands_server.cc | 
| diff --git a/components/policy/core/common/remote_commands/testing_remote_commands_server.cc b/components/policy/core/common/remote_commands/testing_remote_commands_server.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..96e2454670375f054848a54a9551cee75af20001 | 
| --- /dev/null | 
| +++ b/components/policy/core/common/remote_commands/testing_remote_commands_server.cc | 
| @@ -0,0 +1,126 @@ | 
| +// 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 "components/policy/core/common/remote_commands/testing_remote_commands_server.h" | 
| + | 
| +#include <algorithm> | 
| + | 
| +#include "base/bind.h" | 
| +#include "base/callback.h" | 
| +#include "base/location.h" | 
| +#include "base/logging.h" | 
| +#include "base/run_loop.h" | 
| +#include "base/single_thread_task_runner.h" | 
| +#include "base/thread_task_runner_handle.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; | 
| + | 
| +namespace policy { | 
| + | 
| +TestingRemoteCommandsServer::TestingRemoteCommandsServer() | 
| + : clock_(new base::DefaultClock()), | 
| + task_runner_(base::ThreadTaskRunnerHandle::Get()), | 
| + weak_ptr_to_this_(AsWeakPtr()) { | 
| +} | 
| + | 
| +TestingRemoteCommandsServer::~TestingRemoteCommandsServer() { | 
| + DCHECK(thread_checker_.CalledOnValidThread()); | 
| +} | 
| + | 
| +void TestingRemoteCommandsServer::IssueCommand(em::RemoteCommand_Type type, | 
| + const std::string& payload, | 
| + bool skip_next_fetch) { | 
| + DCHECK(thread_checker_.CalledOnValidThread()); | 
| + | 
| + base::AutoLock auto_lock(lock_); | 
| + | 
| + em::RemoteCommand command; | 
| + command.set_type(type); | 
| + command.set_unique_id(++last_generated_unique_id_); | 
| + if (!payload.empty()) | 
| + command.set_payload(payload); | 
| + command.set_timestamp( | 
| + (clock_->Now() - base::Time::UnixEpoch()).InMilliseconds()); | 
| + | 
| + if (skip_next_fetch) | 
| + commands_issued_after_next_fetch_.push_back(command); | 
| + else | 
| + commands_.push_back(command); | 
| +} | 
| + | 
| +TestingRemoteCommandsServer::RemoteCommandVector | 
| +TestingRemoteCommandsServer::FetchCommands( | 
| + RemoteCommandJob::UniqueIDType last_command_id, | 
| + const TestingRemoteCommandsServer::RemoteCommandResultVector& | 
| 
bartfab (slow)
2015/02/23 13:13:57
Nit: No need for the TestingRemoteCommandsServer::
 
binjin
2015/02/24 05:29:50
Done.
 | 
| + previous_job_results) { | 
| + base::AutoLock auto_lock(lock_); | 
| + | 
| + // Verify that acknowledged ID from client is non-decreasing. | 
| + EXPECT_LE(last_acknowledged_unique_id_, last_command_id); | 
| + last_acknowledged_unique_id_ = last_command_id; | 
| + | 
| + for (const auto& job_result : previous_job_results) { | 
| + EXPECT_TRUE(job_result.has_unique_id()); | 
| + EXPECT_TRUE(job_result.has_result()); | 
| + | 
| + EXPECT_GE(last_command_id, job_result.unique_id()); | 
| + | 
| + 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; | 
| + } | 
| + } | 
| + | 
| + // Verify that command result is for an existing commands actually expecting | 
| + // command result. | 
| + EXPECT_TRUE(found_command); | 
| + | 
| + // Post task to original thread for result reporting. | 
| + task_runner_->PostTask( | 
| + FROM_HERE, base::Bind(&TestingRemoteCommandsServer::ReportJobResult, | 
| + weak_ptr_to_this_, job_result)); | 
| + } | 
| + | 
| + TestingRemoteCommandsServer::RemoteCommandVector fetched_commands; | 
| 
bartfab (slow)
2015/02/23 13:13:57
Nit: No need for the TestingRemoteCommandsServer::
 
binjin
2015/02/24 05:29:50
Done.
 | 
| + for (const auto& command : commands_) { | 
| + if (command.unique_id() > last_command_id) | 
| + fetched_commands.push_back(command); | 
| + } | 
| + | 
| + // Push delayed commands into the main queue. | 
| + commands_.insert(commands_.end(), commands_issued_after_next_fetch_.begin(), | 
| + commands_issued_after_next_fetch_.end()); | 
| + commands_issued_after_next_fetch_.clear(); | 
| + | 
| + return fetched_commands; | 
| +} | 
| + | 
| +void TestingRemoteCommandsServer::SetClock(scoped_ptr<base::Clock> clock) { | 
| + DCHECK(thread_checker_.CalledOnValidThread()); | 
| + clock_ = clock.Pass(); | 
| +} | 
| + | 
| +size_t TestingRemoteCommandsServer::NumberOfCommandsPendingResult() const { | 
| + DCHECK(thread_checker_.CalledOnValidThread()); | 
| + return commands_.size(); | 
| +} | 
| + | 
| +void TestingRemoteCommandsServer::OnJobResultReported( | 
| + const em::RemoteCommandResult& job_result) const { | 
| +} | 
| + | 
| +void TestingRemoteCommandsServer::ReportJobResult( | 
| + const em::RemoteCommandResult& job_result) const { | 
| + DCHECK(thread_checker_.CalledOnValidThread()); | 
| + OnJobResultReported(job_result); | 
| +} | 
| + | 
| +} // namespace policy |