Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / base_screen_handler_utils.h
blobaf6925cbac4b0d7c0525aa3a1f0b0877a682920c
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_
8 #include <cstddef>
9 #include <string>
11 #include "base/callback.h"
12 #include "base/logging.h"
13 #include "base/strings/string16.h"
14 #include "base/values.h"
16 namespace chromeos {
18 template<typename T>
19 struct UnwrapConstRef {
20 typedef T Type;
23 template<typename T>
24 struct UnwrapConstRef<const T&> {
25 typedef T Type;
28 template<typename T>
29 inline bool ParseValue(const base::Value* value, T* out_value);
31 template<>
32 inline bool ParseValue<bool>(const base::Value* value, bool* out_value) {
33 return value->GetAsBoolean(out_value);
36 template<>
37 inline bool ParseValue<int>(const base::Value* value, int* out_value) {
38 return value->GetAsInteger(out_value);
41 template<>
42 inline bool ParseValue<double>(const base::Value* value, double* out_value) {
43 return value->GetAsDouble(out_value);
46 template<>
47 inline bool ParseValue<std::string>(const base::Value* value,
48 std::string* out_value) {
49 return value->GetAsString(out_value);
52 template<>
53 inline bool ParseValue<base::string16>(const base::Value* value,
54 base::string16* out_value) {
55 return value->GetAsString(out_value);
58 template<>
59 inline bool ParseValue<const base::DictionaryValue*>(
60 const base::Value* value,
61 const base::DictionaryValue** out_value) {
62 return value->GetAsDictionary(out_value);
65 template<typename T>
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))
69 return false;
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);
93 template<typename T>
94 inline const T& MakeValue(const T& v) {
95 return v;
98 inline void CallbackWrapper0(base::Callback<void()> callback,
99 const base::ListValue* args) {
100 DCHECK(args);
101 DCHECK(args->empty());
102 callback.Run();
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;
115 callback.Run(arg1);
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;
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) {
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;
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) {
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;
165 callback.Run(arg1, arg2, arg3, arg4);
168 } // namespace chromeos
170 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_