OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "tools/json_schema_compiler/util.h" |
| 6 |
| 7 #include "base/values.h" |
| 8 |
| 9 namespace json_schema_compiler { |
| 10 namespace util { |
| 11 |
| 12 bool GetStrings( |
| 13 const base::DictionaryValue& from, |
| 14 const std::string& name, |
| 15 std::vector<std::string>* out) { |
| 16 base::ListValue* list = NULL; |
| 17 if (!from.GetListWithoutPathExpansion(name, &list)) |
| 18 return false; |
| 19 |
| 20 std::string string; |
| 21 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 22 if (!list->GetString(i, &string)) |
| 23 return false; |
| 24 out->push_back(string); |
| 25 } |
| 26 |
| 27 return true; |
| 28 } |
| 29 |
| 30 bool GetOptionalStrings( |
| 31 const base::DictionaryValue& from, |
| 32 const std::string& name, |
| 33 scoped_ptr<std::vector<std::string> >* out) { |
| 34 base::ListValue* list = NULL; |
| 35 { |
| 36 base::Value* maybe_list = NULL; |
| 37 if (!from.GetWithoutPathExpansion(name, &maybe_list)) |
| 38 return true; |
| 39 if (!maybe_list->IsType(base::Value::TYPE_LIST)) |
| 40 return false; |
| 41 list = static_cast<base::ListValue*>(maybe_list); |
| 42 } |
| 43 |
| 44 out->reset(new std::vector<std::string>()); |
| 45 std::string string; |
| 46 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 47 if (!list->GetString(i, &string)) { |
| 48 out->reset(); |
| 49 return false; |
| 50 } |
| 51 (*out)->push_back(string); |
| 52 } |
| 53 |
| 54 return true; |
| 55 } |
| 56 |
| 57 void SetStrings( |
| 58 const std::vector<std::string>& from, |
| 59 const std::string& name, |
| 60 base::DictionaryValue* out) { |
| 61 base::ListValue* list = new base::ListValue(); |
| 62 out->SetWithoutPathExpansion(name, list); |
| 63 for (std::vector<std::string>::const_iterator it = from.begin(); |
| 64 it != from.end(); ++it) { |
| 65 list->Append(base::Value::CreateStringValue(*it)); |
| 66 } |
| 67 } |
| 68 |
| 69 void SetOptionalStrings( |
| 70 const scoped_ptr<std::vector<std::string> >& from, |
| 71 const std::string& name, |
| 72 base::DictionaryValue* out) { |
| 73 if (!from.get()) |
| 74 return; |
| 75 |
| 76 SetStrings(*from, name, out); |
| 77 } |
| 78 |
| 79 } // namespace api_util |
| 80 } // namespace extensions |
OLD | NEW |