# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import logging
import os
import time

from common import chrome_proxy_metrics
from common import network_metrics
from common.chrome_proxy_metrics import ChromeProxyMetricException
from telemetry.page import page_test
from telemetry.value import scalar
from metrics import Metric

class ChromeProxyMetric(network_metrics.NetworkMetric):
  """A Chrome proxy timeline metric."""

  def __init__(self):
    super(ChromeProxyMetric, self).__init__()
    self.compute_data_saving = True

  def SetEvents(self, events):
    """Used for unittest."""
    self._events = events

  def ResponseFromEvent(self, event):
    return chrome_proxy_metrics.ChromeProxyResponse(event)

  def AddResults(self, tab, results):
    raise NotImplementedError

  def AddResultsForDataSaving(self, tab, results):
    resources_via_proxy = 0
    resources_from_cache = 0
    resources_direct = 0

    super(ChromeProxyMetric, self).AddResults(tab, results)
    for resp in self.IterResponses(tab):
      if resp.response.served_from_cache:
        resources_from_cache += 1
      if resp.HasChromeProxyViaHeader():
        resources_via_proxy += 1
      else:
        resources_direct += 1

    if resources_from_cache + resources_via_proxy + resources_direct == 0:
      raise ChromeProxyMetricException, (
          'Expected at least one response, but zero responses were received.')

    results.AddValue(scalar.ScalarValue(
        results.current_page, 'resources_via_proxy', 'count',
        resources_via_proxy))
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'resources_from_cache', 'count',
        resources_from_cache))
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'resources_direct', 'count', resources_direct))

  def AddResultsForHeaderValidation(self, tab, results):
    via_count = 0

    for resp in self.IterResponses(tab):
      if resp.IsValidByViaHeader():
        via_count += 1
      else:
        r = resp.response
        raise ChromeProxyMetricException, (
            '%s: Via header (%s) is not valid (refer=%s, status=%d)' % (
                r.url, r.GetHeader('Via'), r.GetHeader('Referer'), r.status))

    if via_count == 0:
      raise ChromeProxyMetricException, (
          'Expected at least one response through the proxy, but zero such '
          'responses were received.')
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'checked_via_header', 'count', via_count))

  def AddResultsForLatency(self, tab, results):
    # TODO(bustamante): This is a hack to workaround crbug.com/467174,
    #   once fixed just pull down window.performance.timing object and
    #   reference that everywhere.
    load_event_start = tab.EvaluateJavaScript(
        'window.performance.timing.loadEventStart')
    navigation_start = tab.EvaluateJavaScript(
        'window.performance.timing.navigationStart')
    dom_content_loaded_event_start = tab.EvaluateJavaScript(
        'window.performance.timing.domContentLoadedEventStart')
    fetch_start = tab.EvaluateJavaScript(
        'window.performance.timing.fetchStart')
    request_start = tab.EvaluateJavaScript(
        'window.performance.timing.requestStart')
    domain_lookup_end = tab.EvaluateJavaScript(
        'window.performance.timing.domainLookupEnd')
    domain_lookup_start = tab.EvaluateJavaScript(
        'window.performance.timing.domainLookupStart')
    connect_end = tab.EvaluateJavaScript(
        'window.performance.timing.connectEnd')
    connect_start = tab.EvaluateJavaScript(
        'window.performance.timing.connectStart')
    response_end = tab.EvaluateJavaScript(
        'window.performance.timing.responseEnd')
    response_start = tab.EvaluateJavaScript(
        'window.performance.timing.responseStart')

    # NavigationStart relative markers in milliseconds.
    load_start = (float(load_event_start) - navigation_start)
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'load_start', 'ms', load_start))

    dom_content_loaded_start = (
        float(dom_content_loaded_event_start) - navigation_start)
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'dom_content_loaded_start', 'ms',
        dom_content_loaded_start))

    fetch_start = (float(fetch_start) - navigation_start)
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'fetch_start', 'ms', fetch_start,
        important=False))

    request_start = (float(request_start) - navigation_start)
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'request_start', 'ms', request_start,
        important=False))

    response_start = (float(response_start) - navigation_start)
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'response_start', 'ms', response_start,
        important=False))

    response_end = (float(response_end) - navigation_start)
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'response_end', 'ms', response_end,
        important=False))

    # Phase measurements in milliseconds.
    domain_lookup_duration = (float(domain_lookup_end) - domain_lookup_start)
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'domain_lookup_duration', 'ms',
        domain_lookup_duration, important=False))

    connect_duration = (float(connect_end) - connect_start)
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'connect_duration', 'ms', connect_duration,
        important=False))

    request_duration = (float(response_start) - request_start)
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'request_duration', 'ms', request_duration,
        important=False))

    response_duration = (float(response_end) - response_start)
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'response_duration', 'ms', response_duration,
        important=False))

  def AddResultsForExtraViaHeader(self, tab, results, extra_via_header):
    extra_via_count = 0

    for resp in self.IterResponses(tab):
      if resp.HasChromeProxyViaHeader():
        if resp.HasExtraViaHeader(extra_via_header):
          extra_via_count += 1
        else:
          raise ChromeProxyMetricException, (
              '%s: Should have via header %s.' % (resp.response.url,
                                                  extra_via_header))

    results.AddValue(scalar.ScalarValue(
        results.current_page, 'extra_via_header', 'count', extra_via_count))

  def AddResultsForClientVersion(self, tab, results):
    via_count = 0
    for resp in self.IterResponses(tab):
      r = resp.response
      if resp.response.status != 200:
        raise ChromeProxyMetricException, ('%s: Response is not 200: %d' %
                                           (r.url, r.status))
      if not resp.IsValidByViaHeader():
        raise ChromeProxyMetricException, ('%s: Response missing via header' %
                                           (r.url))
      via_count += 1

    if via_count == 0:
      raise ChromeProxyMetricException, (
          'Expected at least one response through the proxy, but zero such '
          'responses were received.')
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'responses_via_proxy', 'count', via_count))

  def GetClientTypeFromRequests(self, tab):
    """Get the Chrome-Proxy client type value from requests made in this tab.

    Returns:
        The client type value from the first request made in this tab that
        specifies a client type in the Chrome-Proxy request header. See
        ChromeProxyResponse.GetChromeProxyClientType for more details about the
        Chrome-Proxy client type. Returns None if none of the requests made in
        this tab specify a client type.
    """
    for resp in self.IterResponses(tab):
      client_type = resp.GetChromeProxyClientType()
      if client_type:
        return client_type
    return None

  def AddResultsForClientType(self, tab, results, client_type,
                              bypass_for_client_type):
    via_count = 0
    bypass_count = 0

    for resp in self.IterResponses(tab):
      if resp.HasChromeProxyViaHeader():
        via_count += 1
        if client_type.lower() == bypass_for_client_type.lower():
          raise ChromeProxyMetricException, (
              '%s: Response for client of type "%s" has via header, but should '
              'be bypassed.' % (resp.response.url, bypass_for_client_type))
      elif resp.ShouldHaveChromeProxyViaHeader():
        bypass_count += 1
        if client_type.lower() != bypass_for_client_type.lower():
          raise ChromeProxyMetricException, (
              '%s: Response missing via header. Only "%s" clients should '
              'bypass for this page, but this client is "%s".' % (
                  resp.response.url, bypass_for_client_type, client_type))

    if via_count + bypass_count == 0:
      raise ChromeProxyMetricException, (
          'Expected at least one response that was eligible to be proxied, but '
          'zero such responses were received.')

    results.AddValue(scalar.ScalarValue(
        results.current_page, 'via', 'count', via_count))
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'bypass', 'count', bypass_count))

  def AddResultsForLoFi(self, tab, results):
    lo_fi_request_count = 0
    lo_fi_response_count = 0

    for resp in self.IterResponses(tab):
      if 'favicon.ico' in resp.response.url:
        continue

      if resp.HasChromeProxyLoFiRequest():
        lo_fi_request_count += 1
      else:
        raise ChromeProxyMetricException, (
            '%s: LoFi not in request header.' % (resp.response.url))

      if resp.HasChromeProxyLoFiResponse():
        lo_fi_response_count += 1
      else:
        raise ChromeProxyMetricException, (
            '%s: LoFi not in response header.' % (resp.response.url))

      if resp.content_length > 100:
        raise ChromeProxyMetricException, (
            'Image %s is %d bytes. Expecting less than 100 bytes.' %
            (resp.response.url, resp.content_length))

    if lo_fi_request_count == 0:
      raise ChromeProxyMetricException, (
          'Expected at least one LoFi request, but zero such requests were '
          'sent.')
    if lo_fi_response_count == 0:
      raise ChromeProxyMetricException, (
          'Expected at least one LoFi response, but zero such responses were '
          'received.')

    results.AddValue(scalar.ScalarValue(
        results.current_page, 'lo_fi_request', 'count', lo_fi_request_count))
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'lo_fi_response', 'count', lo_fi_response_count))
    super(ChromeProxyMetric, self).AddResults(tab, results)

  def AddResultsForLoFiPreview(self, tab, results):
    lo_fi_request_count = 0
    lo_fi_preview_request_count = 0
    lo_fi_preview_response_count = 0

    for resp in self.IterResponses(tab):
      if 'favicon.ico' in resp.response.url:
        continue
      if resp.response.url.startswith('data:'):
        continue

      if resp.HasChromeProxyLoFiPreviewRequest():
        lo_fi_preview_request_count += 1
      elif resp.HasChromeProxyLoFiRequest():
        lo_fi_request_count += 1
      else:
        raise ChromeProxyMetricException, (
            '%s: LoFi not in request header.' % (resp.response.url))

      if resp.HasChromeProxyLoFiPreviewResponse():
        lo_fi_preview_response_count += 1

    if lo_fi_preview_request_count == 0:
      raise ChromeProxyMetricException, (
          'Expected at least one LoFi preview request, but zero such requests '
          'were sent.')
    if lo_fi_preview_response_count == 0:
      raise ChromeProxyMetricException, (
          'Expected at least one LoFi preview response, but zero such '
          'responses were received.')

    results.AddValue(
        scalar.ScalarValue(
            results.current_page, 'lo_fi_preview_request',
            'count', lo_fi_preview_request_count))
    results.AddValue(
        scalar.ScalarValue(
            results.current_page, 'lo_fi_request',
            'count', lo_fi_request_count))
    results.AddValue(
        scalar.ScalarValue(
            results.current_page, 'lo_fi_preview_response',
            'count', lo_fi_preview_response_count))
    super(ChromeProxyMetric, self).AddResults(tab, results)

  def AddResultsForPassThrough(self, tab, results):
    compressed_count = 0
    compressed_size = 0
    pass_through_count = 0
    pass_through_size = 0

    for resp in self.IterResponses(tab):
      if 'favicon.ico' in resp.response.url:
        continue
      if not resp.HasChromeProxyViaHeader():
        r = resp.response
        raise ChromeProxyMetricException, (
            '%s: Should have Via header (%s) (refer=%s, status=%d)' % (
                r.url, r.GetHeader('Via'), r.GetHeader('Referer'), r.status))
      if resp.HasChromeProxyPassThroughRequest():
        pass_through_count += 1
        pass_through_size = resp.content_length
      else:
        compressed_count += 1
        compressed_size = resp.content_length

    if pass_through_count != 1:
      raise ChromeProxyMetricException, (
          'Expected exactly one Chrome-Proxy pass-through request, but %d '
          'such requests were sent.' % (pass_through_count))

    if compressed_count != 1:
      raise ChromeProxyMetricException, (
          'Expected exactly one compressed request, but %d such requests were '
          'received.' % (compressed_count))

    if compressed_size >= pass_through_size:
      raise ChromeProxyMetricException, (
          'Compressed image is %d bytes and pass-through image is %d. '
          'Expecting compressed image size to be less than pass-through '
          'image.' % (compressed_size, pass_through_size))

    results.AddValue(scalar.ScalarValue(
        results.current_page, 'compressed', 'count', compressed_count))
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'compressed_size', 'bytes', compressed_size))
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'pass_through', 'count', pass_through_count))
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'pass_through_size', 'bytes', pass_through_size))

  def AddResultsForHTTPSBypass(self, tab, results):
    bypass_count = 0

    for resp in self.IterResponses(tab):
      # Only check https url's
      if "https://" not in resp.response.url:
        continue

      # If a Chrome Proxy Via appears fail the test
      if resp.HasChromeProxyViaHeader():
        r = resp.response
        raise ChromeProxyMetricException, (
            '%s: Should not have Via header (%s) (refer=%s, status=%d)' % (
                r.url, r.GetHeader('Via'), r.GetHeader('Referer'), r.status))
      bypass_count += 1

    if bypass_count == 0:
      raise ChromeProxyMetricException, (
          'Expected at least one https response was expected, but zero such '
          'responses were received.')

    results.AddValue(scalar.ScalarValue(
        results.current_page, 'bypass', 'count', bypass_count))

  def AddResultsForHTML5Test(self, tab, results):
    # Wait for the number of "points" of HTML5 compatibility to appear to verify
    # the HTML5 elements have loaded successfully.
    tab.WaitForJavaScriptExpression(
        'document.getElementsByClassName("pointsPanel")', 15)

  def AddResultsForYouTube(self, tab, results):
    # Wait for the video to begin playing.
    tab.WaitForJavaScriptExpression(
        'window.playerState == YT.PlayerState.PLAYING', 30)

  def AddResultsForBypass(self, tab, results, url_pattern=""):
    bypass_count = 0
    skipped_count = 0

    for resp in self.IterResponses(tab):
      # Only check the url's that contain the specified pattern.
      if url_pattern and url_pattern not in resp.response.url:
        skipped_count += 1
        continue

      if resp.HasChromeProxyViaHeader():
        r = resp.response
        raise ChromeProxyMetricException, (
            '%s: Should not have Via header (%s) (refer=%s, status=%d)' % (
                r.url, r.GetHeader('Via'), r.GetHeader('Referer'), r.status))
      bypass_count += 1

    if bypass_count == 0:
      raise ChromeProxyMetricException, (
          'Expected at least one response to be bypassed, but zero such '
          'responses were received.')

    results.AddValue(scalar.ScalarValue(
        results.current_page, 'bypass', 'count', bypass_count))
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'skipped', 'count', skipped_count))

  def AddResultsForCorsBypass(self, tab, results):
    eligible_response_count = 0
    bypass_count = 0
    bypasses = {}
    for resp in self.IterResponses(tab):
      logging.warn('got a resource %s' % (resp.response.url))

    for resp in self.IterResponses(tab):
      if resp.ShouldHaveChromeProxyViaHeader():
        eligible_response_count += 1
        if not resp.HasChromeProxyViaHeader():
          bypass_count += 1
        elif resp.response.status == 502:
          bypasses[resp.response.url] = 0

    for resp in self.IterResponses(tab):
      if resp.ShouldHaveChromeProxyViaHeader():
        if not resp.HasChromeProxyViaHeader():
          if resp.response.status == 200:
            if (bypasses.has_key(resp.response.url)):
              bypasses[resp.response.url] = bypasses[resp.response.url] + 1

    for url in bypasses:
      if bypasses[url] == 0:
        raise ChromeProxyMetricException, (
            '%s: Got a 502 without a subsequent 200' % (url))
      elif bypasses[url] > 1:
        raise ChromeProxyMetricException, (
            '%s: Got a 502 and multiple 200s: %d' % (url, bypasses[url]))
    if bypass_count == 0:
      raise ChromeProxyMetricException, (
          'At least one response should be bypassed. '
          '(eligible_response_count=%d, bypass_count=%d)\n' % (
              eligible_response_count, bypass_count))

    results.AddValue(scalar.ScalarValue(
        results.current_page, 'cors_bypass', 'count', bypass_count))

  def AddResultsForBlockOnce(self, tab, results):
    eligible_response_count = 0
    via_proxy = 0
    visited_urls = []

    for resp in self.IterResponses(tab):
      # Add debug information in case of failure
      visited_urls.append(resp.response.url)

      # Block-once test URLs (Data Reduction Proxy always returns
      # block-once) should not have the Chrome-Compression-Proxy Via header.
      if (IsTestUrlForBlockOnce(resp.response.url)):
        eligible_response_count += 1
        if resp.HasChromeProxyViaHeader():
          raise ChromeProxyMetricException, (
              'Response has a Chrome-Compression-Proxy Via header: ' +
              resp.response.url)
      elif resp.ShouldHaveChromeProxyViaHeader():
        via_proxy += 1
        if not resp.HasChromeProxyViaHeader():
          # For all other URLs, confirm that via header is present if expected.
          raise ChromeProxyMetricException, (
              'Missing Chrome-Compression-Proxy Via header.' +
              resp.response.url)

    if via_proxy == 0:
      raise ChromeProxyMetricException, (
          'None of the requests went via data reduction proxy')

    if (eligible_response_count != 2):
      raise ChromeProxyMetricException, (
          'Did not make expected number of requests to whitelisted block-once'
          ' test URLs. Expected: 2, Actual: %s, Visited URLs: %s' %
          (eligible_response_count, visited_urls))

    results.AddValue(scalar.ScalarValue(results.current_page,
                                        'eligible_responses', 'count', 2))
    results.AddValue(scalar.ScalarValue(results.current_page,
                                        'via_proxy', 'count', via_proxy))

  def AddResultsForSafebrowsingOn(self, tab, results):
    results.AddValue(scalar.ScalarValue(
        results.current_page, 'safebrowsing', 'timeout responses', 1))

  def AddResultsForSafebrowsingOff(self, tab, results):
    response_count = 0
    for resp in self.IterResponses(tab):
      # Data reduction proxy should return the real response for sites with
      # malware.
      response_count += 1
      if not resp.HasChromeProxyViaHeader():
        r = resp.response
        raise ChromeProxyMetricException, (
            '%s: Safebrowsing feature should be off for desktop and webview.\n'
            'Reponse: status=(%d, %s)\nHeaders:\n %s' % (
                r.url, r.status, r.status_text, r.headers))

    if response_count == 0:
      raise ChromeProxyMetricException, (
          'Safebrowsing test failed: No valid responses received')

    results.AddValue(scalar.ScalarValue(
        results.current_page, 'safebrowsing', 'responses', response_count))

  def AddResultsForHTTPFallback(self, tab, results):
    via_fallback_count = 0

    for resp in self.IterResponses(tab):
      if resp.ShouldHaveChromeProxyViaHeader():
        # All responses should have come through the HTTP fallback proxy, which
        # means that they should have the via header, and if a remote port is
        # defined, it should be port 80.
        if (not resp.HasChromeProxyViaHeader() or
            (resp.remote_port and resp.remote_port != 80)):
          r = resp.response
          raise ChromeProxyMetricException, (
              '%s: Should have come through the fallback proxy.\n'
              'Reponse: remote_port=%s status=(%d, %s)\nHeaders:\n %s' % (
                  r.url, str(resp.remote_port), r.status, r.status_text,
                  r.headers))
        via_fallback_count += 1

    if via_fallback_count == 0:
      raise ChromeProxyMetricException, (
          'Expected at least one response through the fallback proxy, but zero '
          'such responses were received.')
    results.AddValue(scalar.ScalarValue(