Chromium Code Reviews| Index: tools/telemetry/telemetry/extension_dict_backend.py |
| =================================================================== |
| --- tools/telemetry/telemetry/extension_dict_backend.py (revision 0) |
| +++ tools/telemetry/telemetry/extension_dict_backend.py (revision 0) |
| @@ -0,0 +1,90 @@ |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| +import httplib |
| +import json |
| +import os |
| +import re |
| +import socket |
| +import urllib2 |
| +import weakref |
| + |
| +from telemetry import browser_gone_exception |
| +from telemetry import crx_id |
| +from telemetry import extension_page |
| +from telemetry import inspector_backend |
| + |
| +class BrowserConnectionGoneException( |
| + browser_gone_exception.BrowserGoneException): |
| + pass |
| + |
| +class ExtensionDictBackend(object): |
| + def __init__(self, browser_backend): |
| + self._browser_backend = browser_backend |
| + # Maps extension ids to ExtensionPage objects. |
| + self._extension_dict = weakref.WeakValueDictionary() |
| + |
| + def __iter__(self): |
| + return self._GetExtensionIds().__iter__() |
| + |
| + def __len__(self): |
| + return len(self._GetExtensionIds()) |
| + |
| + def __getitem__(self, extension_id): |
| + extension_object = self._extension_dict.get(extension_id) |
| + if not extension_object: |
| + extension_object = self._CreateExtensionObject(extension_id) |
| + if extension_object: |
| + self._extension_dict[extension_id] = extension_object |
| + return extension_object |
| + |
| + @staticmethod |
| + def _ExtractExtensionId(url): |
| + m = re.match(r"(chrome-extension://)([^/]+)", url) |
| + assert m |
| + return m.group(2) |
| + |
| + @staticmethod |
| + def _GetExtensionId(extension_info): |
| + if 'url' not in extension_info: |
| + return None |
| + return ExtensionDictBackend._ExtractExtensionId(extension_info['url']) |
| + |
| + def _CreateExtensionObject(self, extension_id): |
| + extension_info = self._FindExtensionInfo(extension_id) |
| + if not extension_info: |
| + return None |
| + return extension_page.ExtensionPage( |
| + self._CreateInspectorBackendForDebuggerUrl( |
| + extension_info['webSocketDebuggerUrl'])) |
| + |
| + def _CreateInspectorBackendForDebuggerUrl(self, debugger_url): |
| + return inspector_backend.InspectorBackend(self._browser_backend.browser, |
| + self._browser_backend, |
| + debugger_url) |
| + |
| + def _FindExtensionInfo(self, extension_id): |
| + for extension_info in self._GetExtensionInfoList(): |
| + if self._GetExtensionId(extension_info) == extension_id: |
| + return extension_info |
| + return None |
| + |
| + def _GetExtensionInfoList(self, timeout=None): |
| + try: |
| + data = self._browser_backend.Request('', timeout=timeout) |
| + return self._FilterExtensions(json.loads(data)) |
| + except (socket.error, httplib.BadStatusLine, urllib2.URLError): |
| + if not self._browser_backend.IsBrowserRunning(): |
| + raise browser_gone_exception.BrowserGoneException() |
| + raise BrowserConnectionGoneException() |
| + |
| + def _FilterExtensions(self, all_extensions): |
| + return [page_info for page_info in all_extensions |
| + if page_info['url'].startswith('chrome-extension://')] |
| + |
| + def _GetExtensionIds(self): |
| + return map(self._GetExtensionId, self._GetExtensionInfoList()) |
| + |
| + def GetExtensionByPath(self, extension_path): |
| + abs_path = os.path.abspath(extension_path) |
| + return self.__getitem__(crx_id.GetCRXAppID(abs_path)) |
|
nduca
2013/02/01 00:25:50
No. You shouldn't do this. That assumes that all e
achuithb
2013/02/06 00:23:03
Done.
|
| Property changes on: tools/telemetry/telemetry/extension_dict_backend.py |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |