summaryrefslogtreecommitdiff
path: root/chromium/components/login/base_screen_handler_utils.h
blob: e6a6e645edd94de0e24a3811b1c13260bc943a5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_
#define COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_

#include <stddef.h>

#include <cstddef>
#include <string>

#include "base/callback.h"
#include "base/logging.h"
#include "base/strings/string16.h"
#include "base/tuple.h"
#include "base/values.h"
#include "components/login/login_export.h"
#include "components/signin/core/account_id/account_id.h"

namespace login {

typedef std::vector<std::string> StringList;
typedef std::vector<base::string16> String16List;

template <typename T>
struct LOGIN_EXPORT UnwrapConstRef {
  typedef T Type;
};

template <typename T>
struct UnwrapConstRef<const T&> {
  typedef T Type;
};

bool LOGIN_EXPORT ParseValue(const base::Value* value, bool* out_value);
bool LOGIN_EXPORT ParseValue(const base::Value* value, int* out_value);
bool LOGIN_EXPORT ParseValue(const base::Value* value, double* out_value);
bool LOGIN_EXPORT ParseValue(const base::Value* value, std::string* out_value);
bool LOGIN_EXPORT
    ParseValue(const base::Value* value, base::string16* out_value);
bool LOGIN_EXPORT ParseValue(const base::Value* value,
                             const base::DictionaryValue** out_value);
bool LOGIN_EXPORT ParseValue(const base::Value* value, StringList* out_value);
bool LOGIN_EXPORT ParseValue(const base::Value* value, String16List* out_value);
bool LOGIN_EXPORT ParseValue(const base::Value* value, AccountId* out_value);

template <typename T>
inline bool GetArg(const base::ListValue* args, size_t index, T* out_value) {
  const base::Value* value;
  if (!args->Get(index, &value))
    return false;
  return ParseValue(value, out_value);
}

base::Value LOGIN_EXPORT MakeValue(bool v);
base::Value LOGIN_EXPORT MakeValue(int v);
base::Value LOGIN_EXPORT MakeValue(double v);
base::StringValue LOGIN_EXPORT MakeValue(const std::string& v);
base::StringValue LOGIN_EXPORT MakeValue(const base::string16& v);
base::StringValue LOGIN_EXPORT MakeValue(const AccountId& v);

template <typename T>
inline const T& MakeValue(const T& v) {
  return v;
}

template <typename T>
struct ParsedValueContainer {
  T value;
};

template <>
struct LOGIN_EXPORT ParsedValueContainer<AccountId> {
  ParsedValueContainer();
  AccountId value = EmptyAccountId();
};

template <typename Arg, size_t index>
typename UnwrapConstRef<Arg>::Type ParseArg(const base::ListValue* args) {
  ParsedValueContainer<typename UnwrapConstRef<Arg>::Type> parsed;
  CHECK(GetArg(args, index, &parsed.value));
  return parsed.value;
}

template <typename... Args, size_t... Ns>
inline void DispatchToCallback(const base::Callback<void(Args...)>& callback,
                               const base::ListValue* args,
                               base::IndexSequence<Ns...> indexes) {
  DCHECK(args);
  DCHECK_EQ(sizeof...(Args), args->GetSize());

  callback.Run(ParseArg<Args, Ns>(args)...);
}

template <typename... Args>
void CallbackWrapper(const base::Callback<void(Args...)>& callback,
                     const base::ListValue* args) {
  DispatchToCallback(callback, args,
                     base::MakeIndexSequence<sizeof...(Args)>());
}


}  // namespace login

#endif  // COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_