- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / chromeos / 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 CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_
6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_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
16 namespace chromeos {
17
18 template<typename T>
19 struct UnwrapConstRef {
20   typedef T Type;
21 };
22
23 template<typename T>
24 struct UnwrapConstRef<const T&> {
25   typedef T Type;
26 };
27
28 template<typename T>
29 inline bool ParseValue(const Value* value, T* out_value);
30
31 template<>
32 inline bool ParseValue<bool>(const Value* value, bool* out_value) {
33   return value->GetAsBoolean(out_value);
34 }
35
36 template<>
37 inline bool ParseValue<int>(const Value* value, int* out_value) {
38   return value->GetAsInteger(out_value);
39 }
40
41 template<>
42 inline bool ParseValue<double>(const Value* value, double* out_value) {
43   return value->GetAsDouble(out_value);
44 }
45
46 template<>
47 inline bool ParseValue<std::string>(const Value* value,
48                                     std::string* out_value) {
49   return value->GetAsString(out_value);
50 }
51
52 template<>
53 inline bool ParseValue<base::string16>(const Value* value,
54                                        base::string16* out_value) {
55   return value->GetAsString(out_value);
56 }
57
58 template<>
59 inline bool ParseValue<const base::DictionaryValue*>(
60     const Value* value,
61     const base::DictionaryValue** out_value) {
62   return value->GetAsDictionary(out_value);
63 }
64
65 template<typename T>
66 inline bool GetArg(const base::ListValue* args, size_t index, T* out_value) {
67   const Value* value;
68   if (!args->Get(index, &value))
69     return false;
70   return ParseValue(value, out_value);
71 }
72
73 inline base::FundamentalValue MakeValue(bool v) {
74   return base::FundamentalValue(v);
75 }
76
77 inline base::FundamentalValue MakeValue(int v) {
78   return base::FundamentalValue(v);
79 }
80
81 inline base::FundamentalValue MakeValue(double v) {
82   return base::FundamentalValue(v);
83 }
84
85 inline base::StringValue MakeValue(const std::string& v) {
86   return base::StringValue(v);
87 }
88
89 inline base::StringValue MakeValue(const string16& v) {
90   return base::StringValue(v);
91 }
92
93 template<typename T>
94 inline const T& MakeValue(const T& v) {
95   return v;
96 }
97
98 inline void CallbackWrapper0(base::Callback<void()> callback,
99                              const base::ListValue* args) {
100   DCHECK(args);
101   DCHECK(args->empty());
102   callback.Run();
103 }
104
105 template<typename A1>
106 void CallbackWrapper1(base::Callback<void(A1)> callback,
107                       const base::ListValue* args) {
108   DCHECK(args);
109   DCHECK_EQ(1u, args->GetSize());
110   typename UnwrapConstRef<A1>::Type arg1;
111   if (!GetArg(args, 0, &arg1)) {
112     NOTREACHED();
113     return;
114   }
115   callback.Run(arg1);
116 }
117
118 template<typename A1, typename A2>
119 void CallbackWrapper2(base::Callback<void(A1, A2)> callback,
120                       const base::ListValue* args) {
121   DCHECK(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)) {
126     NOTREACHED();
127     return;
128   }
129   callback.Run(arg1, arg2);
130 }
131
132 template<typename A1, typename A2, typename A3>
133 void CallbackWrapper3(base::Callback<void(A1, A2, A3)> callback,
134                       const base::ListValue* args) {
135   DCHECK(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)) {
143     NOTREACHED();
144     return;
145   }
146   callback.Run(arg1, arg2, arg3);
147 }
148
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) {
152   DCHECK(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)) {
162     NOTREACHED();
163     return;
164   }
165   callback.Run(arg1, arg2, arg3, arg4);
166 }
167
168 }  // namespace chromeos
169
170 #endif  // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_