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):
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  platform_clients = _platform_backends_to_devtools_clients_maps.setdefault(
31      devtools_client_backend.platform_backend, {})
32  platform_clients[remote_port] = devtools_client_backend
33
34
35def GetDevToolsClients(platform_backend):
36  """Get DevTools clients including the ones that are no longer connectable."""
37  devtools_clients_map = _platform_backends_to_devtools_clients_maps.get(
38      platform_backend, {})
39  if not devtools_clients_map:
40    return []
41  return devtools_clients_map.values()
42
43def GetActiveDevToolsClients(platform_backend):
44  """Get DevTools clients that are still connectable."""
45  _RemoveStaleDevToolsClient(platform_backend)
46  return GetDevToolsClients(platform_backend)
47