blob: f176cad5fa7ed5a2bf3139ecf5b26b74c9f2afdf [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#ifndef BUFFET_COMMANDS_COMMAND_MANAGER_H_
6#define BUFFET_COMMANDS_COMMAND_MANAGER_H_
7
Alex Vakulenko95110752014-09-03 16:27:21 -07008#include <memory>
Alex Vakulenkofd448692014-07-22 07:46:53 -07009#include <string>
10
Alex Vakulenkofd448692014-07-22 07:46:53 -070011#include <base/files/file_path.h>
Alex Vakulenko95110752014-09-03 16:27:21 -070012#include <base/macros.h>
13#include <base/memory/weak_ptr.h>
Alex Vakulenko7c36b672014-07-16 14:50:58 -070014
15#include "buffet/commands/command_dictionary.h"
Alex Vakulenko95110752014-09-03 16:27:21 -070016#include "buffet/commands/command_queue.h"
17#include "buffet/commands/dbus_command_dispatcher.h"
18
19namespace chromeos {
20namespace dbus_utils {
21class ExportedObjectManager;
22} // namespace dbus_utils
23} // namespace chromeos
24
Alex Vakulenko7c36b672014-07-16 14:50:58 -070025
26namespace buffet {
27
Alex Vakulenko95110752014-09-03 16:27:21 -070028class CommandInstance;
29
Alex Vakulenko7c36b672014-07-16 14:50:58 -070030// CommandManager class that will have a list of all the device command
31// schemas as well as the live command queue of pending command instances
32// dispatched to the device.
Alex Vakulenko95110752014-09-03 16:27:21 -070033class CommandManager final {
Alex Vakulenko7c36b672014-07-16 14:50:58 -070034 public:
Alex Vakulenko95110752014-09-03 16:27:21 -070035 CommandManager();
36 explicit CommandManager(
37 const base::WeakPtr<chromeos::dbus_utils::ExportedObjectManager>&
38 object_manager);
Alex Vakulenko7c36b672014-07-16 14:50:58 -070039
40 // Get the command definitions for the device.
41 const CommandDictionary& GetCommandDictionary() const;
42
Alex Vakulenkofd448692014-07-22 07:46:53 -070043 // Loads base/standard GCD command definitions.
44 // |json| is the full JSON schema of standard GCD commands. These commands
45 // are not necessarily supported by a particular device but rather
46 // all the standard commands defined by GCD standard for all known/supported
47 // device kinds.
48 // On success, returns true. Otherwise, |error| contains additional
49 // error information.
50 bool LoadBaseCommands(const base::DictionaryValue& json,
Alex Vakulenko5f472062014-08-14 17:54:04 -070051 chromeos::ErrorPtr* error);
Alex Vakulenkofd448692014-07-22 07:46:53 -070052
53 // Same as the overload above, but takes a path to a json file to read
54 // the base command definitions from.
55 bool LoadBaseCommands(const base::FilePath& json_file_path,
Alex Vakulenko5f472062014-08-14 17:54:04 -070056 chromeos::ErrorPtr* error);
Alex Vakulenkofd448692014-07-22 07:46:53 -070057
58 // Loads device command schema for particular category.
59 // See CommandDictionary::LoadCommands for detailed description of the
60 // parameters.
61 bool LoadCommands(const base::DictionaryValue& json,
Alex Vakulenko5f472062014-08-14 17:54:04 -070062 const std::string& category,
63 chromeos::ErrorPtr* error);
Alex Vakulenkofd448692014-07-22 07:46:53 -070064
65 // Same as the overload above, but takes a path to a json file to read
66 // the base command definitions from. Also, the command category is
67 // derived from file name (without extension). So, if the path points to
68 // "power_manager.json", the command category used will be "power_manager".
69 bool LoadCommands(const base::FilePath& json_file_path,
Alex Vakulenko5f472062014-08-14 17:54:04 -070070 chromeos::ErrorPtr* error);
Alex Vakulenkofd448692014-07-22 07:46:53 -070071
Alex Vakulenkoc2bc9a42014-07-23 10:57:58 -070072 // Startup method to be called by buffet daemon at startup.
Christopher Wileybb5b8482015-02-12 13:42:16 -080073 // Initializes the object and reads files in |definitions_path| to load
74 // 1) the standard GCD command dictionary
75 // 2) static vendor-provided command definitions
76 // If |test_definitions_path| is not empty, we'll also look there for
77 // additional commands.
78 void Startup(const base::FilePath& definitions_path,
79 const base::FilePath& test_definitions_path);
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070080
Anton Muhin5191e812014-10-30 17:49:48 +040081 // Adds a new command to the command queue.
82 void AddCommand(std::unique_ptr<CommandInstance> command_instance);
83
84 // Finds a command by the command |id|. Returns nullptr if the command with
85 // the given |id| is not found. The returned pointer should not be persisted
86 // for a long period of time.
87 CommandInstance* FindCommand(const std::string& id) const;
Alex Vakulenko95110752014-09-03 16:27:21 -070088
Alex Vakulenko7c36b672014-07-16 14:50:58 -070089 private:
Alex Vakulenkofd448692014-07-22 07:46:53 -070090 CommandDictionary base_dictionary_; // Base/std command definitions/schemas.
Alex Vakulenko7c36b672014-07-16 14:50:58 -070091 CommandDictionary dictionary_; // Command definitions/schemas.
Alex Vakulenko95110752014-09-03 16:27:21 -070092 CommandQueue command_queue_;
93 DBusCommandDispacher command_dispatcher_;
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070094
Alex Vakulenko7c36b672014-07-16 14:50:58 -070095 DISALLOW_COPY_AND_ASSIGN(CommandManager);
96};
97
98} // namespace buffet
99
100#endif // BUFFET_COMMANDS_COMMAND_MANAGER_H_