- add sources.
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / wpr_server.py
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
5 from telemetry.core import util
6
7 # TODO(tonyg): Move webpagereplay.py to a common location.
8 util.AddDirToPythonPath(
9     util.GetChromiumSrcDir(), 'chrome', 'test', 'functional')
10 import webpagereplay  # pylint: disable=F0401
11
12 def GetChromeFlags(replay_host, http_port, https_port):
13   return webpagereplay.GetChromeFlags(replay_host, http_port, https_port)
14
15 class ReplayServer(object):
16   def __init__(self, browser_backend, path, is_record_mode, is_append_mode,
17                make_javascript_deterministic):
18     self._browser_backend = browser_backend
19     self._forwarder = None
20     self._web_page_replay = None
21     self._is_record_mode = is_record_mode
22     self._is_append_mode = is_append_mode
23
24     self._forwarder = browser_backend.CreateForwarder(
25         util.PortPair(browser_backend.webpagereplay_local_http_port,
26                       browser_backend.webpagereplay_remote_http_port),
27         util.PortPair(browser_backend.webpagereplay_local_https_port,
28                       browser_backend.webpagereplay_remote_https_port))
29
30     wpr_args = browser_backend.browser_options.extra_wpr_args
31     if self._is_record_mode:
32       if self._is_append_mode:
33         wpr_args.append('--append')
34       else:
35         wpr_args.append('--record')
36     if not make_javascript_deterministic:
37       wpr_args.append('--inject_scripts=')
38     browser_backend.AddReplayServerOptions(wpr_args)
39     self._web_page_replay = webpagereplay.ReplayServer(
40         path,
41         browser_backend.WEBPAGEREPLAY_HOST,
42         browser_backend.webpagereplay_local_http_port,
43         browser_backend.webpagereplay_local_https_port,
44         wpr_args)
45     # Remove --no-dns_forwarding if it wasn't explicitly requested by backend.
46     if '--no-dns_forwarding' not in wpr_args:
47       self._web_page_replay.replay_options.remove('--no-dns_forwarding')
48     self._web_page_replay.StartServer()
49
50   def __enter__(self):
51     return self
52
53   def __exit__(self, *args):
54     self.Close()
55
56   def Close(self):
57     if self._forwarder:
58       self._forwarder.Close()
59       self._forwarder = None
60     if self._web_page_replay:
61       self._web_page_replay.StopServer()
62       self._web_page_replay = None