1# Copyright 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
5import tempfile
6
7from telemetry.core.platform import profiler
8
9
10class NetLogProfiler(profiler.Profiler):
11
12  _NET_LOG_ARG = '--log-net-log='
13
14  @classmethod
15  def name(cls):
16    return 'netlog'
17
18  @classmethod
19  def is_supported(cls, browser_type):
20    return not browser_type.startswith('cros')
21
22  @classmethod
23  def CustomizeBrowserOptions(cls, browser_type, options):
24    if browser_type.startswith('android'):
25      dump_file = '/sdcard/net-internals-profile.json'
26    else:
27      dump_file = tempfile.mkstemp()[1]
28    options.AppendExtraBrowserArgs([cls._NET_LOG_ARG + dump_file])
29
30  def CollectProfile(self):
31    # Find output filename from browser argument.
32    for i in self._browser_backend.browser_options.extra_browser_args:
33      if i.startswith(self._NET_LOG_ARG):
34        output_file = i[len(self._NET_LOG_ARG):]
35    assert output_file
36    # On Android pull the output file to the host.
37    if self._platform_backend.GetOSName() == 'android':
38      host_output_file = '%s.json' % self._output_path
39      self._browser_backend.adb.device().old_interface.Adb().Pull(
40          output_file, host_output_file)
41      # Clean the device
42      self._browser_backend.adb.device().RunShellCommand('rm %s' % output_file)
43      output_file = host_output_file
44    print 'Net-internals log saved as %s' % output_file
45    print 'To view, open in chrome://net-internals'
46    return [output_file]
47