blob: 8d73bf4c022dfd1c6822526426639d70e7dee63e [file] [log] [blame]
Eric Carusocc7106c2017-04-27 14:22:42 -07001// Copyright 2017 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 DEBUGD_SRC_VARIANT_UTILS_H_
6#define DEBUGD_SRC_VARIANT_UTILS_H_
7
8#include <string>
Qijiang Fan713061e2021-03-08 15:45:12 +09009#include <base/check.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070010#include <brillo/errors/error.h>
Simon Glass2b1da092020-05-21 12:24:16 -060011#include <brillo/process/process.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070012#include <brillo/type_name_undecorate.h>
13#include <brillo/variant_dictionary.h>
14
15#include "debugd/src/error_utils.h"
16#include "debugd/src/process_with_id.h"
17
18namespace debugd {
19
20const char kOptionParsingErrorString[] =
21 "org.chromium.debugd.error.OptionParsing";
22
23enum class ParseResult {
24 NOT_PRESENT,
25 PARSE_ERROR,
26 PARSED,
27};
28
29// Looks up an option in the |options| dictionary. If the
30// option is not present, returns NOT_PRESENT. If parsing
31// fails (i.e. the supplied option is the wrong type) then
32// add to the error chain in |error| and return PARSE_ERROR.
33// If the option is present and of the right type, return
34// PARSED.
35template <typename T>
36ParseResult GetOption(const brillo::VariantDictionary& options,
37 const std::string& key,
38 T* value,
39 brillo::ErrorPtr* error) {
40 DCHECK(value);
41
42 const auto& it = options.find(key);
43 if (it == options.end())
44 return ParseResult::NOT_PRESENT;
45
46 if (!it->second.GetValue(value)) {
47 std::string expected = brillo::GetUndecoratedTypeName<T>();
48 std::string got = it->second.GetUndecoratedTypeName();
Tom Hughesd6c2d392020-08-24 18:12:11 -070049 DEBUGD_ADD_ERROR_FMT(
50 error, kOptionParsingErrorString,
51 "Option \"%s\" has the wrong type (expected %s, got %s)", key.c_str(),
52 expected.c_str(), got.c_str());
Eric Carusocc7106c2017-04-27 14:22:42 -070053 return ParseResult::PARSE_ERROR;
54 }
55 return ParseResult::PARSED;
56}
57
58// Looks up an option in the |options| dictionary. If it exists and
59// isn't an integer, returns false. Otherwise, returns true, and if it
60// exists in the dictionary adds it to the command line for |process|
61// as the value for key |flag_name|.
Nicole Anderson-Aud9f0aed2020-12-22 17:16:25 +000062bool AddIntOption(SandboxedProcess* process,
Eric Carusocc7106c2017-04-27 14:22:42 -070063 const brillo::VariantDictionary& options,
64 const std::string& key,
65 const std::string& flag_name,
66 brillo::ErrorPtr* error);
67
Nicole Anderson-Au296864c2020-12-22 17:52:31 +000068// Looks up an option in the |options| dictionary. If it exists and
69// isn't a boolean, returns false. Otherwise, returns true, and if it
70// exists in the dictionary adds it to the command line for |process|.
71bool AddBoolOption(SandboxedProcess* process,
72 const brillo::VariantDictionary& options,
73 const std::string& key,
74 const std::string& flag_name,
75 brillo::ErrorPtr* error);
76
Eric Carusocc7106c2017-04-27 14:22:42 -070077} // namespace debugd
78
79#endif // DEBUGD_SRC_VARIANT_UTILS_H_