1// Copyright (c) 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
5#ifndef CHROME_TEST_CHROMEDRIVER_CHROME_WEB_VIEW_H_
6#define CHROME_TEST_CHROMEDRIVER_CHROME_WEB_VIEW_H_
7
8#include <list>
9#include <string>
10#include <vector>
11
12#include "base/memory/scoped_ptr.h"
13
14namespace base {
15class DictionaryValue;
16class FilePath;
17class ListValue;
18class TimeDelta;
19class Value;
20}
21
22class DevToolsClient;
23struct Geoposition;
24class JavaScriptDialogManager;
25struct KeyEvent;
26struct MouseEvent;
27struct TouchEvent;
28class Status;
29
30class WebView {
31 public:
32  virtual ~WebView() {}
33
34  // Return the id for this WebView.
35  virtual std::string GetId() = 0;
36
37  // Return true if the web view was crashed.
38  virtual bool WasCrashed() = 0;
39
40  // Make DevToolsCient connect to DevTools if it is disconnected.
41  virtual Status ConnectIfNecessary() = 0;
42
43  // Handles events that have been received but not yet handled.
44  virtual Status HandleReceivedEvents() = 0;
45
46  // Load a given URL in the main frame.
47  virtual Status Load(const std::string& url) = 0;
48
49  // Reload the current page.
50  virtual Status Reload() = 0;
51
52  // Evaluates a JavaScript expression in a specified frame and returns
53  // the result. |frame| is a frame ID or an empty string for the main frame.
54  // If the expression evaluates to a element, it will be bound to a unique ID
55  // (per frame) and the ID will be returned.
56  // |result| will never be NULL on success.
57  virtual Status EvaluateScript(const std::string& frame,
58                                const std::string& expression,
59                                scoped_ptr<base::Value>* result) = 0;
60
61  // Calls a JavaScript function in a specified frame with the given args and
62  // returns the result. |frame| is a frame ID or an empty string for the main
63  // frame. |args| may contain IDs that refer to previously returned elements.
64  // These will be translated back to their referred objects before invoking the
65  // function.
66  // |result| will never be NULL on success.
67  virtual Status CallFunction(const std::string& frame,
68                              const std::string& function,
69                              const base::ListValue& args,
70                              scoped_ptr<base::Value>* result) = 0;
71
72  // Calls a JavaScript function in a specified frame with the given args and
73  // two callbacks. The first may be invoked with a value to return to the user.
74  // The second may be used to report an error. This function waits until
75  // one of the callbacks is invoked or the timeout occurs.
76  // |result| will never be NULL on success.
77  virtual Status CallAsyncFunction(const std::string& frame,
78                                   const std::string& function,
79                                   const base::ListValue& args,
80                                   const base::TimeDelta& timeout,
81                                   scoped_ptr<base::Value>* result) = 0;
82
83  // Same as |CallAsyncFunction|, except no additional error callback is passed
84  // to the function. Also, |kJavaScriptError| or |kScriptTimeout| is used
85  // as the error code instead of |kUnknownError| in appropriate cases.
86  // |result| will never be NULL on success.
87  virtual Status CallUserAsyncFunction(const std::string& frame,
88                                       const std::string& function,
89                                       const base::ListValue& args,
90                                       const base::TimeDelta& timeout,
91                                       scoped_ptr<base::Value>* result) = 0;
92
93  // Gets the frame ID for a frame element returned by invoking the given
94  // JavaScript function. |frame| is a frame ID or an empty string for the main
95  // frame.
96  virtual Status GetFrameByFunction(const std::string& frame,
97                                    const std::string& function,
98                                    const base::ListValue& args,
99                                    std::string* out_frame) = 0;
100
101  // Dispatch a sequence of mouse events.
102  virtual Status DispatchMouseEvents(const std::list<MouseEvent>& events,
103                                     const std::string& frame) = 0;
104
105  // Dispatch a single touch event.
106  virtual Status DispatchTouchEvent(const TouchEvent& event) = 0;
107
108  // Dispatch a sequence of touch events.
109  virtual Status DispatchTouchEvents(const std::list<TouchEvent>& events) = 0;
110
111  // Dispatch a sequence of key events.
112  virtual Status DispatchKeyEvents(const std::list<KeyEvent>& events) = 0;
113
114  // Return all the cookies visible to the current page.
115  virtual Status GetCookies(scoped_ptr<base::ListValue>* cookies) = 0;
116
117  // Delete the cookie with the given name.
118  virtual Status DeleteCookie(const std::string& name,
119                              const std::string& url) = 0;
120
121  // Waits until all pending navigations have completed in the given frame.
122  // If |frame_id| is "", waits for navigations on the main frame.
123  // If a modal dialog appears while waiting, kUnexpectedAlertOpen will be
124  // returned.
125  // If timeout is exceeded, will return a timeout status.
126  // If |stop_load_on_timeout| is true, will attempt to stop the page load on
127  // timeout before returning the timeout status.
128  virtual Status WaitForPendingNavigations(const std::string& frame_id,
129                                           const base::TimeDelta& timeout,
130                                           bool stop_load_on_timeout) = 0;
131
132  // Returns whether the frame is pending navigation.
133  virtual Status IsPendingNavigation(
134      const std::string& frame_id, bool* is_pending) = 0;
135
136  // Returns the JavaScriptDialogManager. Never null.
137  virtual JavaScriptDialogManager* GetJavaScriptDialogManager() = 0;
138
139  // Overrides normal geolocation with a given geoposition.
140  virtual Status OverrideGeolocation(const Geoposition& geoposition) = 0;
141
142  // Captures the visible portions of the web view as a base64-encoded PNG.
143  virtual Status CaptureScreenshot(std::string* screenshot) = 0;
144
145  // Set files in a file input element.
146  // |element| is the WebElement JSON Object of the input element.
147  virtual Status SetFileInputFiles(
148      const std::string& frame,
149      const base::DictionaryValue& element,
150      const std::vector<base::FilePath>& files) = 0;
151
152  // Take a heap snapshot which can build up a graph of Javascript objects.
153  // A raw heap snapshot is in JSON format:
154  //  1. A meta data element "snapshot" about how to parse data elements.
155  //  2. Data elements: "nodes", "edges", "strings".
156  virtual Status TakeHeapSnapshot(scoped_ptr<base::Value>* snapshot) = 0;
157
158  // Start recording Javascript CPU Profile.
159  virtual Status StartProfile() = 0;
160
161  // Stop recording Javascript CPU Profile and returns a graph of
162  // CPUProfile objects. The format for the captured profile is defined
163  // (by DevTools) in protocol.json.
164  virtual Status EndProfile(scoped_ptr<base::Value>* profile_data) = 0;
165};
166
167#endif  // CHROME_TEST_CHROMEDRIVER_CHROME_WEB_VIEW_H_
168