summaryrefslogtreecommitdiffstats
path: root/chrome/common/automation_id.cc
blob: bd788eddbb3e40417b5046c922c6391c5095db0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/common/automation_id.h"

#include "base/stringprintf.h"
#include "base/values.h"

using base::DictionaryValue;
using base::Value;

// static
bool AutomationId::FromValue(
    Value* value, AutomationId* id, std::string* error) {
  DictionaryValue* dict;
  if (!value->GetAsDictionary(&dict)) {
    *error = "automation ID must be a dictionary";
    return false;
  }
  int type;
  if (!dict->GetInteger("type", &type)) {
    *error = "automation ID 'type' missing or invalid";
    return false;
  }
  std::string type_id;
  if (!dict->GetString("id", &type_id)) {
    *error = "automation ID 'type_id' missing or invalid";
    return false;
  }
  *id = AutomationId(static_cast<Type>(type), type_id);
  return true;
}

// static
bool AutomationId::FromValueInDictionary(
    DictionaryValue* dict,
    const std::string& key,
    AutomationId* id,
    std::string* error) {
  Value* id_value;
  if (!dict->Get(key, &id_value)) {
    *error = base::StringPrintf("automation ID '%s' missing", key.c_str());
    return false;
  }
  return FromValue(id_value, id, error);
}

AutomationId::AutomationId() : type_(kTypeInvalid) { }

AutomationId::AutomationId(Type type, const std::string& id)
    : type_(type), id_(id) { }

bool AutomationId::operator==(const AutomationId& id) const {
  return type_ == id.type_ && id_ == id.id_;
}

DictionaryValue* AutomationId::ToValue() const {
  DictionaryValue* dict = new DictionaryValue();
  dict->SetInteger("type", type_);
  dict->SetString("id", id_);
  return dict;
}

bool AutomationId::is_valid() const {
  return type_ != kTypeInvalid;
}

AutomationId::Type AutomationId::type() const {
  return type_;
}

const std::string& AutomationId::id() const {
  return id_;
}