Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / login / base_screen_handler_utils.h
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.
4
5 #ifndef COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_
6 #define COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_
7
8 #include <cstddef>
9 #include <string>
10
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"
16
17 namespace login {
18
19 typedef std::vector<std::string> StringList;
20 typedef std::vector<base::string16> String16List;
21
22 template <typename T>
23 struct LOGIN_EXPORT UnwrapConstRef {
24   typedef T Type;
25 };
26
27 template <typename T>
28 struct LOGIN_EXPORT UnwrapConstRef<const T&> {
29   typedef T Type;
30 };
31
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);
36 bool LOGIN_EXPORT
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);
42
43 template <typename T>
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))
47     return false;
48   return ParseValue(value, out_value);
49 }
50
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);
56
57 template <typename T>
58 inline const T& MakeValue(const T& v) {
59   return v;
60 }
61
62 void LOGIN_EXPORT CallbackWrapper0(base::Callback<void()> callback,
63                                    const base::ListValue* args);
64
65 template <typename A1>
66 void CallbackWrapper1(base::Callback<void(A1)> callback,
67                       const base::ListValue* args) {
68   DCHECK(args);
69   DCHECK_EQ(1u, args->GetSize());
70   typename UnwrapConstRef<A1>::Type arg1;
71   if (!GetArg(args, 0, &arg1)) {
72     NOTREACHED();
73     return;
74   }
75   callback.Run(arg1);
76 }
77
78 template <typename A1, typename A2>
79 void CallbackWrapper2(base::Callback<void(A1, A2)> callback,
80                       const base::ListValue* args) {
81   DCHECK(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)) {
86     NOTREACHED();
87     return;
88   }
89   callback.Run(arg1, arg2);
90 }
91
92 template <typename A1, typename A2, typename A3>
93 void CallbackWrapper3(base::Callback<void(A1, A2, A3)> callback,
94                       const base::ListValue* args) {
95   DCHECK(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)) {
102     NOTREACHED();
103     return;
104   }
105   callback.Run(arg1, arg2, arg3);
106 }
107
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) {
111   DCHECK(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)) {
119     NOTREACHED();
120     return;
121   }
122   callback.Run(arg1, arg2, arg3, arg4);
123 }
124
125 }  // namespace login
126
127 #endif  // COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_