Upload upstream chromium 71.0.3578.0
[platform/framework/web/chromium-efl.git] / 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 <stddef.h>
9
10 #include <cstddef>
11 #include <string>
12 #include <utility>
13
14 #include "base/callback.h"
15 #include "base/logging.h"
16 #include "base/strings/string16.h"
17 #include "base/values.h"
18 #include "components/account_id/account_id.h"
19 #include "components/login/login_export.h"
20
21 namespace login {
22
23 typedef std::vector<std::string> StringList;
24 typedef std::vector<base::string16> String16List;
25
26 template <typename T>
27 struct LOGIN_EXPORT UnwrapConstRef {
28   typedef T Type;
29 };
30
31 template <typename T>
32 struct UnwrapConstRef<const T&> {
33   typedef T Type;
34 };
35
36 bool LOGIN_EXPORT ParseValue(const base::Value* value, bool* out_value);
37 bool LOGIN_EXPORT ParseValue(const base::Value* value, int* out_value);
38 bool LOGIN_EXPORT ParseValue(const base::Value* value, double* out_value);
39 bool LOGIN_EXPORT ParseValue(const base::Value* value, std::string* out_value);
40 bool LOGIN_EXPORT
41     ParseValue(const base::Value* value, base::string16* out_value);
42 bool LOGIN_EXPORT ParseValue(const base::Value* value,
43                              const base::DictionaryValue** out_value);
44 bool LOGIN_EXPORT ParseValue(const base::Value* value, StringList* out_value);
45 bool LOGIN_EXPORT ParseValue(const base::Value* value, String16List* out_value);
46 bool LOGIN_EXPORT ParseValue(const base::Value* value, AccountId* out_value);
47
48 template <typename T>
49 inline bool GetArg(const base::ListValue* args, size_t index, T* out_value) {
50   const base::Value* value;
51   if (!args->Get(index, &value))
52     return false;
53   return ParseValue(value, out_value);
54 }
55
56 base::Value LOGIN_EXPORT MakeValue(bool v);
57 base::Value LOGIN_EXPORT MakeValue(int v);
58 base::Value LOGIN_EXPORT MakeValue(double v);
59 base::Value LOGIN_EXPORT MakeValue(const std::string& v);
60 base::Value LOGIN_EXPORT MakeValue(const base::string16& v);
61 base::Value LOGIN_EXPORT MakeValue(const AccountId& v);
62
63 template <typename T>
64 inline const T& MakeValue(const T& v) {
65   return v;
66 }
67
68 template <typename T>
69 struct ParsedValueContainer {
70   T value;
71 };
72
73 template <>
74 struct LOGIN_EXPORT ParsedValueContainer<AccountId> {
75   ParsedValueContainer();
76   AccountId value = EmptyAccountId();
77 };
78
79 template <typename Arg, size_t index>
80 typename UnwrapConstRef<Arg>::Type ParseArg(const base::ListValue* args) {
81   ParsedValueContainer<typename UnwrapConstRef<Arg>::Type> parsed;
82   CHECK(GetArg(args, index, &parsed.value));
83   return parsed.value;
84 }
85
86 template <typename... Args, size_t... Ns>
87 inline void DispatchToCallback(const base::Callback<void(Args...)>& callback,
88                                const base::ListValue* args,
89                                std::index_sequence<Ns...> indexes) {
90   DCHECK(args);
91   DCHECK_EQ(sizeof...(Args), args->GetSize());
92
93   callback.Run(ParseArg<Args, Ns>(args)...);
94 }
95
96 template <typename... Args>
97 void CallbackWrapper(const base::Callback<void(Args...)>& callback,
98                      const base::ListValue* args) {
99   DispatchToCallback(callback, args, std::index_sequence_for<Args...>());
100 }
101
102
103 }  // namespace login
104
105 #endif  // COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_