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