summaryrefslogtreecommitdiffstats
path: root/mojo/public/bindings/pylib/generate/mojom.py
blob: 7cb7518349601579a34aa38fe8b5d47f357e4cbd (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Copyright 2013 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.

# mojom's classes provide an interface to mojo modules. Modules are collections
# of interfaces and structs to be used by mojo ipc clients and servers.
#
# A simple interface would be created this way:
# module = mojom.Module('Foo')
# interface = module.AddInterface('Bar')
# method = interface.AddMethod('Tat', 0)
# method.AddParameter('baz', 0, mojom.INT32)
#
import copy

class Kind(object):
  def __init__(self, spec = None):
    self.spec = spec


# Initialize the set of primitive types. These can be accessed by clients.
BOOL    = Kind('b')
INT8    = Kind('i8')
INT16   = Kind('i16')
INT32   = Kind('i32')
INT64   = Kind('i64')
UINT8   = Kind('u8')
UINT16  = Kind('u16')
UINT32  = Kind('u32')
UINT64  = Kind('u64')
FLOAT   = Kind('f')
DOUBLE  = Kind('d')
STRING  = Kind('s')
HANDLE  = Kind('h')
DCPIPE  = Kind('h:d:c')
DPPIPE  = Kind('h:d:p')
MSGPIPE = Kind('h:m')


# Collection of all Primitive types
PRIMITIVES = (
  BOOL,
  INT8,
  INT16,
  INT32,
  INT64,
  UINT8,
  UINT16,
  UINT32,
  UINT64,
  FLOAT,
  DOUBLE,
  STRING,
  HANDLE,
  DCPIPE,
  DPPIPE,
  MSGPIPE
)


class Field(object):
  def __init__(self, name = None, kind = None, ordinal = None, default = None):
    self.name = name
    self.kind = kind
    self.ordinal = ordinal
    self.default = default


class Struct(Kind):
  def __init__(self, name = None):
    self.name = name
    self.imported_from = None
    if name != None:
      spec = 'x:' + name
    else:
      spec = None
    Kind.__init__(self, spec)
    self.fields = []

  @classmethod
  def CreateFromImport(cls, kind, imported_from):
    """Used with 'import module' - clones the kind imported from the
    given module's namespace."""
    kind = copy.deepcopy(kind)
    kind.imported_from = imported_from
    return kind

  def AddField(self, name, kind, ordinal = None, default = None):
    field = Field(name, kind, ordinal, default)
    self.fields.append(field)
    return field

  def GetFullName(self, separator):
    """Returns the fully qualified type name, including namespace prefix."""
    if self.imported_from:
      return separator.join([self.imported_from["namespace"], self.name])
    return self.name

  def GetFullNameInternal(self, separator):
    """Returns the fully qualified type name for an internal data structure,
    including namespace prefix."""
    if self.imported_from:
      return separator.join(
          [self.imported_from["namespace"], "internal", self.name])
    return self.name


class Array(Kind):
  def __init__(self, kind = None):
    self.kind = kind
    if kind != None:
      Kind.__init__(self, 'a:' + kind.spec)
    else:
      Kind.__init__(self)


class Parameter(object):
  def __init__(self, name = None, kind = None, ordinal = None, default = None):
    self.name = name
    self.ordinal = ordinal
    self.kind = kind
    self.default = default


class Method(object):
  def __init__(self, name = None, ordinal = None):
    self.name = name
    self.ordinal = ordinal
    self.parameters = []

  def AddParameter(self, name, kind, ordinal = None, default = None):
    parameter = Parameter(name, kind, ordinal, default)
    self.parameters.append(parameter)
    return parameter


class Interface(Kind):
  def __init__(self, name = None, peer = None):
    self.name = name
    if name != None:
      spec = 'x:' + name
    else:
      spec = None
    Kind.__init__(self, spec)
    self.peer = peer
    self.methods = []

  def AddMethod(self, name, ordinal = None):
    method = Method(name, ordinal)
    self.methods.append(method)
    return method;


class EnumField(object):
  def __init__(self, name = None, value = None):
    self.name = name
    self.value = value


class Enum(object):
  def __init__(self, name = None):
    self.name = name
    self.fields = []


class Module(object):
  def __init__(self, name = None, namespace = None):
    self.name = name
    self.path = name
    self.namespace = namespace
    self.structs = []
    self.interfaces = []

  def AddInterface(self, name):
    interface = Interface(name);
    self.interfaces.append(interface)
    return interface;

  def AddStruct(self, name):
    struct = Struct(name)
    self.structs.append(struct)
    return struct;