1# Copyright 2015 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# A singleton map from platform backends to maps of uniquely-identifying
6# remote port (which may be the same as local port) to DevToolsClientBackend.
7# There is no guarantee that the devtools agent is still alive.
8_platform_backends_to_devtools_clients_maps = {}
9
10
11def _RemoveStaleDevToolsClient(platform_backend):
12  """Removes DevTools clients that are no longer connectable."""
13  devtools_clients_map = _platform_backends_to_devtools_clients_maps.get(
14      platform_backend, {})
15  devtools_clients_map = {
16      port: client
17      for port, client in devtools_clients_map.iteritems()
18      if client.IsAlive()
19      }
20  _platform_backends_to_devtools_clients_maps[platform_backend] = (
21      devtools_clients_map)
22
23
24def RegisterDevToolsClient(devtools_client_backend, platform_backend):
25  """Register DevTools client
26
27  This should only be called from DevToolsClientBackend when it is initialized.
28  """
29  remote_port = str(devtools_client_backend.remote_port)
30  if platform_backend not in _platform_backends_to_devtools_clients_maps:
31    _platform_backends_to_devtools_clients_maps[platform_backend] = {}
32  devtools_clients_map = (
33    _platform_backends_to_devtools_clients_maps[platform_backend])
34  devtools_clients_map[remote_port] = devtools_client_backend
35
36
37def IsSupported(platform_backend):
38  _RemoveStaleDevToolsClient(platform_backend)
39  devtools_clients_map = _platform_backends_to_devtools_clients_maps.get(
40      platform_backend, {})
41  for _, devtools_client in devtools_clients_map.iteritems():
42    if devtools_client.IsChromeTracingSupported():
43      return True
44  return False
45
46
47def GetDevToolsClients(platform_backend):
48  """Get DevTools clients including the ones that are no longer connectable."""
49  devtools_clients_map = _platform_backends_to_devtools_clients_maps.get(
50      platform_backend, {})
51  if not devtools_clients_map:
52    return []
53  return devtools_clients_map.values()
54
55def GetActiveDevToolsClients(platform_backend):
56  """Get DevTools clients that are still connectable."""
57  _RemoveStaleDevToolsClient(platform_backend)
58  return GetDevToolsClients(platform_backend)
59