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/tuple.h"
15 #include "base/values.h"
16 #include "components/login/login_export.h"
20 typedef std::vector
<std::string
> StringList
;
21 typedef std::vector
<base::string16
> String16List
;
24 struct LOGIN_EXPORT UnwrapConstRef
{
29 struct UnwrapConstRef
<const T
&> {
33 bool LOGIN_EXPORT
ParseValue(const base::Value
* value
, bool* out_value
);
34 bool LOGIN_EXPORT
ParseValue(const base::Value
* value
, int* out_value
);
35 bool LOGIN_EXPORT
ParseValue(const base::Value
* value
, double* out_value
);
36 bool LOGIN_EXPORT
ParseValue(const base::Value
* value
, std::string
* out_value
);
38 ParseValue(const base::Value
* value
, base::string16
* out_value
);
39 bool LOGIN_EXPORT
ParseValue(const base::Value
* value
,
40 const base::DictionaryValue
** out_value
);
41 bool LOGIN_EXPORT
ParseValue(const base::Value
* value
, StringList
* out_value
);
42 bool LOGIN_EXPORT
ParseValue(const base::Value
* value
, String16List
* out_value
);
45 inline bool GetArg(const base::ListValue
* args
, size_t index
, T
* out_value
) {
46 const base::Value
* value
;
47 if (!args
->Get(index
, &value
))
49 return ParseValue(value
, out_value
);
52 base::FundamentalValue LOGIN_EXPORT
MakeValue(bool v
);
53 base::FundamentalValue LOGIN_EXPORT
MakeValue(int v
);
54 base::FundamentalValue LOGIN_EXPORT
MakeValue(double v
);
55 base::StringValue LOGIN_EXPORT
MakeValue(const std::string
& v
);
56 base::StringValue LOGIN_EXPORT
MakeValue(const base::string16
& v
);
59 inline const T
& MakeValue(const T
& v
) {
63 template <typename Arg
, size_t index
>
64 typename UnwrapConstRef
<Arg
>::Type
ParseArg(const base::ListValue
* args
) {
65 typename UnwrapConstRef
<Arg
>::Type parsed
;
66 CHECK(GetArg(args
, index
, &parsed
));
70 template <typename
... Args
, size_t... Ns
>
71 inline void DispatchToCallback(const base::Callback
<void(Args
...)>& callback
,
72 const base::ListValue
* args
,
73 base::IndexSequence
<Ns
...> indexes
) {
75 DCHECK_EQ(sizeof...(Args
), args
->GetSize());
77 callback
.Run(ParseArg
<Args
, Ns
>(args
)...);
80 template <typename
... Args
>
81 void CallbackWrapper(const base::Callback
<void(Args
...)>& callback
,
82 const base::ListValue
* args
) {
83 DispatchToCallback(callback
, args
,
84 base::MakeIndexSequence
<sizeof...(Args
)>());
90 #endif // COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_