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.
5 #include "chrome/test/automation/tab_proxy.h"
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/logging.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/platform_thread.h"
14 #include "chrome/common/automation_constants.h"
15 #include "chrome/common/automation_messages.h"
16 #include "chrome/test/automation/automation_proxy.h"
17 #include "chrome/test/automation/browser_proxy.h"
20 TabProxy::TabProxy(AutomationMessageSender
* sender
,
21 AutomationHandleTracker
* tracker
,
23 : AutomationResourceProxy(tracker
, sender
, handle
) {
26 bool TabProxy::GetTabTitle(std::wstring
* title
) const {
35 int tab_title_size_response
= 0;
37 bool succeeded
= sender_
->Send(
38 new AutomationMsg_TabTitle(handle_
, &tab_title_size_response
, title
));
42 bool TabProxy::GetTabIndex(int* index
) const {
51 return sender_
->Send(new AutomationMsg_TabIndex(handle_
, index
));
54 int TabProxy::FindInPage(const std::wstring
& search_string
,
55 FindInPageDirection forward
,
56 FindInPageCase match_case
,
62 AutomationMsg_Find_Params params
;
63 params
.search_string
= base::WideToUTF16Hack(search_string
);
64 params
.find_next
= find_next
;
65 params
.match_case
= (match_case
== CASE_SENSITIVE
);
66 params
.forward
= (forward
== FWD
);
70 bool succeeded
= sender_
->Send(new AutomationMsg_Find(handle_
,
81 AutomationMsg_NavigationResponseValues
TabProxy::NavigateToURL(
83 return NavigateToURLBlockUntilNavigationsComplete(url
, 1);
86 AutomationMsg_NavigationResponseValues
87 TabProxy::NavigateToURLBlockUntilNavigationsComplete(
88 const GURL
& url
, int number_of_navigations
) {
90 return AUTOMATION_MSG_NAVIGATION_ERROR
;
92 AutomationMsg_NavigationResponseValues navigate_response
=
93 AUTOMATION_MSG_NAVIGATION_ERROR
;
95 sender_
->Send(new AutomationMsg_NavigateToURLBlockUntilNavigationsComplete(
96 handle_
, url
, number_of_navigations
, &navigate_response
));
98 return navigate_response
;
101 AutomationMsg_NavigationResponseValues
TabProxy::GoBack() {
102 return GoBackBlockUntilNavigationsComplete(1);
105 AutomationMsg_NavigationResponseValues
106 TabProxy::GoBackBlockUntilNavigationsComplete(int number_of_navigations
) {
108 return AUTOMATION_MSG_NAVIGATION_ERROR
;
110 AutomationMsg_NavigationResponseValues navigate_response
=
111 AUTOMATION_MSG_NAVIGATION_ERROR
;
112 sender_
->Send(new AutomationMsg_GoBackBlockUntilNavigationsComplete(
113 handle_
, number_of_navigations
, &navigate_response
));
114 return navigate_response
;
117 AutomationMsg_NavigationResponseValues
TabProxy::GoForward() {
118 return GoForwardBlockUntilNavigationsComplete(1);
121 AutomationMsg_NavigationResponseValues
122 TabProxy::GoForwardBlockUntilNavigationsComplete(
123 int number_of_navigations
) {
125 return AUTOMATION_MSG_NAVIGATION_ERROR
;
127 AutomationMsg_NavigationResponseValues navigate_response
=
128 AUTOMATION_MSG_NAVIGATION_ERROR
;
129 sender_
->Send(new AutomationMsg_GoForwardBlockUntilNavigationsComplete(
130 handle_
, number_of_navigations
, &navigate_response
));
131 return navigate_response
;
134 AutomationMsg_NavigationResponseValues
TabProxy::Reload() {
136 return AUTOMATION_MSG_NAVIGATION_ERROR
;
138 AutomationMsg_NavigationResponseValues navigate_response
=
139 AUTOMATION_MSG_NAVIGATION_ERROR
;
140 sender_
->Send(new AutomationMsg_Reload(handle_
, &navigate_response
));
141 return navigate_response
;
144 bool TabProxy::GetCurrentURL(GURL
* url
) const {
153 bool succeeded
= false;
154 sender_
->Send(new AutomationMsg_TabURL(handle_
, &succeeded
, url
));
158 bool TabProxy::NavigateToURLAsync(const GURL
& url
) {
163 sender_
->Send(new AutomationMsg_NavigationAsync(handle_
,
169 bool TabProxy::ExecuteAndExtractString(const std::wstring
& frame_xpath
,
170 const std::wstring
& jscript
,
171 std::wstring
* string_value
) {
172 scoped_ptr
<base::Value
> root(ExecuteAndExtractValue(frame_xpath
, jscript
));
176 DCHECK(root
->IsType(base::Value::TYPE_LIST
));
177 base::Value
* value
= NULL
;
178 bool succeeded
= static_cast<base::ListValue
*>(root
.get())->Get(0, &value
);
180 base::string16 read_value
;
181 succeeded
= value
->GetAsString(&read_value
);
183 // TODO(viettrungluu): remove conversion. (But should |jscript| be UTF-8?)
184 *string_value
= base::UTF16ToWideHack(read_value
);
190 bool TabProxy::ExecuteAndExtractBool(const std::wstring
& frame_xpath
,
191 const std::wstring
& jscript
,
193 scoped_ptr
<base::Value
> root(ExecuteAndExtractValue(frame_xpath
, jscript
));
197 bool read_value
= false;
198 DCHECK(root
->IsType(base::Value::TYPE_LIST
));
199 base::Value
* value
= NULL
;
200 bool succeeded
= static_cast<base::ListValue
*>(root
.get())->Get(0, &value
);
202 succeeded
= value
->GetAsBoolean(&read_value
);
204 *bool_value
= read_value
;
210 bool TabProxy::ExecuteAndExtractInt(const std::wstring
& frame_xpath
,
211 const std::wstring
& jscript
,
213 scoped_ptr
<base::Value
> root(ExecuteAndExtractValue(frame_xpath
, jscript
));
218 DCHECK(root
->IsType(base::Value::TYPE_LIST
));
219 base::Value
* value
= NULL
;
220 bool succeeded
= static_cast<base::ListValue
*>(root
.get())->Get(0, &value
);
222 succeeded
= value
->GetAsInteger(&read_value
);
224 *int_value
= read_value
;
230 base::Value
* TabProxy::ExecuteAndExtractValue(const std::wstring
& frame_xpath
,
231 const std::wstring
& jscript
) {
236 if (!sender_
->Send(new AutomationMsg_DomOperation(handle_
, frame_xpath
,
240 // Wrap |json| in an array before deserializing because valid JSON has an
241 // array or an object as the root.
245 JSONStringValueSerializer
deserializer(json
);
246 return deserializer
.Deserialize(NULL
, NULL
);
249 bool TabProxy::GetCookies(const GURL
& url
, std::string
* cookies
) {
254 return sender_
->Send(new AutomationMsg_GetCookies(url
, handle_
, &size
,
258 bool TabProxy::GetCookieByName(const GURL
& url
,
259 const std::string
& name
,
260 std::string
* cookie
) {
262 if (!GetCookies(url
, &cookies
))
265 std::string namestr
= name
+ "=";
266 std::string::size_type idx
= cookies
.find(namestr
);
267 if (idx
!= std::string::npos
) {
268 cookies
.erase(0, idx
+ namestr
.length());
269 *cookie
= cookies
.substr(0, cookies
.find(";"));
277 bool TabProxy::Close() {
281 bool TabProxy::Close(bool wait_until_closed
) {
285 bool succeeded
= false;
286 sender_
->Send(new AutomationMsg_CloseTab(handle_
, wait_until_closed
,
291 bool TabProxy::WaitForInfoBarCount(size_t target_count
) {
295 bool success
= false;
296 return sender_
->Send(new AutomationMsg_WaitForInfoBarCount(
297 handle_
, target_count
, &success
)) && success
;
300 bool TabProxy::OverrideEncoding(const std::string
& encoding
) {
304 bool succeeded
= false;
305 sender_
->Send(new AutomationMsg_OverrideEncoding(handle_
, encoding
,
310 void TabProxy::SelectAll() {
311 sender_
->Send(new AutomationMsg_SelectAll(handle_
));
314 void TabProxy::Cut() {
315 sender_
->Send(new AutomationMsg_Cut(handle_
));
318 void TabProxy::Copy() {
319 sender_
->Send(new AutomationMsg_Copy(handle_
));
322 void TabProxy::Paste() {
323 sender_
->Send(new AutomationMsg_Paste(handle_
));
326 void TabProxy::ReloadAsync() {
327 sender_
->Send(new AutomationMsg_ReloadAsync(handle_
));
330 void TabProxy::StopAsync() {
331 sender_
->Send(new AutomationMsg_StopAsync(handle_
));
334 void TabProxy::JavaScriptStressTestControl(int cmd
, int param
) {
338 sender_
->Send(new AutomationMsg_JavaScriptStressTestControl(
339 handle_
, cmd
, param
));
342 void TabProxy::AddObserver(TabProxyDelegate
* observer
) {
343 base::AutoLock
lock(list_lock_
);
344 observers_list_
.AddObserver(observer
);
347 void TabProxy::RemoveObserver(TabProxyDelegate
* observer
) {
348 base::AutoLock
lock(list_lock_
);
349 observers_list_
.RemoveObserver(observer
);
352 // Called on Channel background thread, if TabMessages filter is installed.
353 bool TabProxy::OnMessageReceived(const IPC::Message
& message
) {
354 base::AutoLock
lock(list_lock_
);
355 FOR_EACH_OBSERVER(TabProxyDelegate
, observers_list_
,
356 OnMessageReceived(this, message
));
360 void TabProxy::OnChannelError() {
361 base::AutoLock
lock(list_lock_
);
362 FOR_EACH_OBSERVER(TabProxyDelegate
, observers_list_
, OnChannelError(this));
365 TabProxy::~TabProxy() {}
367 void TabProxy::FirstObjectAdded() {
371 void TabProxy::LastObjectRemoved() {