OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/common/automation_id.h" |
| 6 |
| 7 #include "base/stringprintf.h" |
| 8 #include "base/values.h" |
| 9 |
| 10 using base::DictionaryValue; |
| 11 using base::Value; |
| 12 |
| 13 // static |
| 14 bool AutomationId::FromValue( |
| 15 Value* value, AutomationId* id, std::string* error) { |
| 16 DictionaryValue* dict; |
| 17 if (!value->GetAsDictionary(&dict)) { |
| 18 *error = "automation ID must be a dictionary"; |
| 19 return false; |
| 20 } |
| 21 int type; |
| 22 if (!dict->GetInteger("type", &type)) { |
| 23 *error = "automation ID 'type' missing or invalid"; |
| 24 return false; |
| 25 } |
| 26 std::string type_id; |
| 27 if (!dict->GetString("id", &type_id)) { |
| 28 *error = "automation ID 'type_id' missing or invalid"; |
| 29 return false; |
| 30 } |
| 31 *id = AutomationId(static_cast<Type>(type), type_id); |
| 32 return true; |
| 33 } |
| 34 |
| 35 // static |
| 36 bool AutomationId::FromValueInDictionary( |
| 37 DictionaryValue* dict, |
| 38 const std::string& key, |
| 39 AutomationId* id, |
| 40 std::string* error) { |
| 41 Value* id_value; |
| 42 if (!dict->Get(key, &id_value)) { |
| 43 *error = base::StringPrintf("automation ID '%s' missing", key.c_str()); |
| 44 return false; |
| 45 } |
| 46 return FromValue(id_value, id, error); |
| 47 } |
| 48 |
| 49 AutomationId::AutomationId() : type_(kTypeInvalid) { } |
| 50 |
| 51 AutomationId::AutomationId(Type type, const std::string& id) |
| 52 : type_(type), id_(id) { } |
| 53 |
| 54 bool AutomationId::operator==(const AutomationId& id) const { |
| 55 return type_ == id.type_ && id_ == id.id_; |
| 56 } |
| 57 |
| 58 DictionaryValue* AutomationId::ToValue() const { |
| 59 DictionaryValue* dict = new DictionaryValue(); |
| 60 dict->SetInteger("type", type_); |
| 61 dict->SetString("id", id_); |
| 62 return dict; |
| 63 } |
| 64 |
| 65 bool AutomationId::is_valid() const { |
| 66 return type_ != kTypeInvalid; |
| 67 } |
| 68 |
| 69 AutomationId::Type AutomationId::type() const { |
| 70 return type_; |
| 71 } |
| 72 |
| 73 const std::string& AutomationId::id() const { |
| 74 return id_; |
| 75 } |
OLD | NEW |