OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/automation/tab_proxy.h" | 5 #include "chrome/test/automation/tab_proxy.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/string16.h" | 11 #include "base/string16.h" |
12 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "chrome/common/automation_constants.h" | 14 #include "chrome/common/automation_constants.h" |
15 #include "chrome/common/automation_messages.h" | 15 #include "chrome/common/automation_messages.h" |
16 #include "chrome/test/automation/automation_json_requests.h" | 16 #include "chrome/test/automation/automation_json_requests.h" |
17 #include "chrome/test/automation/automation_proxy.h" | 17 #include "chrome/test/automation/automation_proxy.h" |
18 #include "chrome/test/automation/browser_proxy.h" | 18 #include "chrome/test/automation/browser_proxy.h" |
19 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
20 | 20 |
21 TabProxy::TabProxy(AutomationMessageSender* sender, | 21 TabProxy::TabProxy(AutomationMessageSender* sender, |
22 AutomationHandleTracker* tracker, | 22 AutomationHandleTracker* tracker, |
23 int handle) | 23 int handle) |
24 : AutomationResourceProxy(tracker, sender, handle) { | 24 : AutomationResourceProxy(tracker, sender, handle) { |
25 } | 25 } |
26 | 26 |
27 scoped_refptr<BrowserProxy> TabProxy::GetParentBrowser() const { | |
28 if (!is_valid()) | |
29 return NULL; | |
30 | |
31 int browser_handle = 0; | |
32 bool success = false; | |
33 sender_->Send(new AutomationMsg_GetParentBrowserOfTab( | |
34 handle_, &browser_handle, &success)); | |
35 if (!success) | |
36 return NULL; | |
37 | |
38 BrowserProxy* browser = static_cast<BrowserProxy*>( | |
39 tracker_->GetResource(browser_handle)); | |
40 if (!browser) { | |
41 browser = new BrowserProxy(sender_, tracker_, browser_handle); | |
42 browser->AddRef(); | |
43 } | |
44 | |
45 // Since there is no scoped_refptr::attach. | |
46 scoped_refptr<BrowserProxy> result; | |
47 result.swap(&browser); | |
48 return result; | |
49 } | |
50 | |
51 bool TabProxy::GetTabTitle(std::wstring* title) const { | 27 bool TabProxy::GetTabTitle(std::wstring* title) const { |
52 if (!is_valid()) | 28 if (!is_valid()) |
53 return false; | 29 return false; |
54 | 30 |
55 if (!title) { | 31 if (!title) { |
56 NOTREACHED(); | 32 NOTREACHED(); |
57 return false; | 33 return false; |
58 } | 34 } |
59 | 35 |
60 int tab_title_size_response = 0; | 36 int tab_title_size_response = 0; |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 if (!is_valid()) | 160 if (!is_valid()) |
185 return false; | 161 return false; |
186 | 162 |
187 bool status = false; | 163 bool status = false; |
188 sender_->Send(new AutomationMsg_NavigationAsync(handle_, | 164 sender_->Send(new AutomationMsg_NavigationAsync(handle_, |
189 url, | 165 url, |
190 &status)); | 166 &status)); |
191 return status; | 167 return status; |
192 } | 168 } |
193 | 169 |
194 bool TabProxy::NavigateToURLAsyncWithDisposition( | |
195 const GURL& url, | |
196 WindowOpenDisposition disposition) { | |
197 if (!is_valid()) | |
198 return false; | |
199 | |
200 bool status = false; | |
201 sender_->Send(new AutomationMsg_NavigationAsyncWithDisposition(handle_, | |
202 url, | |
203 disposition, | |
204 &status)); | |
205 return status; | |
206 } | |
207 | |
208 bool TabProxy::GetProcessID(int* process_id) const { | |
209 if (!is_valid()) | |
210 return false; | |
211 | |
212 if (!process_id) { | |
213 NOTREACHED(); | |
214 return false; | |
215 } | |
216 | |
217 return sender_->Send(new AutomationMsg_TabProcessID(handle_, process_id)); | |
218 } | |
219 | |
220 bool TabProxy::ExecuteAndExtractString(const std::wstring& frame_xpath, | 170 bool TabProxy::ExecuteAndExtractString(const std::wstring& frame_xpath, |
221 const std::wstring& jscript, | 171 const std::wstring& jscript, |
222 std::wstring* string_value) { | 172 std::wstring* string_value) { |
223 scoped_ptr<Value> root(ExecuteAndExtractValue(frame_xpath, jscript)); | 173 scoped_ptr<Value> root(ExecuteAndExtractValue(frame_xpath, jscript)); |
224 if (root == NULL) | 174 if (root == NULL) |
225 return false; | 175 return false; |
226 | 176 |
227 DCHECK(root->IsType(Value::TYPE_LIST)); | 177 DCHECK(root->IsType(Value::TYPE_LIST)); |
228 Value* value = NULL; | 178 Value* value = NULL; |
229 bool succeeded = static_cast<ListValue*>(root.get())->Get(0, &value); | 179 bool succeeded = static_cast<ListValue*>(root.get())->Get(0, &value); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 DOMElementProxyRef TabProxy::GetDOMDocument() { | 250 DOMElementProxyRef TabProxy::GetDOMDocument() { |
301 if (!is_valid()) | 251 if (!is_valid()) |
302 return NULL; | 252 return NULL; |
303 | 253 |
304 int element_handle; | 254 int element_handle; |
305 if (!ExecuteJavaScriptAndGetReturn("document", &element_handle)) | 255 if (!ExecuteJavaScriptAndGetReturn("document", &element_handle)) |
306 return NULL; | 256 return NULL; |
307 return GetObjectProxy<DOMElementProxy>(element_handle); | 257 return GetObjectProxy<DOMElementProxy>(element_handle); |
308 } | 258 } |
309 | 259 |
310 bool TabProxy::GetBlockedPopupCount(int* count) const { | |
311 if (!is_valid()) | |
312 return false; | |
313 | |
314 if (!count) { | |
315 NOTREACHED(); | |
316 return false; | |
317 } | |
318 | |
319 return sender_->Send(new AutomationMsg_BlockedPopupCount( | |
320 handle_, count)); | |
321 } | |
322 | |
323 bool TabProxy::WaitForBlockedPopupCountToChangeTo(int target_count, | |
324 int wait_timeout) { | |
325 int intervals = std::max(wait_timeout / automation::kSleepTime, 1); | |
326 for (int i = 0; i < intervals; ++i) { | |
327 base::PlatformThread::Sleep( | |
328 base::TimeDelta::FromMilliseconds(automation::kSleepTime)); | |
329 int new_count = -1; | |
330 bool succeeded = GetBlockedPopupCount(&new_count); | |
331 if (!succeeded) | |
332 return false; | |
333 if (target_count == new_count) | |
334 return true; | |
335 } | |
336 // Constrained Window count did not change, return false. | |
337 return false; | |
338 } | |
339 | |
340 bool TabProxy::GetCookies(const GURL& url, std::string* cookies) { | 260 bool TabProxy::GetCookies(const GURL& url, std::string* cookies) { |
341 if (!is_valid()) | 261 if (!is_valid()) |
342 return false; | 262 return false; |
343 | 263 |
344 int size = 0; | 264 int size = 0; |
345 return sender_->Send(new AutomationMsg_GetCookies(url, handle_, &size, | 265 return sender_->Send(new AutomationMsg_GetCookies(url, handle_, &size, |
346 cookies)); | 266 cookies)); |
347 } | 267 } |
348 | 268 |
349 bool TabProxy::GetCookieByName(const GURL& url, | 269 bool TabProxy::GetCookieByName(const GURL& url, |
(...skipping 14 matching lines...) Expand all Loading... |
364 | 284 |
365 return true; | 285 return true; |
366 } | 286 } |
367 | 287 |
368 bool TabProxy::SetCookie(const GURL& url, const std::string& value) { | 288 bool TabProxy::SetCookie(const GURL& url, const std::string& value) { |
369 int response_value = 0; | 289 int response_value = 0; |
370 return sender_->Send(new AutomationMsg_SetCookie(url, value, handle_, | 290 return sender_->Send(new AutomationMsg_SetCookie(url, value, handle_, |
371 &response_value)); | 291 &response_value)); |
372 } | 292 } |
373 | 293 |
374 bool TabProxy::DeleteCookie(const GURL& url, const std::string& name) { | |
375 bool succeeded; | |
376 sender_->Send(new AutomationMsg_DeleteCookie(url, name, handle_, | |
377 &succeeded)); | |
378 return succeeded; | |
379 } | |
380 | |
381 int TabProxy::InspectElement(int x, int y) { | |
382 if (!is_valid()) | |
383 return -1; | |
384 | |
385 int ret = -1; | |
386 sender_->Send(new AutomationMsg_InspectElement(handle_, x, y, &ret)); | |
387 return ret; | |
388 } | |
389 | |
390 bool TabProxy::GetDownloadDirectory(FilePath* directory) { | 294 bool TabProxy::GetDownloadDirectory(FilePath* directory) { |
391 DCHECK(directory); | 295 DCHECK(directory); |
392 if (!is_valid()) | 296 if (!is_valid()) |
393 return false; | 297 return false; |
394 | 298 |
395 return sender_->Send(new AutomationMsg_DownloadDirectory(handle_, | 299 return sender_->Send(new AutomationMsg_DownloadDirectory(handle_, |
396 directory)); | 300 directory)); |
397 } | 301 } |
398 | 302 |
399 bool TabProxy::ShowInterstitialPage(const std::string& html_text) { | |
400 if (!is_valid()) | |
401 return false; | |
402 | |
403 AutomationMsg_NavigationResponseValues result = | |
404 AUTOMATION_MSG_NAVIGATION_ERROR; | |
405 if (!sender_->Send(new AutomationMsg_ShowInterstitialPage( | |
406 handle_, html_text, &result))) { | |
407 return false; | |
408 } | |
409 | |
410 return result == AUTOMATION_MSG_NAVIGATION_SUCCESS; | |
411 } | |
412 | |
413 bool TabProxy::HideInterstitialPage() { | |
414 if (!is_valid()) | |
415 return false; | |
416 | |
417 bool result = false; | |
418 sender_->Send(new AutomationMsg_HideInterstitialPage(handle_, &result)); | |
419 return result; | |
420 } | |
421 | |
422 bool TabProxy::Close() { | 303 bool TabProxy::Close() { |
423 return Close(false); | 304 return Close(false); |
424 } | 305 } |
425 | 306 |
426 bool TabProxy::Close(bool wait_until_closed) { | 307 bool TabProxy::Close(bool wait_until_closed) { |
427 if (!is_valid()) | 308 if (!is_valid()) |
428 return false; | 309 return false; |
429 | 310 |
430 bool succeeded = false; | 311 bool succeeded = false; |
431 sender_->Send(new AutomationMsg_CloseTab(handle_, wait_until_closed, | 312 sender_->Send(new AutomationMsg_CloseTab(handle_, wait_until_closed, |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 result == AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED; | 417 result == AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED; |
537 } | 418 } |
538 | 419 |
539 bool TabProxy::PrintAsync() { | 420 bool TabProxy::PrintAsync() { |
540 if (!is_valid()) | 421 if (!is_valid()) |
541 return false; | 422 return false; |
542 | 423 |
543 return sender_->Send(new AutomationMsg_PrintAsync(handle_)); | 424 return sender_->Send(new AutomationMsg_PrintAsync(handle_)); |
544 } | 425 } |
545 | 426 |
546 bool TabProxy::SavePage(const FilePath& file_name, | |
547 const FilePath& dir_path, | |
548 content::SavePageType type) { | |
549 if (!is_valid()) | |
550 return false; | |
551 | |
552 bool succeeded = false; | |
553 sender_->Send(new AutomationMsg_SavePage(handle_, file_name, dir_path, | |
554 static_cast<int>(type), | |
555 &succeeded)); | |
556 return succeeded; | |
557 } | |
558 | |
559 bool TabProxy::GetInfoBarCount(size_t* count) { | |
560 if (!is_valid()) | |
561 return false; | |
562 | |
563 return sender_->Send(new AutomationMsg_GetInfoBarCount(handle_, count)); | |
564 } | |
565 | |
566 bool TabProxy::WaitForInfoBarCount(size_t target_count) { | 427 bool TabProxy::WaitForInfoBarCount(size_t target_count) { |
567 if (!is_valid()) | 428 if (!is_valid()) |
568 return false; | 429 return false; |
569 | 430 |
570 bool success = false; | 431 bool success = false; |
571 return sender_->Send(new AutomationMsg_WaitForInfoBarCount( | 432 return sender_->Send(new AutomationMsg_WaitForInfoBarCount( |
572 handle_, target_count, &success)) && success; | 433 handle_, target_count, &success)) && success; |
573 } | 434 } |
574 | 435 |
575 bool TabProxy::ClickInfoBarAccept(size_t info_bar_index, | |
576 bool wait_for_navigation) { | |
577 if (!is_valid()) | |
578 return false; | |
579 | |
580 AutomationMsg_NavigationResponseValues result = | |
581 AUTOMATION_MSG_NAVIGATION_ERROR; | |
582 sender_->Send(new AutomationMsg_ClickInfoBarAccept( | |
583 handle_, info_bar_index, wait_for_navigation, &result)); | |
584 return result == AUTOMATION_MSG_NAVIGATION_SUCCESS || | |
585 result == AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED; | |
586 } | |
587 | |
588 bool TabProxy::GetLastNavigationTime(int64* nav_time) { | |
589 if (!is_valid()) | |
590 return false; | |
591 | |
592 bool success = false; | |
593 success = sender_->Send(new AutomationMsg_GetLastNavigationTime( | |
594 handle_, nav_time)); | |
595 return success; | |
596 } | |
597 | |
598 bool TabProxy::WaitForNavigation(int64 last_navigation_time) { | |
599 if (!is_valid()) | |
600 return false; | |
601 | |
602 AutomationMsg_NavigationResponseValues result = | |
603 AUTOMATION_MSG_NAVIGATION_ERROR; | |
604 sender_->Send(new AutomationMsg_WaitForNavigation(handle_, | |
605 last_navigation_time, | |
606 &result)); | |
607 return result == AUTOMATION_MSG_NAVIGATION_SUCCESS || | |
608 result == AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED; | |
609 } | |
610 | |
611 bool TabProxy::GetPageCurrentEncoding(std::string* encoding) { | |
612 if (!is_valid()) | |
613 return false; | |
614 | |
615 bool succeeded = sender_->Send( | |
616 new AutomationMsg_GetPageCurrentEncoding(handle_, encoding)); | |
617 return succeeded; | |
618 } | |
619 | |
620 bool TabProxy::OverrideEncoding(const std::string& encoding) { | 436 bool TabProxy::OverrideEncoding(const std::string& encoding) { |
621 if (!is_valid()) | 437 if (!is_valid()) |
622 return false; | 438 return false; |
623 | 439 |
624 bool succeeded = false; | 440 bool succeeded = false; |
625 sender_->Send(new AutomationMsg_OverrideEncoding(handle_, encoding, | 441 sender_->Send(new AutomationMsg_OverrideEncoding(handle_, encoding, |
626 &succeeded)); | 442 &succeeded)); |
627 return succeeded; | 443 return succeeded; |
628 } | 444 } |
629 | 445 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 json)); | 560 json)); |
745 } | 561 } |
746 | 562 |
747 void TabProxy::FirstObjectAdded() { | 563 void TabProxy::FirstObjectAdded() { |
748 AddRef(); | 564 AddRef(); |
749 } | 565 } |
750 | 566 |
751 void TabProxy::LastObjectRemoved() { | 567 void TabProxy::LastObjectRemoved() { |
752 Release(); | 568 Release(); |
753 } | 569 } |
OLD | NEW |