Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 # This module's classes provide an interface to mojo modules. Modules are | 5 # This module's classes provide an interface to mojo modules. Modules are |
| 6 # collections of interfaces and structs to be used by mojo ipc clients and | 6 # collections of interfaces and structs to be used by mojo ipc clients and |
| 7 # servers. | 7 # servers. |
| 8 # | 8 # |
| 9 # A simple interface would be created this way: | 9 # A simple interface would be created this way: |
| 10 # module = mojom.generate.module.Module('Foo') | 10 # module = mojom.generate.module.Module('Foo') |
| 11 # interface = module.AddInterface('Bar') | 11 # interface = module.AddInterface('Bar') |
| 12 # method = interface.AddMethod('Tat', 0) | 12 # method = interface.AddMethod('Tat', 0) |
| 13 # method.AddParameter('baz', 0, mojom.INT32) | 13 # method.AddParameter('baz', 0, mojom.INT32) |
| 14 # | 14 # |
| 15 | 15 |
| 16 | |
| 16 class Kind(object): | 17 class Kind(object): |
| 17 def __init__(self, spec=None): | 18 def __init__(self, spec=None): |
| 18 self.spec = spec | 19 self.spec = spec |
| 19 self.parent_kind = None | 20 self.parent_kind = None |
| 20 | 21 |
| 22 | |
| 23 class ReferenceKind(Kind): | |
| 24 """ReferenceKind represents pointer types and handle types. | |
| 25 A type is nullable means that NULL (for pointer types) or invalid handle | |
| 26 (for handle types) is a legal value for the type. | |
| 27 """ | |
| 28 | |
| 29 def __init__(self, spec=None, is_nullable=False): | |
| 30 assert spec is None or is_nullable == spec.startswith('?') | |
| 31 Kind.__init__(self, spec) | |
| 32 self.is_nullable = is_nullable | |
| 33 | |
| 34 | |
| 21 # Initialize the set of primitive types. These can be accessed by clients. | 35 # Initialize the set of primitive types. These can be accessed by clients. |
| 22 BOOL = Kind('b') | 36 BOOL = Kind('b') |
| 23 INT8 = Kind('i8') | 37 INT8 = Kind('i8') |
| 24 INT16 = Kind('i16') | 38 INT16 = Kind('i16') |
| 25 INT32 = Kind('i32') | 39 INT32 = Kind('i32') |
| 26 INT64 = Kind('i64') | 40 INT64 = Kind('i64') |
| 27 UINT8 = Kind('u8') | 41 UINT8 = Kind('u8') |
| 28 UINT16 = Kind('u16') | 42 UINT16 = Kind('u16') |
| 29 UINT32 = Kind('u32') | 43 UINT32 = Kind('u32') |
| 30 UINT64 = Kind('u64') | 44 UINT64 = Kind('u64') |
| 31 FLOAT = Kind('f') | 45 FLOAT = Kind('f') |
| 32 DOUBLE = Kind('d') | 46 DOUBLE = Kind('d') |
| 33 STRING = Kind('s') | 47 STRING = ReferenceKind('s') |
| 34 HANDLE = Kind('h') | 48 HANDLE = ReferenceKind('h') |
| 35 DCPIPE = Kind('h:d:c') | 49 DCPIPE = ReferenceKind('h:d:c') |
| 36 DPPIPE = Kind('h:d:p') | 50 DPPIPE = ReferenceKind('h:d:p') |
| 37 MSGPIPE = Kind('h:m') | 51 MSGPIPE = ReferenceKind('h:m') |
| 38 SHAREDBUFFER = Kind('h:s') | 52 SHAREDBUFFER = ReferenceKind('h:s') |
| 53 NLBL_STRING = ReferenceKind('?s', True) | |
| 54 NLBL_HANDLE = ReferenceKind('?h', True) | |
| 55 NLBL_DCPIPE = ReferenceKind('?h:d:c', True) | |
| 56 NLBL_DPPIPE = ReferenceKind('?h:d:p', True) | |
| 57 NLBL_MSGPIPE = ReferenceKind('?h:m', True) | |
| 58 NLBL_SHAREDBUFFER = ReferenceKind('?h:s', True) | |
| 39 | 59 |
| 40 | 60 |
| 41 # Collection of all Primitive types | 61 # Collection of all Primitive types |
| 42 PRIMITIVES = ( | 62 PRIMITIVES = ( |
| 43 BOOL, | 63 BOOL, |
| 44 INT8, | 64 INT8, |
| 45 INT16, | 65 INT16, |
| 46 INT32, | 66 INT32, |
| 47 INT64, | 67 INT64, |
| 48 UINT8, | 68 UINT8, |
| 49 UINT16, | 69 UINT16, |
| 50 UINT32, | 70 UINT32, |
| 51 UINT64, | 71 UINT64, |
| 52 FLOAT, | 72 FLOAT, |
| 53 DOUBLE, | 73 DOUBLE, |
| 54 STRING, | 74 STRING, |
| 55 HANDLE, | 75 HANDLE, |
| 56 DCPIPE, | 76 DCPIPE, |
| 57 DPPIPE, | 77 DPPIPE, |
| 58 MSGPIPE, | 78 MSGPIPE, |
| 59 SHAREDBUFFER | 79 SHAREDBUFFER, |
| 80 NLBL_STRING, | |
| 81 NLBL_HANDLE, | |
| 82 NLBL_DCPIPE, | |
| 83 NLBL_DPPIPE, | |
| 84 NLBL_MSGPIPE, | |
| 85 NLBL_SHAREDBUFFER | |
| 60 ) | 86 ) |
| 61 | 87 |
| 62 | 88 |
| 63 class NamedValue(object): | 89 class NamedValue(object): |
| 64 def __init__(self, module, parent_kind, name): | 90 def __init__(self, module, parent_kind, name): |
| 65 self.module = module | 91 self.module = module |
| 66 self.namespace = module.namespace | 92 self.namespace = module.namespace |
| 67 self.parent_kind = parent_kind | 93 self.parent_kind = parent_kind |
| 68 self.name = name | 94 self.name = name |
| 69 self.imported_from = None | 95 self.imported_from = None |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 93 | 119 |
| 94 | 120 |
| 95 class Field(object): | 121 class Field(object): |
| 96 def __init__(self, name=None, kind=None, ordinal=None, default=None): | 122 def __init__(self, name=None, kind=None, ordinal=None, default=None): |
| 97 self.name = name | 123 self.name = name |
| 98 self.kind = kind | 124 self.kind = kind |
| 99 self.ordinal = ordinal | 125 self.ordinal = ordinal |
| 100 self.default = default | 126 self.default = default |
| 101 | 127 |
| 102 | 128 |
| 103 class Struct(Kind): | 129 class Struct(ReferenceKind): |
| 104 def __init__(self, name=None, module=None): | 130 def __init__(self, name=None, module=None): |
| 105 self.name = name | 131 self.name = name |
| 106 self.module = module | 132 self.module = module |
| 107 self.imported_from = None | 133 self.imported_from = None |
| 108 if name is not None: | 134 if name is not None: |
| 109 spec = 'x:' + name | 135 spec = 'x:' + name |
| 110 else: | 136 else: |
| 111 spec = None | 137 spec = None |
| 112 Kind.__init__(self, spec) | 138 ReferenceKind.__init__(self, spec) |
| 113 self.fields = [] | 139 self.fields = [] |
| 114 | 140 |
| 115 def AddField(self, name, kind, ordinal=None, default=None): | 141 def AddField(self, name, kind, ordinal=None, default=None): |
| 116 field = Field(name, kind, ordinal, default) | 142 field = Field(name, kind, ordinal, default) |
| 117 self.fields.append(field) | 143 self.fields.append(field) |
| 118 return field | 144 return field |
| 119 | 145 |
| 120 | 146 |
| 121 class Array(Kind): | 147 class Array(ReferenceKind): |
| 122 def __init__(self, kind=None): | 148 def __init__(self, kind=None): |
| 123 self.kind = kind | 149 self.kind = kind |
| 124 if kind is not None: | 150 if kind is not None: |
| 125 Kind.__init__(self, 'a:' + kind.spec) | 151 ReferenceKind.__init__(self, 'a:' + kind.spec) |
| 126 else: | 152 else: |
| 127 Kind.__init__(self) | 153 ReferenceKind.__init__(self) |
| 128 | 154 |
| 129 class FixedArray(Kind): | 155 |
| 130 def __init__(self, length, kind=None): | 156 class FixedArray(ReferenceKind): |
| 157 def __init__(self, length=-1, kind=None): | |
| 131 self.kind = kind | 158 self.kind = kind |
| 132 self.length = length | 159 self.length = length |
| 133 if kind is not None: | 160 if kind is not None: |
| 134 Kind.__init__(self, 'a' + length + ':' + kind.spec) | 161 ReferenceKind.__init__(self, 'a%d:%s' % (length, kind.spec)) |
| 135 else: | 162 else: |
| 136 Kind.__init__(self) | 163 ReferenceKind.__init__(self) |
| 137 | 164 |
| 138 class InterfaceRequest(Kind): | 165 |
| 166 class InterfaceRequest(ReferenceKind): | |
| 139 def __init__(self, kind=None): | 167 def __init__(self, kind=None): |
| 140 self.kind = kind | 168 self.kind = kind |
| 141 if kind is not None: | 169 if kind is not None: |
| 142 Kind.__init__(self, 'r:' + kind.spec) | 170 ReferenceKind.__init__(self, 'r:' + kind.spec) |
| 143 else: | 171 else: |
| 144 Kind.__init__(self) | 172 ReferenceKind.__init__(self) |
| 145 | 173 |
| 146 | 174 |
| 147 class Parameter(object): | 175 class Parameter(object): |
| 148 def __init__(self, name=None, kind=None, ordinal=None, default=None): | 176 def __init__(self, name=None, kind=None, ordinal=None, default=None): |
| 149 self.name = name | 177 self.name = name |
| 150 self.ordinal = ordinal | 178 self.ordinal = ordinal |
| 151 self.kind = kind | 179 self.kind = kind |
| 152 self.default = default | 180 self.default = default |
| 153 | 181 |
| 154 | 182 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 166 return parameter | 194 return parameter |
| 167 | 195 |
| 168 def AddResponseParameter(self, name, kind, ordinal=None, default=None): | 196 def AddResponseParameter(self, name, kind, ordinal=None, default=None): |
| 169 if self.response_parameters == None: | 197 if self.response_parameters == None: |
| 170 self.response_parameters = [] | 198 self.response_parameters = [] |
| 171 parameter = Parameter(name, kind, ordinal, default) | 199 parameter = Parameter(name, kind, ordinal, default) |
| 172 self.response_parameters.append(parameter) | 200 self.response_parameters.append(parameter) |
| 173 return parameter | 201 return parameter |
| 174 | 202 |
| 175 | 203 |
| 176 class Interface(Kind): | 204 class Interface(ReferenceKind): |
| 177 def __init__(self, name=None, client=None, module=None): | 205 def __init__(self, name=None, client=None, module=None): |
| 178 self.module = module | 206 self.module = module |
| 179 self.name = name | 207 self.name = name |
| 180 self.imported_from = None | 208 self.imported_from = None |
| 181 if name is not None: | 209 if name is not None: |
| 182 spec = 'x:' + name | 210 spec = 'x:' + name |
| 183 else: | 211 else: |
| 184 spec = None | 212 spec = None |
| 185 Kind.__init__(self, spec) | 213 ReferenceKind.__init__(self, spec) |
| 186 self.client = client | 214 self.client = client |
| 187 self.methods = [] | 215 self.methods = [] |
| 188 | 216 |
| 189 def AddMethod(self, name, ordinal=None): | 217 def AddMethod(self, name, ordinal=None): |
| 190 method = Method(self, name, ordinal=ordinal) | 218 method = Method(self, name, ordinal=ordinal) |
| 191 self.methods.append(method) | 219 self.methods.append(method) |
| 192 return method | 220 return method |
| 193 | 221 |
| 194 | 222 |
| 195 class EnumField(object): | 223 class EnumField(object): |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 221 | 249 |
| 222 def AddInterface(self, name): | 250 def AddInterface(self, name): |
| 223 interface=Interface(name, module=self); | 251 interface=Interface(name, module=self); |
| 224 self.interfaces.append(interface) | 252 self.interfaces.append(interface) |
| 225 return interface | 253 return interface |
| 226 | 254 |
| 227 def AddStruct(self, name): | 255 def AddStruct(self, name): |
| 228 struct=Struct(name, module=self) | 256 struct=Struct(name, module=self) |
| 229 self.structs.append(struct) | 257 self.structs.append(struct) |
| 230 return struct | 258 return struct |
| 259 | |
| 260 | |
| 261 def MakeNullableKind(ref_kind): | |
| 262 assert isinstance(ref_kind, ReferenceKind) | |
| 263 assert not ref_kind.is_nullable | |
| 264 | |
| 265 if ref_kind == STRING: | |
| 266 return NLBL_STRING | |
| 267 if ref_kind == HANDLE: | |
| 268 return NLBL_HANDLE | |
| 269 if ref_kind == DCPIPE: | |
| 270 return NLBL_DCPIPE | |
| 271 if ref_kind == DPPIPE: | |
| 272 return NLBL_DPPIPE | |
| 273 if ref_kind == MSGPIPE: | |
| 274 return NLBL_MSGPIPE | |
| 275 if ref_kind == SHAREDBUFFER: | |
| 276 return NLBL_SHAREDBUFFER | |
| 277 | |
| 278 nullable_kind = type(ref_kind)() | |
| 279 # Make a shallow copy of the instance's attributes. | |
| 280 nullable_kind.__dict__ = ref_kind.__dict__.copy() | |
|
yzshen1
2014/08/05 05:11:43
Hmm... Thinking more about this solution and it se
yzshen1
2014/08/06 21:39:27
I think I have addressed this issue in the latest
| |
| 281 if ref_kind.spec is not None: | |
| 282 nullable_kind.spec = '?' + ref_kind.spec | |
| 283 nullable_kind.is_nullable = True | |
| 284 | |
| 285 return nullable_kind | |
| OLD | NEW |