| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_ | 5 #ifndef COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_ |
| 6 #define COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_ | 6 #define COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_ |
| 7 | 7 |
| 8 #include <cstddef> | 8 #include <cstddef> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "base/tuple.h" |
| 14 #include "base/values.h" | 15 #include "base/values.h" |
| 15 #include "components/login/login_export.h" | 16 #include "components/login/login_export.h" |
| 16 | 17 |
| 17 namespace login { | 18 namespace login { |
| 18 | 19 |
| 19 typedef std::vector<std::string> StringList; | 20 typedef std::vector<std::string> StringList; |
| 20 typedef std::vector<base::string16> String16List; | 21 typedef std::vector<base::string16> String16List; |
| 21 | 22 |
| 22 template <typename T> | 23 template <typename T> |
| 23 struct LOGIN_EXPORT UnwrapConstRef { | 24 struct LOGIN_EXPORT UnwrapConstRef { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 52 base::FundamentalValue LOGIN_EXPORT MakeValue(int v); | 53 base::FundamentalValue LOGIN_EXPORT MakeValue(int v); |
| 53 base::FundamentalValue LOGIN_EXPORT MakeValue(double v); | 54 base::FundamentalValue LOGIN_EXPORT MakeValue(double v); |
| 54 base::StringValue LOGIN_EXPORT MakeValue(const std::string& v); | 55 base::StringValue LOGIN_EXPORT MakeValue(const std::string& v); |
| 55 base::StringValue LOGIN_EXPORT MakeValue(const base::string16& v); | 56 base::StringValue LOGIN_EXPORT MakeValue(const base::string16& v); |
| 56 | 57 |
| 57 template <typename T> | 58 template <typename T> |
| 58 inline const T& MakeValue(const T& v) { | 59 inline const T& MakeValue(const T& v) { |
| 59 return v; | 60 return v; |
| 60 } | 61 } |
| 61 | 62 |
| 62 void LOGIN_EXPORT CallbackWrapper0(base::Callback<void()> callback, | 63 template <typename Arg, size_t index> |
| 63 const base::ListValue* args); | 64 typename UnwrapConstRef<Arg>::Type ParseArg(const base::ListValue* args) { |
| 64 | 65 typename UnwrapConstRef<Arg>::Type parsed; |
| 65 template <typename A1> | 66 CHECK(GetArg(args, index, &parsed)); |
| 66 void CallbackWrapper1(base::Callback<void(A1)> callback, | 67 return parsed; |
| 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 } | 68 } |
| 77 | 69 |
| 78 template <typename A1, typename A2> | 70 template <typename... Args, size_t... Ns> |
| 79 void CallbackWrapper2(base::Callback<void(A1, A2)> callback, | 71 inline void DispatchToCallback(const base::Callback<void(Args...)>& callback, |
| 80 const base::ListValue* args) { | 72 const base::ListValue* args, |
| 73 IndexSequence<Ns...> indexes) { |
| 81 DCHECK(args); | 74 DCHECK(args); |
| 82 DCHECK_EQ(2u, args->GetSize()); | 75 DCHECK_EQ(sizeof...(Args), args->GetSize()); |
| 83 typename UnwrapConstRef<A1>::Type arg1; | 76 |
| 84 typename UnwrapConstRef<A2>::Type arg2; | 77 callback.Run(ParseArg<Args, Ns>(args)...); |
| 85 if (!GetArg(args, 0, &arg1) || !GetArg(args, 1, &arg2)) { | |
| 86 NOTREACHED(); | |
| 87 return; | |
| 88 } | |
| 89 callback.Run(arg1, arg2); | |
| 90 } | 78 } |
| 91 | 79 |
| 92 template <typename A1, typename A2, typename A3> | 80 template <typename... Args> |
| 93 void CallbackWrapper3(base::Callback<void(A1, A2, A3)> callback, | 81 void CallbackWrapper(const base::Callback<void(Args...)>& callback, |
| 94 const base::ListValue* args) { | 82 const base::ListValue* args) { |
| 95 DCHECK(args); | 83 DispatchToCallback(callback, args, MakeIndexSequence<sizeof...(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 } | 84 } |
| 107 | 85 |
| 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 template <typename A1, typename A2, typename A3, typename A4, typename A5> | |
| 126 void CallbackWrapper5(base::Callback<void(A1, A2, A3, A4, A5)> callback, | |
| 127 const base::ListValue* args) { | |
| 128 DCHECK(args); | |
| 129 DCHECK_EQ(5u, args->GetSize()); | |
| 130 typename UnwrapConstRef<A1>::Type arg1; | |
| 131 typename UnwrapConstRef<A2>::Type arg2; | |
| 132 typename UnwrapConstRef<A3>::Type arg3; | |
| 133 typename UnwrapConstRef<A4>::Type arg4; | |
| 134 typename UnwrapConstRef<A5>::Type arg5; | |
| 135 if (!GetArg(args, 0, &arg1) || !GetArg(args, 1, &arg2) || | |
| 136 !GetArg(args, 2, &arg3) || !GetArg(args, 3, &arg4) || | |
| 137 !GetArg(args, 4, &arg5)) { | |
| 138 NOTREACHED(); | |
| 139 return; | |
| 140 } | |
| 141 callback.Run(arg1, arg2, arg3, arg4, arg5); | |
| 142 } | |
| 143 | 86 |
| 144 } // namespace login | 87 } // namespace login |
| 145 | 88 |
| 146 #endif // COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_ | 89 #endif // COMPONENTS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_ |
| OLD | NEW |