blob: 7a1701aae2d52508b194cb150f7f671ff427e3de [file] [log] [blame]
Alex Vakulenko7c36b672014-07-16 14:50:58 -07001// 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#include "buffet/commands/command_manager.h"
6
Alex Vakulenko5841c302014-07-23 10:49:49 -07007#include <base/files/file_enumerator.h>
Alex Vakulenko5841c302014-07-23 10:49:49 -07008#include <base/values.h>
Alex Vakulenko95110752014-09-03 16:27:21 -07009#include <chromeos/dbus/exported_object_manager.h>
Alex Vakulenkoa8b95bc2014-08-27 11:00:57 -070010#include <chromeos/errors/error.h>
Alex Vakulenkofd448692014-07-22 07:46:53 -070011
12#include "buffet/commands/schema_constants.h"
Alex Vakulenkob04936f2014-09-19 14:53:58 -070013#include "buffet/utils.h"
Alex Vakulenkofd448692014-07-22 07:46:53 -070014
Alex Vakulenko95110752014-09-03 16:27:21 -070015using chromeos::dbus_utils::ExportedObjectManager;
16
Alex Vakulenko7c36b672014-07-16 14:50:58 -070017namespace buffet {
18
Alex Vakulenko95110752014-09-03 16:27:21 -070019CommandManager::CommandManager() {
20 command_queue_.SetCommandDispachInterface(&command_dispatcher_);
21}
22
23CommandManager::CommandManager(
24 const base::WeakPtr<ExportedObjectManager>& object_manager)
25 : command_dispatcher_(object_manager->GetBus(), object_manager.get()) {
26 command_queue_.SetCommandDispachInterface(&command_dispatcher_);
27}
28
Alex Vakulenko7c36b672014-07-16 14:50:58 -070029const CommandDictionary& CommandManager::GetCommandDictionary() const {
30 return dictionary_;
31}
32
Alex Vakulenkofd448692014-07-22 07:46:53 -070033bool CommandManager::LoadBaseCommands(const base::DictionaryValue& json,
Alex Vakulenko5f472062014-08-14 17:54:04 -070034 chromeos::ErrorPtr* error) {
Alex Vakulenkofd448692014-07-22 07:46:53 -070035 return base_dictionary_.LoadCommands(json, "", nullptr, error);
36}
37
38bool CommandManager::LoadBaseCommands(const base::FilePath& json_file_path,
Alex Vakulenko5f472062014-08-14 17:54:04 -070039 chromeos::ErrorPtr* error) {
Alex Vakulenkofd448692014-07-22 07:46:53 -070040 std::unique_ptr<const base::DictionaryValue> json = LoadJsonDict(
41 json_file_path, error);
42 if (!json)
43 return false;
44 return LoadBaseCommands(*json, error);
45}
46
47bool CommandManager::LoadCommands(const base::DictionaryValue& json,
48 const std::string& category,
Alex Vakulenko5f472062014-08-14 17:54:04 -070049 chromeos::ErrorPtr* error) {
Alex Vakulenkofd448692014-07-22 07:46:53 -070050 return dictionary_.LoadCommands(json, category, &base_dictionary_, error);
51}
52
53bool CommandManager::LoadCommands(const base::FilePath& json_file_path,
Alex Vakulenko5f472062014-08-14 17:54:04 -070054 chromeos::ErrorPtr* error) {
Alex Vakulenkofd448692014-07-22 07:46:53 -070055 std::unique_ptr<const base::DictionaryValue> json = LoadJsonDict(
56 json_file_path, error);
57 if (!json)
58 return false;
59 std::string category = json_file_path.BaseName().RemoveExtension().value();
60 return LoadCommands(*json, category, error);
61}
62
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070063void CommandManager::Startup() {
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070064 LOG(INFO) << "Initializing CommandManager.";
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070065 // Load global standard GCD command dictionary.
66 base::FilePath base_command_file("/etc/buffet/gcd.json");
67 LOG(INFO) << "Loading standard commands from " << base_command_file.value();
Alex Vakulenkoc2bc9a42014-07-23 10:57:58 -070068 CHECK(LoadBaseCommands(base_command_file, nullptr))
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070069 << "Failed to load the standard command definitions.";
70
71 // Load static device command definitions.
72 base::FilePath device_command_dir("/etc/buffet/commands");
73 base::FileEnumerator enumerator(device_command_dir, false,
74 base::FileEnumerator::FILES,
75 FILE_PATH_LITERAL("*.json"));
76 base::FilePath json_file_path = enumerator.Next();
77 while (!json_file_path.empty()) {
78 LOG(INFO) << "Loading command schema from " << json_file_path.value();
Alex Vakulenkoc2bc9a42014-07-23 10:57:58 -070079 CHECK(LoadCommands(json_file_path, nullptr))
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070080 << "Failed to load the command definition file.";
81 json_file_path = enumerator.Next();
82 }
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070083}
84
Alex Vakulenko95110752014-09-03 16:27:21 -070085std::string CommandManager::AddCommand(
86 std::unique_ptr<CommandInstance> command_instance) {
87 return command_queue_.Add(std::move(command_instance));
88}
89
Alex Vakulenko7c36b672014-07-16 14:50:58 -070090} // namespace buffet