blob: 072d05516becf306b15e8a5210ca255f23a99b2e [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>
Alex Vakulenkoe03af6d2015-04-20 11:00:54 -070010#include <vector>
Alex Vakulenkofd448692014-07-22 07:46:53 -070011
Alex Vakulenko9ea5a322015-04-17 15:35:34 -070012#include <base/callback.h>
13#include <base/callback_list.h>
Alex Vakulenkofd448692014-07-22 07:46:53 -070014#include <base/files/file_path.h>
Alex Vakulenko95110752014-09-03 16:27:21 -070015#include <base/macros.h>
16#include <base/memory/weak_ptr.h>
Alex Vakulenko7c36b672014-07-16 14:50:58 -070017
18#include "buffet/commands/command_dictionary.h"
Alex Vakulenko95110752014-09-03 16:27:21 -070019#include "buffet/commands/command_queue.h"
20#include "buffet/commands/dbus_command_dispatcher.h"
21
22namespace chromeos {
23namespace dbus_utils {
24class ExportedObjectManager;
25} // namespace dbus_utils
26} // namespace chromeos
27
Alex Vakulenko7c36b672014-07-16 14:50:58 -070028
29namespace buffet {
30
Alex Vakulenko95110752014-09-03 16:27:21 -070031class CommandInstance;
32
Alex Vakulenko7c36b672014-07-16 14:50:58 -070033// CommandManager class that will have a list of all the device command
34// schemas as well as the live command queue of pending command instances
35// dispatched to the device.
Alex Vakulenko95110752014-09-03 16:27:21 -070036class CommandManager final {
Alex Vakulenko7c36b672014-07-16 14:50:58 -070037 public:
Alex Vakulenko9ea5a322015-04-17 15:35:34 -070038 // A token given by CommandManager in response to AddOnCommandDefChanged().
39 // When the CallbackToken is destroyed, the registered notification
40 // callback associated with it will automatically be removed from the command
41 // manager's callback list.
42 using CallbackToken =
43 std::unique_ptr<base::CallbackList<void()>::Subscription>;
44
Alex Vakulenko95110752014-09-03 16:27:21 -070045 CommandManager();
46 explicit CommandManager(
47 const base::WeakPtr<chromeos::dbus_utils::ExportedObjectManager>&
48 object_manager);
Alex Vakulenko9e2f8cd2015-04-07 16:28:09 -070049 // Special constructor to help mock out command dispatcher for testing.
50 explicit CommandManager(CommandDispachInterface* dispatch_interface);
Alex Vakulenko7c36b672014-07-16 14:50:58 -070051
Vitaly Bukaaabadee2015-03-18 23:33:44 -070052 // Sets callback which is called when command definitions is changed.
Alex Vakulenkoe03af6d2015-04-20 11:00:54 -070053 CallbackToken AddOnCommandDefChanged(
54 const base::Closure& callback) WARN_UNUSED_RESULT {
Alex Vakulenko9ea5a322015-04-17 15:35:34 -070055 return CallbackToken{on_command_changed_.Add(callback).release()};
Vitaly Bukaaabadee2015-03-18 23:33:44 -070056 }
57
58 // Returns the command definitions for the device.
Alex Vakulenko7c36b672014-07-16 14:50:58 -070059 const CommandDictionary& GetCommandDictionary() const;
60
Alex Vakulenkofd448692014-07-22 07:46:53 -070061 // Loads base/standard GCD command definitions.
62 // |json| is the full JSON schema of standard GCD commands. These commands
63 // are not necessarily supported by a particular device but rather
64 // all the standard commands defined by GCD standard for all known/supported
65 // device kinds.
66 // On success, returns true. Otherwise, |error| contains additional
67 // error information.
68 bool LoadBaseCommands(const base::DictionaryValue& json,
Alex Vakulenko5f472062014-08-14 17:54:04 -070069 chromeos::ErrorPtr* error);
Alex Vakulenkofd448692014-07-22 07:46:53 -070070
71 // Same as the overload above, but takes a path to a json file to read
72 // the base command definitions from.
73 bool LoadBaseCommands(const base::FilePath& json_file_path,
Alex Vakulenko5f472062014-08-14 17:54:04 -070074 chromeos::ErrorPtr* error);
Alex Vakulenkofd448692014-07-22 07:46:53 -070075
76 // Loads device command schema for particular category.
77 // See CommandDictionary::LoadCommands for detailed description of the
78 // parameters.
79 bool LoadCommands(const base::DictionaryValue& json,
Alex Vakulenko5f472062014-08-14 17:54:04 -070080 const std::string& category,
81 chromeos::ErrorPtr* error);
Alex Vakulenkofd448692014-07-22 07:46:53 -070082
83 // Same as the overload above, but takes a path to a json file to read
84 // the base command definitions from. Also, the command category is
85 // derived from file name (without extension). So, if the path points to
86 // "power_manager.json", the command category used will be "power_manager".
87 bool LoadCommands(const base::FilePath& json_file_path,
Alex Vakulenko5f472062014-08-14 17:54:04 -070088 chromeos::ErrorPtr* error);
Alex Vakulenkofd448692014-07-22 07:46:53 -070089
Alex Vakulenkoc2bc9a42014-07-23 10:57:58 -070090 // Startup method to be called by buffet daemon at startup.
Christopher Wileybb5b8482015-02-12 13:42:16 -080091 // Initializes the object and reads files in |definitions_path| to load
92 // 1) the standard GCD command dictionary
93 // 2) static vendor-provided command definitions
94 // If |test_definitions_path| is not empty, we'll also look there for
95 // additional commands.
96 void Startup(const base::FilePath& definitions_path,
97 const base::FilePath& test_definitions_path);
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070098
Anton Muhin5191e812014-10-30 17:49:48 +040099 // Adds a new command to the command queue.
100 void AddCommand(std::unique_ptr<CommandInstance> command_instance);
101
102 // Finds a command by the command |id|. Returns nullptr if the command with
103 // the given |id| is not found. The returned pointer should not be persisted
104 // for a long period of time.
105 CommandInstance* FindCommand(const std::string& id) const;
Alex Vakulenko95110752014-09-03 16:27:21 -0700106
Alex Vakulenkoe03af6d2015-04-20 11:00:54 -0700107 // Changes the visibility of commands.
108 bool SetCommandVisibility(const std::vector<std::string>& command_names,
109 CommandDefinition::Visibility visibility,
110 chromeos::ErrorPtr* error);
111
Alex Vakulenko7c36b672014-07-16 14:50:58 -0700112 private:
Alex Vakulenkofd448692014-07-22 07:46:53 -0700113 CommandDictionary base_dictionary_; // Base/std command definitions/schemas.
Alex Vakulenko7c36b672014-07-16 14:50:58 -0700114 CommandDictionary dictionary_; // Command definitions/schemas.
Alex Vakulenko95110752014-09-03 16:27:21 -0700115 CommandQueue command_queue_;
116 DBusCommandDispacher command_dispatcher_;
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700117 base::CallbackList<void()> on_command_changed_;
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -0700118
Alex Vakulenko7c36b672014-07-16 14:50:58 -0700119 DISALLOW_COPY_AND_ASSIGN(CommandManager);
120};
121
122} // namespace buffet
123
124#endif // BUFFET_COMMANDS_COMMAND_MANAGER_H_