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. 4 5 #ifndef CHROME_TEST_CHROMEDRIVER_PERFORMANCE_LOGGER_H_ 6 #define CHROME_TEST_CHROMEDRIVER_PERFORMANCE_LOGGER_H_ 7 8 #include <string> 9 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" 15 16 class Log; 17 struct Session; 18 19 // Translates DevTools profiler events into Log messages with info level. 20 // 21 // The message is a JSON string of the following structure: 22 // { 23 // "webview": <originating WebView ID>, 24 // "message": { "method": "...", "params": { ... }} // DevTools message. 25 // } 26 // 27 // Also translates buffered trace events into Log messages of info level with 28 // the same structure if tracing categories are specified. 29 30 class PerformanceLogger : public DevToolsEventListener, public CommandListener { 31 public: 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); 35 36 // Creates a |PerformanceLogger| with specific preferences. 37 PerformanceLogger(Log* log, 38 const Session* session, 39 const PerfLoggingPrefs& prefs); 40 41 // PerformanceLogger subscribes to browser-wide |DevToolsClient| for tracing. 42 virtual bool subscribes_to_browser() OVERRIDE; 43 44 // For browser-wide client: enables tracing if trace categories are specified, 45 // sets |browser_client_|. For other clients: calls EnableInspectorDomains. 46 virtual Status OnConnected(DevToolsClient* client) OVERRIDE; 47 48 // Calls HandleInspectorEvents or HandleTraceEvents depending on client type. 49 virtual Status OnEvent(DevToolsClient* client, 50 const std::string& method, 51 const base::DictionaryValue& params) OVERRIDE; 52 53 // Before whitelisted commands, if tracing enabled, calls CollectTraceEvents. 54 virtual Status BeforeCommand(const std::string& command_name) OVERRIDE; 55 56 private: 57 void AddLogEntry(Log::Level level, 58 const std::string& webview, 59 const std::string& method, 60 const base::DictionaryValue& params); 61 62 void AddLogEntry(const std::string& webview, 63 const std::string& method, 64 const base::DictionaryValue& params); 65 66 // Enables Network, Page and Timeline domains according to |PerfLoggingPrefs|. 67 Status EnableInspectorDomains(DevToolsClient* client); 68 69 // Logs Network, Page, and Timeline events. 70 Status HandleInspectorEvents(DevToolsClient* client, 71 const std::string& method, 72 const base::DictionaryValue& params); 73 74 // Logs trace events and monitors trace buffer usage. 75 Status HandleTraceEvents(DevToolsClient* client, 76 const std::string& method, 77 const base::DictionaryValue& params); 78 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. 83 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. 89 90 DISALLOW_COPY_AND_ASSIGN(PerformanceLogger); 91 }; 92 93 #endif // CHROME_TEST_CHROMEDRIVER_PERFORMANCE_LOGGER_H_ 94