OLD | NEW |
| (Empty) |
1 # Copyright (c) 2012 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 import httplib | |
5 import json | |
6 import re | |
7 import socket | |
8 import urllib2 | |
9 import weakref | |
10 | |
11 from telemetry import browser_gone_exception | |
12 from telemetry import extension_page | |
13 from telemetry import inspector_backend | |
14 | |
15 class ExtensionNotFoundException(Exception): | |
16 pass | |
17 | |
18 class ExtensionDictBackend(object): | |
19 def __init__(self, browser_backend): | |
20 self._browser_backend = browser_backend | |
21 # Maps extension ids to ExtensionPage objects. | |
22 self._extension_dict = weakref.WeakValueDictionary() | |
23 | |
24 def __getitem__(self, extension_id): | |
25 extension_object = self._extension_dict.get(extension_id) | |
26 if not extension_object: | |
27 extension_object = self._CreateExtensionObject(extension_id) | |
28 assert extension_object | |
29 self._extension_dict[extension_id] = extension_object | |
30 return extension_object | |
31 | |
32 def __contains__(self, extension_id): | |
33 return extension_id in self._GetExtensionIds() | |
34 | |
35 @staticmethod | |
36 def _ExtractExtensionId(url): | |
37 m = re.match(r"(chrome-extension://)([^/]+)", url) | |
38 assert m | |
39 return m.group(2) | |
40 | |
41 @staticmethod | |
42 def _GetExtensionId(extension_info): | |
43 if 'url' not in extension_info: | |
44 return None | |
45 return ExtensionDictBackend._ExtractExtensionId(extension_info['url']) | |
46 | |
47 def _CreateExtensionObject(self, extension_id): | |
48 extension_info = self._FindExtensionInfo(extension_id) | |
49 if not extension_info or not 'webSocketDebuggerUrl' in extension_info: | |
50 raise ExtensionNotFoundException() | |
51 return extension_page.ExtensionPage( | |
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 try: | |
68 data = self._browser_backend.Request('', timeout=timeout) | |
69 return self._FilterExtensions(json.loads(data)) | |
70 except (socket.error, httplib.BadStatusLine, urllib2.URLError): | |
71 if not self._browser_backend.IsBrowserRunning(): | |
72 raise browser_gone_exception.BrowserGoneException() | |
73 raise browser_gone_exception.BrowserConnectionGoneException() | |
74 | |
75 def _FilterExtensions(self, all_pages): | |
76 return [page_info for page_info in all_pages | |
77 if page_info['url'].startswith('chrome-extension://')] | |
78 | |
79 def _GetExtensionIds(self): | |
80 return map(self._GetExtensionId, self._GetExtensionInfoList()) | |
OLD | NEW |