OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 import 'dart:async'; | |
6 | |
7 import 'package:mojo/core.dart'; | |
8 import 'package:mojo_services/tracing/tracing.mojom.dart'; | |
9 | |
10 class TraceProviderImpl implements TraceProvider { | |
11 // Any messages sent before the tracing service connects to us will be | |
12 // recorded and kept until one second after construction of the trace | |
13 // provider. If the tracing service connects before that time, we will replay | |
14 // the recorded trace events. | |
15 // | |
16 // This allows the client to record trace events early during initialization | |
17 // of the app. | |
18 List<String> _message_queue; | |
19 bool _enqueuing; | |
20 | |
21 TraceProviderStub _stub; | |
22 TraceRecorderProxy _recorder; | |
23 // TODO(rudominer) We currently ignore _categories. | |
24 String _categories; | |
25 | |
26 TraceProviderImpl() { | |
27 _message_queue = []; | |
28 _enqueuing = true; | |
29 new Future(() { | |
30 new Future.delayed(const Duration(seconds: 1), () { | |
31 if (_enqueuing) { | |
32 _enqueuing = false; | |
33 _message_queue.clear(); | |
34 } | |
35 }); | |
36 }); | |
37 } | |
38 | |
39 void connect(MojoMessagePipeEndpoint e) { | |
40 _stub = TraceProviderStub.newFromEndpoint(e); | |
41 _stub.impl = this; | |
42 } | |
43 | |
44 @override | |
45 void startTracing(String categories, TraceRecorderProxy recorder) { | |
46 assert(_recorder == null); | |
47 _recorder = recorder; | |
48 _categories = categories; | |
49 _enqueuing = false; | |
50 } | |
51 | |
52 @override | |
53 void stopTracing() { | |
54 assert(_recorder != null); | |
55 for (String message in _message_queue) { | |
56 _recorder.ptr.record(message); | |
57 } | |
58 _recorder.close(); | |
59 _recorder = null; | |
60 _message_queue.clear(); | |
61 } | |
62 | |
63 bool isActive() { | |
64 return _enqueuing || _recorder != null; | |
65 } | |
66 | |
67 void sendTraceMessage(String message) { | |
68 if (isActive()) { | |
69 _message_queue.add(message); | |
70 } | |
71 } | |
72 } | |
OLD | NEW |