Chromium Code Reviews| Index: components/policy/core/common/remote_commands/remote_commands_service.cc |
| diff --git a/components/policy/core/common/remote_commands/remote_commands_service.cc b/components/policy/core/common/remote_commands/remote_commands_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7478026c60023cdb7e1bdbc7d004588430d44b33 |
| --- /dev/null |
| +++ b/components/policy/core/common/remote_commands/remote_commands_service.cc |
| @@ -0,0 +1,154 @@ |
| +// 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/remote_commands_service.h" |
| + |
| +#include <algorithm> |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/location.h" |
| +#include "base/logging.h" |
| +#include "base/time/time.h" |
| +#include "components/policy/core/common/cloud/cloud_policy_client.h" |
| +#include "components/policy/core/common/cloud/device_management_service.h" |
| + |
| +namespace { |
| + |
| +// XXX |
| +const int kMaxUnhandledFetchRequests = 30; |
| + |
| +// XXX |
| +const int kAutoFetchingIntervalInMinutes = 30; |
|
bartfab (slow)
2015/01/29 10:18:05
The server should send us a Tango invalidation whe
binjin
2015/01/29 10:44:10
I would like to keep the auto fetching as a backup
bartfab (slow)
2015/01/29 15:28:58
The purpose of our invalidation work was to signif
|
| + |
| +} // namespace |
| + |
| +namespace policy { |
| + |
| +namespace em = enterprise_management; |
| + |
| +RemoteCommandsService::RemoteCommandsService( |
| + scoped_ptr<RemoteCommandJob::Factory> factory, |
| + CloudPolicyClient* client) |
| + : request_job_in_progress_(false), |
| + unhandled_fetch_requests(0), |
| + last_fetched_command_id_(0), |
| + last_executed_command_id_(0), |
| + factory_(factory.Pass()), |
| + client_(client) { |
| + queue_.AddObserver(this); |
| +} |
| + |
| +RemoteCommandsService::~RemoteCommandsService() { |
| + queue_.RemoveObserver(this); |
| +} |
| + |
| +void RemoteCommandsService::FetchRemoteCommands() { |
| + if (request_job_in_progress_ && |
| + unhandled_fetch_requests < kMaxUnhandledFetchRequests) { |
|
bartfab (slow)
2015/01/29 10:18:05
Why do we need a queue of unhandled fetch requests
binjin
2015/01/29 10:44:10
The entire logic here is for reliability. There is
bartfab (slow)
2015/01/29 15:28:58
CloudPolicyClient keeps track of timeouts on its o
|
| + unhandled_fetch_requests++; |
| + return; |
| + } |
| + |
| + request_job_in_progress_ = true; |
| + unhandled_fetch_requests = 0; |
| + |
| + request_job_ = client_->CreateRemoteCommandsJob(); |
| + |
| + em::DeviceRemoteCommandRequest* request = |
| + request_job_->GetRequest()->mutable_remote_command_request(); |
| + |
| + request->set_last_command_unique_id(last_fetched_command_id_); |
| + request->clear_command_results(); |
| + for (const auto& command_result : unsent_results_) |
|
bartfab (slow)
2015/01/29 10:18:05
This has an interesting interaction with the reboo
binjin
2015/01/29 10:44:10
Yes, I will make necessary change.
|
| + *request->add_command_results() = command_result; |
| + unsent_results_.clear(); |
| + |
| + request_job_->Start(base::Bind( |
| + &RemoteCommandsService::OnRemoteCommandsFetched, base::Unretained(this))); |
| +} |
| + |
| +void RemoteCommandsService::StartAutoFetching() { |
| + timer_.Start(FROM_HERE, |
| + base::TimeDelta::FromMinutes(kAutoFetchingIntervalInMinutes), |
| + this, &RemoteCommandsService::FetchRemoteCommands); |
| +} |
| + |
| +void RemoteCommandsService::StopAutoFetching() { |
| + timer_.Stop(); |
| +} |
| + |
| +void RemoteCommandsService::AddCommand( |
| + const enterprise_management::RemoteCommand& command) { |
| + if (!command.has_type() || !command.has_unique_id()) { |
| + LOG(WARNING) << "Invalid remote command from server."; |
| + return; |
| + } |
| + |
| + last_fetched_command_id_ = |
|
bartfab (slow)
2015/01/29 10:18:05
This means that we auto-acknowledge the command wi
binjin
2015/01/29 10:44:10
Actually this patchset considered "RemoteCommandsS
bartfab (slow)
2015/01/29 15:28:58
I think this plan is heading in the right directio
|
| + std::max(last_fetched_command_id_, command.unique_id()); |
| + |
| + scoped_ptr<RemoteCommandJob> job = factory_->BuildJobForType(command.type()); |
| + |
| + if (!job || !job->Init(command)) { |
| + em::RemoteCommandResult ignored_result; |
| + ignored_result.set_result( |
| + em::RemoteCommandResult_ResultType_RESULT_IGNORED); |
| + ignored_result.set_unique_id(command.unique_id()); |
| + unsent_results_.push_back(ignored_result); |
| + unhandled_fetch_requests++; |
| + return; |
| + } |
| + |
| + queue_.AddJob(job.Pass()); |
| +} |
| + |
| +void RemoteCommandsService::OnJobStarted(RemoteCommandJob* command) { |
| + last_executed_command_id_ = |
| + std::max(last_executed_command_id_, command->unique_id()); |
| + // XXX: sync |last_executed_command_id_| to persistent source, and reload |
| + // later. |
| +} |
| + |
| +void RemoteCommandsService::OnJobFinished(RemoteCommandJob* command) { |
| + em::RemoteCommandResult result; |
| + result.set_unique_id(command->unique_id()); |
| + result.set_timestamp((command->execution_started_time() - |
| + base::Time::UnixEpoch()).InMilliseconds()); |
| + |
| + if (command->status() == RemoteCommandJob::SUCCEEDED) { |
| + result.set_result(em::RemoteCommandResult_ResultType_RESULT_SUCCESS); |
| + scoped_ptr<std::string> result_payload = command->GetResultPayload(); |
| + if (result_payload) |
| + result.set_payload(*result_payload); |
| + } else if (command->status() == RemoteCommandJob::EXPIRED || |
| + command->status() == RemoteCommandJob::INVALID) { |
| + result.set_result(em::RemoteCommandResult_ResultType_RESULT_IGNORED); |
| + } else { |
| + result.set_result(em::RemoteCommandResult_ResultType_RESULT_FAILURE); |
| + } |
| + |
| + unsent_results_.push_back(result); |
| + unhandled_fetch_requests++; |
| + |
| + FetchRemoteCommands(); |
| +} |
| + |
| +void RemoteCommandsService::OnRemoteCommandsFetched( |
| + DeviceManagementStatus status, |
| + int net_error, |
| + const em::DeviceManagementResponse& response) { |
| + request_job_in_progress_ = false; |
| + |
| + if (status == DM_STATUS_SUCCESS && response.has_remote_command_response()) { |
| + for (const auto& command : response.remote_command_response().commands()) |
| + AddCommand(command); |
| + } |
| + |
| + if (unhandled_fetch_requests > 0) |
| + FetchRemoteCommands(); |
| +} |
| + |
| +} // namespace policy |