aboutsummaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/cloud_command_proxy.cc11
-rw-r--r--src/commands/cloud_command_proxy.h18
-rw-r--r--src/commands/cloud_command_proxy_unittest.cc21
-rw-r--r--src/commands/command_dictionary.cc151
-rw-r--r--src/commands/command_dictionary.h68
-rw-r--r--src/commands/command_dictionary_unittest.cc159
-rw-r--r--src/commands/command_instance.cc1
-rw-r--r--src/commands/command_manager.cc107
-rw-r--r--src/commands/command_manager.h75
-rw-r--r--src/commands/command_manager_unittest.cc99
10 files changed, 24 insertions, 686 deletions
diff --git a/src/commands/cloud_command_proxy.cc b/src/commands/cloud_command_proxy.cc
index 3d472c7..f8f8d1f 100644
--- a/src/commands/cloud_command_proxy.cc
+++ b/src/commands/cloud_command_proxy.cc
@@ -17,15 +17,15 @@ namespace weave {
CloudCommandProxy::CloudCommandProxy(
CommandInstance* command_instance,
CloudCommandUpdateInterface* cloud_command_updater,
- StateChangeQueueInterface* state_change_queue,
+ ComponentManager* component_manager,
std::unique_ptr<BackoffEntry> backoff_entry,
provider::TaskRunner* task_runner)
: command_instance_{command_instance},
cloud_command_updater_{cloud_command_updater},
- state_change_queue_{state_change_queue},
+ component_manager_{component_manager},
task_runner_{task_runner},
cloud_backoff_entry_{std::move(backoff_entry)} {
- callback_token_ = state_change_queue_->AddOnStateUpdatedCallback(
+ callback_token_ = component_manager_->AddServerStateUpdatedCallback(
base::Bind(&CloudCommandProxy::OnDeviceStateUpdated,
weak_ptr_factory_.GetWeakPtr()));
observer_.Add(command_instance);
@@ -67,7 +67,7 @@ void CloudCommandProxy::OnCommandDestroyed() {
void CloudCommandProxy::QueueCommandUpdate(
std::unique_ptr<base::DictionaryValue> patch) {
- UpdateID id = state_change_queue_->GetLastStateChangeId();
+ ComponentManager::UpdateID id = component_manager_->GetLastStateChangeId();
if (update_queue_.empty() || update_queue_.back().first != id) {
// If queue is currently empty or the device state has changed since the
// last patch request queued, add a new request to the queue.
@@ -158,7 +158,8 @@ void CloudCommandProxy::OnUpdateCommandDone(ErrorPtr error) {
SendCommandUpdate();
}
-void CloudCommandProxy::OnDeviceStateUpdated(UpdateID update_id) {
+void CloudCommandProxy::OnDeviceStateUpdated(
+ ComponentManager::UpdateID update_id) {
last_state_update_id_ = update_id;
// Try to send out any queued command updates that could be performed after
// a device state is updated.
diff --git a/src/commands/cloud_command_proxy.h b/src/commands/cloud_command_proxy.h
index ee6358f..13f4654 100644
--- a/src/commands/cloud_command_proxy.h
+++ b/src/commands/cloud_command_proxy.h
@@ -18,7 +18,7 @@
#include "src/backoff_entry.h"
#include "src/commands/cloud_command_update_interface.h"
#include "src/commands/command_instance.h"
-#include "src/states/state_change_queue_interface.h"
+#include "src/component_manager.h"
namespace weave {
@@ -33,7 +33,7 @@ class CloudCommandProxy final : public CommandInstance::Observer {
public:
CloudCommandProxy(CommandInstance* command_instance,
CloudCommandUpdateInterface* cloud_command_updater,
- StateChangeQueueInterface* state_change_queue,
+ ComponentManager* component_manager,
std::unique_ptr<BackoffEntry> backoff_entry,
provider::TaskRunner* task_runner);
~CloudCommandProxy() override = default;
@@ -46,10 +46,8 @@ class CloudCommandProxy final : public CommandInstance::Observer {
void OnStateChanged() override;
private:
- using UpdateID = StateChangeQueueInterface::UpdateID;
- using UpdateQueueEntry =
- std::pair<UpdateID, std::unique_ptr<base::DictionaryValue>>;
-
+ using UpdateQueueEntry = std::pair<ComponentManager::UpdateID,
+ std::unique_ptr<base::DictionaryValue>>;
// Puts a command update data into the update queue, and optionally sends an
// asynchronous request to GCD server to update the command resource, if there
// are no pending device status updates.
@@ -68,11 +66,11 @@ class CloudCommandProxy final : public CommandInstance::Observer {
// Callback invoked by the device state change queue to notify of the
// successful device state update. |update_id| is the ID of the state that
// has been updated on the server.
- void OnDeviceStateUpdated(UpdateID update_id);
+ void OnDeviceStateUpdated(ComponentManager::UpdateID update_id);
CommandInstance* command_instance_;
CloudCommandUpdateInterface* cloud_command_updater_;
- StateChangeQueueInterface* state_change_queue_;
+ ComponentManager* component_manager_;
provider::TaskRunner* task_runner_{nullptr};
// Backoff for SendCommandUpdate() method.
@@ -87,11 +85,11 @@ class CloudCommandProxy final : public CommandInstance::Observer {
// Callback token from the state change queue for OnDeviceStateUpdated()
// callback for ask the device state change queue to call when the state
// is updated on the server.
- StateChangeQueueInterface::Token callback_token_;
+ ComponentManager::Token callback_token_;
// Last device state update ID that has been sent out to the server
// successfully.
- UpdateID last_state_update_id_{0};
+ ComponentManager::UpdateID last_state_update_id_{0};
ScopedObserver<CommandInstance, CommandInstance::Observer> observer_{this};
diff --git a/src/commands/cloud_command_proxy_unittest.cc b/src/commands/cloud_command_proxy_unittest.cc
index c022b79..d3a9965 100644
--- a/src/commands/cloud_command_proxy_unittest.cc
+++ b/src/commands/cloud_command_proxy_unittest.cc
@@ -12,9 +12,8 @@
#include <weave/provider/test/fake_task_runner.h>
#include <weave/test/unittest_utils.h>
-#include "src/commands/command_dictionary.h"
#include "src/commands/command_instance.h"
-#include "src/states/mock_state_change_queue_interface.h"
+#include "src/mock_component_manager.h"
using testing::_;
using testing::DoAll;
@@ -66,14 +65,14 @@ class TestBackoffEntry : public BackoffEntry {
class CloudCommandProxyTest : public ::testing::Test {
protected:
void SetUp() override {
- // Set up the test StateChangeQueue.
- auto callback = [this](
- const base::Callback<void(StateChangeQueueInterface::UpdateID)>& call) {
+ // Set up the test ComponentManager.
+ auto callback =
+ [this](const base::Callback<void(ComponentManager::UpdateID)>& call) {
return callbacks_.Add(call).release();
};
- EXPECT_CALL(state_change_queue_, MockAddOnStateUpdatedCallback(_))
+ EXPECT_CALL(component_manager_, MockAddServerStateUpdatedCallback(_))
.WillRepeatedly(Invoke(callback));
- EXPECT_CALL(state_change_queue_, GetLastStateChangeId())
+ EXPECT_CALL(component_manager_, GetLastStateChangeId())
.WillRepeatedly(testing::ReturnPointee(&current_state_update_id_));
CreateCommandInstance();
@@ -102,7 +101,7 @@ class CloudCommandProxyTest : public ::testing::Test {
// Finally construct the CloudCommandProxy we are going to test here.
std::unique_ptr<CloudCommandProxy> proxy{new CloudCommandProxy{
- command_instance_.get(), &cloud_updater_, &state_change_queue_,
+ command_instance_.get(), &cloud_updater_, &component_manager_,
std::move(backoff), &task_runner_}};
// CloudCommandProxy::CloudCommandProxy() subscribe itself to weave::Command
// notifications. When weave::Command is being destroyed it sends
@@ -110,10 +109,10 @@ class CloudCommandProxyTest : public ::testing::Test {
proxy.release();
}
- StateChangeQueueInterface::UpdateID current_state_update_id_{0};
- base::CallbackList<void(StateChangeQueueInterface::UpdateID)> callbacks_;
+ ComponentManager::UpdateID current_state_update_id_{0};
+ base::CallbackList<void(ComponentManager::UpdateID)> callbacks_;
testing::StrictMock<MockCloudCommandUpdateInterface> cloud_updater_;
- testing::StrictMock<MockStateChangeQueueInterface> state_change_queue_;
+ testing::StrictMock<MockComponentManager> component_manager_;
testing::StrictMock<provider::test::FakeTaskRunner> task_runner_;
std::queue<base::Closure> task_queue_;
std::unique_ptr<CommandInstance> command_instance_;
diff --git a/src/commands/command_dictionary.cc b/src/commands/command_dictionary.cc
deleted file mode 100644
index f6a409d..0000000
--- a/src/commands/command_dictionary.cc
+++ /dev/null
@@ -1,151 +0,0 @@
-// Copyright 2015 The Weave 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 "src/commands/command_dictionary.h"
-
-#include <algorithm>
-
-#include <base/values.h>
-#include <weave/enum_to_string.h>
-
-#include "src/commands/schema_constants.h"
-#include "src/string_utils.h"
-
-namespace weave {
-
-namespace {
-const EnumToStringMap<UserRole>::Map kMap[] = {
- {UserRole::kViewer, commands::attributes::kCommand_Role_Viewer},
- {UserRole::kUser, commands::attributes::kCommand_Role_User},
- {UserRole::kOwner, commands::attributes::kCommand_Role_Owner},
- {UserRole::kManager, commands::attributes::kCommand_Role_Manager},
-};
-} // anonymous namespace
-
-template <>
-LIBWEAVE_EXPORT EnumToStringMap<UserRole>::EnumToStringMap()
- : EnumToStringMap(kMap) {}
-
-bool CommandDictionary::LoadCommands(const base::DictionaryValue& json,
- ErrorPtr* error) {
- // |json| contains a list of nested objects with the following structure:
- // {"<pkg_name>": {"<cmd_name>": {"parameters": {object_schema}}, ...}, ...}
- // Iterate over traits
- base::DictionaryValue::Iterator trait_iter(json);
- for (base::DictionaryValue::Iterator trait_iter(json);
- !trait_iter.IsAtEnd(); trait_iter.Advance()) {
- std::string trait_name = trait_iter.key();
- const base::DictionaryValue* trait_def = nullptr;
- if (!trait_iter.value().GetAsDictionary(&trait_def)) {
- Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain,
- errors::commands::kTypeMismatch,
- "Expecting an object for trait '%s'",
- trait_name.c_str());
- return false;
- }
- // Iterate over command definitions within the current trait.
- for (base::DictionaryValue::Iterator command_iter(*trait_def);
- !command_iter.IsAtEnd(); command_iter.Advance()) {
- std::string command_name = command_iter.key();
- if (command_name.empty()) {
- Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain,
- errors::commands::kInvalidCommandName,
- "Unnamed command encountered in trait '%s'",
- trait_name.c_str());
- return false;
- }
- const base::DictionaryValue* command_def_json = nullptr;
- if (!command_iter.value().GetAsDictionary(&command_def_json)) {
- Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain,
- errors::commands::kTypeMismatch,
- "Expecting an object for command '%s'",
- command_name.c_str());
- return false;
- }
-
- // Construct the compound command name as "trait_name.cmd_name".
- std::string full_command_name = Join(".", trait_name, command_name);
-
- // Validate the 'minimalRole' value if present. That's the only thing we
- // care about so far.
- std::string value;
- if (!command_def_json->GetString(commands::attributes::kCommand_Role,
- &value)) {
- Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain,
- errors::commands::kInvalidMinimalRole,
- "Missing '%s' attribute for command '%s'",
- commands::attributes::kCommand_Role,
- full_command_name.c_str());
- return false;
- }
- UserRole minimal_role;
- if (!StringToEnum(value, &minimal_role)) {
- Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain,
- errors::commands::kInvalidMinimalRole,
- "Invalid role '%s' for command '%s'", value.c_str(),
- full_command_name.c_str());
- return false;
- }
- // Check if we already have this command defined.
- CHECK(!definitions_.Get(full_command_name, nullptr))
- << "Definition for command '" << full_command_name
- << "' overrides an earlier definition";
- definitions_.Set(full_command_name, command_def_json->DeepCopy());
- }
- }
- return true;
-}
-
-const base::DictionaryValue& CommandDictionary::GetCommandsAsJson() const {
- return definitions_;
-}
-
-size_t CommandDictionary::GetSize() const {
- size_t size = 0;
- base::DictionaryValue::Iterator trait_iter(definitions_);
- while (!trait_iter.IsAtEnd()) {
- std::string trait_name = trait_iter.key();
- const base::DictionaryValue* trait_def = nullptr;
- CHECK(trait_iter.value().GetAsDictionary(&trait_def));
- size += trait_def->size();
- trait_iter.Advance();
- }
- return size;
-}
-
-const base::DictionaryValue* CommandDictionary::FindCommand(
- const std::string& command_name) const {
- const base::DictionaryValue* definition = nullptr;
- // Make sure the |command_name| came in form of trait_name.command_name.
- // For this, we just verify it has a single period in its name.
- if (std::count(command_name.begin(), command_name.end(), '.') != 1)
- return definition;
- definitions_.GetDictionary(command_name, &definition);
- return definition;
-}
-
-void CommandDictionary::Clear() {
- definitions_.Clear();
-}
-
-bool CommandDictionary::GetMinimalRole(const std::string& command_name,
- UserRole* minimal_role,
- ErrorPtr* error) const {
- const base::DictionaryValue* command_def = FindCommand(command_name);
- if (!command_def) {
- Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain,
- errors::commands::kInvalidCommandName,
- "Command definition for '%s' not found",
- command_name.c_str());
- return false;
- }
- std::string value;
- // The JSON definition has been pre-validated already in LoadCommands, so
- // just using CHECKs here.
- CHECK(command_def->GetString(commands::attributes::kCommand_Role, &value));
- CHECK(StringToEnum(value, minimal_role));
- return true;
-}
-
-} // namespace weave
diff --git a/src/commands/command_dictionary.h b/src/commands/command_dictionary.h
deleted file mode 100644
index 12f7e40..0000000
--- a/src/commands/command_dictionary.h
+++ /dev/null
@@ -1,68 +0,0 @@
-// Copyright 2015 The Weave 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 LIBWEAVE_SRC_COMMANDS_COMMAND_DICTIONARY_H_
-#define LIBWEAVE_SRC_COMMANDS_COMMAND_DICTIONARY_H_
-
-#include <map>
-#include <memory>
-#include <set>
-#include <string>
-#include <vector>
-
-#include <base/macros.h>
-#include <base/values.h>
-#include <weave/error.h>
-
-namespace weave {
-
-enum class UserRole {
- kViewer,
- kUser,
- kManager,
- kOwner,
-};
-
-// CommandDictionary is a wrapper around a container of command definition
-// schema. The command name is a compound name in a form of
-// "trait_name.command_name", where "trait_name" is a name of command trait such
-// as "base", "onOff", and others. So the full command name could be
-// "base.reboot", for example.
-class CommandDictionary final {
- public:
- CommandDictionary() = default;
-
- // Loads command definitions from a JSON object. This is done at the daemon
- // startup and whenever a device daemon decides to update its command list.
- // |json| is a JSON dictionary that describes the complete commands. Optional
- // Returns false on failure and |error| provides additional error information
- // when provided.
- bool LoadCommands(const base::DictionaryValue& json,
- ErrorPtr* error);
- // Converts all the command definitions to a JSON object for CDD/Device
- // draft.
- const base::DictionaryValue& GetCommandsAsJson() const;
- // Returns the number of command definitions in the dictionary.
- size_t GetSize() const;
- // Checks if the dictionary has no command definitions.
- bool IsEmpty() const { return definitions_.empty(); }
- // Remove all the command definitions from the dictionary.
- void Clear();
- // Finds a definition for the given command.
- const base::DictionaryValue* FindCommand(
- const std::string& command_name) const;
- // Determines the minimal role for the given command. Returns false if the
- // command with given name is not found.
- bool GetMinimalRole(const std::string& command_name,
- UserRole* minimal_role,
- ErrorPtr* error) const;
-
- private:
- base::DictionaryValue definitions_; // List of all available command defs.
- DISALLOW_COPY_AND_ASSIGN(CommandDictionary);
-};
-
-} // namespace weave
-
-#endif // LIBWEAVE_SRC_COMMANDS_COMMAND_DICTIONARY_H_
diff --git a/src/commands/command_dictionary_unittest.cc b/src/commands/command_dictionary_unittest.cc
deleted file mode 100644
index 7c53935..0000000
--- a/src/commands/command_dictionary_unittest.cc
+++ /dev/null
@@ -1,159 +0,0 @@
-// Copyright 2015 The Weave 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 "src/commands/command_dictionary.h"
-
-#include <gtest/gtest.h>
-#include <weave/test/unittest_utils.h>
-
-namespace weave {
-
-using test::CreateDictionaryValue;
-using test::IsEqualValue;
-
-TEST(CommandDictionary, Empty) {
- CommandDictionary dict;
- EXPECT_TRUE(dict.IsEmpty());
- EXPECT_EQ(nullptr, dict.FindCommand("robot.jump"));
-}
-
-TEST(CommandDictionary, LoadCommands) {
- auto json = CreateDictionaryValue(R"({
- 'robot': {
- 'jump': {
- 'minimalRole': 'manager',
- 'parameters': {
- 'height': 'integer',
- '_jumpType': ['_withAirFlip', '_withSpin', '_withKick']
- },
- 'progress': {
- 'progress': 'integer'
- },
- 'results': {}
- }
- }
- })");
- CommandDictionary dict;
- EXPECT_TRUE(dict.LoadCommands(*json, nullptr));
- EXPECT_EQ(1u, dict.GetSize());
- EXPECT_NE(nullptr, dict.FindCommand("robot.jump"));
- json = CreateDictionaryValue(R"({
- 'base': {
- 'reboot': {
- 'minimalRole': 'owner',
- 'parameters': {'delay': 'integer'}
- },
- 'shutdown': {
- 'minimalRole': 'user'
- }
- }
- })");
- EXPECT_TRUE(dict.LoadCommands(*json, nullptr));
- EXPECT_EQ(3u, dict.GetSize());
- EXPECT_NE(nullptr, dict.FindCommand("robot.jump"));
- EXPECT_NE(nullptr, dict.FindCommand("base.reboot"));
- EXPECT_NE(nullptr, dict.FindCommand("base.shutdown"));
- EXPECT_EQ(nullptr, dict.FindCommand("foo.bar"));
-}
-
-TEST(CommandDictionary, LoadCommands_Failures) {
- CommandDictionary dict;
- ErrorPtr error;
-
- // Command definition is not an object.
- auto json = CreateDictionaryValue("{'robot':{'jump':0}}");
- EXPECT_FALSE(dict.LoadCommands(*json, &error));
- EXPECT_EQ("type_mismatch", error->GetCode());
- error.reset();
-
- // Package definition is not an object.
- json = CreateDictionaryValue("{'robot':'blah'}");
- EXPECT_FALSE(dict.LoadCommands(*json, &error));
- EXPECT_EQ("type_mismatch", error->GetCode());
- error.reset();
-
- // Empty command name.
- json = CreateDictionaryValue("{'robot':{'':{'parameters':{},'results':{}}}}");
- EXPECT_FALSE(dict.LoadCommands(*json, &error));
- EXPECT_EQ("invalid_command_name", error->GetCode());
- error.reset();
-
- // No 'minimalRole'.
- json = CreateDictionaryValue(R"({
- 'base': {
- 'reboot': {
- 'parameters': {'delay': 'integer'}
- }
- }
- })");
- EXPECT_FALSE(dict.LoadCommands(*json, &error));
- EXPECT_EQ("invalid_minimal_role", error->GetCode());
- error.reset();
-
- // Invalid 'minimalRole'.
- json = CreateDictionaryValue(R"({
- 'base': {
- 'reboot': {
- 'minimalRole': 'foo',
- 'parameters': {'delay': 'integer'}
- }
- }
- })");
- EXPECT_FALSE(dict.LoadCommands(*json, &error));
- EXPECT_EQ("invalid_minimal_role", error->GetCode());
- error.reset();
-}
-
-TEST(CommandDictionaryDeathTest, LoadCommands_Redefine) {
- // Redefine commands.
- CommandDictionary dict;
- ErrorPtr error;
- auto json =
- CreateDictionaryValue("{'robot':{'jump':{'minimalRole': 'viewer'}}}");
- dict.LoadCommands(*json, nullptr);
- ASSERT_DEATH(dict.LoadCommands(*json, &error),
- ".*Definition for command 'robot.jump' overrides an "
- "earlier definition");
-}
-
-TEST(CommandDictionary, GetMinimalRole) {
- CommandDictionary base_dict;
- auto json = CreateDictionaryValue(R"({
- 'base': {
- 'command1': {
- 'minimalRole': 'viewer',
- 'parameters': {},
- 'results': {}
- },
- 'command2': {
- 'minimalRole': 'user',
- 'parameters': {},
- 'results': {}
- },
- 'command3': {
- 'minimalRole': 'manager',
- 'parameters': {},
- 'results': {}
- },
- 'command4': {
- 'minimalRole': 'owner',
- 'parameters': {},
- 'results': {}
- }
- }
- })");
- EXPECT_TRUE(base_dict.LoadCommands(*json, nullptr));
- UserRole role;
- EXPECT_TRUE(base_dict.GetMinimalRole("base.command1", &role, nullptr));
- EXPECT_EQ(UserRole::kViewer, role);
- EXPECT_TRUE(base_dict.GetMinimalRole("base.command2", &role, nullptr));
- EXPECT_EQ(UserRole::kUser, role);
- EXPECT_TRUE(base_dict.GetMinimalRole("base.command3", &role, nullptr));
- EXPECT_EQ(UserRole::kManager, role);
- EXPECT_TRUE(base_dict.GetMinimalRole("base.command4", &role, nullptr));
- EXPECT_EQ(UserRole::kOwner, role);
- EXPECT_FALSE(base_dict.GetMinimalRole("base.command5", &role, nullptr));
-}
-
-} // namespace weave
diff --git a/src/commands/command_instance.cc b/src/commands/command_instance.cc
index 702a819..da62887 100644
--- a/src/commands/command_instance.cc
+++ b/src/commands/command_instance.cc
@@ -9,7 +9,6 @@
#include <weave/error.h>
#include <weave/export.h>
-#include "src/commands/command_dictionary.h"
#include "src/commands/command_queue.h"
#include "src/commands/schema_constants.h"
#include "src/json_error_codes.h"
diff --git a/src/commands/command_manager.cc b/src/commands/command_manager.cc
deleted file mode 100644
index 9e9852b..0000000
--- a/src/commands/command_manager.cc
+++ /dev/null
@@ -1,107 +0,0 @@
-// Copyright 2015 The Weave 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 "src/commands/command_manager.h"
-
-#include <base/values.h>
-#include <weave/enum_to_string.h>
-#include <weave/error.h>
-
-#include "src/commands/schema_constants.h"
-#include "src/utils.h"
-
-namespace weave {
-
-CommandManager::CommandManager() {}
-
-CommandManager::~CommandManager() {}
-
-void CommandManager::AddCommandDefChanged(const base::Closure& callback) {
- on_command_changed_.push_back(callback);
- callback.Run();
-}
-
-const CommandDictionary& CommandManager::GetCommandDictionary() const {
- return dictionary_;
-}
-
-bool CommandManager::LoadCommands(const base::DictionaryValue& dict,
- ErrorPtr* error) {
- bool result = dictionary_.LoadCommands(dict, error);
- for (const auto& cb : on_command_changed_)
- cb.Run();
- return result;
-}
-
-bool CommandManager::LoadCommands(const std::string& json,
- ErrorPtr* error) {
- std::unique_ptr<const base::DictionaryValue> dict = LoadJsonDict(json, error);
- if (!dict)
- return false;
- return LoadCommands(*dict, error);
-}
-
-void CommandManager::AddCommand(
- std::unique_ptr<CommandInstance> command_instance) {
- command_queue_.Add(std::move(command_instance));
-}
-
-bool CommandManager::AddCommand(const base::DictionaryValue& command,
- std::string* id,
- ErrorPtr* error) {
- return AddCommand(command, UserRole::kOwner, id, error);
-}
-
-bool CommandManager::AddCommand(const base::DictionaryValue& command,
- UserRole role,
- std::string* id,
- ErrorPtr* error) {
- auto command_instance = CommandInstance::FromJson(
- &command, Command::Origin::kLocal, nullptr, error);
- if (!command_instance)
- return false;
-
- UserRole minimal_role;
- if (!GetCommandDictionary().GetMinimalRole(command_instance->GetName(),
- &minimal_role, error)) {
- return false;
- }
- if (role < minimal_role) {
- Error::AddToPrintf(
- error, FROM_HERE, errors::commands::kDomain, "access_denied",
- "User role '%s' less than minimal: '%s'", EnumToString(role).c_str(),
- EnumToString(minimal_role).c_str());
- return false;
- }
-
- command_instance->SetComponent("device");
- *id = std::to_string(++next_command_id_);
- command_instance->SetID(*id);
- AddCommand(std::move(command_instance));
- return true;
-}
-
-CommandInstance* CommandManager::FindCommand(const std::string& id) {
- return command_queue_.Find(id);
-}
-
-void CommandManager::AddCommandAddedCallback(
- const CommandQueue::CommandCallback& callback) {
- command_queue_.AddCommandAddedCallback(callback);
-}
-
-void CommandManager::AddCommandRemovedCallback(
- const CommandQueue::CommandCallback& callback) {
- command_queue_.AddCommandRemovedCallback(callback);
-}
-
-void CommandManager::AddCommandHandler(
- const std::string& command_name,
- const Device::CommandHandlerCallback& callback) {
- CHECK(command_name.empty() || dictionary_.FindCommand(command_name))
- << "Command undefined: " << command_name;
- command_queue_.AddCommandHandler("device", command_name, callback);
-}
-
-} // namespace weave
diff --git a/src/commands/command_manager.h b/src/commands/command_manager.h
deleted file mode 100644
index 644c165..0000000
--- a/src/commands/command_manager.h
+++ /dev/null
@@ -1,75 +0,0 @@
-// Copyright 2015 The Weave 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 LIBWEAVE_SRC_COMMANDS_COMMAND_MANAGER_H_
-#define LIBWEAVE_SRC_COMMANDS_COMMAND_MANAGER_H_
-
-#include <memory>
-#include <string>
-#include <vector>
-
-#include <base/callback.h>
-#include <base/macros.h>
-#include <base/memory/weak_ptr.h>
-
-#include "src/commands/command_dictionary.h"
-#include "src/commands/command_queue.h"
-
-namespace weave {
-
-class CommandInstance;
-
-// CommandManager class that will have a list of all the device command
-// schemas as well as the live command queue of pending command instances
-// dispatched to the device.
-class CommandManager final {
- public:
- CommandManager();
-
- ~CommandManager();
-
- bool AddCommand(const base::DictionaryValue& command,
- std::string* id,
- ErrorPtr* error);
- CommandInstance* FindCommand(const std::string& id);
- void AddCommandAddedCallback(const CommandQueue::CommandCallback& callback);
- void AddCommandRemovedCallback(const CommandQueue::CommandCallback& callback);
- void AddCommandHandler(const std::string& command_name,
- const Device::CommandHandlerCallback& callback);
-
- // Sets callback which is called when command definitions is changed.
- void AddCommandDefChanged(const base::Closure& callback);
-
- // Returns the command definitions for the device.
- const CommandDictionary& GetCommandDictionary() const;
-
- // Loads device command schema.
- bool LoadCommands(const base::DictionaryValue& dict,
- ErrorPtr* error);
-
- // Same as the overload above, but takes a path to a json file to read
- // the base command definitions from.
- bool LoadCommands(const std::string& json,
- ErrorPtr* error);
-
- // Adds a new command to the command queue.
- void AddCommand(std::unique_ptr<CommandInstance> command_instance);
-
- bool AddCommand(const base::DictionaryValue& command,
- UserRole role,
- std::string* id,
- ErrorPtr* error);
-
- private:
- CommandDictionary dictionary_; // Registered definitions.
- CommandQueue command_queue_;
- std::vector<base::Callback<void()>> on_command_changed_;
- uint32_t next_command_id_{0};
-
- DISALLOW_COPY_AND_ASSIGN(CommandManager);
-};
-
-} // namespace weave
-
-#endif // LIBWEAVE_SRC_COMMANDS_COMMAND_MANAGER_H_
diff --git a/src/commands/command_manager_unittest.cc b/src/commands/command_manager_unittest.cc
deleted file mode 100644
index 303cafa..0000000
--- a/src/commands/command_manager_unittest.cc
+++ /dev/null
@@ -1,99 +0,0 @@
-// Copyright 2015 The Weave 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 "src/commands/command_manager.h"
-
-#include <map>
-
-#include <base/json/json_writer.h>
-#include <gtest/gtest.h>
-#include <weave/provider/test/mock_config_store.h>
-#include <weave/test/unittest_utils.h>
-
-#include "src/bind_lambda.h"
-
-using testing::Return;
-
-namespace weave {
-
-using test::CreateDictionaryValue;
-
-namespace {
-
-const char kTestVendorCommands[] = R"({
- "robot": {
- "_jump": {
- "minimalRole": "user",
- "parameters": {"height": "integer"},
- "results": {}
- },
- "_speak": {
- "minimalRole": "user",
- "parameters": {"phrase": "string"},
- "results": {}
- }
- }
-})";
-
-const char kTestTestCommands[] = R"({
- "test": {
- "_yo": {
- "minimalRole": "user",
- "parameters": {"name": "string"},
- "results": {}
- }
- }
-})";
-
-} // namespace
-
-TEST(CommandManager, Empty) {
- CommandManager manager;
- EXPECT_TRUE(manager.GetCommandDictionary().IsEmpty());
-}
-
-TEST(CommandManager, LoadCommandsDict) {
- CommandManager manager;
- auto json = CreateDictionaryValue(kTestVendorCommands);
- EXPECT_TRUE(manager.LoadCommands(*json, nullptr));
-}
-
-TEST(CommandManager, LoadCommandsJson) {
- CommandManager manager;
-
- // Load device-supported commands.
- auto json_str = R"({
- "base": {
- "reboot": {
- "minimalRole": "user",
- "parameters": {"delay": "integer"},
- "results": {}
- }
- },
- "robot": {
- "_jump": {
- "minimalRole": "user",
- "parameters": {"height": "integer"},
- "results": {}
- }
- }
- })";
- EXPECT_TRUE(manager.LoadCommands(json_str, nullptr));
- EXPECT_EQ(2u, manager.GetCommandDictionary().GetSize());
- EXPECT_NE(nullptr, manager.GetCommandDictionary().FindCommand("base.reboot"));
- EXPECT_NE(nullptr, manager.GetCommandDictionary().FindCommand("robot._jump"));
-}
-
-TEST(CommandManager, ShouldLoadStandardAndTestDefinitions) {
- CommandManager manager;
- ASSERT_TRUE(manager.LoadCommands(kTestVendorCommands, nullptr));
- ASSERT_TRUE(manager.LoadCommands(kTestTestCommands, nullptr));
- EXPECT_EQ(3u, manager.GetCommandDictionary().GetSize());
- EXPECT_NE(nullptr, manager.GetCommandDictionary().FindCommand("robot._jump"));
- EXPECT_NE(nullptr,
- manager.GetCommandDictionary().FindCommand("robot._speak"));
- EXPECT_NE(nullptr, manager.GetCommandDictionary().FindCommand("test._yo"));
-}
-
-} // namespace weave