blob: 05c0304e042c04c4b34cd50c36595b4d49437681 [file] [log] [blame]
Tim Volodinec8095422017-11-10 21:07:36 +00001/*
2 * Copyright 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.webkit;
18
Tim Volodine88604f22018-01-18 20:14:08 +000019import android.annotation.CallbackExecutor;
Tim Volodinec8095422017-11-10 21:07:36 +000020import android.annotation.NonNull;
21import android.annotation.Nullable;
Tim Volodine88604f22018-01-18 20:14:08 +000022
23import java.io.OutputStream;
24import java.util.concurrent.Executor;
Tim Volodinec8095422017-11-10 21:07:36 +000025
26/**
27 * Manages tracing of WebViews. In particular provides functionality for the app
28 * to enable/disable tracing of parts of code and to collect tracing data.
29 * This is useful for profiling performance issues, debugging and memory usage
30 * analysis in production and real life scenarios.
31 * <p>
32 * The resulting trace data is sent back as a byte sequence in json format. This
33 * file can be loaded in "chrome://tracing" for further analysis.
34 * <p>
Tim Volodinec8095422017-11-10 21:07:36 +000035 * Example usage:
36 * <pre class="prettyprint">
37 * TracingController tracingController = TracingController.getInstance();
Tim Volodine90980b42018-04-04 17:49:28 +010038 * tracingController.start(new TracingConfig.Builder()
Tim Volodine88604f22018-01-18 20:14:08 +000039 * .addCategories(CATEGORIES_WEB_DEVELOPER).build());
Tim Volodine90980b42018-04-04 17:49:28 +010040 * ...
Tim Volodine88604f22018-01-18 20:14:08 +000041 * tracingController.stop(new FileOutputStream("trace.json"),
42 * Executors.newSingleThreadExecutor());
Tim Volodinec8095422017-11-10 21:07:36 +000043 * </pre></p>
44 */
45public abstract class TracingController {
46
47 /**
Tim Volodinec8095422017-11-10 21:07:36 +000048 * Returns the default TracingController instance. At present there is
49 * only one TracingController instance for all WebView instances,
Tim Volodine88604f22018-01-18 20:14:08 +000050 * however this restriction may be relaxed in a future Android release.
Tim Volodinec8095422017-11-10 21:07:36 +000051 *
Tim Volodine90980b42018-04-04 17:49:28 +010052 * @return The default TracingController instance.
Tim Volodinec8095422017-11-10 21:07:36 +000053 */
54 @NonNull
55 public static TracingController getInstance() {
56 return WebViewFactory.getProvider().getTracingController();
57 }
58
59 /**
Tim Volodine88604f22018-01-18 20:14:08 +000060 * Starts tracing all webviews. Depending on the trace mode in traceConfig
Tim Volodinec8095422017-11-10 21:07:36 +000061 * specifies how the trace events are recorded.
62 *
Tim Volodine1a07ccc2018-03-19 20:07:23 +000063 * For tracing modes {@link TracingConfig#RECORD_UNTIL_FULL} and
64 * {@link TracingConfig#RECORD_CONTINUOUSLY} the events are recorded
Tim Volodinec8095422017-11-10 21:07:36 +000065 * using an internal buffer and flushed to the outputStream when
Tim Volodine88604f22018-01-18 20:14:08 +000066 * {@link #stop(OutputStream, Executor)} is called.
Tim Volodinec8095422017-11-10 21:07:36 +000067 *
Tim Volodine90980b42018-04-04 17:49:28 +010068 * @param tracingConfig Configuration options to use for tracing.
69 * @throws IllegalStateException If the system is already tracing.
70 * @throws IllegalArgumentException If the configuration is invalid (e.g.
71 * invalid category pattern or invalid tracing mode).
Tim Volodinec8095422017-11-10 21:07:36 +000072 */
Tim Volodine88604f22018-01-18 20:14:08 +000073 public abstract void start(@NonNull TracingConfig tracingConfig);
Tim Volodinec8095422017-11-10 21:07:36 +000074
75 /**
Tim Volodine88604f22018-01-18 20:14:08 +000076 * Stops tracing and flushes tracing data to the specified outputStream.
Tim Volodinec8095422017-11-10 21:07:36 +000077 *
Tim Volodine88604f22018-01-18 20:14:08 +000078 * The data is sent to the specified output stream in json format typically
79 * in chunks by invoking {@link java.io.OutputStream#write(byte[])}. On completion
80 * the {@link java.io.OutputStream#close()} method is called.
Tim Volodinec8095422017-11-10 21:07:36 +000081 *
Tim Volodine90980b42018-04-04 17:49:28 +010082 * @param outputStream The output stream the tracing data will be sent to. If null
Tim Volodine88604f22018-01-18 20:14:08 +000083 * the tracing data will be discarded.
Tim Volodine90980b42018-04-04 17:49:28 +010084 * @param executor The {@link java.util.concurrent.Executor} on which the
85 * outputStream {@link java.io.OutputStream#write(byte[])} and
86 * {@link java.io.OutputStream#close()} methods will be invoked.
87 * @return False if the WebView framework was not tracing at the time of the call,
88 * true otherwise.
Tim Volodinec8095422017-11-10 21:07:36 +000089 */
Tim Volodine88604f22018-01-18 20:14:08 +000090 public abstract boolean stop(@Nullable OutputStream outputStream,
91 @NonNull @CallbackExecutor Executor executor);
Tim Volodinec8095422017-11-10 21:07:36 +000092
Tim Volodine90980b42018-04-04 17:49:28 +010093 /**
94 * Returns whether the WebView framework is tracing.
95 *
96 * @return True if tracing is enabled.
97 */
Tim Volodinec8095422017-11-10 21:07:36 +000098 public abstract boolean isTracing();
99
Tim Volodinec8095422017-11-10 21:07:36 +0000100}