• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 The Chromium OS 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"""An interface to access the local browser facade."""
6
7import logging
8
9class BrowserFacadeNativeError(Exception):
10    """Error in BrowserFacadeNative."""
11    pass
12
13
14class BrowserFacadeNative(object):
15    """Facade to access the browser-related functionality."""
16
17    def __init__(self, resource):
18        """Initializes the USB facade.
19
20        @param resource: A FacadeResource object.
21
22        """
23        self._resource = resource
24
25
26    def start_custom_chrome(self, kwargs):
27        """Start a custom Chrome with given arguments.
28
29        @param kwargs: A dict of keyword arguments passed to Chrome.
30        @return: True on success, False otherwise.
31
32        """
33        return self._resource.start_custom_chrome(kwargs)
34
35
36    def start_default_chrome(self, restart=False, extra_browser_args=None,
37                             disable_arc=False):
38        """Start the default Chrome.
39
40        @param restart: True to start Chrome without clearing previous state.
41        @param extra_browser_args: A list containing extra browser args passed
42                                   to Chrome in addition to default ones.
43        @param disable_arc: True to disable ARC++.
44        @return: True on success, False otherwise.
45
46        """
47        return self._resource.start_default_chrome(restart, extra_browser_args,
48                                                   disable_arc)
49
50
51    def set_http_server_directories(self, directories):
52        """Starts an HTTP server.
53
54        @param directories: Directories to start serving.
55
56        @return True on success. False otherwise.
57
58        """
59        return self._resource.set_http_server_directories(directories)
60
61
62    def http_server_url_of(self, fullpath):
63        """Converts a path to a URL.
64
65        @param fullpath: String containing the full path to the content.
66
67        @return the URL for the provided path.
68
69        """
70        return self._resource.http_server_url_of(fullpath)
71
72
73    def new_tab(self, url):
74        """Opens a new tab and loads URL.
75
76        @param url: The URL to load.
77        @return a str, the tab descriptor of the opened tab.
78
79        """
80        logging.debug('Load URL %s', url)
81        return self._resource.load_url(url)
82
83
84    def close_tab(self, tab_descriptor):
85        """Closes a previously opened tab.
86
87        @param tab_descriptor: Indicate which tab to be closed.
88
89        """
90        tab = self._resource.get_tab_by_descriptor(tab_descriptor)
91        logging.debug('Closing URL %s', tab.url)
92        self._resource.close_tab(tab_descriptor)
93
94
95    def wait_for_javascript_expression(
96            self, tab_descriptor, expression, timeout):
97        """Waits for the given JavaScript expression to be True on the
98        given tab.
99
100        @param tab_descriptor: Indicate on which tab to wait for the expression.
101        @param expression: Indiate for what expression to wait.
102        @param timeout: Indicate the timeout of the expression.
103        """
104        self._resource.wait_for_javascript_expression(
105                tab_descriptor, expression, timeout)
106
107
108    def execute_javascript(self, tab_descriptor, statement, timeout):
109        """Executes a JavaScript statement on the given tab.
110
111        @param tab_descriptor: Indicate on which tab to execute the statement.
112        @param statement: Indiate what statement to execute.
113        @param timeout: Indicate the timeout of the statement.
114        """
115        self._resource.execute_javascript(
116                tab_descriptor, statement, timeout)
117
118
119    def evaluate_javascript(self, tab_descriptor, expression, timeout):
120        """Evaluates a JavaScript expression on the given tab.
121
122        @param tab_descriptor: Indicate on which tab to evaluate the expression.
123        @param expression: Indiate what expression to evaluate.
124        @param timeout: Indicate the timeout of the expression.
125        @return the JSONized result of the given expression
126        """
127        return self._resource.evaluate_javascript(
128                tab_descriptor, expression, timeout)
129
130
131    def get_tab_urls(self):
132        """Gets urls from current Chrome tabs.
133
134        @returns: A list of str objects which contain urls from current Chrome
135        tabs.
136        """
137        logging.info("Getting tab objects from Chrome...")
138        tabs = self._resource.get_tabs()
139
140        return [tab.url for tab in tabs]
141