OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <string> |
| 6 #include <vector> |
| 7 |
5 #include "chrome/test/chromedriver/performance_logger.h" | 8 #include "chrome/test/chromedriver/performance_logger.h" |
6 | 9 |
7 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
8 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
9 #include "base/values.h" | 12 #include "base/values.h" |
10 #include "chrome/test/chromedriver/chrome/devtools_client.h" | 13 #include "chrome/test/chromedriver/chrome/devtools_client.h" |
11 #include "chrome/test/chromedriver/chrome/devtools_client_impl.h" | 14 #include "chrome/test/chromedriver/chrome/devtools_client_impl.h" |
12 #include "chrome/test/chromedriver/chrome/log.h" | 15 #include "chrome/test/chromedriver/chrome/log.h" |
13 #include "chrome/test/chromedriver/chrome/status.h" | 16 #include "chrome/test/chromedriver/chrome/status.h" |
14 | 17 |
15 namespace { | 18 namespace { |
16 | 19 |
17 // DevTools event domain prefixes to intercept. | 20 // DevTools event domain prefixes to intercept. |
18 const char* const kDomains[] = {"Network.", "Page.", "Timeline."}; | 21 const char* const kDomains[] = {"Network.", "Page.", "Timeline."}; |
19 | 22 |
20 const char* const kDomainEnableCommands[] = { | |
21 "Network.enable", "Page.enable", "Timeline.start" | |
22 }; | |
23 | |
24 // Returns whether the event belongs to one of kDomains. | 23 // Returns whether the event belongs to one of kDomains. |
25 bool ShouldLogEvent(const std::string& method) { | 24 bool ShouldLogEvent(const std::string& method) { |
26 for (size_t i_domain = 0; i_domain < arraysize(kDomains); ++i_domain) { | 25 for (size_t i_domain = 0; i_domain < arraysize(kDomains); ++i_domain) { |
27 if (StartsWithASCII(method, kDomains[i_domain], true /* case_sensitive */)) | 26 if (StartsWithASCII(method, kDomains[i_domain], true /* case_sensitive */)) |
28 return true; | 27 return true; |
29 } | 28 } |
30 return false; | 29 return false; |
31 } | 30 } |
32 | 31 |
| 32 bool IsEnabled(const PerfLoggingPrefs::InspectorDomainStatus& domain_status) { |
| 33 return (domain_status == |
| 34 PerfLoggingPrefs::InspectorDomainStatus::kDefaultEnabled || |
| 35 domain_status == |
| 36 PerfLoggingPrefs::InspectorDomainStatus::kExplicitlyEnabled); |
| 37 } |
| 38 |
33 } // namespace | 39 } // namespace |
34 | 40 |
35 PerformanceLogger::PerformanceLogger(Log* log) | 41 PerformanceLogger::PerformanceLogger(Log* log) |
36 : log_(log) {} | 42 : log_(log) {} |
37 | 43 |
| 44 PerformanceLogger::PerformanceLogger(Log* log, const PerfLoggingPrefs& prefs) |
| 45 : log_(log), |
| 46 prefs_(prefs) { |
| 47 if (!prefs_.trace_categories.empty()) { |
| 48 LOG(WARNING) << "Ignoring trace categories because tracing support not yet " |
| 49 << "implemented: " << prefs_.trace_categories; |
| 50 prefs_.trace_categories = ""; |
| 51 } |
| 52 } |
| 53 |
38 bool PerformanceLogger::subscribes_to_browser() { | 54 bool PerformanceLogger::subscribes_to_browser() { |
39 return true; | 55 return true; |
40 } | 56 } |
41 | 57 |
42 Status PerformanceLogger::OnConnected(DevToolsClient* client) { | 58 Status PerformanceLogger::OnConnected(DevToolsClient* client) { |
43 if (client->GetId() == DevToolsClientImpl::kBrowserwideDevToolsClientId) { | 59 if (client->GetId() == DevToolsClientImpl::kBrowserwideDevToolsClientId) { |
44 // TODO(johnmoore): Implement tracing log. | 60 // TODO(johnmoore): Implement tracing log. |
45 return Status(kOk); | 61 return Status(kOk); |
46 } | 62 } |
47 base::DictionaryValue params; // All our enable commands have empty params. | 63 std::vector<std::string> enable_commands; |
48 for (size_t i_cmd = 0; i_cmd < arraysize(kDomainEnableCommands); ++i_cmd) { | 64 if (IsEnabled(prefs_.network)) |
49 Status status = client->SendCommand(kDomainEnableCommands[i_cmd], params); | 65 enable_commands.push_back("Network.enable"); |
| 66 if (IsEnabled(prefs_.page)) |
| 67 enable_commands.push_back("Page.enable"); |
| 68 if (IsEnabled(prefs_.timeline)) { |
| 69 // Timeline feed implicitly disabled when trace categories are specified. |
| 70 // So even if kDefaultEnabled, don't enable unless empty |trace_categories|. |
| 71 if (prefs_.trace_categories.empty() || prefs_.timeline == |
| 72 PerfLoggingPrefs::InspectorDomainStatus::kExplicitlyEnabled) |
| 73 enable_commands.push_back("Timeline.start"); |
| 74 } |
| 75 for (std::vector<std::string>::const_iterator it = enable_commands.begin(); |
| 76 it != enable_commands.end(); ++it) { |
| 77 base::DictionaryValue params; // All the enable commands have empty params. |
| 78 Status status = client->SendCommand(*it, params); |
50 if (status.IsError()) | 79 if (status.IsError()) |
51 return status; | 80 return status; |
52 } | 81 } |
53 return Status(kOk); | 82 return Status(kOk); |
54 } | 83 } |
55 | 84 |
56 Status PerformanceLogger::OnEvent( | 85 Status PerformanceLogger::OnEvent( |
57 DevToolsClient* client, | 86 DevToolsClient* client, |
58 const std::string& method, | 87 const std::string& method, |
59 const base::DictionaryValue& params) { | 88 const base::DictionaryValue& params) { |
(...skipping 10 matching lines...) Expand all Loading... |
70 base::JSONWriter::Write(&log_message_dict, &log_message_json); | 99 base::JSONWriter::Write(&log_message_dict, &log_message_json); |
71 | 100 |
72 log_->AddEntry(Log::kInfo, log_message_json); | 101 log_->AddEntry(Log::kInfo, log_message_json); |
73 return Status(kOk); | 102 return Status(kOk); |
74 } | 103 } |
75 | 104 |
76 // TODO(johnmoore): Use BeforeCommand to implement tracing log. | 105 // TODO(johnmoore): Use BeforeCommand to implement tracing log. |
77 Status PerformanceLogger::BeforeCommand(const std::string& command_name) { | 106 Status PerformanceLogger::BeforeCommand(const std::string& command_name) { |
78 return Status(kOk); | 107 return Status(kOk); |
79 } | 108 } |
OLD | NEW |