| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/policy/core/common/remote_commands/testing_remote_commands_
server.h" | 5 #include "components/policy/core/common/remote_commands/testing_remote_commands_
server.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 em::RemoteCommand_Type type, | 55 em::RemoteCommand_Type type, |
| 56 const std::string& payload, | 56 const std::string& payload, |
| 57 const ResultReportedCallback& reported_callback, | 57 const ResultReportedCallback& reported_callback, |
| 58 bool skip_next_fetch) { | 58 bool skip_next_fetch) { |
| 59 DCHECK(thread_checker_.CalledOnValidThread()); | 59 DCHECK(thread_checker_.CalledOnValidThread()); |
| 60 | 60 |
| 61 base::AutoLock auto_lock(lock_); | 61 base::AutoLock auto_lock(lock_); |
| 62 | 62 |
| 63 em::RemoteCommand command; | 63 em::RemoteCommand command; |
| 64 command.set_type(type); | 64 command.set_type(type); |
| 65 command.set_unique_id(++last_generated_unique_id_); | 65 command.set_command_id(++last_generated_unique_id_); |
| 66 if (!payload.empty()) | 66 if (!payload.empty()) |
| 67 command.set_payload(payload); | 67 command.set_payload(payload); |
| 68 | 68 |
| 69 const RemoteCommandWithCallback command_with_callback( | 69 const RemoteCommandWithCallback command_with_callback( |
| 70 command, clock_->NowTicks(), reported_callback); | 70 command, clock_->NowTicks(), reported_callback); |
| 71 if (skip_next_fetch) | 71 if (skip_next_fetch) |
| 72 commands_issued_after_next_fetch_.push_back(command_with_callback); | 72 commands_issued_after_next_fetch_.push_back(command_with_callback); |
| 73 else | 73 else |
| 74 commands_.push_back(command_with_callback); | 74 commands_.push_back(command_with_callback); |
| 75 } | 75 } |
| 76 | 76 |
| 77 TestingRemoteCommandsServer::RemoteCommands | 77 TestingRemoteCommandsServer::RemoteCommands |
| 78 TestingRemoteCommandsServer::FetchCommands( | 78 TestingRemoteCommandsServer::FetchCommands( |
| 79 std::unique_ptr<RemoteCommandJob::UniqueIDType> last_command_id, | 79 std::unique_ptr<RemoteCommandJob::UniqueIDType> last_command_id, |
| 80 const RemoteCommandResults& previous_job_results) { | 80 const RemoteCommandResults& previous_job_results) { |
| 81 base::AutoLock auto_lock(lock_); | 81 base::AutoLock auto_lock(lock_); |
| 82 | 82 |
| 83 for (const auto& job_result : previous_job_results) { | 83 for (const auto& job_result : previous_job_results) { |
| 84 EXPECT_TRUE(job_result.has_unique_id()); | 84 EXPECT_TRUE(job_result.has_command_id()); |
| 85 EXPECT_TRUE(job_result.has_result()); | 85 EXPECT_TRUE(job_result.has_result()); |
| 86 | 86 |
| 87 bool found_command = false; | 87 bool found_command = false; |
| 88 ResultReportedCallback reported_callback; | 88 ResultReportedCallback reported_callback; |
| 89 | 89 |
| 90 if (last_command_id) { | 90 if (last_command_id) { |
| 91 // This relies on us generating commands with increasing IDs. | 91 // This relies on us generating commands with increasing IDs. |
| 92 EXPECT_GE(*last_command_id, job_result.unique_id()); | 92 EXPECT_GE(*last_command_id, job_result.command_id()); |
| 93 } | 93 } |
| 94 | 94 |
| 95 for (auto it = commands_.begin(); it != commands_.end(); ++it) { | 95 for (auto it = commands_.begin(); it != commands_.end(); ++it) { |
| 96 if (it->command_proto.unique_id() == job_result.unique_id()) { | 96 if (it->command_proto.command_id() == job_result.command_id()) { |
| 97 reported_callback = it->reported_callback; | 97 reported_callback = it->reported_callback; |
| 98 commands_.erase(it); | 98 commands_.erase(it); |
| 99 found_command = true; | 99 found_command = true; |
| 100 break; | 100 break; |
| 101 } | 101 } |
| 102 } | 102 } |
| 103 | 103 |
| 104 // Verify that the command result is for an existing command actually | 104 // Verify that the command result is for an existing command actually |
| 105 // expecting a result. | 105 // expecting a result. |
| 106 EXPECT_TRUE(found_command); | 106 EXPECT_TRUE(found_command); |
| 107 EXPECT_FALSE(reported_callback.is_null()); | 107 EXPECT_FALSE(reported_callback.is_null()); |
| 108 | 108 |
| 109 if (reported_callback.is_null()) { | 109 if (reported_callback.is_null()) { |
| 110 // Post task to the original thread which will report the result. | 110 // Post task to the original thread which will report the result. |
| 111 task_runner_->PostTask( | 111 task_runner_->PostTask( |
| 112 FROM_HERE, | 112 FROM_HERE, |
| 113 base::Bind(&TestingRemoteCommandsServer::ReportJobResult, | 113 base::Bind(&TestingRemoteCommandsServer::ReportJobResult, |
| 114 weak_ptr_to_this_, reported_callback, job_result)); | 114 weak_ptr_to_this_, reported_callback, job_result)); |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 | 117 |
| 118 RemoteCommands fetched_commands; | 118 RemoteCommands fetched_commands; |
| 119 for (const auto& command_with_callback : commands_) { | 119 for (const auto& command_with_callback : commands_) { |
| 120 if (!last_command_id || | 120 if (!last_command_id || |
| 121 command_with_callback.command_proto.unique_id() > *last_command_id) { | 121 command_with_callback.command_proto.command_id() > *last_command_id) { |
| 122 fetched_commands.push_back(command_with_callback.command_proto); | 122 fetched_commands.push_back(command_with_callback.command_proto); |
| 123 // Simulate the age of commands calculation on the server side. | 123 // Simulate the age of commands calculation on the server side. |
| 124 fetched_commands.back().set_age_of_command( | 124 fetched_commands.back().set_age_of_command( |
| 125 (clock_->NowTicks() - command_with_callback.issued_time) | 125 (clock_->NowTicks() - command_with_callback.issued_time) |
| 126 .InMilliseconds()); | 126 .InMilliseconds()); |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 | 129 |
| 130 // Push delayed commands into the main queue. | 130 // Push delayed commands into the main queue. |
| 131 commands_.insert(commands_.end(), commands_issued_after_next_fetch_.begin(), | 131 commands_.insert(commands_.end(), commands_issued_after_next_fetch_.begin(), |
| (...skipping 15 matching lines...) Expand all Loading... |
| 147 } | 147 } |
| 148 | 148 |
| 149 void TestingRemoteCommandsServer::ReportJobResult( | 149 void TestingRemoteCommandsServer::ReportJobResult( |
| 150 const ResultReportedCallback& reported_callback, | 150 const ResultReportedCallback& reported_callback, |
| 151 const em::RemoteCommandResult& job_result) const { | 151 const em::RemoteCommandResult& job_result) const { |
| 152 DCHECK(thread_checker_.CalledOnValidThread()); | 152 DCHECK(thread_checker_.CalledOnValidThread()); |
| 153 reported_callback.Run(job_result); | 153 reported_callback.Run(job_result); |
| 154 } | 154 } |
| 155 | 155 |
| 156 } // namespace policy | 156 } // namespace policy |
| OLD | NEW |