| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/browser/automation/automation_provider.h" | 5 #include "chrome/browser/automation/automation_provider.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "app/message_box_flags.h" | 9 #include "app/message_box_flags.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 ui_controls::SendMouseClick(button); | 153 ui_controls::SendMouseClick(button); |
| 154 } | 154 } |
| 155 | 155 |
| 156 private: | 156 private: |
| 157 int flags_; | 157 int flags_; |
| 158 | 158 |
| 159 DISALLOW_COPY_AND_ASSIGN(ClickTask); | 159 DISALLOW_COPY_AND_ASSIGN(ClickTask); |
| 160 }; | 160 }; |
| 161 | 161 |
| 162 AutomationProvider::AutomationProvider(Profile* profile) | 162 AutomationProvider::AutomationProvider(Profile* profile) |
| 163 : redirect_query_(0), | 163 : profile_(profile), |
| 164 profile_(profile), | |
| 165 reply_message_(NULL), | 164 reply_message_(NULL), |
| 166 popup_menu_waiter_(NULL) { | 165 popup_menu_waiter_(NULL) { |
| 167 TRACE_EVENT_BEGIN("AutomationProvider::AutomationProvider", 0, ""); | 166 TRACE_EVENT_BEGIN("AutomationProvider::AutomationProvider", 0, ""); |
| 168 | 167 |
| 169 browser_tracker_.reset(new AutomationBrowserTracker(this)); | 168 browser_tracker_.reset(new AutomationBrowserTracker(this)); |
| 170 extension_tracker_.reset(new AutomationExtensionTracker(this)); | 169 extension_tracker_.reset(new AutomationExtensionTracker(this)); |
| 171 tab_tracker_.reset(new AutomationTabTracker(this)); | 170 tab_tracker_.reset(new AutomationTabTracker(this)); |
| 172 window_tracker_.reset(new AutomationWindowTracker(this)); | 171 window_tracker_.reset(new AutomationWindowTracker(this)); |
| 173 autocomplete_edit_tracker_.reset( | 172 autocomplete_edit_tracker_.reset( |
| 174 new AutomationAutocompleteEditTracker(this)); | 173 new AutomationAutocompleteEditTracker(this)); |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 ExtensionsService* service = profile_->GetExtensionsService(); | 327 ExtensionsService* service = profile_->GetExtensionsService(); |
| 329 if (extension && service && | 328 if (extension && service && |
| 330 service->GetExtensionById(extension->id(), true) && | 329 service->GetExtensionById(extension->id(), true) && |
| 331 !service->GetExtensionById(extension->id(), false)) | 330 !service->GetExtensionById(extension->id(), false)) |
| 332 return extension; | 331 return extension; |
| 333 return NULL; | 332 return NULL; |
| 334 } | 333 } |
| 335 | 334 |
| 336 void AutomationProvider::OnMessageReceived(const IPC::Message& message) { | 335 void AutomationProvider::OnMessageReceived(const IPC::Message& message) { |
| 337 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message) | 336 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message) |
| 338 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync) | |
| 339 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsyncWithDisposition, | |
| 340 NavigationAsyncWithDisposition) | |
| 341 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack) | |
| 342 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward) | |
| 343 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload) | |
| 344 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth) | |
| 345 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth) | |
| 346 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth) | |
| 347 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom, | |
| 348 GetRedirectsFrom) | |
| 349 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount) | |
| 350 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount, | |
| 351 GetNormalBrowserWindowCount) | |
| 352 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow) | 337 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow) |
| 353 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale) | 338 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale) |
| 354 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow, | 339 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow, |
| 355 GetLastActiveBrowserWindow) | 340 GetLastActiveBrowserWindow) |
| 356 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow) | 341 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow) |
| 357 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow, | 342 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow, |
| 358 FindNormalBrowserWindow) | 343 FindNormalBrowserWindow) |
| 359 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive) | 344 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive) |
| 360 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow) | 345 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow) |
| 361 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized) | 346 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized) |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 IPC_MESSAGE_HANDLER(AutomationMsg_RunUnloadHandlers, OnRunUnloadHandlers) | 551 IPC_MESSAGE_HANDLER(AutomationMsg_RunUnloadHandlers, OnRunUnloadHandlers) |
| 567 IPC_MESSAGE_HANDLER(AutomationMsg_SetZoomLevel, OnSetZoomLevel) | 552 IPC_MESSAGE_HANDLER(AutomationMsg_SetZoomLevel, OnSetZoomLevel) |
| 568 #endif // defined(OS_WIN) | 553 #endif // defined(OS_WIN) |
| 569 #if defined(OS_CHROMEOS) | 554 #if defined(OS_CHROMEOS) |
| 570 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass, | 555 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass, |
| 571 LoginWithUserAndPass) | 556 LoginWithUserAndPass) |
| 572 #endif // defined(OS_CHROMEOS) | 557 #endif // defined(OS_CHROMEOS) |
| 573 IPC_END_MESSAGE_MAP() | 558 IPC_END_MESSAGE_MAP() |
| 574 } | 559 } |
| 575 | 560 |
| 576 void AutomationProvider::NavigationAsync(int handle, | |
| 577 const GURL& url, | |
| 578 bool* status) { | |
| 579 NavigationAsyncWithDisposition(handle, url, CURRENT_TAB, status); | |
| 580 } | |
| 581 | |
| 582 void AutomationProvider::NavigationAsyncWithDisposition( | |
| 583 int handle, | |
| 584 const GURL& url, | |
| 585 WindowOpenDisposition disposition, | |
| 586 bool* status) { | |
| 587 *status = false; | |
| 588 | |
| 589 if (tab_tracker_->ContainsHandle(handle)) { | |
| 590 NavigationController* tab = tab_tracker_->GetResource(handle); | |
| 591 | |
| 592 // Simulate what a user would do. Activate the tab and then navigate. | |
| 593 // We could allow navigating in a background tab in future. | |
| 594 Browser* browser = FindAndActivateTab(tab); | |
| 595 | |
| 596 if (browser) { | |
| 597 // Don't add any listener unless a callback mechanism is desired. | |
| 598 // TODO(vibhor): Do this if such a requirement arises in future. | |
| 599 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED); | |
| 600 *status = true; | |
| 601 } | |
| 602 } | |
| 603 } | |
| 604 | |
| 605 void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) { | |
| 606 if (tab_tracker_->ContainsHandle(handle)) { | |
| 607 NavigationController* tab = tab_tracker_->GetResource(handle); | |
| 608 Browser* browser = FindAndActivateTab(tab); | |
| 609 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) { | |
| 610 AddNavigationStatusListener(tab, reply_message, 1, false); | |
| 611 browser->GoBack(CURRENT_TAB); | |
| 612 return; | |
| 613 } | |
| 614 } | |
| 615 | |
| 616 AutomationMsg_GoBack::WriteReplyParams( | |
| 617 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR); | |
| 618 Send(reply_message); | |
| 619 } | |
| 620 | |
| 621 void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) { | |
| 622 if (tab_tracker_->ContainsHandle(handle)) { | |
| 623 NavigationController* tab = tab_tracker_->GetResource(handle); | |
| 624 Browser* browser = FindAndActivateTab(tab); | |
| 625 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) { | |
| 626 AddNavigationStatusListener(tab, reply_message, 1, false); | |
| 627 browser->GoForward(CURRENT_TAB); | |
| 628 return; | |
| 629 } | |
| 630 } | |
| 631 | |
| 632 AutomationMsg_GoForward::WriteReplyParams( | |
| 633 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR); | |
| 634 Send(reply_message); | |
| 635 } | |
| 636 | |
| 637 void AutomationProvider::Reload(int handle, IPC::Message* reply_message) { | |
| 638 if (tab_tracker_->ContainsHandle(handle)) { | |
| 639 NavigationController* tab = tab_tracker_->GetResource(handle); | |
| 640 Browser* browser = FindAndActivateTab(tab); | |
| 641 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) { | |
| 642 AddNavigationStatusListener(tab, reply_message, 1, false); | |
| 643 browser->Reload(CURRENT_TAB); | |
| 644 return; | |
| 645 } | |
| 646 } | |
| 647 | |
| 648 AutomationMsg_Reload::WriteReplyParams( | |
| 649 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR); | |
| 650 Send(reply_message); | |
| 651 } | |
| 652 | |
| 653 void AutomationProvider::SetAuth(int tab_handle, | |
| 654 const std::wstring& username, | |
| 655 const std::wstring& password, | |
| 656 IPC::Message* reply_message) { | |
| 657 if (tab_tracker_->ContainsHandle(tab_handle)) { | |
| 658 NavigationController* tab = tab_tracker_->GetResource(tab_handle); | |
| 659 LoginHandlerMap::iterator iter = login_handler_map_.find(tab); | |
| 660 | |
| 661 if (iter != login_handler_map_.end()) { | |
| 662 // If auth is needed again after this, assume login has failed. This is | |
| 663 // not strictly correct, because a navigation can require both proxy and | |
| 664 // server auth, but it should be OK for now. | |
| 665 LoginHandler* handler = iter->second; | |
| 666 AddNavigationStatusListener(tab, reply_message, 1, false); | |
| 667 handler->SetAuth(username, password); | |
| 668 return; | |
| 669 } | |
| 670 } | |
| 671 | |
| 672 AutomationMsg_SetAuth::WriteReplyParams( | |
| 673 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED); | |
| 674 Send(reply_message); | |
| 675 } | |
| 676 | |
| 677 void AutomationProvider::CancelAuth(int tab_handle, | |
| 678 IPC::Message* reply_message) { | |
| 679 if (tab_tracker_->ContainsHandle(tab_handle)) { | |
| 680 NavigationController* tab = tab_tracker_->GetResource(tab_handle); | |
| 681 LoginHandlerMap::iterator iter = login_handler_map_.find(tab); | |
| 682 | |
| 683 if (iter != login_handler_map_.end()) { | |
| 684 // If auth is needed again after this, something is screwy. | |
| 685 LoginHandler* handler = iter->second; | |
| 686 AddNavigationStatusListener(tab, reply_message, 1, false); | |
| 687 handler->CancelAuth(); | |
| 688 return; | |
| 689 } | |
| 690 } | |
| 691 | |
| 692 AutomationMsg_CancelAuth::WriteReplyParams( | |
| 693 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED); | |
| 694 Send(reply_message); | |
| 695 } | |
| 696 | |
| 697 void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) { | |
| 698 *needs_auth = false; | |
| 699 | |
| 700 if (tab_tracker_->ContainsHandle(tab_handle)) { | |
| 701 NavigationController* tab = tab_tracker_->GetResource(tab_handle); | |
| 702 LoginHandlerMap::iterator iter = login_handler_map_.find(tab); | |
| 703 | |
| 704 if (iter != login_handler_map_.end()) { | |
| 705 // The LoginHandler will be in our map IFF the tab needs auth. | |
| 706 *needs_auth = true; | |
| 707 } | |
| 708 } | |
| 709 } | |
| 710 | |
| 711 void AutomationProvider::GetRedirectsFrom(int tab_handle, | |
| 712 const GURL& source_url, | |
| 713 IPC::Message* reply_message) { | |
| 714 DCHECK(!redirect_query_) << "Can only handle one redirect query at once."; | |
| 715 if (tab_tracker_->ContainsHandle(tab_handle)) { | |
| 716 NavigationController* tab = tab_tracker_->GetResource(tab_handle); | |
| 717 HistoryService* history_service = | |
| 718 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS); | |
| 719 | |
| 720 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " << | |
| 721 "has no history service"; | |
| 722 if (history_service) { | |
| 723 DCHECK(reply_message_ == NULL); | |
| 724 reply_message_ = reply_message; | |
| 725 // Schedule a history query for redirects. The response will be sent | |
| 726 // asynchronously from the callback the history system uses to notify us | |
| 727 // that it's done: OnRedirectQueryComplete. | |
| 728 redirect_query_ = history_service->QueryRedirectsFrom( | |
| 729 source_url, &consumer_, | |
| 730 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete)); | |
| 731 return; // Response will be sent when query completes. | |
| 732 } | |
| 733 } | |
| 734 | |
| 735 // Send failure response. | |
| 736 std::vector<GURL> empty; | |
| 737 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty); | |
| 738 Send(reply_message); | |
| 739 } | |
| 740 | |
| 741 void AutomationProvider::GetBrowserLocale(string16* locale) { | 561 void AutomationProvider::GetBrowserLocale(string16* locale) { |
| 742 DCHECK(g_browser_process); | 562 DCHECK(g_browser_process); |
| 743 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale()); | 563 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale()); |
| 744 } | 564 } |
| 745 | 565 |
| 746 void AutomationProvider::GetBrowserWindowCount(int* window_count) { | |
| 747 *window_count = static_cast<int>(BrowserList::size()); | |
| 748 } | |
| 749 | |
| 750 void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) { | |
| 751 *window_count = static_cast<int>( | |
| 752 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL)); | |
| 753 } | |
| 754 | |
| 755 void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog, | 566 void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog, |
| 756 int* dialog_button) { | 567 int* dialog_button) { |
| 757 AppModalDialog* dialog_delegate = | 568 AppModalDialog* dialog_delegate = |
| 758 Singleton<AppModalDialogQueue>()->active_dialog(); | 569 Singleton<AppModalDialogQueue>()->active_dialog(); |
| 759 *showing_dialog = (dialog_delegate != NULL); | 570 *showing_dialog = (dialog_delegate != NULL); |
| 760 if (*showing_dialog) | 571 if (*showing_dialog) |
| 761 *dialog_button = dialog_delegate->GetDialogButtons(); | 572 *dialog_button = dialog_delegate->GetDialogButtons(); |
| 762 else | 573 else |
| 763 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE; | 574 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE; |
| 764 } | 575 } |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1007 if (window_tracker_->ContainsHandle(handle)) { | 818 if (window_tracker_->ContainsHandle(handle)) { |
| 1008 window_tracker_->Remove(window_tracker_->GetResource(handle)); | 819 window_tracker_->Remove(window_tracker_->GetResource(handle)); |
| 1009 } | 820 } |
| 1010 } | 821 } |
| 1011 | 822 |
| 1012 void AutomationProvider::OnChannelError() { | 823 void AutomationProvider::OnChannelError() { |
| 1013 LOG(INFO) << "AutomationProxy went away, shutting down app."; | 824 LOG(INFO) << "AutomationProxy went away, shutting down app."; |
| 1014 AutomationProviderList::GetInstance()->RemoveProvider(this); | 825 AutomationProviderList::GetInstance()->RemoveProvider(this); |
| 1015 } | 826 } |
| 1016 | 827 |
| 1017 // TODO(brettw) change this to accept GURLs when history supports it | |
| 1018 void AutomationProvider::OnRedirectQueryComplete( | |
| 1019 HistoryService::Handle request_handle, | |
| 1020 GURL from_url, | |
| 1021 bool success, | |
| 1022 history::RedirectList* redirects) { | |
| 1023 DCHECK(request_handle == redirect_query_); | |
| 1024 DCHECK(reply_message_ != NULL); | |
| 1025 | |
| 1026 std::vector<GURL> redirects_gurl; | |
| 1027 reply_message_->WriteBool(success); | |
| 1028 if (success) { | |
| 1029 for (size_t i = 0; i < redirects->size(); i++) | |
| 1030 redirects_gurl.push_back(redirects->at(i)); | |
| 1031 } | |
| 1032 | |
| 1033 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl); | |
| 1034 | |
| 1035 Send(reply_message_); | |
| 1036 redirect_query_ = 0; | |
| 1037 reply_message_ = NULL; | |
| 1038 } | |
| 1039 | |
| 1040 bool AutomationProvider::Send(IPC::Message* msg) { | 828 bool AutomationProvider::Send(IPC::Message* msg) { |
| 1041 DCHECK(channel_.get()); | 829 DCHECK(channel_.get()); |
| 1042 return channel_->Send(msg); | 830 return channel_->Send(msg); |
| 1043 } | 831 } |
| 1044 | 832 |
| 1045 Browser* AutomationProvider::FindAndActivateTab( | 833 Browser* AutomationProvider::FindAndActivateTab( |
| 1046 NavigationController* controller) { | 834 NavigationController* controller) { |
| 1047 int tab_index; | 835 int tab_index; |
| 1048 Browser* browser = Browser::GetBrowserForController(controller, &tab_index); | 836 Browser* browser = Browser::GetBrowserForController(controller, &tab_index); |
| 1049 if (browser) | 837 if (browser) |
| (...skipping 3288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4338 } | 4126 } |
| 4339 | 4127 |
| 4340 void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) { | 4128 void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) { |
| 4341 NOTIMPLEMENTED(); | 4129 NOTIMPLEMENTED(); |
| 4342 } | 4130 } |
| 4343 #endif // !defined(TOOLKIT_VIEWS) | 4131 #endif // !defined(TOOLKIT_VIEWS) |
| 4344 | 4132 |
| 4345 void AutomationProvider::ResetToDefaultTheme() { | 4133 void AutomationProvider::ResetToDefaultTheme() { |
| 4346 profile_->ClearTheme(); | 4134 profile_->ClearTheme(); |
| 4347 } | 4135 } |
| OLD | NEW |