blob: 02d92fd9863affc98aafce26c407bad82c2dc994 [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 Vakulenkofd448692014-07-22 07:46:53 -07008#include <string>
9
Alex Vakulenko7c36b672014-07-16 14:50:58 -070010#include <base/basictypes.h>
Alex Vakulenkofd448692014-07-22 07:46:53 -070011#include <base/files/file_path.h>
Alex Vakulenko7c36b672014-07-16 14:50:58 -070012
13#include "buffet/commands/command_dictionary.h"
14
15namespace buffet {
16
17// CommandManager class that will have a list of all the device command
18// schemas as well as the live command queue of pending command instances
19// dispatched to the device.
20class CommandManager {
21 public:
22 CommandManager() = default;
23
24 // Get the command definitions for the device.
25 const CommandDictionary& GetCommandDictionary() const;
26
Alex Vakulenkofd448692014-07-22 07:46:53 -070027 // Loads base/standard GCD command definitions.
28 // |json| is the full JSON schema of standard GCD commands. These commands
29 // are not necessarily supported by a particular device but rather
30 // all the standard commands defined by GCD standard for all known/supported
31 // device kinds.
32 // On success, returns true. Otherwise, |error| contains additional
33 // error information.
34 bool LoadBaseCommands(const base::DictionaryValue& json,
35 ErrorPtr* error);
36
37 // Same as the overload above, but takes a path to a json file to read
38 // the base command definitions from.
39 bool LoadBaseCommands(const base::FilePath& json_file_path,
40 ErrorPtr* error);
41
42 // Loads device command schema for particular category.
43 // See CommandDictionary::LoadCommands for detailed description of the
44 // parameters.
45 bool LoadCommands(const base::DictionaryValue& json,
46 const std::string& category, ErrorPtr* error);
47
48 // Same as the overload above, but takes a path to a json file to read
49 // the base command definitions from. Also, the command category is
50 // derived from file name (without extension). So, if the path points to
51 // "power_manager.json", the command category used will be "power_manager".
52 bool LoadCommands(const base::FilePath& json_file_path,
53 ErrorPtr* error);
54
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070055 // Factory static method to get the global singleton instance of the object.
56 static CommandManager* GetInstance();
57 // Global startup method to be called by buffet daemon at startup.
58 // Initializes the global object singleton and loads the standard GCD command
59 // dictionary as well as static vendor-provided command definitions for
60 // the current device.
61 static void Startup();
62
Alex Vakulenko7c36b672014-07-16 14:50:58 -070063 private:
Alex Vakulenkofd448692014-07-22 07:46:53 -070064 // Helper function to load a JSON file that is expected to be
65 // an object/dictionary. In case of error, returns empty unique ptr and fills
66 // in error details in |error|.
67 std::unique_ptr<const base::DictionaryValue> LoadJsonDict(
68 const base::FilePath& json_file_path, ErrorPtr* error);
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070069 // Helper method to be called at buffet shutdown to clean up the global
70 // singleton instance of this class.
71 static void Shutdown();
Alex Vakulenkofd448692014-07-22 07:46:53 -070072
73 CommandDictionary base_dictionary_; // Base/std command definitions/schemas.
Alex Vakulenko7c36b672014-07-16 14:50:58 -070074 CommandDictionary dictionary_; // Command definitions/schemas.
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070075
76 static CommandManager* instance_; // Global singleton instance of the object.
Alex Vakulenko7c36b672014-07-16 14:50:58 -070077 DISALLOW_COPY_AND_ASSIGN(CommandManager);
78};
79
80} // namespace buffet
81
82#endif // BUFFET_COMMANDS_COMMAND_MANAGER_H_