1# Copyright (c) 2013 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.
4import os
5
6from telemetry.core.chrome import crx_id
7
8class ExtensionPathNonExistentException(Exception):
9  pass
10
11class MissingPublicKeyException(Exception):
12  pass
13
14class ExtensionToLoad(object):
15  def __init__(self, path, browser_type, is_component=False):
16    if not os.path.isdir(path):
17      raise ExtensionPathNonExistentException(
18          'Extension path not a directory %s' % path)
19    self._path = path
20    self._local_path = path
21    self._is_component = is_component
22    if is_component and not crx_id.HasPublicKey(path):
23      raise MissingPublicKeyException(
24         'Component extension %s must have a public key' % path)
25
26    # It is possible that we are running telemetry on Windows targeting
27    # a remote CrOS or Android device. In this case, we need the
28    # browser_type argument to determine how we should encode
29    # the extension path.
30    self._is_win = (os.name == 'nt'
31        and not (browser_type.startswith('android')
32                 or browser_type.startswith('cros')))
33
34  @property
35  def extension_id(self):
36    """Unique extension id of this extension."""
37    if crx_id.HasPublicKey(self._path):
38      # Calculate extension id from the public key.
39      return crx_id.GetCRXAppID(os.path.realpath(self._path))
40    else:
41      # Calculate extension id based on the path on the device.
42      return crx_id.GetCRXAppID(
43          os.path.realpath(self._local_path),
44          from_file_path=True,
45          is_win_path=self._is_win)
46
47  @property
48  def path(self):
49    """Path to extension source directory."""
50    return self._path
51
52  @property
53  def local_path(self):
54    """Path to extension destination directory, for remote instances of
55    chrome"""
56    return self._local_path
57
58  @local_path.setter
59  def local_path(self, local_path):
60    self._local_path = local_path
61
62  @property
63  def is_component(self):
64    """Whether this extension should be loaded as a component extension."""
65    return self._is_component
66