|
| 1 | +// Copyright 2022 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 | +import 'package:meta/meta.dart'; |
| 6 | + |
| 7 | +import '../model.dart'; |
| 8 | +import 'messages.dart'; |
| 9 | + |
| 10 | +/// Generic parameter is not used for encoder, because the message type cannot be detected in runtime. |
| 11 | +typedef AppMessageEncoder = Map<String, dynamic> Function(dynamic message); |
| 12 | + |
| 13 | +typedef AppMessageDecoder<T> = T Function(Map<String, dynamic> message); |
| 14 | + |
| 15 | +enum Channel { |
| 16 | + requestToApp, |
| 17 | + eventFromApp, |
| 18 | + responseFromApp, |
| 19 | +} |
| 20 | + |
| 21 | +/// Codes to identify event types in interaction between appliocation and DevTools. |
| 22 | +/// |
| 23 | +/// When application starts real tracking, it sends [started]. As soon as it |
| 24 | +/// catch new leaks, it sends [summary] information about collected leaks. |
| 25 | +/// |
| 26 | +/// When user wants to get more about the collected leaks, they request details in |
| 27 | +/// DevTools, devtools sends [detailsRequest] to the app, and the app responds with |
| 28 | +/// [leakDetails]. |
| 29 | +@visibleForTesting |
| 30 | +enum Codes { |
| 31 | + // Events from app. |
| 32 | + started, |
| 33 | + summary, |
| 34 | + |
| 35 | + // Requests to app. |
| 36 | + detailsRequest, |
| 37 | + |
| 38 | + // Successfull responses from app. |
| 39 | + leakDetails, |
| 40 | + |
| 41 | + // Error responses from app. |
| 42 | + leakTrackingTurnedOffError, |
| 43 | + unexpectedError, |
| 44 | + unexpectedRequestTypeError, |
| 45 | + ; |
| 46 | + |
| 47 | + static Codes byName(String name) => |
| 48 | + Codes.values.where((e) => e.name == name).single; |
| 49 | +} |
| 50 | + |
| 51 | +class _JsonFields { |
| 52 | + static const envelopeCode = 'code'; |
| 53 | + static const content = 'content'; |
| 54 | +} |
| 55 | + |
| 56 | +/// Serializes an object so that it's type can be reconstructed. |
| 57 | +Map<String, dynamic> sealEnvelope(Object message, Channel channel) { |
| 58 | + final theEnvelope = envelopeByType(message.runtimeType); |
| 59 | + assert(theEnvelope.channel == channel); |
| 60 | + return { |
| 61 | + _JsonFields.envelopeCode: theEnvelope.code.name, |
| 62 | + _JsonFields.content: theEnvelope.encode(message), |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +/// Deserialize [message] into an opbejct of a right type. |
| 67 | +Object openEnvelope( |
| 68 | + Map<String, dynamic> message, |
| 69 | + Channel channel, |
| 70 | +) { |
| 71 | + final envelope = envelopeByCode(message[_JsonFields.envelopeCode] as String); |
| 72 | + assert(envelope.channel == channel); |
| 73 | + return envelope.decode(message[_JsonFields.content]); |
| 74 | +} |
| 75 | + |
| 76 | +/// Information necessary to serialize and deserialize an instance of type [T], |
| 77 | +/// so that the message type can be auto-detected. |
| 78 | +class _Envelope<T> { |
| 79 | + const _Envelope(this.code, this.channel, this.decode, this.encode); |
| 80 | + |
| 81 | + /// Serialization code, that corresponts to [T]. |
| 82 | + final Codes code; |
| 83 | + |
| 84 | + /// Communication channel, that should be used for messages of type [T]. |
| 85 | + final Channel channel; |
| 86 | + |
| 87 | + /// Decoder for the message. |
| 88 | + final AppMessageDecoder<T> decode; |
| 89 | + |
| 90 | + /// Encoder for the message. |
| 91 | + final AppMessageEncoder encode; |
| 92 | + |
| 93 | + Type get type => T; |
| 94 | +} |
| 95 | + |
| 96 | +/// Envelopes should be unique by message type. |
| 97 | +@visibleForTesting |
| 98 | +final envelopes = [ |
| 99 | + // Events from app. |
| 100 | + |
| 101 | + _Envelope<LeakTrackingStarted>( |
| 102 | + Codes.started, |
| 103 | + Channel.eventFromApp, |
| 104 | + (Map<String, dynamic> json) => LeakTrackingStarted.fromJson(json), |
| 105 | + (message) => (message as LeakTrackingStarted).toJson(), |
| 106 | + ), |
| 107 | + _Envelope<LeakSummary>( |
| 108 | + Codes.summary, |
| 109 | + Channel.eventFromApp, |
| 110 | + (Map<String, dynamic> json) => LeakSummary.fromJson(json), |
| 111 | + (message) => (message as LeakSummary).toJson(), |
| 112 | + ), |
| 113 | + |
| 114 | + // Requests to app. |
| 115 | + |
| 116 | + _Envelope<RequestForLeakDetails>( |
| 117 | + Codes.detailsRequest, |
| 118 | + Channel.requestToApp, |
| 119 | + (Map<String, dynamic> json) => RequestForLeakDetails(), |
| 120 | + (message) => {}, |
| 121 | + ), |
| 122 | + |
| 123 | + // Responses from app. |
| 124 | + |
| 125 | + _Envelope<Leaks>( |
| 126 | + Codes.leakDetails, |
| 127 | + Channel.responseFromApp, |
| 128 | + (Map<String, dynamic> json) => Leaks.fromJson(json), |
| 129 | + (message) => (message as Leaks).toJson(), |
| 130 | + ), |
| 131 | + |
| 132 | + _Envelope<LeakTrackingTurnedOffError>( |
| 133 | + Codes.leakTrackingTurnedOffError, |
| 134 | + Channel.responseFromApp, |
| 135 | + (Map<String, dynamic> json) => LeakTrackingTurnedOffError(), |
| 136 | + (message) => {}, |
| 137 | + ), |
| 138 | + |
| 139 | + _Envelope<UnexpectedRequestTypeError>( |
| 140 | + Codes.unexpectedRequestTypeError, |
| 141 | + Channel.responseFromApp, |
| 142 | + (Map<String, dynamic> json) => UnexpectedRequestTypeError.fromJson(json), |
| 143 | + (message) => (message as UnexpectedRequestTypeError).toJson(), |
| 144 | + ), |
| 145 | + |
| 146 | + _Envelope<UnexpectedError>( |
| 147 | + Codes.unexpectedError, |
| 148 | + Channel.responseFromApp, |
| 149 | + (Map<String, dynamic> json) => UnexpectedError.fromJson(json), |
| 150 | + (message) => (message as UnexpectedError).toJson(), |
| 151 | + ), |
| 152 | +]; |
| 153 | + |
| 154 | +_Envelope<T> envelopeByCode<T>(String codeString) { |
| 155 | + return _envelopesByCode[codeString]! as _Envelope<T>; |
| 156 | +} |
| 157 | + |
| 158 | +_Envelope envelopeByType(Type type) => _envelopesByType[type]!; |
| 159 | + |
| 160 | +late final _envelopesByCode = Map<String, _Envelope>.fromIterable( |
| 161 | + envelopes, |
| 162 | + key: (e) => (e as _Envelope).code.name, |
| 163 | + value: (e) => e, |
| 164 | +); |
| 165 | + |
| 166 | +late final _envelopesByType = Map<Type, _Envelope>.fromIterable( |
| 167 | + envelopes, |
| 168 | + key: (e) => e.type, |
| 169 | + value: (e) => e, |
| 170 | +); |
0 commit comments