Chromium Code Reviews| Index: components/policy/core/common/remote_commands/remote_commands_service.h |
| diff --git a/components/policy/core/common/remote_commands/remote_commands_service.h b/components/policy/core/common/remote_commands/remote_commands_service.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6308ef8f386ae4fa139b9f5c3e3980c8125b1cdd |
| --- /dev/null |
| +++ b/components/policy/core/common/remote_commands/remote_commands_service.h |
| @@ -0,0 +1,106 @@ |
| +// 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. |
| + |
| +#ifndef COMPONENTS_POLICY_CORE_COMMON_REMOTE_COMMANDS_REMOTE_COMMANDS_SERVICE_H_ |
| +#define COMPONENTS_POLICY_CORE_COMMON_REMOTE_COMMANDS_REMOTE_COMMANDS_SERVICE_H_ |
| + |
| +#include <deque> |
| +#include <vector> |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "components/policy/core/common/cloud/cloud_policy_constants.h" |
| +#include "components/policy/core/common/remote_commands/remote_command_job.h" |
| +#include "components/policy/core/common/remote_commands/remote_commands_queue.h" |
| +#include "components/policy/policy_export.h" |
| +#include "policy/proto/device_management_backend.pb.h" |
| + |
| +namespace base { |
| +class Clock; |
| +} // namespace base |
| + |
| +namespace policy { |
| + |
| +class CloudPolicyClient; |
| +class RemoteCommandsFactory; |
| + |
| +// Service class which will connect to a CloudPolicyClient in order to fetch |
| +// remote commands from DMServer and send results for executed commands |
| +// back to the server. |
| +class POLICY_EXPORT RemoteCommandsService |
| + : public RemoteCommandsQueue::Observer { |
| + public: |
| + RemoteCommandsService(scoped_ptr<RemoteCommandsFactory> factory, |
| + CloudPolicyClient* client); |
| + ~RemoteCommandsService() override; |
| + |
| + // Attempts to fetch remote commands, mainly supposed to be called by |
| + // invalidation service. Note that there will be at most one ongoing fetch |
| + // request and all other fetch request will be enqueued if another fetch |
| + // request is in-progress. And in such a case, another request will be made |
| + // immediately after the current ongoing request finishes. |
| + // Returns true if the new request was started immediately. Returns false if |
| + // another request was in progress already and the new request got enqueued. |
| + bool FetchRemoteCommands(); |
| + |
| + // Returns whether a command fetch request is in progress or not. |
| + bool IsCommandFetchInProgressForTesting() const { |
| + return command_fetch_in_progress_; |
| + } |
| + |
| + // Set an alternative clock for testing. |
| + void SetClockForTesting(scoped_ptr<base::Clock> clock); |
| + |
| + private: |
| + // Helper function to enqueue a command which we get from server. |
| + void EnqueueCommand(const enterprise_management::RemoteCommand& command); |
| + |
| + // RemoteCommandsQueue::Observer: |
| + void OnJobStarted(RemoteCommandJob* command) override; |
| + void OnJobFinished(RemoteCommandJob* command) override; |
| + |
| + // Callback to handle commands we get from the server. |
| + void OnRemoteCommandsFetched( |
| + DeviceManagementStatus status, |
| + const std::vector<enterprise_management::RemoteCommand>& commands); |
| + |
| + // Whether there is a command fetch on going or not. |
| + bool command_fetch_in_progress_ = false; |
| + |
| + // Whether there is an enqueued fetch request, which indicates there were |
| + // additional FetchRemoteCommands() calls while a fetch request was ongoing. |
| + bool has_enqueued_fetch_request_ = false; |
| + |
| + // Command results that have not been sent back to the server yet. |
| + std::vector<enterprise_management::RemoteCommandResult> unsent_results_; |
| + |
| + // Whether an command exists or not. |
|
bartfab (slow)
2015/03/17 14:01:22
You changed this comment from "Whether an executed
binjin
2015/03/18 10:09:38
Done.
|
| + bool has_finished_command_ = false; |
| + |
| + // ID of the latest command which has finished execution if |
| + // |has_finished_command_| is true. We will acknowledge this ID to the |
| + // server so that we can re-fetch commands that have not been executed yet |
| + // after a crash or browser restart. |
| + RemoteCommandJob::UniqueIDType lastest_finished_command_id_; |
| + |
| + // Collects the IDs of all fetched commands. We need this since the command ID |
| + // is opaque. |
| + // IDs will be stored in the order that they are fetched from the server, |
| + // and acknowledging a command will discard its ID from |
| + // |fetched_command_ids_|, as well as IDs of every command before it. |
|
bartfab (slow)
2015/03/17 14:01:22
Nit: s/IDs/the IDs/
binjin
2015/03/18 10:09:38
Done.
|
| + std::deque<RemoteCommandJob::UniqueIDType> fetched_command_ids_; |
| + |
| + RemoteCommandsQueue queue_; |
| + scoped_ptr<RemoteCommandsFactory> factory_; |
| + CloudPolicyClient* const client_; |
| + |
| + base::WeakPtrFactory<RemoteCommandsService> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(RemoteCommandsService); |
| +}; |
| + |
| +} // namespace policy |
| + |
| +#endif // COMPONENTS_POLICY_CORE_COMMON_REMOTE_COMMANDS_REMOTE_COMMANDS_SERVICE_H_ |