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