| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 import json | |
| 6 import re | |
| 7 | |
| 8 from telemetry.core import extension_page | |
| 9 from telemetry.core.backends.chrome import inspector_backend | |
| 10 | |
| 11 | |
| 12 class ExtensionNotFoundException(Exception): | |
| 13 pass | |
| 14 | |
| 15 | |
| 16 class ExtensionDictBackend(object): | |
| 17 def __init__(self, browser_backend): | |
| 18 self._browser_backend = browser_backend | |
| 19 # Maps extension ids to ExtensionPage objects. | |
| 20 self._extension_dict = {} | |
| 21 | |
| 22 def __getitem__(self, extension_id): | |
| 23 extension_object = self._extension_dict.get(extension_id) | |
| 24 if not extension_object: | |
| 25 extension_object = self._CreateExtensionObject(extension_id) | |
| 26 assert extension_object | |
| 27 self._extension_dict[extension_id] = extension_object | |
| 28 return extension_object | |
| 29 | |
| 30 def __contains__(self, extension_id): | |
| 31 return extension_id in self.GetExtensionIds() | |
| 32 | |
| 33 @staticmethod | |
| 34 def _ExtractExtensionId(url): | |
| 35 m = re.match(r"(chrome-extension://)([^/]+)", url) | |
| 36 assert m | |
| 37 return m.group(2) | |
| 38 | |
| 39 @staticmethod | |
| 40 def _GetExtensionId(extension_info): | |
| 41 if 'url' not in extension_info: | |
| 42 return None | |
| 43 return ExtensionDictBackend._ExtractExtensionId(extension_info['url']) | |
| 44 | |
| 45 def _CreateExtensionObject(self, extension_id): | |
| 46 extension_info = self._FindExtensionInfo(extension_id) | |
| 47 if not extension_info or not 'webSocketDebuggerUrl' in extension_info: | |
| 48 raise ExtensionNotFoundException() | |
| 49 return extension_page.ExtensionPage( | |
| 50 extension_id, | |
| 51 extension_info['url'], | |
| 52 self._CreateInspectorBackendForDebuggerUrl( | |
| 53 extension_info['webSocketDebuggerUrl'])) | |
| 54 | |
| 55 def _CreateInspectorBackendForDebuggerUrl(self, debugger_url): | |
| 56 return inspector_backend.InspectorBackend(self._browser_backend.browser, | |
| 57 self._browser_backend, | |
| 58 debugger_url) | |
| 59 | |
| 60 def _FindExtensionInfo(self, extension_id): | |
| 61 for extension_info in self.GetExtensionInfoList(): | |
| 62 if self._GetExtensionId(extension_info) == extension_id: | |
| 63 return extension_info | |
| 64 return None | |
| 65 | |
| 66 def GetExtensionInfoList(self, timeout=None): | |
| 67 data = self._browser_backend.Request('', timeout=timeout) | |
| 68 return self._FilterExtensions(json.loads(data)) | |
| 69 | |
| 70 def _FilterExtensions(self, all_pages): | |
| 71 return [page_info for page_info in all_pages | |
| 72 if page_info['url'].startswith('chrome-extension://')] | |
| 73 | |
| 74 def GetExtensionIds(self): | |
| 75 return map(self._GetExtensionId, self.GetExtensionInfoList()) | |
| OLD | NEW |