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
5from telemetry.internal.backends.chrome_inspector import inspector_websocket
6from telemetry.internal.platform import system_info
7from telemetry.internal.util import camel_case
8
9
10class SystemInfoBackend(object):
11  def __init__(self, devtools_port, devtools_page=None):
12    self._port = devtools_port
13    self._page = devtools_page
14
15  def GetSystemInfo(self, timeout=10):
16    req = {'method': 'SystemInfo.getInfo'}
17    websocket = inspector_websocket.InspectorWebsocket()
18    try:
19      if self._page:
20        websocket.Connect('ws://127.0.0.1:%i/devtools/page/%i' %
21                          (self._port, self._page))
22      else:
23        websocket.Connect('ws://127.0.0.1:%i/devtools/browser' % self._port)
24      res = websocket.SyncRequest(req, timeout)
25    finally:
26      websocket.Disconnect()
27    if 'error' in res:
28      return None
29    return system_info.SystemInfo.FromDict(
30        camel_case.ToUnderscore(res['result']))
31