Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium 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 "chrome/browser/policy/remote_commands/testing_remote_commands_server.h " | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "base/location.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/message_loop/message_loop_proxy.h" | |
| 15 #include "base/run_loop.h" | |
| 16 #include "base/single_thread_task_runner.h" | |
| 17 #include "base/time/clock.h" | |
| 18 #include "base/time/default_clock.h" | |
| 19 #include "base/time/time.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 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.
| |
| 23 | |
| 24 namespace policy { | |
| 25 | |
| 26 TestingRemoteCommandsServer::TestingRemoteCommandsServer() | |
| 27 : 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.
| |
| 28 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.
| |
| 29 clock_(new base::DefaultClock()), | |
| 30 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.
| |
| 31 } | |
| 32 | |
| 33 TestingRemoteCommandsServer::~TestingRemoteCommandsServer() { | |
| 34 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.
| |
| 35 } | |
| 36 | |
| 37 void TestingRemoteCommandsServer::IssueCommand( | |
| 38 enterprise_management::RemoteCommand_Type type, | |
| 39 const std::string& payload) { | |
| 40 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 41 | |
| 42 base::AutoLock auto_lock(lock_); | |
| 43 | |
| 44 em::RemoteCommand command; | |
| 45 command.set_type(type); | |
| 46 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.
| |
| 47 if (!payload.empty()) | |
| 48 command.set_payload(payload); | |
| 49 command.set_timestamp( | |
| 50 (clock_->Now() - base::Time::UnixEpoch()).InMilliseconds()); | |
| 51 | |
| 52 commands_.push_back(command); | |
| 53 } | |
| 54 | |
| 55 void TestingRemoteCommandsServer::SetClockForTesting( | |
| 56 scoped_ptr<base::Clock> clock) { | |
| 57 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 58 clock_ = clock.Pass(); | |
| 59 } | |
| 60 | |
| 61 void TestingRemoteCommandsServer::FetchCommandsFromIO( | |
| 62 RemoteCommandJob::UniqueIDType last_command_id, | |
| 63 const std::vector<enterprise_management::RemoteCommandResult>& | |
| 64 previous_job_results, | |
| 65 std::vector<enterprise_management::RemoteCommand>* fetched_commands) { | |
| 66 DCHECK(!task_runner_->BelongsToCurrentThread()); | |
| 67 | |
| 68 base::AutoLock auto_lock(lock_); | |
| 69 | |
| 70 DCHECK(fetched_commands); | |
| 71 fetched_commands->clear(); | |
| 72 | |
| 73 // Verify that acknowledged ID from client is non-decreasing. | |
| 74 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.
| |
| 75 last_acknowledged_id_ = last_command_id; | |
| 76 | |
| 77 for (const auto& job_result : previous_job_results) { | |
| 78 DCHECK(job_result.has_unique_id()); | |
| 79 DCHECK(job_result.has_result()); | |
| 80 | |
| 81 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.
| |
| 82 | |
| 83 bool found_command = false; | |
| 84 for (auto it = commands_.begin(); it != commands_.end(); ++it) { | |
| 85 if (it->unique_id() == job_result.unique_id()) { | |
| 86 commands_.erase(it); | |
| 87 found_command = true; | |
| 88 break; | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 EXPECT_TRUE(found_command); | |
| 93 | |
| 94 task_runner_->PostTask( | |
| 95 FROM_HERE, base::Bind(&TestingRemoteCommandsServer::JobResultReported, | |
| 96 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.
| |
| 97 } | |
| 98 | |
| 99 for (const auto& command : commands_) { | |
| 100 if (command.unique_id() > last_command_id) | |
| 101 fetched_commands->push_back(command); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 size_t TestingRemoteCommandsServer::GetUnreportedCommandCount() const { | |
| 106 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 107 return commands_.size(); | |
| 108 } | |
| 109 | |
| 110 } // namespace policy | |
| OLD | NEW |