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#ifndef CHROME_TEST_AUTOMATION_TAB_PROXY_H_
6#define CHROME_TEST_AUTOMATION_TAB_PROXY_H_
7
8#include "build/build_config.h"  // NOLINT
9
10#if defined(OS_WIN)
11#include <wtypes.h>  // NOLINT
12#endif
13
14#include <string>
15#include <vector>
16
17#include "base/compiler_specific.h"
18#include "base/memory/ref_counted.h"
19#include "base/observer_list.h"
20#include "chrome/common/automation_constants.h"
21#include "chrome/test/automation/automation_handle_tracker.h"
22#include "content/public/browser/save_page_type.h"
23#include "content/public/common/page_type.h"
24#include "content/public/common/security_style.h"
25#include "net/cert/cert_status_flags.h"
26#include "ui/base/window_open_disposition.h"
27#include "ui/events/keycodes/keyboard_codes.h"
28
29class BrowserProxy;
30class GURL;
31
32namespace IPC {
33class Message;
34}
35
36namespace base {
37class FilePath;
38class Value;
39}
40
41class TabProxy : public AutomationResourceProxy {
42 public:
43  class TabProxyDelegate {
44   public:
45    virtual bool OnMessageReceived(TabProxy* tab, const IPC::Message& msg) {
46      return false;
47    }
48    virtual void OnChannelError(TabProxy* tab) {}
49
50   protected:
51    virtual ~TabProxyDelegate() {}
52  };
53
54  TabProxy(AutomationMessageSender* sender,
55           AutomationHandleTracker* tracker,
56           int handle);
57
58  // Gets the current url of the tab.
59  bool GetCurrentURL(GURL* url) const WARN_UNUSED_RESULT;
60
61  // Gets the title of the tab.
62  bool GetTabTitle(std::wstring* title) const WARN_UNUSED_RESULT;
63
64  // Gets the tabstrip index of the tab.
65  bool GetTabIndex(int* index) const WARN_UNUSED_RESULT;
66
67  // Executes a javascript in a frame's context whose xpath is provided as the
68  // first parameter and extract the values from the resulting json string.
69  // Examples:
70  // jscript = "window.domAutomationController.send('string');"
71  // will result in value = "string"
72  // jscript = "window.domAutomationController.send(24);"
73  // will result in value = 24
74  // NOTE: If this is called from a ui test, |dom_automation_enabled_| must be
75  // set to true for these functions to work.
76  bool ExecuteAndExtractString(const std::wstring& frame_xpath,
77                               const std::wstring& jscript,
78                               std::wstring* value) WARN_UNUSED_RESULT;
79  bool ExecuteAndExtractBool(const std::wstring& frame_xpath,
80                             const std::wstring& jscript,
81                             bool* value) WARN_UNUSED_RESULT;
82  bool ExecuteAndExtractInt(const std::wstring& frame_xpath,
83                            const std::wstring& jscript,
84                            int* value) WARN_UNUSED_RESULT;
85
86  // Navigates to a url. This method accepts the same kinds of URL input that
87  // can be passed to Chrome on the command line. This is a synchronous call and
88  // hence blocks until the navigation completes.
89  AutomationMsg_NavigationResponseValues NavigateToURL(
90      const GURL& url) WARN_UNUSED_RESULT;
91
92  // Navigates to a url. This method accepts the same kinds of URL input that
93  // can be passed to Chrome on the command line. This is a synchronous call and
94  // hence blocks until the |number_of_navigations| navigations complete.
95  AutomationMsg_NavigationResponseValues
96      NavigateToURLBlockUntilNavigationsComplete(
97          const GURL& url, int number_of_navigations) WARN_UNUSED_RESULT;
98
99
100  // Navigates to a url. This is an asynchronous version of NavigateToURL.
101  // The function returns immediately after sending the LoadURL notification
102  // to the browser.
103  // TODO(vibhor): Add a callback if needed in future.
104  // TODO(mpcomplete): If the navigation results in an auth challenge, the
105  // TabProxy we attach won't know about it.  See bug 666730.
106  bool NavigateToURLAsync(const GURL& url) WARN_UNUSED_RESULT;
107
108  // Equivalent to hitting the Back button. This is a synchronous call and
109  // hence blocks until the navigation completes.
110  AutomationMsg_NavigationResponseValues GoBack() WARN_UNUSED_RESULT;
111
112  // Equivalent to hitting the Back button. This is a synchronous call and
113  // hence blocks until the |number_of_navigations| navigations complete.
114  AutomationMsg_NavigationResponseValues GoBackBlockUntilNavigationsComplete(
115      int number_of_navigations) WARN_UNUSED_RESULT;
116
117  // Equivalent to hitting the Forward button. This is a synchronous call and
118  // hence blocks until the navigation completes.
119  AutomationMsg_NavigationResponseValues GoForward() WARN_UNUSED_RESULT;
120
121  // Equivalent to hitting the Forward button. This is a synchronous call and
122  // hence blocks until the |number_of_navigations| navigations complete.
123  AutomationMsg_NavigationResponseValues GoForwardBlockUntilNavigationsComplete(
124      int number_of_navigations) WARN_UNUSED_RESULT;
125
126  // Equivalent to hitting the Reload button. This is a synchronous call and
127  // hence blocks until the navigation completes.
128  AutomationMsg_NavigationResponseValues Reload() WARN_UNUSED_RESULT;
129
130  // Closes the tab. This is synchronous, but does NOT block until the tab has
131  // closed, rather it blocks until the browser has initiated the close. Use
132  // Close(true) if you need to block until tab completely closes.
133  //
134  // Note that this proxy is invalid after this call.
135  bool Close() WARN_UNUSED_RESULT;
136
137  // Variant of close that allows you to specify whether you want to block
138  // until the tab has completely closed (wait_until_closed == true) or block
139  // until the browser has initiated the close (wait_until_closed = false).
140  //
141  // When a tab is closed the browser does additional work via invoke later
142  // and may wait for messages from the renderer. Supplying a value of true to
143  // this method waits until all processing is done. Be careful with this,
144  // when closing the last tab it is possible for the browser to shutdown BEFORE
145  // the tab has completely closed. In other words, this may NOT be sent for
146  // the last tab.
147  bool Close(bool wait_until_closed) WARN_UNUSED_RESULT;
148
149  // Starts a search within the current tab. The parameter |search_string|
150  // specifies what string to search for, |forward| specifies whether to search
151  // in forward direction, and |match_case| specifies case sensitivity
152  // (true=case sensitive). |find_next| specifies whether this is a new search
153  // or a continuation of the old one. |ordinal| is an optional parameter that
154  // returns the ordinal of the active match (also known as "the 7" part of
155  // "7 of 9"). A return value of -1 indicates failure.
156  int FindInPage(const std::wstring& search_string, FindInPageDirection forward,
157                 FindInPageCase match_case, bool find_next, int* ordinal);
158
159  bool GetCookies(const GURL& url, std::string* cookies) WARN_UNUSED_RESULT;
160  bool GetCookieByName(const GURL& url,
161                       const std::string& name,
162                       std::string* cookies) WARN_UNUSED_RESULT;
163
164#if defined(OS_WIN)
165  // The functions in this block are for external tabs, hence Windows only.
166
167  // The container of an externally hosted tab calls this to reflect any
168  // accelerator keys that it did not process. This gives the tab a chance
169  // to handle the keys
170  bool ProcessUnhandledAccelerator(const MSG& msg) WARN_UNUSED_RESULT;
171
172  // Ask the tab to set focus to either the first or last element on the page.
173  // When the restore_focus_to_view parameter is true, the render view
174  // associated with the current tab is informed that it is receiving focus.
175  // For external tabs only.
176  bool SetInitialFocus(bool reverse, bool restore_focus_to_view)
177      WARN_UNUSED_RESULT;
178
179  // Navigates to a url in an externally hosted tab.
180  // This method accepts the same kinds of URL input that
181  // can be passed to Chrome on the command line. This is a synchronous call and
182  // hence blocks until the navigation completes.
183  AutomationMsg_NavigationResponseValues NavigateInExternalTab(
184      const GURL& url, const GURL& referrer) WARN_UNUSED_RESULT;
185
186  AutomationMsg_NavigationResponseValues NavigateExternalTabAtIndex(
187      int index) WARN_UNUSED_RESULT;
188
189  // Posts a message to the external tab.
190  void HandleMessageFromExternalHost(const std::string& message,
191                                     const std::string& origin,
192                                     const std::string& target);
193#endif  // defined(OS_WIN)
194
195  // Sends off an asynchronous request for printing.
196  bool PrintAsync() WARN_UNUSED_RESULT;
197
198  // Waits until the infobar count is |count|.
199  // Returns true on success.
200  bool WaitForInfoBarCount(size_t count) WARN_UNUSED_RESULT;
201
202  // Uses the specified encoding to override encoding of the page in the tab.
203  bool OverrideEncoding(const std::string& encoding) WARN_UNUSED_RESULT;
204
205#if defined(OS_WIN)
206  // Resizes the tab window.
207  // The parent_window parameter allows a parent to be specified for the window
208  // passed in.
209  void Reposition(HWND window, HWND window_insert_after, int left, int top,
210                  int width, int height, int flags, HWND parent_window);
211
212  // Sends the selected context menu command to the chrome instance
213  void SendContextMenuCommand(int selected_command);
214
215  // To be called when the window hosting the tab has moved.
216  void OnHostMoved();
217#endif  // defined(OS_WIN)
218
219  // Selects all contents on the page.
220  void SelectAll();
221
222  // Edit operations on the page.
223  void Cut();
224  void Copy();
225  void Paste();
226
227  // These handlers issue asynchronous Reload, Stop and SaveAs notifications to
228  // the chrome instance.
229  void ReloadAsync();
230  void StopAsync();
231  void SaveAsAsync();
232
233  // Notify the JavaScript engine in the render to change its parameters
234  // while performing stress testing. See
235  // |ViewHostMsg_JavaScriptStressTestControl_Commands| in render_messages.h
236  // for information on the arguments.
237  void JavaScriptStressTestControl(int cmd, int param);
238
239  // Calls delegates
240  void AddObserver(TabProxyDelegate* observer);
241  void RemoveObserver(TabProxyDelegate* observer);
242  bool OnMessageReceived(const IPC::Message& message);
243  void OnChannelError();
244 protected:
245  virtual ~TabProxy();
246
247  // Called when tracking the first object. Used for reference counting
248  // purposes.
249  void FirstObjectAdded();
250
251  // Called when no longer tracking any objects. Used for reference counting
252  // purposes.
253  void LastObjectRemoved();
254
255  // Caller takes ownership over returned value.  Returns NULL on failure.
256  base::Value* ExecuteAndExtractValue(
257      const std::wstring& frame_xpath,
258      const std::wstring& jscript) WARN_UNUSED_RESULT;
259
260 private:
261  base::Lock list_lock_;  // Protects the observers_list_.
262  ObserverList<TabProxyDelegate> observers_list_;
263  DISALLOW_COPY_AND_ASSIGN(TabProxy);
264};
265
266#endif  // CHROME_TEST_AUTOMATION_TAB_PROXY_H_
267