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 uuid
5import sys
6
7from py_utils import cloud_storage  # pylint: disable=import-error
8
9from telemetry import decorators
10from telemetry.internal.backends import app_backend
11from telemetry.internal.browser import web_contents
12from telemetry.internal.platform import profiling_controller_backend
13
14
15class ExtensionsNotSupportedException(Exception):
16  pass
17
18
19class BrowserBackend(app_backend.AppBackend):
20  """A base class for browser backends."""
21
22  def __init__(self, platform_backend, supports_extensions, browser_options,
23               tab_list_backend):
24    assert browser_options.browser_type
25    super(BrowserBackend, self).__init__(
26        browser_options.browser_type, platform_backend)
27    self._supports_extensions = supports_extensions
28    self.browser_options = browser_options
29    self._tab_list_backend_class = tab_list_backend
30    self._profiling_controller_backend = (
31        profiling_controller_backend.ProfilingControllerBackend(
32          platform_backend, self))
33
34  def SetBrowser(self, browser):
35    super(BrowserBackend, self).SetApp(app=browser)
36
37  @property
38  def log_file_path(self):
39    # Specific browser backend is responsible for overriding this properly.
40    raise NotImplementedError
41
42  def GetLogFileContents(self):
43    if not self.log_file_path:
44      return 'No log file'
45    with file(self.log_file_path) as f:
46      return f.read()
47
48  def UploadLogsToCloudStorage(self):
49    """ Uploading log files produce by this browser instance to cloud storage.
50
51    Check supports_uploading_logs before calling this method.
52    """
53    assert self.supports_uploading_logs
54    remote_path = (self.browser_options.logs_cloud_remote_path or
55                   'log_%s' % uuid.uuid4())
56    cloud_url = cloud_storage.Insert(
57        bucket=self.browser_options.logs_cloud_bucket,
58        remote_path=remote_path,
59        local_path=self.log_file_path)
60    sys.stderr.write('Uploading browser log to %s\n' % cloud_url)
61
62  @property
63  def browser(self):
64    return self.app
65
66  @property
67  def profiling_controller_backend(self):
68    return self._profiling_controller_backend
69
70  @property
71  def browser_type(self):
72    return self.app_type
73
74  @property
75  def supports_uploading_logs(self):
76    # Specific browser backend is responsible for overriding this properly.
77    return False
78
79  @property
80  def supports_extensions(self):
81    """True if this browser backend supports extensions."""
82    return self._supports_extensions
83
84  @property
85  def supports_tab_control(self):
86    raise NotImplementedError()
87
88  @property
89  @decorators.Cache
90  def tab_list_backend(self):
91    return self._tab_list_backend_class(self)
92
93  @property
94  def supports_tracing(self):
95    raise NotImplementedError()
96
97  @property
98  def supports_system_info(self):
99    return False
100
101  def StartTracing(self, trace_options,
102                   timeout=web_contents.DEFAULT_WEB_CONTENTS_TIMEOUT):
103    raise NotImplementedError()
104
105  def StopTracing(self):
106    raise NotImplementedError()
107
108  def CollectTracingData(self, trace_data_builder):
109    raise NotImplementedError()
110
111  def Start(self):
112    raise NotImplementedError()
113
114  def IsBrowserRunning(self):
115    raise NotImplementedError()
116
117  def IsAppRunning(self):
118    return self.IsBrowserRunning()
119
120  def GetStandardOutput(self):
121    raise NotImplementedError()
122
123  def GetStackTrace(self):
124    raise NotImplementedError()
125
126  def GetMostRecentMinidumpPath(self):
127    raise NotImplementedError()
128
129  def GetAllMinidumpPaths(self):
130    raise NotImplementedError()
131
132  def GetAllUnsymbolizedMinidumpPaths(self):
133    raise NotImplementedError()
134
135  def SymbolizeMinidump(self, minidump_path):
136    raise NotImplementedError()
137
138  def GetSystemInfo(self):
139    raise NotImplementedError()
140
141  @property
142  def supports_memory_dumping(self):
143    return False
144
145  def DumpMemory(self, timeout=web_contents.DEFAULT_WEB_CONTENTS_TIMEOUT):
146    raise NotImplementedError()
147
148  @property
149  def supports_overriding_memory_pressure_notifications(self):
150    return False
151
152  def SetMemoryPressureNotificationsSuppressed(
153      self, suppressed, timeout=web_contents.DEFAULT_WEB_CONTENTS_TIMEOUT):
154    raise NotImplementedError()
155
156  def SimulateMemoryPressureNotification(
157      self, pressure_level, timeout=web_contents.DEFAULT_WEB_CONTENTS_TIMEOUT):
158    raise NotImplementedError()
159
160  @property
161  def supports_cpu_metrics(self):
162    raise NotImplementedError()
163
164  @property
165  def supports_memory_metrics(self):
166    raise NotImplementedError()
167
168  @property
169  def supports_power_metrics(self):
170    raise NotImplementedError()
171