blob: afdd5189e9d9cf15a464f487205ee00a633672fd [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) {
Vitaly Bukaaabadee2015-03-18 23:33:44 -070050 bool result =
51 dictionary_.LoadCommands(json, category, &base_dictionary_, error);
52 if (!on_command_defs_changed_.is_null())
53 on_command_defs_changed_.Run();
54 return result;
Alex Vakulenkofd448692014-07-22 07:46:53 -070055}
56
57bool CommandManager::LoadCommands(const base::FilePath& json_file_path,
Alex Vakulenko5f472062014-08-14 17:54:04 -070058 chromeos::ErrorPtr* error) {
Alex Vakulenkofd448692014-07-22 07:46:53 -070059 std::unique_ptr<const base::DictionaryValue> json = LoadJsonDict(
60 json_file_path, error);
61 if (!json)
62 return false;
63 std::string category = json_file_path.BaseName().RemoveExtension().value();
64 return LoadCommands(*json, category, error);
65}
66
Christopher Wileybb5b8482015-02-12 13:42:16 -080067void CommandManager::Startup(const base::FilePath& definitions_path,
68 const base::FilePath& test_definitions_path) {
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070069 LOG(INFO) << "Initializing CommandManager.";
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070070 // Load global standard GCD command dictionary.
Christopher Wileybb5b8482015-02-12 13:42:16 -080071 base::FilePath base_command_file{definitions_path.Append("gcd.json")};
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070072 LOG(INFO) << "Loading standard commands from " << base_command_file.value();
Alex Vakulenkoc2bc9a42014-07-23 10:57:58 -070073 CHECK(LoadBaseCommands(base_command_file, nullptr))
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070074 << "Failed to load the standard command definitions.";
75
Christopher Wileybb5b8482015-02-12 13:42:16 -080076 auto LoadPackages = [this](const base::FilePath& root,
77 const base::FilePath::StringType& pattern) {
78 base::FilePath device_command_dir{root.Append("commands")};
79 VLOG(2) << "Looking for commands in " << root.value();
80 base::FileEnumerator enumerator(device_command_dir, false,
81 base::FileEnumerator::FILES,
82 pattern);
83 base::FilePath json_file_path = enumerator.Next();
84 while (!json_file_path.empty()) {
85 LOG(INFO) << "Loading command schema from " << json_file_path.value();
86 CHECK(this->LoadCommands(json_file_path, nullptr))
87 << "Failed to load the command definition file.";
88 json_file_path = enumerator.Next();
89 }
90 };
91 LoadPackages(definitions_path, FILE_PATH_LITERAL("*.json"));
92 LoadPackages(test_definitions_path, FILE_PATH_LITERAL("*test.json"));
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070093}
94
Anton Muhin5191e812014-10-30 17:49:48 +040095void CommandManager::AddCommand(
Alex Vakulenko95110752014-09-03 16:27:21 -070096 std::unique_ptr<CommandInstance> command_instance) {
Anton Muhin5191e812014-10-30 17:49:48 +040097 command_queue_.Add(std::move(command_instance));
98}
99
100CommandInstance* CommandManager::FindCommand(const std::string& id) const {
101 return command_queue_.Find(id);
Alex Vakulenko95110752014-09-03 16:27:21 -0700102}
103
Alex Vakulenko7c36b672014-07-16 14:50:58 -0700104} // namespace buffet