buffet: Pass CommandManager instance to DeviceRegistrationInfo
In order to generate device draft for device registration with
GCD cloud server, DeviceRegistrationInfo class needs access
to CommandManager class to get the CDD for supported commands.
BUG=chromium:396716
TEST=USE=buffet P2_TEST_FILTER="buffet::*" FEATURES=test emerge-link platform2
Change-Id: I076249a890dc865f3af119315ab1c1c50c6edcf4
Reviewed-on: https://chromium-review.googlesource.com/209564
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Chris Sosa <sosa@chromium.org>
Reviewed-by: Christopher Wiley <wiley@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc
index 0225a5b..0ec3de7 100644
--- a/buffet/device_registration_info.cc
+++ b/buffet/device_registration_info.cc
@@ -137,17 +137,22 @@
namespace buffet {
-DeviceRegistrationInfo::DeviceRegistrationInfo()
+DeviceRegistrationInfo::DeviceRegistrationInfo(
+ const std::shared_ptr<CommandManager>& command_manager)
: transport_(new http::curl::Transport()),
// TODO(avakulenko): Figure out security implications of storing
// this data unencrypted.
- storage_(new FileStorage(base::FilePath(kDeviceInfoFilePath))) {
+ storage_(new FileStorage(base::FilePath(kDeviceInfoFilePath))),
+ command_manager_(command_manager) {
}
DeviceRegistrationInfo::DeviceRegistrationInfo(
- std::shared_ptr<http::Transport> transport,
- std::shared_ptr<StorageInterface> storage) : transport_(transport),
- storage_(storage) {
+ const std::shared_ptr<CommandManager>& command_manager,
+ const std::shared_ptr<http::Transport>& transport,
+ const std::shared_ptr<StorageInterface>& storage)
+ : transport_(transport),
+ storage_(storage),
+ command_manager_(command_manager) {
}
std::pair<std::string, std::string>
diff --git a/buffet/device_registration_info.h b/buffet/device_registration_info.h
index d1c0e6e..a150253 100644
--- a/buffet/device_registration_info.h
+++ b/buffet/device_registration_info.h
@@ -24,6 +24,8 @@
namespace buffet {
+class CommandManager;
+
extern const char kErrorDomainOAuth2[];
extern const char kErrorDomainGCD[];
extern const char kErrorDomainGCDServer[];
@@ -34,12 +36,14 @@
public:
// This is a helper class for unit testing.
class TestHelper;
- // Default-constructed uses CURL HTTP transport.
- DeviceRegistrationInfo();
+ // This constructor uses CURL HTTP transport.
+ explicit DeviceRegistrationInfo(
+ const std::shared_ptr<CommandManager>& command_manager);
// This constructor allows to pass in a custom HTTP transport
// (mainly for testing).
- DeviceRegistrationInfo(std::shared_ptr<http::Transport> transport,
- std::shared_ptr<StorageInterface> storage);
+ DeviceRegistrationInfo(const std::shared_ptr<CommandManager>& command_manager,
+ const std::shared_ptr<http::Transport>& transport,
+ const std::shared_ptr<StorageInterface>& storage);
// Returns the authorization HTTP header that can be used to talk
// to GCD server for authenticated device communication.
@@ -136,6 +140,8 @@
std::shared_ptr<http::Transport> transport_;
// Serialization interface to save and load device registration info.
std::shared_ptr<StorageInterface> storage_;
+ // Global command manager.
+ std::shared_ptr<CommandManager> command_manager_;
friend class TestHelper;
DISALLOW_COPY_AND_ASSIGN(DeviceRegistrationInfo);
diff --git a/buffet/device_registration_info_unittest.cc b/buffet/device_registration_info_unittest.cc
index 5108e35..b8059cd 100644
--- a/buffet/device_registration_info_unittest.cc
+++ b/buffet/device_registration_info_unittest.cc
@@ -7,6 +7,7 @@
#include <gtest/gtest.h>
#include "buffet/bind_lambda.h"
+#include "buffet/commands/command_manager.h"
#include "buffet/device_registration_info.h"
#include "buffet/device_registration_storage_keys.h"
#include "buffet/http_request.h"
@@ -161,7 +162,8 @@
storage->Save(&data);
transport = std::make_shared<fake::Transport>();
dev_reg = std::unique_ptr<DeviceRegistrationInfo>(
- new DeviceRegistrationInfo(transport, storage));
+ new DeviceRegistrationInfo(std::make_shared<CommandManager>(),
+ transport, storage));
}
base::DictionaryValue data;
diff --git a/buffet/manager.cc b/buffet/manager.cc
index e493329..aa29552 100644
--- a/buffet/manager.cc
+++ b/buffet/manager.cc
@@ -131,7 +131,9 @@
sequencer->OnAllTasksCompletedCall({claim_interface_task, cb});
command_manager_ = std::make_shared<CommandManager>();
command_manager_->Startup();
- device_info_.Load();
+ device_info_ = std::unique_ptr<DeviceRegistrationInfo>(
+ new DeviceRegistrationInfo(command_manager_));
+ device_info_->Load();
}
scoped_ptr<dbus::Response> Manager::HandleCheckDeviceRegistered(
@@ -146,7 +148,7 @@
LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()";
buffet::ErrorPtr error;
- bool registered = device_info_.CheckRegistration(&error);
+ bool registered = device_info_->CheckRegistration(&error);
// If it fails due to any reason other than 'device not registered',
// treat it as a real error and report it to the caller.
if (!registered &&
@@ -156,7 +158,7 @@
std::string device_id;
if (registered) {
- device_id = device_info_.GetDeviceId(&error);
+ device_id = device_info_->GetDeviceId(&error);
if (device_id.empty())
return GetDBusError(method_call, error.get());
}
@@ -181,7 +183,7 @@
std::string device_info_str;
buffet::ErrorPtr error;
- auto device_info = device_info_.GetDeviceInfo(&error);
+ auto device_info = device_info_->GetDeviceInfo(&error);
if (!device_info)
return GetDBusError(method_call, error.get());
@@ -226,7 +228,7 @@
LOG(INFO) << "Received call to Manager.StartRegisterDevice()";
buffet::ErrorPtr error;
- std::string id = device_info_.StartRegistration(params, &error);
+ std::string id = device_info_->StartRegistration(params, &error);
if (id.empty())
return GetDBusError(method_call, error.get());
@@ -257,10 +259,10 @@
LOG(INFO) << "Received call to Manager.FinishRegisterDevice()";
buffet::ErrorPtr error;
- if (!device_info_.FinishRegistration(user_auth_code, &error))
+ if (!device_info_->FinishRegistration(user_auth_code, &error))
return GetDBusError(method_call, error.get());
- std::string device_id = device_info_.GetDeviceId(&error);
+ std::string device_id = device_info_->GetDeviceId(&error);
if (device_id.empty())
return GetDBusError(method_call, error.get());
diff --git a/buffet/manager.h b/buffet/manager.h
index c0b346d..6f2f8a6 100644
--- a/buffet/manager.h
+++ b/buffet/manager.h
@@ -76,7 +76,7 @@
scoped_ptr<Properties> properties_;
std::shared_ptr<CommandManager> command_manager_;
- DeviceRegistrationInfo device_info_;
+ std::unique_ptr<DeviceRegistrationInfo> device_info_;
DISALLOW_COPY_AND_ASSIGN(Manager);
};