Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 1 | // 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 Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 8 | #include <bitset> |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 9 | #include <string> |
| 10 | |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 11 | #include <base/macros.h> |
| 12 | #include <base/memory/weak_ptr.h> |
| 13 | |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 14 | #include "buffet/commands/command_proxy_interface.h" |
| 15 | |
| 16 | namespace buffet { |
| 17 | |
| 18 | class CommandInstance; |
| 19 | class DeviceRegistrationInfo; |
| 20 | |
| 21 | // Command proxy which publishes command updates to the cloud. |
| 22 | class 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 Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 29 | void OnResultsChanged() override; |
| 30 | void OnStatusChanged() override; |
| 31 | void OnProgressChanged() override; |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 32 | |
| 33 | private: |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 34 | // 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 Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 49 | CommandInstance* command_instance_; |
| 50 | DeviceRegistrationInfo* device_registration_info_; |
| 51 | |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 52 | // 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 Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 60 | DISALLOW_COPY_AND_ASSIGN(CloudCommandProxy); |
| 61 | }; |
| 62 | |
| 63 | } // namespace buffet |
| 64 | |
| 65 | #endif // BUFFET_COMMANDS_CLOUD_COMMAND_PROXY_H_ |