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