Home | History | Annotate | Download | only in login
      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 typedef std::vector<std::string> StringList;
     19 typedef std::vector<base::string16> String16List;
     20 
     21 template<typename T>
     22 struct UnwrapConstRef {
     23   typedef T Type;
     24 };
     25 
     26 template<typename T>
     27 struct UnwrapConstRef<const T&> {
     28   typedef T Type;
     29 };
     30 
     31 bool ParseValue(const base::Value* value, bool* out_value);
     32 bool ParseValue(const base::Value* value, int* out_value);
     33 bool ParseValue(const base::Value* value, double* out_value);
     34 bool ParseValue(const base::Value* value, std::string* out_value);
     35 bool ParseValue(const base::Value* value, base::string16* out_value);
     36 bool ParseValue(const base::Value* value,
     37                 const base::DictionaryValue** out_value);
     38 bool ParseValue(const base::Value* value, StringList* out_value);
     39 bool ParseValue(const base::Value* value, String16List* out_value);
     40 
     41 template<typename T>
     42 inline bool GetArg(const base::ListValue* args, size_t index, T* out_value) {
     43   const base::Value* value;
     44   if (!args->Get(index, &value))
     45     return false;
     46   return ParseValue(value, out_value);
     47 }
     48 
     49 base::FundamentalValue MakeValue(bool v);
     50 base::FundamentalValue MakeValue(int v);
     51 base::FundamentalValue MakeValue(double v);
     52 base::StringValue MakeValue(const std::string& v);
     53 base::StringValue MakeValue(const base::string16& v);
     54 
     55 template<typename T>
     56 inline const T& MakeValue(const T& v) {
     57   return v;
     58 }
     59 
     60 void CallbackWrapper0(base::Callback<void()> callback,
     61                       const base::ListValue* args);
     62 
     63 template<typename A1>
     64 void CallbackWrapper1(base::Callback<void(A1)> callback,
     65                       const base::ListValue* args) {
     66   DCHECK(args);
     67   DCHECK_EQ(1u, args->GetSize());
     68   typename UnwrapConstRef<A1>::Type arg1;
     69   if (!GetArg(args, 0, &arg1)) {
     70     NOTREACHED();
     71     return;
     72   }
     73   callback.Run(arg1);
     74 }
     75 
     76 template<typename A1, typename A2>
     77 void CallbackWrapper2(base::Callback<void(A1, A2)> callback,
     78                       const base::ListValue* args) {
     79   DCHECK(args);
     80   DCHECK_EQ(2u, args->GetSize());
     81   typename UnwrapConstRef<A1>::Type arg1;
     82   typename UnwrapConstRef<A2>::Type arg2;
     83   if (!GetArg(args, 0, &arg1) || !GetArg(args, 1, &arg2)) {
     84     NOTREACHED();
     85     return;
     86   }
     87   callback.Run(arg1, arg2);
     88 }
     89 
     90 template<typename A1, typename A2, typename A3>
     91 void CallbackWrapper3(base::Callback<void(A1, A2, A3)> callback,
     92                       const base::ListValue* args) {
     93   DCHECK(args);
     94   DCHECK_EQ(3u, args->GetSize());
     95   typename UnwrapConstRef<A1>::Type arg1;
     96   typename UnwrapConstRef<A2>::Type arg2;
     97   typename UnwrapConstRef<A3>::Type arg3;
     98   if (!GetArg(args, 0, &arg1) ||
     99       !GetArg(args, 1, &arg2) ||
    100       !GetArg(args, 2, &arg3)) {
    101     NOTREACHED();
    102     return;
    103   }
    104   callback.Run(arg1, arg2, arg3);
    105 }
    106 
    107 template<typename A1, typename A2, typename A3, typename A4>
    108 void CallbackWrapper4(base::Callback<void(A1, A2, A3, A4)> callback,
    109                       const base::ListValue* args) {
    110   DCHECK(args);
    111   DCHECK_EQ(4u, args->GetSize());
    112   typename UnwrapConstRef<A1>::Type arg1;
    113   typename UnwrapConstRef<A2>::Type arg2;
    114   typename UnwrapConstRef<A3>::Type arg3;
    115   typename UnwrapConstRef<A4>::Type arg4;
    116   if (!GetArg(args, 0, &arg1) ||
    117       !GetArg(args, 1, &arg2) ||
    118       !GetArg(args, 2, &arg3) ||
    119       !GetArg(args, 3, &arg4)) {
    120     NOTREACHED();
    121     return;
    122   }
    123   callback.Run(arg1, arg2, arg3, arg4);
    124 }
    125 
    126 }  // namespace chromeos
    127 
    128 #endif  // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_
    129