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 COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_
6 #define COMPONENTS_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"
15 #include "components/login/login_export.h"
19 typedef std::vector
<std::string
> StringList
;
20 typedef std::vector
<base::string16
> String16List
;
23 struct LOGIN_EXPORT UnwrapConstRef
{
28 struct LOGIN_EXPORT UnwrapConstRef
<const T
&> {
32 bool LOGIN_EXPORT
ParseValue(const base::Value
* value
, bool* out_value
);
33 bool LOGIN_EXPORT
ParseValue(const base::Value
* value
, int* out_value
);
34 bool LOGIN_EXPORT
ParseValue(const base::Value
* value
, double* out_value
);
35 bool LOGIN_EXPORT
ParseValue(const base::Value
* value
, std::string
* out_value
);
37 ParseValue(const base::Value
* value
, base::string16
* out_value
);
38 bool LOGIN_EXPORT
ParseValue(const base::Value
* value
,
39 const base::DictionaryValue
** out_value
);
40 bool LOGIN_EXPORT
ParseValue(const base::Value
* value
, StringList
* out_value
);
41 bool LOGIN_EXPORT
ParseValue(const base::Value
* value
, String16List
* out_value
);
44 inline bool GetArg(const base::ListValue
* args
, size_t index
, T
* out_value
) {
45 const base::Value
* value
;
46 if (!args
->Get(index
, &value
))
48 return ParseValue(value
, out_value
);
51 base::FundamentalValue LOGIN_EXPORT
MakeValue(bool v
);
52 base::FundamentalValue LOGIN_EXPORT
MakeValue(int v
);
53 base::FundamentalValue LOGIN_EXPORT
MakeValue(double v
);
54 base::StringValue LOGIN_EXPORT
MakeValue(const std::string
& v
);
55 base::StringValue LOGIN_EXPORT
MakeValue(const base::string16
& v
);
58 inline const T
& MakeValue(const T
& v
) {
62 void LOGIN_EXPORT
CallbackWrapper0(base::Callback
<void()> callback
,
63 const base::ListValue
* args
);
65 template <typename A1
>
66 void CallbackWrapper1(base::Callback
<void(A1
)> callback
,
67 const base::ListValue
* args
) {
69 DCHECK_EQ(1u, args
->GetSize());
70 typename UnwrapConstRef
<A1
>::Type arg1
;
71 if (!GetArg(args
, 0, &arg1
)) {
78 template <typename A1
, typename A2
>
79 void CallbackWrapper2(base::Callback
<void(A1
, A2
)> callback
,
80 const base::ListValue
* args
) {
82 DCHECK_EQ(2u, args
->GetSize());
83 typename UnwrapConstRef
<A1
>::Type arg1
;
84 typename UnwrapConstRef
<A2
>::Type arg2
;
85 if (!GetArg(args
, 0, &arg1
) || !GetArg(args
, 1, &arg2
)) {
89 callback
.Run(arg1
, arg2
);
92 template <typename A1
, typename A2
, typename A3
>
93 void CallbackWrapper3(base::Callback
<void(A1
, A2
, A3
)> callback
,
94 const base::ListValue
* args
) {
96 DCHECK_EQ(3u, args
->GetSize());
97 typename UnwrapConstRef
<A1
>::Type arg1
;
98 typename UnwrapConstRef
<A2
>::Type arg2
;
99 typename UnwrapConstRef
<A3
>::Type arg3
;
100 if (!GetArg(args
, 0, &arg1
) || !GetArg(args
, 1, &arg2
) ||
101 !GetArg(args
, 2, &arg3
)) {
105 callback
.Run(arg1
, arg2
, arg3
);
108 template <typename A1
, typename A2
, typename A3
, typename A4
>
109 void CallbackWrapper4(base::Callback
<void(A1
, A2
, A3
, A4
)> callback
,
110 const base::ListValue
* args
) {
112 DCHECK_EQ(4u, args
->GetSize());
113 typename UnwrapConstRef
<A1
>::Type arg1
;
114 typename UnwrapConstRef
<A2
>::Type arg2
;
115 typename UnwrapConstRef
<A3
>::Type arg3
;
116 typename UnwrapConstRef
<A4
>::Type arg4
;
117 if (!GetArg(args
, 0, &arg1
) || !GetArg(args
, 1, &arg2
) ||
118 !GetArg(args
, 2, &arg3
) || !GetArg(args
, 3, &arg4
)) {
122 callback
.Run(arg1
, arg2
, arg3
, arg4
);
127 #endif // COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_