1 // Copyright 2014 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.
5 #ifndef CHROME_TEST_CHROMEDRIVER_PERFORMANCE_LOGGER_H_
6 #define CHROME_TEST_CHROMEDRIVER_PERFORMANCE_LOGGER_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "chrome/test/chromedriver/capabilities.h"
13 #include "chrome/test/chromedriver/chrome/devtools_event_listener.h"
14 #include "chrome/test/chromedriver/command_listener.h"
19 // Translates DevTools profiler events into Log messages with info level.
21 // The message is a JSON string of the following structure:
23 // "webview": <originating WebView ID>,
24 // "message": { "method": "...", "params": { ... }} // DevTools message.
27 // Also translates buffered trace events into Log messages of info level with
28 // the same structure if tracing categories are specified.
30 class PerformanceLogger
: public DevToolsEventListener
, public CommandListener
{
32 // Creates a |PerformanceLogger| with default preferences that creates entries
33 // in the given Log object. The log is owned elsewhere and must not be null.
34 PerformanceLogger(Log
* log
, const Session
* session
);
36 // Creates a |PerformanceLogger| with specific preferences.
37 PerformanceLogger(Log
* log
,
38 const Session
* session
,
39 const PerfLoggingPrefs
& prefs
);
41 // PerformanceLogger subscribes to browser-wide |DevToolsClient| for tracing.
42 bool subscribes_to_browser() override
;
44 // For browser-wide client: enables tracing if trace categories are specified,
45 // sets |browser_client_|. For other clients: calls EnableInspectorDomains.
46 Status
OnConnected(DevToolsClient
* client
) override
;
48 // Calls HandleInspectorEvents or HandleTraceEvents depending on client type.
49 Status
OnEvent(DevToolsClient
* client
,
50 const std::string
& method
,
51 const base::DictionaryValue
& params
) override
;
53 // Before whitelisted commands, if tracing enabled, calls CollectTraceEvents.
54 Status
BeforeCommand(const std::string
& command_name
) override
;
57 void AddLogEntry(Log::Level level
,
58 const std::string
& webview
,
59 const std::string
& method
,
60 const base::DictionaryValue
& params
);
62 void AddLogEntry(const std::string
& webview
,
63 const std::string
& method
,
64 const base::DictionaryValue
& params
);
66 // Enables Network, Page and Timeline domains according to |PerfLoggingPrefs|.
67 Status
EnableInspectorDomains(DevToolsClient
* client
);
69 // Logs Network, Page, and Timeline events.
70 Status
HandleInspectorEvents(DevToolsClient
* client
,
71 const std::string
& method
,
72 const base::DictionaryValue
& params
);
74 // Logs trace events and monitors trace buffer usage.
75 Status
HandleTraceEvents(DevToolsClient
* client
,
76 const std::string
& method
,
77 const base::DictionaryValue
& params
);
79 bool ShouldReportTracingError();
80 Status
StartTrace(); // Must not call before browser-wide client connects.
81 Status
CollectTraceEvents(); // Ditto.
82 Status
IsTraceDone(bool* trace_done
) const; // True if trace is not buffering.
84 Log
* log_
; // The log where to create entries.
85 const Session
* session_
;
86 PerfLoggingPrefs prefs_
;
87 DevToolsClient
* browser_client_
; // Pointer to browser-wide |DevToolsClient|.
88 bool trace_buffering_
; // True unless trace stopped and all events received.
90 DISALLOW_COPY_AND_ASSIGN(PerformanceLogger
);
93 #endif // CHROME_TEST_CHROMEDRIVER_PERFORMANCE_LOGGER_H_