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