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