buffet: Support cloud command proxy. This proxy allows publishing command updates to cloud API. BUG=chromium:432540 TEST=cros_workon_make buffet --test&&manual Change-Id: Id5730df60c3d40dfb7254d5f65c84dc51aea09eb Reviewed-on: https://chromium-review.googlesource.com/228462 Reviewed-by: Anton Muhin <antonm@chromium.org> Commit-Queue: Anton Muhin <antonm@chromium.org> Tested-by: Anton Muhin <antonm@chromium.org>
diff --git a/buffet/buffet.gyp b/buffet/buffet.gyp index 65cb8b8..12f4300 100644 --- a/buffet/buffet.gyp +++ b/buffet/buffet.gyp
@@ -32,6 +32,7 @@ 'commands/command_queue.cc', 'commands/dbus_command_dispatcher.cc', 'commands/dbus_command_proxy.cc', + 'commands/cloud_command_proxy.cc', 'commands/object_schema.cc', 'commands/prop_constraints.cc', 'commands/prop_types.cc',
diff --git a/buffet/commands/cloud_command_proxy.cc b/buffet/commands/cloud_command_proxy.cc new file mode 100644 index 0000000..5cbf6e6 --- /dev/null +++ b/buffet/commands/cloud_command_proxy.cc
@@ -0,0 +1,35 @@ +// Copyright 2014 The Chromium OS 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 "buffet/commands/cloud_command_proxy.h" + +#include "buffet/commands/command_instance.h" +#include "buffet/commands/prop_constraints.h" +#include "buffet/commands/prop_types.h" +#include "buffet/device_registration_info.h" + +namespace buffet { + +CloudCommandProxy::CloudCommandProxy( + CommandInstance* command_instance, + DeviceRegistrationInfo* device_registration_info) + : command_instance_(command_instance), + device_registration_info_(device_registration_info) { +} + +void CloudCommandProxy::OnStatusChanged(const std::string& status) { + base::DictionaryValue patch; + // TODO(antonm): Change status to state. + patch.SetString("state", status); + device_registration_info_->UpdateCommand(command_instance_->GetID(), patch); +} + +void CloudCommandProxy::OnProgressChanged(int progress) { + base::DictionaryValue patch; + patch.SetInteger("progress", progress); + // TODO(antonm): Consider batching progress change updates. + device_registration_info_->UpdateCommand(command_instance_->GetID(), patch); +} + +} // namespace buffet
diff --git a/buffet/commands/cloud_command_proxy.h b/buffet/commands/cloud_command_proxy.h new file mode 100644 index 0000000..0c35c0c --- /dev/null +++ b/buffet/commands/cloud_command_proxy.h
@@ -0,0 +1,39 @@ +// Copyright 2014 The Chromium OS 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 BUFFET_COMMANDS_CLOUD_COMMAND_PROXY_H_ +#define BUFFET_COMMANDS_CLOUD_COMMAND_PROXY_H_ + +#include <base/macros.h> + +#include <string> + +#include "buffet/commands/command_proxy_interface.h" + +namespace buffet { + +class CommandInstance; +class DeviceRegistrationInfo; + +// Command proxy which publishes command updates to the cloud. +class CloudCommandProxy final : public CommandProxyInterface { + public: + CloudCommandProxy(CommandInstance* command_instance, + DeviceRegistrationInfo* device_registration_info); + ~CloudCommandProxy() override = default; + + // CommandProxyInterface implementation/overloads. + void OnStatusChanged(const std::string& status) override; + void OnProgressChanged(int progress) override; + + private: + CommandInstance* command_instance_; + DeviceRegistrationInfo* device_registration_info_; + + DISALLOW_COPY_AND_ASSIGN(CloudCommandProxy); +}; + +} // namespace buffet + +#endif // BUFFET_COMMANDS_CLOUD_COMMAND_PROXY_H_
diff --git a/buffet/commands/dbus_command_proxy.h b/buffet/commands/dbus_command_proxy.h index 7286a4d..386ecaf 100644 --- a/buffet/commands/dbus_command_proxy.h +++ b/buffet/commands/dbus_command_proxy.h
@@ -5,7 +5,6 @@ #ifndef BUFFET_COMMANDS_DBUS_COMMAND_PROXY_H_ #define BUFFET_COMMANDS_DBUS_COMMAND_PROXY_H_ -#include <memory> #include <string> #include <base/macros.h>
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc index 8fccc02..b36f114 100644 --- a/buffet/device_registration_info.cc +++ b/buffet/device_registration_info.cc
@@ -19,6 +19,7 @@ #include <chromeos/strings/string_utils.h> #include <chromeos/url_utils.h> +#include "buffet/commands/cloud_command_proxy.h" #include "buffet/commands/command_definition.h" #include "buffet/commands/command_manager.h" #include "buffet/device_registration_storage_keys.h" @@ -654,6 +655,16 @@ base::Unretained(this))))).Run(); } +void DeviceRegistrationInfo::UpdateCommand( + const std::string& command_id, + const base::DictionaryValue& command_patch) { + DoCloudRequest( + chromeos::http::request_type::kPatch, + GetServiceURL("commands/" + command_id), + &command_patch, + base::Bind(&IgnoreCloudResult), base::Bind(&IgnoreCloudError)); +} + void DeviceRegistrationInfo::UpdateDeviceResource(base::Closure callback) { std::unique_ptr<base::DictionaryValue> device_resource = BuildDeviceResource(nullptr); @@ -759,8 +770,12 @@ } // TODO(antonm): Properly process cancellation of commands. - if (!command_manager_->FindCommand(command_instance->GetID())) + if (!command_manager_->FindCommand(command_instance->GetID())) { + std::unique_ptr<CommandProxyInterface> cloud_proxy{ + new CloudCommandProxy(command_instance.get(), this)}; + command_instance->AddProxy(std::move(cloud_proxy)); command_manager_->AddCommand(std::move(command_instance)); + } } }
diff --git a/buffet/device_registration_info.h b/buffet/device_registration_info.h index c9b6335..7fb09f1 100644 --- a/buffet/device_registration_info.h +++ b/buffet/device_registration_info.h
@@ -106,12 +106,18 @@ const std::map<std::string, std::string>& params, chromeos::ErrorPtr* error); - // Start device execution. + // Starts device execution. // Device will do required start up chores and then start to listen // to new commands. // TODO(antonm): Consider moving into some other class. void StartDevice(chromeos::ErrorPtr* error); + // Updates a command. + // TODO(antonm): Should solve the issues with async vs. sync. + // TODO(antonm): Consider moving some other class. + void UpdateCommand(const std::string& command_id, + const base::DictionaryValue& command_patch); + private: // Saves the device registration to cache. bool Save() const;