1 // Copyright (c) 2013 The Chromium 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.
5 #ifndef CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_
6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_
11 #include "base/callback.h"
12 #include "base/logging.h"
13 #include "base/strings/string16.h"
14 #include "base/values.h"
19 struct UnwrapConstRef
{
24 struct UnwrapConstRef
<const T
&> {
29 inline bool ParseValue(const base::Value
* value
, T
* out_value
);
32 inline bool ParseValue
<bool>(const base::Value
* value
, bool* out_value
) {
33 return value
->GetAsBoolean(out_value
);
37 inline bool ParseValue
<int>(const base::Value
* value
, int* out_value
) {
38 return value
->GetAsInteger(out_value
);
42 inline bool ParseValue
<double>(const base::Value
* value
, double* out_value
) {
43 return value
->GetAsDouble(out_value
);
47 inline bool ParseValue
<std::string
>(const base::Value
* value
,
48 std::string
* out_value
) {
49 return value
->GetAsString(out_value
);
53 inline bool ParseValue
<base::string16
>(const base::Value
* value
,
54 base::string16
* out_value
) {
55 return value
->GetAsString(out_value
);
59 inline bool ParseValue
<const base::DictionaryValue
*>(
60 const base::Value
* value
,
61 const base::DictionaryValue
** out_value
) {
62 return value
->GetAsDictionary(out_value
);
66 inline bool GetArg(const base::ListValue
* args
, size_t index
, T
* out_value
) {
67 const base::Value
* value
;
68 if (!args
->Get(index
, &value
))
70 return ParseValue(value
, out_value
);
73 inline base::FundamentalValue
MakeValue(bool v
) {
74 return base::FundamentalValue(v
);
77 inline base::FundamentalValue
MakeValue(int v
) {
78 return base::FundamentalValue(v
);
81 inline base::FundamentalValue
MakeValue(double v
) {
82 return base::FundamentalValue(v
);
85 inline base::StringValue
MakeValue(const std::string
& v
) {
86 return base::StringValue(v
);
89 inline base::StringValue
MakeValue(const base::string16
& v
) {
90 return base::StringValue(v
);
94 inline const T
& MakeValue(const T
& v
) {
98 inline void CallbackWrapper0(base::Callback
<void()> callback
,
99 const base::ListValue
* args
) {
101 DCHECK(args
->empty());
105 template<typename A1
>
106 void CallbackWrapper1(base::Callback
<void(A1
)> callback
,
107 const base::ListValue
* args
) {
109 DCHECK_EQ(1u, args
->GetSize());
110 typename UnwrapConstRef
<A1
>::Type arg1
;
111 if (!GetArg(args
, 0, &arg1
)) {
118 template<typename A1
, typename A2
>
119 void CallbackWrapper2(base::Callback
<void(A1
, A2
)> callback
,
120 const base::ListValue
* args
) {
122 DCHECK_EQ(2u, args
->GetSize());
123 typename UnwrapConstRef
<A1
>::Type arg1
;
124 typename UnwrapConstRef
<A2
>::Type arg2
;
125 if (!GetArg(args
, 0, &arg1
) || !GetArg(args
, 1, &arg2
)) {
129 callback
.Run(arg1
, arg2
);
132 template<typename A1
, typename A2
, typename A3
>
133 void CallbackWrapper3(base::Callback
<void(A1
, A2
, A3
)> callback
,
134 const base::ListValue
* args
) {
136 DCHECK_EQ(3u, args
->GetSize());
137 typename UnwrapConstRef
<A1
>::Type arg1
;
138 typename UnwrapConstRef
<A2
>::Type arg2
;
139 typename UnwrapConstRef
<A3
>::Type arg3
;
140 if (!GetArg(args
, 0, &arg1
) ||
141 !GetArg(args
, 1, &arg2
) ||
142 !GetArg(args
, 2, &arg3
)) {
146 callback
.Run(arg1
, arg2
, arg3
);
149 template<typename A1
, typename A2
, typename A3
, typename A4
>
150 void CallbackWrapper4(base::Callback
<void(A1
, A2
, A3
, A4
)> callback
,
151 const base::ListValue
* args
) {
153 DCHECK_EQ(4u, args
->GetSize());
154 typename UnwrapConstRef
<A1
>::Type arg1
;
155 typename UnwrapConstRef
<A2
>::Type arg2
;
156 typename UnwrapConstRef
<A3
>::Type arg3
;
157 typename UnwrapConstRef
<A4
>::Type arg4
;
158 if (!GetArg(args
, 0, &arg1
) ||
159 !GetArg(args
, 1, &arg2
) ||
160 !GetArg(args
, 2, &arg3
) ||
161 !GetArg(args
, 3, &arg4
)) {
165 callback
.Run(arg1
, arg2
, arg3
, arg4
);
168 } // namespace chromeos
170 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_