| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:mojo/core.dart'; | 7 import 'package:mojo/core.dart'; |
| 8 import 'package:mojo_services/tracing/tracing.mojom.dart'; | 8 import 'package:mojo_services/tracing/tracing.mojom.dart'; |
| 9 | 9 |
| 10 enum TraceSendTiming { |
| 11 IMMEDIATE, |
| 12 // TODO: Add BATCHED? |
| 13 AT_END, |
| 14 } |
| 15 |
| 10 class TraceProviderImpl implements TraceProvider { | 16 class TraceProviderImpl implements TraceProvider { |
| 11 // Any messages sent before the tracing service connects to us will be | 17 // Any messages sent before the tracing service connects to us will be |
| 12 // recorded and kept until one second after construction of the trace | 18 // recorded and kept until one second after construction of the trace |
| 13 // provider. If the tracing service connects before that time, we will replay | 19 // provider. If the tracing service connects before that time, we will replay |
| 14 // the recorded trace events. | 20 // the recorded trace events. |
| 15 // | 21 // |
| 16 // This allows the client to record trace events early during initialization | 22 // This allows the client to record trace events early during initialization |
| 17 // of the app. | 23 // of the app. |
| 18 List<String> _message_queue; | 24 List<String> _message_queue; |
| 19 bool _enqueuing; | 25 bool _enqueuing; |
| 20 | 26 |
| 21 TraceProviderStub _stub; | 27 TraceProviderStub _stub; |
| 22 TraceRecorderProxy _recorder; | 28 TraceRecorderProxy _recorder; |
| 23 // TODO(rudominer) We currently ignore _categories. | 29 // TODO(rudominer) We currently ignore _categories. |
| 24 String _categories; | 30 String _categories; |
| 25 | 31 |
| 26 TraceProviderImpl() { | 32 TraceSendTiming _timing; |
| 33 |
| 34 TraceProviderImpl([TraceSendTiming timing = TraceSendTiming.IMMEDIATE]) { |
| 27 _message_queue = []; | 35 _message_queue = []; |
| 28 _enqueuing = true; | 36 _enqueuing = true; |
| 29 new Future(() { | 37 _timing = timing; |
| 30 new Future.delayed(const Duration(seconds: 1), () { | 38 new Future.delayed(const Duration(seconds: 1), () { |
| 31 if (_enqueuing) { | 39 if (_enqueuing) { |
| 32 _enqueuing = false; | 40 _enqueuing = false; |
| 33 _message_queue.clear(); | 41 _message_queue.clear(); |
| 34 } | 42 } |
| 35 }); | |
| 36 }); | 43 }); |
| 37 } | 44 } |
| 38 | 45 |
| 39 void connect(MojoMessagePipeEndpoint e) { | 46 void connect(MojoMessagePipeEndpoint e) { |
| 40 _stub = TraceProviderStub.newFromEndpoint(e); | 47 _stub = TraceProviderStub.newFromEndpoint(e); |
| 41 _stub.impl = this; | 48 _stub.impl = this; |
| 42 } | 49 } |
| 43 | 50 |
| 44 @override | 51 @override |
| 45 void startTracing(String categories, TraceRecorderProxy recorder) { | 52 void startTracing(String categories, TraceRecorderProxy recorder) { |
| 46 assert(_recorder == null); | 53 assert(_recorder == null); |
| 47 _recorder = recorder; | 54 _recorder = recorder; |
| 48 _categories = categories; | 55 _categories = categories; |
| 49 _enqueuing = false; | 56 _enqueuing = false; |
| 57 if (_timing == TraceSendTiming.IMMEDIATE) { |
| 58 for (String message in _message_queue) { |
| 59 _recorder.ptr.record(message); |
| 60 } |
| 61 _message_queue.clear(); |
| 62 } |
| 50 } | 63 } |
| 51 | 64 |
| 52 @override | 65 @override |
| 53 void stopTracing() { | 66 void stopTracing() { |
| 54 assert(_recorder != null); | 67 assert(_recorder != null); |
| 55 for (String message in _message_queue) { | 68 if (_timing == TraceSendTiming.AT_END) { |
| 56 _recorder.ptr.record(message); | 69 for (String message in _message_queue) { |
| 70 _recorder.ptr.record(message); |
| 71 } |
| 72 _message_queue.clear(); |
| 57 } | 73 } |
| 58 _recorder.close(); | 74 _recorder.close(); |
| 59 _recorder = null; | 75 _recorder = null; |
| 60 _message_queue.clear(); | |
| 61 } | 76 } |
| 62 | 77 |
| 63 bool isActive() { | 78 bool isActive() { |
| 64 return _enqueuing || _recorder != null; | 79 return _enqueuing || _recorder != null; |
| 65 } | 80 } |
| 66 | 81 |
| 67 void sendTraceMessage(String message) { | 82 void sendTraceMessage(String message) { |
| 68 if (isActive()) { | 83 switch (_timing) { |
| 69 _message_queue.add(message); | 84 case TraceSendTiming.IMMEDIATE: |
| 85 if (_recorder != null) { |
| 86 _recorder.ptr.record(message); |
| 87 } else if (_enqueuing) { |
| 88 _message_queue.add(message); |
| 89 } |
| 90 break; |
| 91 case TraceSendTiming.AT_END: |
| 92 if (isActive()) { |
| 93 _message_queue.add(message); |
| 94 } |
| 95 break; |
| 70 } | 96 } |
| 71 } | 97 } |
| 72 } | 98 } |
| OLD | NEW |