blob: 12c7f88bce1b03e2819e0811d36caf8e1ee16701 [file] [log] [blame]
Anton Muhin59755522014-11-05 21:30:12 +04001// Copyright 2014 The Chromium OS 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#ifndef BUFFET_COMMANDS_CLOUD_COMMAND_PROXY_H_
6#define BUFFET_COMMANDS_CLOUD_COMMAND_PROXY_H_
7
Alex Vakulenkob211c102015-04-21 11:43:23 -07008#include <bitset>
Anton Muhin59755522014-11-05 21:30:12 +04009#include <string>
10
Alex Vakulenkob211c102015-04-21 11:43:23 -070011#include <base/macros.h>
12#include <base/memory/weak_ptr.h>
13
Anton Muhin59755522014-11-05 21:30:12 +040014#include "buffet/commands/command_proxy_interface.h"
15
16namespace buffet {
17
18class CommandInstance;
19class DeviceRegistrationInfo;
20
21// Command proxy which publishes command updates to the cloud.
22class CloudCommandProxy final : public CommandProxyInterface {
23 public:
24 CloudCommandProxy(CommandInstance* command_instance,
25 DeviceRegistrationInfo* device_registration_info);
26 ~CloudCommandProxy() override = default;
27
28 // CommandProxyInterface implementation/overloads.
Alex Vakulenkob211c102015-04-21 11:43:23 -070029 void OnResultsChanged() override;
30 void OnStatusChanged() override;
31 void OnProgressChanged() override;
Anton Muhin59755522014-11-05 21:30:12 +040032
33 private:
Alex Vakulenkob211c102015-04-21 11:43:23 -070034 // Flags used to mark the command resource parts that need to be updated on
35 // the server.
36 using CommandUpdateFlags = std::bitset<3>;
37
38 // Sends an asynchronous request to GCD server to update the command resource.
39 void SendCommandUpdate();
40
41 // Retry last failed request.
42 void ResendCommandUpdate();
43
44 // Callback invoked by the asynchronous PATCH request to the server.
45 // Called both in a case of successfully updating server command resource
46 // and in case of an error, indicated by the |success| parameter.
47 void OnUpdateCommandFinished(bool success);
48
Anton Muhin59755522014-11-05 21:30:12 +040049 CommandInstance* command_instance_;
50 DeviceRegistrationInfo* device_registration_info_;
51
Alex Vakulenkob211c102015-04-21 11:43:23 -070052 // Set to true while a pending PATCH request is in flight to the server.
53 bool command_update_in_progress_{false};
54 // The flags indicating of new command resource updates since the last req.
55 CommandUpdateFlags new_pending_command_updates_;
56 // The flags indicating of command updates currently in flight.
57 CommandUpdateFlags in_progress_command_updates_;
58
59 base::WeakPtrFactory<CloudCommandProxy> weak_ptr_factory_{this};
Anton Muhin59755522014-11-05 21:30:12 +040060 DISALLOW_COPY_AND_ASSIGN(CloudCommandProxy);
61};
62
63} // namespace buffet
64
65#endif // BUFFET_COMMANDS_CLOUD_COMMAND_PROXY_H_