1# Copyright 2012 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
5from telemetry.core import extension_to_load
6
7
8class ExtensionDict(object):
9  """Dictionary of ExtensionPage instances, with extension_id as key."""
10
11  def __init__(self, extension_backend):
12    self._extension_backend = extension_backend
13
14  def __getitem__(self, load_extension):
15    """Given an ExtensionToLoad instance, returns the corresponding
16    ExtensionPage instance."""
17    if not isinstance(load_extension, extension_to_load.ExtensionToLoad):
18      raise TypeError("Input param must be of type ExtensionToLoad")
19    return self.GetByExtensionId(load_extension.extension_id)[0]
20
21  def __contains__(self, load_extension):
22    """Checks if this ExtensionToLoad instance has been loaded"""
23    if not isinstance(load_extension, extension_to_load.ExtensionToLoad):
24      raise TypeError("Input param must be of type ExtensionToLoad")
25    return load_extension.extension_id in self._extension_backend
26
27  def keys(self):
28    return self._extension_backend.keys()
29
30  def GetByExtensionId(self, extension_id):
31    """Returns a list of extensions given an extension id. This is useful for
32    connecting to built-in apps and component extensions."""
33    return self._extension_backend[extension_id]
34