1 // Copyright 2018 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 #include "components/mirroring/service/value_util.h"
6
7 namespace mirroring {
8
GetInt(const base::Value & value,const std::string & key,int32_t * result)9 bool GetInt(const base::Value& value, const std::string& key, int32_t* result) {
10 auto* found = value.FindKey(key);
11 if (!found || found->is_none())
12 return true;
13 if (found->is_int()) {
14 *result = found->GetInt();
15 return true;
16 }
17 return false;
18 }
19
GetDouble(const base::Value & value,const std::string & key,double * result)20 bool GetDouble(const base::Value& value,
21 const std::string& key,
22 double* result) {
23 auto* found = value.FindKey(key);
24 if (!found || found->is_none())
25 return true;
26 if (found->is_double()) {
27 *result = found->GetDouble();
28 return true;
29 }
30 if (found->is_int()) {
31 *result = found->GetInt();
32 return true;
33 }
34 return false;
35 }
36
GetString(const base::Value & value,const std::string & key,std::string * result)37 bool GetString(const base::Value& value,
38 const std::string& key,
39 std::string* result) {
40 auto* found = value.FindKey(key);
41 if (!found || found->is_none())
42 return true;
43 if (found->is_string()) {
44 *result = found->GetString();
45 return true;
46 }
47 return false;
48 }
49
GetBool(const base::Value & value,const std::string & key,bool * result)50 bool GetBool(const base::Value& value, const std::string& key, bool* result) {
51 auto* found = value.FindKey(key);
52 if (!found || found->is_none())
53 return true;
54 if (found->is_bool()) {
55 *result = found->GetBool();
56 return true;
57 }
58 return false;
59 }
60
GetIntArray(const base::Value & value,const std::string & key,std::vector<int32_t> * result)61 bool GetIntArray(const base::Value& value,
62 const std::string& key,
63 std::vector<int32_t>* result) {
64 auto* found = value.FindKey(key);
65 if (!found || found->is_none())
66 return true;
67 if (!found->is_list())
68 return false;
69 for (const auto& number_value : found->GetList()) {
70 if (number_value.is_int())
71 result->emplace_back(number_value.GetInt());
72 else
73 return false;
74 }
75 return true;
76 }
77
GetStringArray(const base::Value & value,const std::string & key,std::vector<std::string> * result)78 bool GetStringArray(const base::Value& value,
79 const std::string& key,
80 std::vector<std::string>* result) {
81 auto* found = value.FindKey(key);
82 if (!found || found->is_none())
83 return true;
84 if (!found->is_list())
85 return false;
86 for (const auto& string_value : found->GetList()) {
87 if (string_value.is_string())
88 result->emplace_back(string_value.GetString());
89 else
90 return false;
91 }
92 return true;
93 }
94
95 } // namespace mirroring
96