summaryrefslogtreecommitdiffstats
path: root/chrome/common/automation_id.h
blob: b05f060fa98b9c09012d6d79482f97ff558bb7d2 (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
// 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.

#ifndef CHROME_COMMON_AUTOMATION_ID_H_
#define CHROME_COMMON_AUTOMATION_ID_H_

#include <string>

namespace base {
class DictionaryValue;
class Value;
}

// A unique ID that JSON automation clients can use to refer to browser
// entities. The ID has a type so that:
// 1) supplying an ID of the wrong type can be detected.
// 2) the client does not have to explicitly supply the type in case multiple
//    ID types can be accepted (e.g., can use a tab ID or extension popup ID for
//    executing javascript).
class AutomationId {
 public:
  // The value of each entry should be preserved.
  enum Type {
    kTypeInvalid = 0,
    kTypeTab,
    kTypeExtensionPopup,
    kTypeExtensionBgPage,
    kTypeExtensionInfobar,
    kTypeExtension,
    kTypeAppShell,
  };

  static bool FromValue(
      base::Value* value, AutomationId* id, std::string* error);
  static bool FromValueInDictionary(
      base::DictionaryValue* dict, const std::string& key, AutomationId* id,
      std::string* error);

  // Constructs an invalid ID.
  AutomationId();

  // Constructs an ID from the given type and type-specific ID.
  AutomationId(Type type, const std::string& id);

  bool operator==(const AutomationId& id) const;

  // Returns a new dictionary equivalent to this ID.
  base::DictionaryValue* ToValue() const;

  // Returns whether the automation ID is valid.
  bool is_valid() const;

  Type type() const;
  const std::string& id() const;

 private:
  Type type_;
  std::string id_;
};

#endif  // CHROME_COMMON_AUTOMATION_ID_H_