Home | History | Annotate | Download | only in chrome
      1 // Copyright (c) 2013 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 #include "chrome/test/chromedriver/chrome/performance_logger.h"
      6 
      7 #include "base/json/json_writer.h"
      8 #include "base/strings/string_util.h"
      9 #include "base/values.h"
     10 #include "chrome/test/chromedriver/chrome/devtools_client.h"
     11 #include "chrome/test/chromedriver/chrome/log.h"
     12 #include "chrome/test/chromedriver/chrome/status.h"
     13 
     14 namespace {
     15 
     16 // DevTools event domain prefixes to intercept.
     17 const char* const kDomains[] = {"Network.", "Page.", "Timeline."};
     18 
     19 const char* const kDomainEnableCommands[] = {
     20     "Network.enable", "Page.enable", "Timeline.start"
     21 };
     22 
     23 // Returns whether the event belongs to one of kDomains.
     24 bool ShouldLogEvent(const std::string& method) {
     25   for (size_t i_domain = 0; i_domain < arraysize(kDomains); ++i_domain) {
     26     if (StartsWithASCII(method, kDomains[i_domain], true /* case_sensitive */))
     27       return true;
     28   }
     29   return false;
     30 }
     31 
     32 }  // namespace
     33 
     34 PerformanceLogger::PerformanceLogger(Log* log)
     35     : log_(log) {}
     36 
     37 Status PerformanceLogger::OnConnected(DevToolsClient* client) {
     38   base::DictionaryValue params;  // All our enable commands have empty params.
     39   for (size_t i_cmd = 0; i_cmd < arraysize(kDomainEnableCommands); ++i_cmd) {
     40     Status status = client->SendCommand(kDomainEnableCommands[i_cmd], params);
     41     if (status.IsError())
     42       return status;
     43   }
     44   return Status(kOk);
     45 }
     46 
     47 Status PerformanceLogger::OnEvent(
     48     DevToolsClient* client,
     49     const std::string& method,
     50     const base::DictionaryValue& params) {
     51   if (!ShouldLogEvent(method))
     52     return Status(kOk);
     53 
     54   base::DictionaryValue log_message_dict;
     55   log_message_dict.SetString("webview", client->GetId());
     56   log_message_dict.SetString("message.method", method);
     57   log_message_dict.Set("message.params", params.DeepCopy());
     58   std::string log_message_json;
     59   // TODO(klm): extract timestamp from params?
     60   // Look at where it is for Page, Network, Timeline events.
     61   base::JSONWriter::Write(&log_message_dict, &log_message_json);
     62 
     63   log_->AddEntry(Log::kInfo, log_message_json);
     64   return Status(kOk);
     65 }
     66