# Copyright 2017 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.

from telemetry.core import exceptions


class InspectorServiceWorker(object):
  def __init__(self, inspector_websocket, timeout):
    self._websocket = inspector_websocket
    self._websocket.RegisterDomain('ServiceWorker', self._OnNotification)
    # ServiceWorker.enable RPC must be called before calling any other methods
    # in ServiceWorker domain.
    res = self._websocket.SyncRequest(
        {'method': 'ServiceWorker.enable'}, timeout)
    if 'error' in res:
      raise exceptions.StoryActionError(res['error']['message'])

  def _OnNotification(self, msg):
    # TODO: track service worker events
    # (https://chromedevtools.github.io/devtools-protocol/tot/ServiceWorker/)
    pass

  def StopAllWorkers(self, timeout):
    res = self._websocket.SyncRequest(
        {'method': 'ServiceWorker.stopAllWorkers'}, timeout)
    if 'error' in res:
      raise exceptions.StoryActionError(res['error']['message'])

# Copyright 2017 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.

from telemetry.internal.backends.chrome_inspector import inspector_websocket
from telemetry.core import exceptions

class InspectorServiceWorker(object):
  def __init__(self, inspector_socket, timeout):
    self._websocket = inspector_socket
    self._websocket.RegisterDomain('ServiceWorker', self._OnNotification)
    # ServiceWorker.enable RPC must be called before calling any other methods
    # in ServiceWorker domain.
    res = self._websocket.SyncRequest(
        {'method': 'ServiceWorker.enable'}, timeout)
    if 'error' in res:
      raise exceptions.StoryActionError(res['error']['message'])

  def _OnNotification(self, msg):
    # TODO: track service worker events
    # (https://chromedevtools.github.io/devtools-protocol/tot/ServiceWorker/)
    pass

  def StopAllWorkers(self, timeout):
    res = self._websocket.SyncRequest(
        {'method': 'ServiceWorker.stopAllWorkers'}, timeout)
    if 'error' in res:
      code = res['error']['code']
      if code == inspector_websocket.InspectorWebsocket.METHOD_NOT_FOUND_CODE:
        raise NotImplementedError(
            'DevTools method ServiceWorker.stopAllWorkers is not supported by '
            'this browser.')
      raise exceptions.StoryActionError(res['error']['message'])

Handle error code METHOD_NOT_FOUND_CODE in InspectorServiceWorker.StopAllWorkers()
Handle error code METHOD_NOT_FOUND_CODE in InspectorServiceWorker.StopAllWorkers()

DevTools method ServiceWorker.stopAllWorkers is supported from M63, so
calling this can return METHOD_NOT_FOUND_CODE error in previous browser.
This CL make InspectorServiceWorker.StopAllWorkers() handle this error.
If it receives this error, it raises NotImplementedError.

This is implemented in the same way with memory_backend.py does.
https://cs.chromium.org/chromium/src/third_party/catapult/telemetry/telemetry/internal/backends/chrome_inspector/memory_backend.py?type=cs&q=warn+file:%5Esrc/third_party/catapult/telemetry/telemetry/internal/backends/chrome_inspector/+package:%5Echromium$&l=84

BUG=chromium:736697

Review-Url: https://chromiumcodereview.appspot.com/3013263002
