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_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_OBSERVERS_H_ 6 #define CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_OBSERVERS_H_ 7 8 #include <deque> 9 #include <map> 10 #include <set> 11 #include <string> 12 #include <vector> 13 14 #include "base/callback.h" 15 #include "base/compiler_specific.h" 16 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/weak_ptr.h" 18 #include "base/sequenced_task_runner_helpers.h" 19 #include "base/synchronization/waitable_event.h" 20 #include "base/values.h" 21 #include "chrome/browser/automation/automation_provider_json.h" 22 #include "chrome/browser/bookmarks/bookmark_model_observer.h" 23 #include "components/autofill/core/browser/personal_data_manager.h" 24 #include "components/autofill/core/browser/personal_data_manager_observer.h" 25 #if defined(OS_CHROMEOS) 26 #include "chrome/browser/chromeos/cros/network_library.h" 27 #include "chrome/browser/chromeos/login/enrollment/enrollment_screen.h" 28 #include "chrome/browser/chromeos/login/login_status_consumer.h" 29 #include "chrome/browser/chromeos/login/user_manager.h" 30 #include "chrome/browser/chromeos/login/wizard_controller.h" 31 #endif // defined(OS_CHROMEOS) 32 #include "chrome/browser/common/cancelable_request.h" 33 #include "chrome/browser/download/all_download_item_notifier.h" 34 #include "chrome/browser/history/history_service.h" 35 #include "chrome/browser/history/history_types.h" 36 #include "chrome/browser/importer/importer_progress_observer.h" 37 #include "chrome/browser/memory_details.h" 38 #include "chrome/browser/password_manager/password_store_change.h" 39 #include "chrome/browser/password_manager/password_store_consumer.h" 40 #include "chrome/browser/search_engines/template_url_service_observer.h" 41 #include "chrome/browser/ui/tabs/tab_strip_model.h" 42 #include "chrome/common/automation_constants.h" 43 #include "chrome/common/extensions/extension_constants.h" 44 #include "chrome/common/importer/importer_data_types.h" 45 #include "content/public/browser/browser_thread.h" 46 #include "content/public/browser/download_item.h" 47 #include "content/public/browser/download_manager.h" 48 #include "content/public/browser/notification_observer.h" 49 #include "content/public/browser/notification_registrar.h" 50 #include "content/public/browser/notification_types.h" 51 #include "content/public/browser/render_view_host_observer.h" 52 #include "ui/gfx/point.h" 53 #include "ui/gfx/size.h" 54 55 class AutomationProvider; 56 class BalloonCollection; 57 class Browser; 58 class ExtensionProcessManager; 59 class ExtensionService; 60 class Notification; 61 class Profile; 62 class SavePackage; 63 64 namespace automation { 65 class Error; 66 } 67 68 #if defined(OS_CHROMEOS) 69 namespace chromeos { 70 class ExistingUserController; 71 class WizardScreen; 72 } 73 #endif // defined(OS_CHROMEOS) 74 75 namespace content { 76 class NavigationController; 77 class RenderViewHost; 78 class WebContents; 79 } 80 81 namespace extensions { 82 class Extension; 83 } 84 85 namespace history { 86 class TopSites; 87 } 88 89 namespace IPC { 90 class Message; 91 } 92 93 namespace policy { 94 class BrowserPolicyConnector; 95 } 96 97 class InitialLoadObserver : public content::NotificationObserver { 98 public: 99 InitialLoadObserver(size_t tab_count, AutomationProvider* automation); 100 virtual ~InitialLoadObserver(); 101 102 // Overridden from content::NotificationObserver: 103 virtual void Observe(int type, 104 const content::NotificationSource& source, 105 const content::NotificationDetails& details) OVERRIDE; 106 107 // Caller owns the return value and is responsible for deleting it. 108 // Example return value: 109 // {'tabs': [{'start_time_ms': 1, 'stop_time_ms': 2.5}, 110 // {'start_time_ms': 0.5, 'stop_time_ms': 3}]} 111 // stop_time_ms values may be null if WaitForInitialLoads has not finished. 112 // Only includes entries for the |tab_count| tabs we are monitoring. 113 // There is no defined ordering of the return value. 114 base::DictionaryValue* GetTimingInformation() const; 115 116 private: 117 class TabTime; 118 typedef std::map<uintptr_t, TabTime> TabTimeMap; 119 typedef std::set<uintptr_t> TabSet; 120 121 void ConditionMet(); 122 123 content::NotificationRegistrar registrar_; 124 125 base::WeakPtr<AutomationProvider> automation_; 126 size_t crashed_tab_count_; 127 size_t outstanding_tab_count_; 128 base::TimeTicks init_time_; 129 TabTimeMap loading_tabs_; 130 TabSet finished_tabs_; 131 132 DISALLOW_COPY_AND_ASSIGN(InitialLoadObserver); 133 }; 134 135 #if defined(OS_CHROMEOS) 136 // Watches for NetworkManager events. Because NetworkLibrary loads 137 // asynchronously, this is used to make sure it is done before tests are run. 138 class NetworkManagerInitObserver 139 : public chromeos::NetworkLibrary::NetworkManagerObserver { 140 public: 141 explicit NetworkManagerInitObserver(AutomationProvider* automation); 142 virtual ~NetworkManagerInitObserver(); 143 virtual bool Init(); 144 virtual void OnNetworkManagerChanged(chromeos::NetworkLibrary* obj); 145 146 private: 147 base::WeakPtr<AutomationProvider> automation_; 148 149 DISALLOW_COPY_AND_ASSIGN(NetworkManagerInitObserver); 150 }; 151 152 // Observes when the ChromeOS login WebUI becomes ready (by showing the login 153 // form, account picker, a network error or the OOBE wizard, depending on Chrome 154 // flags and state). 155 class OOBEWebuiReadyObserver : public content::NotificationObserver { 156 public: 157 explicit OOBEWebuiReadyObserver(AutomationProvider* automation); 158 159 // Overridden from content::NotificationObserver: 160 virtual void Observe(int type, 161 const content::NotificationSource& source, 162 const content::NotificationDetails& details) OVERRIDE; 163 164 private: 165 void OOBEWebuiReady(); 166 167 content::NotificationRegistrar registrar_; 168 base::WeakPtr<AutomationProvider> automation_; 169 170 DISALLOW_COPY_AND_ASSIGN(OOBEWebuiReadyObserver); 171 }; 172 #endif // defined(OS_CHROMEOS) 173 174 // Watches for NewTabUI page loads for performance timing purposes. 175 class NewTabUILoadObserver : public content::NotificationObserver { 176 public: 177 explicit NewTabUILoadObserver(AutomationProvider* automation, 178 Profile* profile); 179 virtual ~NewTabUILoadObserver(); 180 181 // Overridden from content::NotificationObserver: 182 virtual void Observe(int type, 183 const content::NotificationSource& source, 184 const content::NotificationDetails& details) OVERRIDE; 185 186 private: 187 content::NotificationRegistrar registrar_; 188 base::WeakPtr<AutomationProvider> automation_; 189 190 DISALLOW_COPY_AND_ASSIGN(NewTabUILoadObserver); 191 }; 192 193 class NavigationControllerRestoredObserver 194 : public content::NotificationObserver { 195 public: 196 NavigationControllerRestoredObserver( 197 AutomationProvider* automation, 198 content::NavigationController* controller, 199 IPC::Message* reply_message); 200 virtual ~NavigationControllerRestoredObserver(); 201 202 // Overridden from content::NotificationObserver: 203 virtual void Observe(int type, 204 const content::NotificationSource& source, 205 const content::NotificationDetails& details) OVERRIDE; 206 207 private: 208 bool FinishedRestoring(); 209 void SendDone(); 210 211 content::NotificationRegistrar registrar_; 212 base::WeakPtr<AutomationProvider> automation_; 213 content::NavigationController* controller_; 214 scoped_ptr<IPC::Message> reply_message_; 215 216 DISALLOW_COPY_AND_ASSIGN(NavigationControllerRestoredObserver); 217 }; 218 219 class NavigationNotificationObserver : public content::NotificationObserver { 220 public: 221 NavigationNotificationObserver(content::NavigationController* controller, 222 AutomationProvider* automation, 223 IPC::Message* reply_message, 224 int number_of_navigations, 225 bool include_current_navigation, 226 bool use_json_interface); 227 virtual ~NavigationNotificationObserver(); 228 229 // Overridden from content::NotificationObserver: 230 virtual void Observe(int type, 231 const content::NotificationSource& source, 232 const content::NotificationDetails& details) OVERRIDE; 233 234 void ConditionMet(AutomationMsg_NavigationResponseValues navigation_result); 235 236 private: 237 content::NotificationRegistrar registrar_; 238 base::WeakPtr<AutomationProvider> automation_; 239 scoped_ptr<IPC::Message> reply_message_; 240 content::NavigationController* controller_; 241 int navigations_remaining_; 242 bool navigation_started_; 243 bool use_json_interface_; 244 245 DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver); 246 }; 247 248 class TabStripNotificationObserver : public content::NotificationObserver { 249 public: 250 TabStripNotificationObserver(int notification, 251 AutomationProvider* automation); 252 virtual ~TabStripNotificationObserver(); 253 254 // Overridden from content::NotificationObserver: 255 virtual void Observe(int type, 256 const content::NotificationSource& source, 257 const content::NotificationDetails& details) OVERRIDE; 258 259 virtual void ObserveTab(content::NavigationController* controller) = 0; 260 261 protected: 262 content::NotificationRegistrar registrar_; 263 base::WeakPtr<AutomationProvider> automation_; 264 int notification_; 265 }; 266 267 class TabAppendedNotificationObserver : public TabStripNotificationObserver { 268 public: 269 TabAppendedNotificationObserver(Browser* parent, 270 AutomationProvider* automation, 271 IPC::Message* reply_message, 272 bool use_json_interface); 273 virtual ~TabAppendedNotificationObserver(); 274 275 virtual void ObserveTab(content::NavigationController* controller); 276 IPC::Message* ReleaseReply(); 277 278 protected: 279 Browser* parent_; 280 scoped_ptr<IPC::Message> reply_message_; 281 bool use_json_interface_; 282 283 private: 284 DISALLOW_COPY_AND_ASSIGN(TabAppendedNotificationObserver); 285 }; 286 287 class TabClosedNotificationObserver : public TabStripNotificationObserver { 288 public: 289 TabClosedNotificationObserver(AutomationProvider* automation, 290 bool wait_until_closed, 291 IPC::Message* reply_message, 292 bool use_json_interface); 293 virtual ~TabClosedNotificationObserver(); 294 295 virtual void ObserveTab(content::NavigationController* controller); 296 297 void set_for_browser_command(bool for_browser_command); 298 299 protected: 300 scoped_ptr<IPC::Message> reply_message_; 301 bool use_json_interface_; 302 bool for_browser_command_; 303 304 private: 305 DISALLOW_COPY_AND_ASSIGN(TabClosedNotificationObserver); 306 }; 307 308 // Notifies when the tab count reaches the target number. 309 class TabCountChangeObserver : public TabStripModelObserver { 310 public: 311 TabCountChangeObserver(AutomationProvider* automation, 312 Browser* browser, 313 IPC::Message* reply_message, 314 int target_tab_count); 315 // Implementation of TabStripModelObserver. 316 virtual void TabInsertedAt(content::WebContents* contents, 317 int index, 318 bool foreground) OVERRIDE; 319 virtual void TabDetachedAt(content::WebContents* contents, 320 int index) OVERRIDE; 321 virtual void TabStripModelDeleted() OVERRIDE; 322 323 private: 324 virtual ~TabCountChangeObserver(); 325 326 // Checks if the current tab count matches our target, and if so, 327 // sends the reply message and deletes self. 328 void CheckTabCount(); 329 330 base::WeakPtr<AutomationProvider> automation_; 331 scoped_ptr<IPC::Message> reply_message_; 332 333 TabStripModel* tab_strip_model_; 334 335 const int target_tab_count_; 336 337 DISALLOW_COPY_AND_ASSIGN(TabCountChangeObserver); 338 }; 339 340 // Observes when an extension has been uninstalled. 341 class ExtensionUninstallObserver : public content::NotificationObserver { 342 public: 343 ExtensionUninstallObserver(AutomationProvider* automation, 344 IPC::Message* reply_message, 345 const std::string& id); 346 virtual ~ExtensionUninstallObserver(); 347 348 // Overridden from content::NotificationObserver: 349 virtual void Observe(int type, 350 const content::NotificationSource& source, 351 const content::NotificationDetails& details) OVERRIDE; 352 353 private: 354 content::NotificationRegistrar registrar_; 355 base::WeakPtr<AutomationProvider> automation_; 356 scoped_ptr<IPC::Message> reply_message_; 357 std::string id_; 358 359 DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallObserver); 360 }; 361 362 // Observes when an extension has finished loading and is ready for use. Also 363 // checks for possible install errors. 364 class ExtensionReadyNotificationObserver 365 : public content::NotificationObserver { 366 public: 367 // Creates an observer that replies using the JSON automation interface. 368 ExtensionReadyNotificationObserver(ExtensionProcessManager* manager, 369 ExtensionService* service, 370 AutomationProvider* automation, 371 IPC::Message* reply_message); 372 virtual ~ExtensionReadyNotificationObserver(); 373 374 // Overridden from content::NotificationObserver: 375 virtual void Observe(int type, 376 const content::NotificationSource& source, 377 const content::NotificationDetails& details) OVERRIDE; 378 379 private: 380 void Init(); 381 382 content::NotificationRegistrar registrar_; 383 ExtensionProcessManager* manager_; 384 ExtensionService* service_; 385 base::WeakPtr<AutomationProvider> automation_; 386 scoped_ptr<IPC::Message> reply_message_; 387 const extensions::Extension* extension_; 388 389 DISALLOW_COPY_AND_ASSIGN(ExtensionReadyNotificationObserver); 390 }; 391 392 class ExtensionUnloadNotificationObserver 393 : public content::NotificationObserver { 394 public: 395 ExtensionUnloadNotificationObserver(); 396 virtual ~ExtensionUnloadNotificationObserver(); 397 398 // Overridden from content::NotificationObserver: 399 virtual void Observe(int type, 400 const content::NotificationSource& source, 401 const content::NotificationDetails& details) OVERRIDE; 402 403 bool did_receive_unload_notification() { 404 return did_receive_unload_notification_; 405 } 406 407 private: 408 content::NotificationRegistrar registrar_; 409 bool did_receive_unload_notification_; 410 411 DISALLOW_COPY_AND_ASSIGN(ExtensionUnloadNotificationObserver); 412 }; 413 414 // Observes when the extensions have been fully updated. The ExtensionUpdater 415 // service provides a notification whem all the updated extensions have been 416 // installed, but it does not wait for all of them to be loaded too. This 417 // observer waits until all updated extensions have actually been loaded. 418 class ExtensionsUpdatedObserver : public content::NotificationObserver { 419 public: 420 ExtensionsUpdatedObserver(ExtensionProcessManager* manager, 421 AutomationProvider* automation, 422 IPC::Message* reply_message); 423 virtual ~ExtensionsUpdatedObserver(); 424 425 // Overridden from content::NotificationObserver: 426 virtual void Observe(int type, 427 const content::NotificationSource& source, 428 const content::NotificationDetails& details) OVERRIDE; 429 430 // Called by ExtensionUpdater when it has finished updating extensions. 431 void UpdateCheckFinished(); 432 433 private: 434 void MaybeReply(); 435 436 content::NotificationRegistrar registrar_; 437 ExtensionProcessManager* manager_; 438 base::WeakPtr<AutomationProvider> automation_; 439 scoped_ptr<IPC::Message> reply_message_; 440 bool updater_finished_; 441 442 DISALLOW_COPY_AND_ASSIGN(ExtensionsUpdatedObserver); 443 }; 444 445 // Observes when a new browser has been opened and a tab within it has stopped 446 // loading. 447 class BrowserOpenedNotificationObserver : public content::NotificationObserver { 448 public: 449 BrowserOpenedNotificationObserver(AutomationProvider* automation, 450 IPC::Message* reply_message, 451 bool use_json_interface); 452 virtual ~BrowserOpenedNotificationObserver(); 453 454 // Overridden from content::NotificationObserver: 455 virtual void Observe(int type, 456 const content::NotificationSource& source, 457 const content::NotificationDetails& details) OVERRIDE; 458 459 void set_for_browser_command(bool for_browser_command); 460 461 private: 462 content::NotificationRegistrar registrar_; 463 base::WeakPtr<AutomationProvider> automation_; 464 scoped_ptr<IPC::Message> reply_message_; 465 int new_window_id_; 466 bool use_json_interface_; 467 bool for_browser_command_; 468 469 DISALLOW_COPY_AND_ASSIGN(BrowserOpenedNotificationObserver); 470 }; 471 472 class BrowserClosedNotificationObserver : public content::NotificationObserver { 473 public: 474 BrowserClosedNotificationObserver(Browser* browser, 475 AutomationProvider* automation, 476 IPC::Message* reply_message, 477 bool use_json_interface); 478 virtual ~BrowserClosedNotificationObserver(); 479 480 // Overridden from content::NotificationObserver: 481 virtual void Observe(int type, 482 const content::NotificationSource& source, 483 const content::NotificationDetails& details) OVERRIDE; 484 485 void set_for_browser_command(bool for_browser_command); 486 487 private: 488 content::NotificationRegistrar registrar_; 489 base::WeakPtr<AutomationProvider> automation_; 490 scoped_ptr<IPC::Message> reply_message_; 491 bool use_json_interface_; 492 bool for_browser_command_; 493 494 DISALLOW_COPY_AND_ASSIGN(BrowserClosedNotificationObserver); 495 }; 496 497 class BrowserCountChangeNotificationObserver 498 : public content::NotificationObserver { 499 public: 500 BrowserCountChangeNotificationObserver(int target_count, 501 AutomationProvider* automation, 502 IPC::Message* reply_message); 503 virtual ~BrowserCountChangeNotificationObserver(); 504 505 // Overridden from content::NotificationObserver: 506 virtual void Observe(int type, 507 const content::NotificationSource& source, 508 const content::NotificationDetails& details) OVERRIDE; 509 510 private: 511 int target_count_; 512 content::NotificationRegistrar registrar_; 513 base::WeakPtr<AutomationProvider> automation_; 514 scoped_ptr<IPC::Message> reply_message_; 515 516 DISALLOW_COPY_AND_ASSIGN(BrowserCountChangeNotificationObserver); 517 }; 518 519 class ExecuteBrowserCommandObserver : public content::NotificationObserver { 520 public: 521 virtual ~ExecuteBrowserCommandObserver(); 522 523 static bool CreateAndRegisterObserver(AutomationProvider* automation, 524 Browser* browser, 525 int command, 526 IPC::Message* reply_message, 527 bool use_json_interface); 528 529 // Overridden from content::NotificationObserver: 530 virtual void Observe(int type, 531 const content::NotificationSource& source, 532 const content::NotificationDetails& details) OVERRIDE; 533 534 private: 535 ExecuteBrowserCommandObserver(AutomationProvider* automation, 536 IPC::Message* reply_message, 537 bool use_json_interface); 538 539 bool Register(int command); 540 541 bool Getint(int command, int* type); 542 543 IPC::Message* ReleaseReply(); 544 545 content::NotificationRegistrar registrar_; 546 base::WeakPtr<AutomationProvider> automation_; 547 int notification_type_; 548 scoped_ptr<IPC::Message> reply_message_; 549 bool use_json_interface_; 550 551 DISALLOW_COPY_AND_ASSIGN(ExecuteBrowserCommandObserver); 552 }; 553 554 class FindInPageNotificationObserver : public content::NotificationObserver { 555 public: 556 FindInPageNotificationObserver(AutomationProvider* automation, 557 content::WebContents* parent_tab, 558 bool reply_with_json, 559 IPC::Message* reply_message); 560 virtual ~FindInPageNotificationObserver(); 561 562 // Overridden from content::NotificationObserver: 563 virtual void Observe(int type, 564 const content::NotificationSource& source, 565 const content::NotificationDetails& details) OVERRIDE; 566 567 // The Find mechanism is over asynchronous IPC, so a search is kicked off and 568 // we wait for notification to find out what the results are. As the user is 569 // typing, new search requests can be issued and the Request ID helps us make 570 // sense of whether this is the current request or an old one. The unit tests, 571 // however, which uses this constant issues only one search at a time, so we 572 // don't need a rolling id to identify each search. But, we still need to 573 // specify one, so we just use a fixed one - its value does not matter. 574 static const int kFindInPageRequestId; 575 576 private: 577 content::NotificationRegistrar registrar_; 578 base::WeakPtr<AutomationProvider> automation_; 579 // We will at some point (before final update) be notified of the ordinal and 580 // we need to preserve it so we can send it later. 581 int active_match_ordinal_; 582 // Send reply using json automation interface. 583 bool reply_with_json_; 584 scoped_ptr<IPC::Message> reply_message_; 585 586 DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver); 587 }; 588 589 class DomOperationObserver : public content::NotificationObserver { 590 public: 591 explicit DomOperationObserver(int automation_id); 592 virtual ~DomOperationObserver(); 593 594 // Overridden from content::NotificationObserver: 595 virtual void Observe(int type, 596 const content::NotificationSource& source, 597 const content::NotificationDetails& details) OVERRIDE; 598 599 virtual void OnDomOperationCompleted(const std::string& json) = 0; 600 virtual void OnModalDialogShown() = 0; 601 virtual void OnJavascriptBlocked() = 0; 602 603 private: 604 int automation_id_; 605 content::NotificationRegistrar registrar_; 606 607 DISALLOW_COPY_AND_ASSIGN(DomOperationObserver); 608 }; 609 610 // Sends a message back to the automation client with the results of the DOM 611 // operation. 612 class DomOperationMessageSender : public DomOperationObserver { 613 public: 614 DomOperationMessageSender(AutomationProvider* automation, 615 IPC::Message* reply_message, 616 bool use_json_interface); 617 virtual ~DomOperationMessageSender(); 618 619 virtual void OnDomOperationCompleted(const std::string& json) OVERRIDE; 620 virtual void OnModalDialogShown() OVERRIDE; 621 virtual void OnJavascriptBlocked() OVERRIDE; 622 623 private: 624 base::WeakPtr<AutomationProvider> automation_; 625 scoped_ptr<IPC::Message> reply_message_; 626 bool use_json_interface_; 627 628 DISALLOW_COPY_AND_ASSIGN(DomOperationMessageSender); 629 }; 630 631 // Collects METRIC_EVENT_DURATION notifications and keep track of the times. 632 class MetricEventDurationObserver : public content::NotificationObserver { 633 public: 634 MetricEventDurationObserver(); 635 virtual ~MetricEventDurationObserver(); 636 637 // Get the duration of an event. Returns -1 if we haven't seen the event. 638 int GetEventDurationMs(const std::string& event_name); 639 640 // Overridden from content::NotificationObserver: 641 virtual void Observe(int type, 642 const content::NotificationSource& source, 643 const content::NotificationDetails& details) OVERRIDE; 644 645 private: 646 content::NotificationRegistrar registrar_; 647 648 typedef std::map<std::string, int> EventDurationMap; 649 EventDurationMap durations_; 650 651 DISALLOW_COPY_AND_ASSIGN(MetricEventDurationObserver); 652 }; 653 654 class InfoBarCountObserver : public content::NotificationObserver { 655 public: 656 InfoBarCountObserver(AutomationProvider* automation, 657 IPC::Message* reply_message, 658 content::WebContents* web_contents, 659 size_t target_count); 660 virtual ~InfoBarCountObserver(); 661 662 // Overridden from content::NotificationObserver: 663 virtual void Observe(int type, 664 const content::NotificationSource& source, 665 const content::NotificationDetails& details) OVERRIDE; 666 667 private: 668 // Checks whether the infobar count matches our target, and if so 669 // sends the reply message and deletes itself. 670 void CheckCount(); 671 672 content::NotificationRegistrar registrar_; 673 base::WeakPtr<AutomationProvider> automation_; 674 scoped_ptr<IPC::Message> reply_message_; 675 content::WebContents* web_contents_; 676 677 const size_t target_count_; 678 679 DISALLOW_COPY_AND_ASSIGN(InfoBarCountObserver); 680 }; 681 682 #if defined(OS_CHROMEOS) 683 class LoginObserver : public chromeos::LoginStatusConsumer { 684 public: 685 LoginObserver(chromeos::ExistingUserController* controller, 686 AutomationProvider* automation, 687 IPC::Message* reply_message); 688 689 virtual ~LoginObserver(); 690 691 virtual void OnLoginFailure(const chromeos::LoginFailure& error); 692 693 virtual void OnLoginSuccess( 694 const chromeos::UserContext& user_context, 695 bool pending_requests, 696 bool using_oauth); 697 698 private: 699 chromeos::ExistingUserController* controller_; 700 base::WeakPtr<AutomationProvider> automation_; 701 scoped_ptr<IPC::Message> reply_message_; 702 703 DISALLOW_COPY_AND_ASSIGN(LoginObserver); 704 }; 705 706 // Waits for a screen change notification from WizardController. 707 class WizardControllerObserver : public chromeos::WizardController::Observer, 708 public content::NotificationObserver { 709 public: 710 WizardControllerObserver(chromeos::WizardController* wizard_controller, 711 AutomationProvider* automation, 712 IPC::Message* reply_message); 713 virtual ~WizardControllerObserver(); 714 715 // If non-empty, waits for a specific change to screen with this name. 716 std::string screen_to_wait_for() { return screen_to_wait_for_; } 717 void set_screen_to_wait_for(const std::string& screen_name) { 718 screen_to_wait_for_ = screen_name; 719 } 720 721 protected: 722 // chromeos::WizardController::Observer overrides: 723 virtual void OnScreenChanged(chromeos::WizardScreen* next_screen) OVERRIDE; 724 virtual void OnSessionStart() OVERRIDE; 725 726 // content::NotificationObserver overrides: 727 void Observe(int type, 728 const content::NotificationSource& source, 729 const content::NotificationDetails& details); 730 731 // Sends reply with the given screen name and deletes |this|. 732 void SendReply(const std::string& screen_name); 733 734 content::NotificationRegistrar registrar_; 735 chromeos::WizardController* wizard_controller_; 736 base::WeakPtr<AutomationProvider> automation_; 737 scoped_ptr<IPC::Message> reply_message_; 738 std::string screen_to_wait_for_; 739 740 DISALLOW_COPY_AND_ASSIGN(WizardControllerObserver); 741 }; 742 743 // Collects SCREEN_LOCK_STATE_CHANGED notifications and returns 744 // whether authentication succeeded to the automation provider. 745 class ScreenLockUnlockObserver : public content::NotificationObserver { 746 public: 747 // Set lock_screen to true to observe lock screen events, 748 // false for unlock screen events. 749 ScreenLockUnlockObserver(AutomationProvider* automation, 750 IPC::Message* reply_message, 751 bool lock_screen); 752 virtual ~ScreenLockUnlockObserver(); 753 754 // Overridden from content::NotificationObserver: 755 virtual void Observe(int type, 756 const content::NotificationSource& source, 757 const content::NotificationDetails& details) OVERRIDE; 758 759 protected: 760 base::WeakPtr<AutomationProvider> automation_; 761 scoped_ptr<IPC::Message> reply_message_; 762 763 private: 764 content::NotificationRegistrar registrar_; 765 bool lock_screen_; 766 767 DISALLOW_COPY_AND_ASSIGN(ScreenLockUnlockObserver); 768 }; 769 770 // Watches SCREEN_LOCK_STATE_CHANGED notifications like the 771 // ScreenLockUnlockObserver, but additionally adds itself as an observer 772 // to a screen locker in order to monitor unlock failure cases. 773 class ScreenUnlockObserver : public ScreenLockUnlockObserver, 774 public chromeos::LoginStatusConsumer { 775 public: 776 ScreenUnlockObserver(AutomationProvider* automation, 777 IPC::Message* reply_message); 778 virtual ~ScreenUnlockObserver(); 779 780 virtual void OnLoginFailure(const chromeos::LoginFailure& error); 781 782 virtual void OnLoginSuccess( 783 const chromeos::UserContext& user_context, 784 bool pending_requests, 785 bool using_oauth) {} 786 787 private: 788 DISALLOW_COPY_AND_ASSIGN(ScreenUnlockObserver); 789 }; 790 791 class NetworkScanObserver 792 : public chromeos::NetworkLibrary::NetworkManagerObserver { 793 public: 794 NetworkScanObserver(AutomationProvider* automation, 795 IPC::Message* reply_message); 796 797 virtual ~NetworkScanObserver(); 798 799 // NetworkLibrary::NetworkManagerObserver implementation. 800 virtual void OnNetworkManagerChanged(chromeos::NetworkLibrary* obj); 801 802 private: 803 base::WeakPtr<AutomationProvider> automation_; 804 scoped_ptr<IPC::Message> reply_message_; 805 806 DISALLOW_COPY_AND_ASSIGN(NetworkScanObserver); 807 }; 808 809 class ToggleNetworkDeviceObserver 810 : public chromeos::NetworkLibrary::NetworkManagerObserver { 811 public: 812 ToggleNetworkDeviceObserver(AutomationProvider* automation, 813 IPC::Message* reply_message, 814 const std::string& device, 815 bool enable); 816 817 virtual ~ToggleNetworkDeviceObserver(); 818 819 // NetworkLibrary::NetworkManagerObserver implementation. 820 virtual void OnNetworkManagerChanged(chromeos::NetworkLibrary* obj); 821 822 private: 823 base::WeakPtr<AutomationProvider> automation_; 824 scoped_ptr<IPC::Message> reply_message_; 825 std::string device_; 826 bool enable_; 827 828 DISALLOW_COPY_AND_ASSIGN(ToggleNetworkDeviceObserver); 829 }; 830 831 class NetworkStatusObserver 832 : public chromeos::NetworkLibrary::NetworkManagerObserver { 833 public: 834 NetworkStatusObserver(AutomationProvider* automation, 835 IPC::Message* reply_message); 836 virtual ~NetworkStatusObserver(); 837 838 virtual const chromeos::Network* GetNetwork( 839 chromeos::NetworkLibrary* network_library) = 0; 840 // NetworkLibrary::NetworkManagerObserver implementation. 841 virtual void OnNetworkManagerChanged(chromeos::NetworkLibrary* obj); 842 virtual void NetworkStatusCheck(const chromeos::Network* network) = 0; 843 844 protected: 845 base::WeakPtr<AutomationProvider> automation_; 846 scoped_ptr<IPC::Message> reply_message_; 847 848 private: 849 DISALLOW_COPY_AND_ASSIGN(NetworkStatusObserver); 850 }; 851 852 // Waits for a connection success or failure for the specified 853 // network and returns the status to the automation provider. 854 class NetworkConnectObserver : public NetworkStatusObserver { 855 public: 856 NetworkConnectObserver(AutomationProvider* automation, 857 IPC::Message* reply_message); 858 859 virtual void NetworkStatusCheck(const chromeos::Network* network); 860 861 private: 862 DISALLOW_COPY_AND_ASSIGN(NetworkConnectObserver); 863 }; 864 865 // Waits until a network has disconnected. Then returns success 866 // or failure. 867 class NetworkDisconnectObserver : public NetworkStatusObserver { 868 public: 869 NetworkDisconnectObserver(AutomationProvider* automation, 870 IPC::Message* reply_message, 871 const std::string& service_path); 872 873 virtual void NetworkStatusCheck(const chromeos::Network* network); 874 const chromeos::Network* GetNetwork( 875 chromeos::NetworkLibrary* network_library); 876 877 private: 878 std::string service_path_; 879 DISALLOW_COPY_AND_ASSIGN(NetworkDisconnectObserver); 880 }; 881 882 // Waits for a connection success or failure for the specified 883 // network and returns the status to the automation provider. 884 class ServicePathConnectObserver : public NetworkConnectObserver { 885 public: 886 ServicePathConnectObserver(AutomationProvider* automation, 887 IPC::Message* reply_message, 888 const std::string& service_path); 889 890 const chromeos::Network* GetNetwork( 891 chromeos::NetworkLibrary* network_library); 892 893 private: 894 std::string service_path_; 895 DISALLOW_COPY_AND_ASSIGN(ServicePathConnectObserver); 896 }; 897 898 // Waits for a connection success or failure for the specified 899 // network and returns the status to the automation provider. 900 class SSIDConnectObserver : public NetworkConnectObserver { 901 public: 902 SSIDConnectObserver(AutomationProvider* automation, 903 IPC::Message* reply_message, 904 const std::string& ssid); 905 906 const chromeos::Network* GetNetwork( 907 chromeos::NetworkLibrary* network_library); 908 909 private: 910 std::string ssid_; 911 DISALLOW_COPY_AND_ASSIGN(SSIDConnectObserver); 912 }; 913 914 // Waits for a connection success or failure for the specified 915 // virtual network and returns the status to the automation provider. 916 class VirtualConnectObserver 917 : public chromeos::NetworkLibrary::NetworkManagerObserver { 918 public: 919 VirtualConnectObserver(AutomationProvider* automation, 920 IPC::Message* reply_message, 921 const std::string& service_name); 922 923 virtual ~VirtualConnectObserver(); 924 925 // NetworkLibrary::NetworkManagerObserver implementation. 926 virtual void OnNetworkManagerChanged(chromeos::NetworkLibrary* cros); 927 928 private: 929 virtual chromeos::VirtualNetwork* GetVirtualNetwork( 930 const chromeos::NetworkLibrary* cros); 931 932 base::WeakPtr<AutomationProvider> automation_; 933 scoped_ptr<IPC::Message> reply_message_; 934 std::string service_name_; 935 936 DISALLOW_COPY_AND_ASSIGN(VirtualConnectObserver); 937 }; 938 939 #endif // defined(OS_CHROMEOS) 940 941 // Waits for the bookmark model to load. 942 class AutomationProviderBookmarkModelObserver : public BookmarkModelObserver { 943 public: 944 AutomationProviderBookmarkModelObserver(AutomationProvider* provider, 945 IPC::Message* reply_message, 946 BookmarkModel* model, 947 bool use_json_interface); 948 virtual ~AutomationProviderBookmarkModelObserver(); 949 950 // BookmarkModelObserver: 951 virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE; 952 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE; 953 virtual void BookmarkNodeMoved(BookmarkModel* model, 954 const BookmarkNode* old_parent, 955 int old_index, 956 const BookmarkNode* new_parent, 957 int new_index) OVERRIDE {} 958 virtual void BookmarkNodeAdded(BookmarkModel* model, 959 const BookmarkNode* parent, 960 int index) OVERRIDE {} 961 virtual void BookmarkNodeRemoved(BookmarkModel* model, 962 const BookmarkNode* parent, 963 int old_index, 964 const BookmarkNode* node) OVERRIDE {} 965 virtual void BookmarkAllNodesRemoved(BookmarkModel* model) OVERRIDE {} 966 virtual void BookmarkNodeChanged(BookmarkModel* model, 967 const BookmarkNode* node) OVERRIDE {} 968 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model, 969 const BookmarkNode* node) OVERRIDE {} 970 virtual void BookmarkNodeChildrenReordered( 971 BookmarkModel* model, 972 const BookmarkNode* node) OVERRIDE {} 973 974 IPC::Message* ReleaseReply(); 975 976 private: 977 // Reply to the automation message with the given success value, 978 // then delete myself (which removes myself from the bookmark model 979 // observer list). 980 void ReplyAndDelete(bool success); 981 982 base::WeakPtr<AutomationProvider> automation_provider_; 983 scoped_ptr<IPC::Message> reply_message_; 984 BookmarkModel* model_; 985 bool use_json_interface_; 986 987 DISALLOW_COPY_AND_ASSIGN(AutomationProviderBookmarkModelObserver); 988 }; 989 990 // Allows the automation provider to wait until the download has been updated 991 // or opened. 992 class AutomationProviderDownloadUpdatedObserver 993 : public content::DownloadItem::Observer { 994 public: 995 AutomationProviderDownloadUpdatedObserver( 996 AutomationProvider* provider, 997 IPC::Message* reply_message, 998 bool wait_for_open, 999 bool incognito); 1000 virtual ~AutomationProviderDownloadUpdatedObserver(); 1001 1002 virtual void OnDownloadUpdated(content::DownloadItem* download); 1003 virtual void OnDownloadOpened(content::DownloadItem* download); 1004 1005 private: 1006 base::WeakPtr<AutomationProvider> provider_; 1007 scoped_ptr<IPC::Message> reply_message_; 1008 bool wait_for_open_; 1009 bool incognito_; 1010 1011 DISALLOW_COPY_AND_ASSIGN(AutomationProviderDownloadUpdatedObserver); 1012 }; 1013 1014 // Allows the automation provider to wait until the download model has changed 1015 // (because a new download has been added or removed). 1016 class AutomationProviderDownloadModelChangedObserver 1017 : public AllDownloadItemNotifier::Observer { 1018 public: 1019 AutomationProviderDownloadModelChangedObserver( 1020 AutomationProvider* provider, 1021 IPC::Message* reply_message, 1022 content::DownloadManager* download_manager); 1023 virtual ~AutomationProviderDownloadModelChangedObserver(); 1024 1025 virtual void OnDownloadCreated( 1026 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE; 1027 virtual void OnDownloadRemoved( 1028 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE; 1029 1030 private: 1031 void ModelChanged(); 1032 1033 base::WeakPtr<AutomationProvider> provider_; 1034 scoped_ptr<IPC::Message> reply_message_; 1035 AllDownloadItemNotifier notifier_; 1036 1037 DISALLOW_COPY_AND_ASSIGN(AutomationProviderDownloadModelChangedObserver); 1038 }; 1039 1040 // Observes when all pending downloads have completed. 1041 class AllDownloadsCompleteObserver 1042 : public content::DownloadManager::Observer, 1043 public content::DownloadItem::Observer { 1044 public: 1045 AllDownloadsCompleteObserver( 1046 AutomationProvider* provider, 1047 IPC::Message* reply_message, 1048 content::DownloadManager* download_manager, 1049 ListValue* pre_download_ids); 1050 virtual ~AllDownloadsCompleteObserver(); 1051 1052 // content::DownloadManager::Observer. 1053 virtual void OnDownloadCreated( 1054 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE; 1055 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; 1056 1057 // content::DownloadItem::Observer. 1058 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; 1059 1060 private: 1061 void ReplyIfNecessary(); 1062 1063 base::WeakPtr<AutomationProvider> provider_; 1064 scoped_ptr<IPC::Message> reply_message_; 1065 content::DownloadManager* download_manager_; 1066 std::set<int> pre_download_ids_; 1067 std::set<content::DownloadItem*> pending_downloads_; 1068 1069 DISALLOW_COPY_AND_ASSIGN(AllDownloadsCompleteObserver); 1070 }; 1071 1072 // Allows automation provider to wait until TemplateURLService has loaded 1073 // before looking up/returning search engine info. 1074 class AutomationProviderSearchEngineObserver 1075 : public TemplateURLServiceObserver { 1076 public: 1077 AutomationProviderSearchEngineObserver( 1078 AutomationProvider* provider, 1079 Profile* profile, 1080 IPC::Message* reply_message); 1081 virtual ~AutomationProviderSearchEngineObserver(); 1082 1083 virtual void OnTemplateURLServiceChanged(); 1084 1085 private: 1086 base::WeakPtr<AutomationProvider> provider_; 1087 Profile* profile_; 1088 scoped_ptr<IPC::Message> reply_message_; 1089 1090 DISALLOW_COPY_AND_ASSIGN(AutomationProviderSearchEngineObserver); 1091 }; 1092 1093 // Allows the automation provider to wait for history queries to finish. 1094 class AutomationProviderHistoryObserver { 1095 public: 1096 AutomationProviderHistoryObserver( 1097 AutomationProvider* provider, 1098 IPC::Message* reply_message); 1099 virtual ~AutomationProviderHistoryObserver(); 1100 1101 void HistoryQueryComplete(HistoryService::Handle request_handle, 1102 history::QueryResults* results); 1103 1104 private: 1105 base::WeakPtr<AutomationProvider> provider_; 1106 scoped_ptr<IPC::Message> reply_message_; 1107 }; 1108 1109 // Allows the automation provider to wait for import queries to finish. 1110 class AutomationProviderImportSettingsObserver 1111 : public importer::ImporterProgressObserver { 1112 public: 1113 AutomationProviderImportSettingsObserver( 1114 AutomationProvider* provider, 1115 IPC::Message* reply_message); 1116 virtual ~AutomationProviderImportSettingsObserver(); 1117 1118 // importer::ImporterProgressObserver: 1119 virtual void ImportStarted() OVERRIDE; 1120 virtual void ImportItemStarted(importer::ImportItem item) OVERRIDE; 1121 virtual void ImportItemEnded(importer::ImportItem item) OVERRIDE; 1122 virtual void ImportEnded() OVERRIDE; 1123 1124 private: 1125 base::WeakPtr<AutomationProvider> provider_; 1126 scoped_ptr<IPC::Message> reply_message_; 1127 }; 1128 1129 // Allows automation provider to wait for getting passwords to finish. 1130 class AutomationProviderGetPasswordsObserver : public PasswordStoreConsumer { 1131 public: 1132 AutomationProviderGetPasswordsObserver( 1133 AutomationProvider* provider, 1134 IPC::Message* reply_message); 1135 virtual ~AutomationProviderGetPasswordsObserver(); 1136 1137 // PasswordStoreConsumer implementation. 1138 virtual void OnPasswordStoreRequestDone( 1139 CancelableRequestProvider::Handle handle, 1140 const std::vector<content::PasswordForm*>& result) OVERRIDE; 1141 virtual void OnGetPasswordStoreResults( 1142 const std::vector<content::PasswordForm*>& results) OVERRIDE; 1143 1144 private: 1145 base::WeakPtr<AutomationProvider> provider_; 1146 scoped_ptr<IPC::Message> reply_message_; 1147 }; 1148 1149 // Observes when login entries stored in the password store are changed. The 1150 // notifications are sent on the DB thread, the thread that interacts with the 1151 // web database. 1152 class PasswordStoreLoginsChangedObserver 1153 : public base::RefCountedThreadSafe< 1154 PasswordStoreLoginsChangedObserver, 1155 content::BrowserThread::DeleteOnUIThread>, 1156 public content::NotificationObserver { 1157 public: 1158 PasswordStoreLoginsChangedObserver(AutomationProvider* automation, 1159 IPC::Message* reply_message, 1160 PasswordStoreChange::Type expected_type, 1161 const std::string& result_key); 1162 1163 // Schedules a task on the DB thread to register the appropriate observers. 1164 virtual void Init(); 1165 1166 // Overridden from content::NotificationObserver: 1167 virtual void Observe(int type, 1168 const content::NotificationSource& source, 1169 const content::NotificationDetails& details) OVERRIDE; 1170 1171 private: 1172 friend struct content::BrowserThread::DeleteOnThread< 1173 content::BrowserThread::UI>; 1174 ~PasswordStoreLoginsChangedObserver(); 1175 friend class base::DeleteHelper<PasswordStoreLoginsChangedObserver>; 1176 1177 // Registers the appropriate observers. Called on the DB thread. 1178 void RegisterObserversTask(); 1179 1180 // Sends the |reply_message_| to |automation_| indicating we're done. Called 1181 // on the UI thread. 1182 void IndicateDone(); 1183 1184 // Sends an error reply to |automation_|. Called on the UI thread. 1185 void IndicateError(const std::string& error); 1186 1187 base::WeakPtr<AutomationProvider> automation_; 1188 scoped_ptr<IPC::Message> reply_message_; 1189 scoped_ptr<content::NotificationRegistrar> registrar_; 1190 PasswordStoreChange::Type expected_type_; 1191 std::string result_key_; 1192 1193 // Used to ensure that the UI thread waits for the DB thread to finish 1194 // registering observers before proceeding. 1195 base::WaitableEvent done_event_; 1196 1197 DISALLOW_COPY_AND_ASSIGN(PasswordStoreLoginsChangedObserver); 1198 }; 1199 1200 // Allows automation provider to wait until page load after selecting an item 1201 // in the omnibox popup. 1202 class OmniboxAcceptNotificationObserver : public content::NotificationObserver { 1203 public: 1204 OmniboxAcceptNotificationObserver(content::NavigationController* controller, 1205 AutomationProvider* automation, 1206 IPC::Message* reply_message); 1207 virtual ~OmniboxAcceptNotificationObserver(); 1208 1209 // Overridden from content::NotificationObserver: 1210 virtual void Observe(int type, 1211 const content::NotificationSource& source, 1212 const content::NotificationDetails& details) OVERRIDE; 1213 1214 private: 1215 content::NotificationRegistrar registrar_; 1216 base::WeakPtr<AutomationProvider> automation_; 1217 scoped_ptr<IPC::Message> reply_message_; 1218 content::NavigationController* controller_; 1219 1220 DISALLOW_COPY_AND_ASSIGN(OmniboxAcceptNotificationObserver); 1221 }; 1222 1223 // Allows the automation provider to wait for a save package notification. 1224 class SavePackageNotificationObserver 1225 : public content::DownloadManager::Observer { 1226 public: 1227 SavePackageNotificationObserver(content::DownloadManager* download_manager, 1228 AutomationProvider* automation, 1229 IPC::Message* reply_message); 1230 virtual ~SavePackageNotificationObserver(); 1231 1232 // Overridden from content::DownloadManager::Observer: 1233 virtual void OnSavePackageSuccessfullyFinished( 1234 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE; 1235 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; 1236 1237 private: 1238 content::DownloadManager* download_manager_; 1239 base::WeakPtr<AutomationProvider> automation_; 1240 scoped_ptr<IPC::Message> reply_message_; 1241 1242 DISALLOW_COPY_AND_ASSIGN(SavePackageNotificationObserver); 1243 }; 1244 1245 class NTPInfoObserver : public content::NotificationObserver { 1246 public: 1247 NTPInfoObserver(AutomationProvider* automation, IPC::Message* reply_message); 1248 virtual ~NTPInfoObserver(); 1249 1250 // Overridden from content::NotificationObserver: 1251 virtual void Observe(int type, 1252 const content::NotificationSource& source, 1253 const content::NotificationDetails& details) OVERRIDE; 1254 1255 private: 1256 void OnTopSitesLoaded(); 1257 void OnTopSitesReceived(const history::MostVisitedURLList& visited_list); 1258 1259 base::WeakPtr<AutomationProvider> automation_; 1260 scoped_ptr<IPC::Message> reply_message_; 1261 CancelableRequestProvider::Handle request_; 1262 scoped_ptr<base::DictionaryValue> ntp_info_; 1263 history::TopSites* top_sites_; 1264 content::NotificationRegistrar registrar_; 1265 1266 DISALLOW_COPY_AND_ASSIGN(NTPInfoObserver); 1267 }; 1268 1269 // Observes when an app has been launched, as indicated by a notification that 1270 // a content load in some tab has stopped. 1271 class AppLaunchObserver : public content::NotificationObserver { 1272 public: 1273 AppLaunchObserver(content::NavigationController* controller, 1274 AutomationProvider* automation, 1275 IPC::Message* reply_message, 1276 extension_misc::LaunchContainer launch_container); 1277 virtual ~AppLaunchObserver(); 1278 1279 // Overridden from content::NotificationObserver: 1280 virtual void Observe(int type, 1281 const content::NotificationSource& source, 1282 const content::NotificationDetails& details) OVERRIDE; 1283 1284 private: 1285 content::NavigationController* controller_; 1286 base::WeakPtr<AutomationProvider> automation_; 1287 scoped_ptr<IPC::Message> reply_message_; 1288 content::NotificationRegistrar registrar_; 1289 extension_misc::LaunchContainer launch_container_; 1290 int new_window_id_; 1291 1292 DISALLOW_COPY_AND_ASSIGN(AppLaunchObserver); 1293 }; 1294 1295 // Allows the automation provider to wait until all the notification 1296 // processes are ready. 1297 class GetAllNotificationsObserver : public content::NotificationObserver { 1298 public: 1299 GetAllNotificationsObserver(AutomationProvider* automation, 1300 IPC::Message* reply_message); 1301 virtual ~GetAllNotificationsObserver(); 1302 1303 // Overridden from content::NotificationObserver: 1304 virtual void Observe(int type, 1305 const content::NotificationSource& source, 1306 const content::NotificationDetails& details) OVERRIDE; 1307 1308 private: 1309 // Sends a message via the |AutomationProvider|. |automation_| must be valid. 1310 // Deletes itself after the message is sent. 1311 void SendMessage(); 1312 // Returns a new dictionary describing the given notification. 1313 base::DictionaryValue* NotificationToJson(const Notification* note); 1314 1315 content::NotificationRegistrar registrar_; 1316 base::WeakPtr<AutomationProvider> automation_; 1317 scoped_ptr<IPC::Message> reply_message_; 1318 1319 DISALLOW_COPY_AND_ASSIGN(GetAllNotificationsObserver); 1320 }; 1321 1322 // Allows the automation provider to wait for a new notification balloon 1323 // to appear and be ready. 1324 class NewNotificationBalloonObserver : public content::NotificationObserver { 1325 public: 1326 NewNotificationBalloonObserver(AutomationProvider* provider, 1327 IPC::Message* reply_message); 1328 virtual ~NewNotificationBalloonObserver(); 1329 1330 // Overridden from content::NotificationObserver: 1331 virtual void Observe(int type, 1332 const content::NotificationSource& source, 1333 const content::NotificationDetails& details) OVERRIDE; 1334 1335 private: 1336 content::NotificationRegistrar registrar_; 1337 base::WeakPtr<AutomationProvider> automation_; 1338 scoped_ptr<IPC::Message> reply_message_; 1339 1340 DISALLOW_COPY_AND_ASSIGN(NewNotificationBalloonObserver); 1341 }; 1342 1343 // Allows the automation provider to wait for a given number of 1344 // notification balloons. 1345 class OnNotificationBalloonCountObserver 1346 : public content::NotificationObserver { 1347 public: 1348 OnNotificationBalloonCountObserver(AutomationProvider* provider, 1349 IPC::Message* reply_message, 1350 int count); 1351 virtual ~OnNotificationBalloonCountObserver(); 1352 1353 // Sends an automation reply message if |automation_| is still valid and the 1354 // number of ready balloons matches the desired count. Deletes itself if the 1355 // message is sent or if |automation_| is invalid. 1356 void CheckBalloonCount(); 1357 1358 // Overridden from content::NotificationObserver: 1359 virtual void Observe(int type, 1360 const content::NotificationSource& source, 1361 const content::NotificationDetails& details) OVERRIDE; 1362 1363 private: 1364 content::NotificationRegistrar registrar_; 1365 base::WeakPtr<AutomationProvider> automation_; 1366 scoped_ptr<IPC::Message> reply_message_; 1367 1368 BalloonCollection* collection_; 1369 int count_; 1370 1371 DISALLOW_COPY_AND_ASSIGN(OnNotificationBalloonCountObserver); 1372 }; 1373 1374 // Allows the automation provider to wait for a RENDERER_PROCESS_CLOSED 1375 // notification. 1376 class RendererProcessClosedObserver : public content::NotificationObserver { 1377 public: 1378 RendererProcessClosedObserver(AutomationProvider* automation, 1379 IPC::Message* reply_message); 1380 virtual ~RendererProcessClosedObserver(); 1381 1382 // Overridden from content::NotificationObserver: 1383 virtual void Observe(int type, 1384 const content::NotificationSource& source, 1385 const content::NotificationDetails& details) OVERRIDE; 1386 1387 private: 1388 content::NotificationRegistrar registrar_; 1389 base::WeakPtr<AutomationProvider> automation_; 1390 scoped_ptr<IPC::Message> reply_message_; 1391 1392 DISALLOW_COPY_AND_ASSIGN(RendererProcessClosedObserver); 1393 }; 1394 1395 // Allows the automation provider to wait for acknowledgement that a certain 1396 // type and number of input events has been processed by the renderer. 1397 class InputEventAckNotificationObserver : public content::NotificationObserver { 1398 public: 1399 InputEventAckNotificationObserver(AutomationProvider* automation, 1400 IPC::Message* reply_message, 1401 int event_type, int count); 1402 virtual ~InputEventAckNotificationObserver(); 1403 1404 // Overridden from content::NotificationObserver: 1405 virtual void Observe(int type, 1406 const content::NotificationSource& source, 1407 const content::NotificationDetails& details) OVERRIDE; 1408 1409 private: 1410 content::NotificationRegistrar registrar_; 1411 base::WeakPtr<AutomationProvider> automation_; 1412 scoped_ptr<IPC::Message> reply_message_; 1413 int event_type_; 1414 int count_; 1415 1416 DISALLOW_COPY_AND_ASSIGN(InputEventAckNotificationObserver); 1417 }; 1418 1419 // Observer used to listen for new tab creation to complete. 1420 class NewTabObserver : public content::NotificationObserver { 1421 public: 1422 NewTabObserver(AutomationProvider* automation, 1423 IPC::Message* reply_message, 1424 bool use_json_interface); 1425 1426 // Overridden from content::NotificationObserver: 1427 virtual void Observe(int type, 1428 const content::NotificationSource& source, 1429 const content::NotificationDetails& details) OVERRIDE; 1430 1431 private: 1432 virtual ~NewTabObserver(); 1433 1434 content::NotificationRegistrar registrar_; 1435 base::WeakPtr<AutomationProvider> automation_; 1436 scoped_ptr<IPC::Message> reply_message_; 1437 bool use_json_interface_; 1438 1439 DISALLOW_COPY_AND_ASSIGN(NewTabObserver); 1440 }; 1441 1442 // Posts a task to the PROCESS_LAUNCHER thread, once processed posts a task 1443 // back to the UI thread that notifies the provider we're done. 1444 class WaitForProcessLauncherThreadToGoIdleObserver 1445 : public base::RefCountedThreadSafe< 1446 WaitForProcessLauncherThreadToGoIdleObserver, 1447 content::BrowserThread::DeleteOnUIThread> { 1448 public: 1449 WaitForProcessLauncherThreadToGoIdleObserver( 1450 AutomationProvider* automation, IPC::Message* reply_message); 1451 1452 private: 1453 friend struct content::BrowserThread::DeleteOnThread< 1454 content::BrowserThread::UI>; 1455 friend class base::DeleteHelper<WaitForProcessLauncherThreadToGoIdleObserver>; 1456 1457 virtual ~WaitForProcessLauncherThreadToGoIdleObserver(); 1458 1459 // Schedules a task on the PROCESS_LAUNCHER thread to execute 1460 // |RunOnProcessLauncherThread2|. By the time the task is executed the 1461 // PROCESS_LAUNCHER thread should be some what idle. 1462 void RunOnProcessLauncherThread(); 1463 1464 // When executed the PROCESS_LAUNCHER thread should have processed any pending 1465 // tasks. Schedules a task on the UI thread that sends the message saying 1466 // we're done. 1467 void RunOnProcessLauncherThread2(); 1468 1469 // Sends the |reply_message_| to |automation_| indicating we're done. 1470 void RunOnUIThread(); 1471 1472 base::WeakPtr<AutomationProvider> automation_; 1473 scoped_ptr<IPC::Message> reply_message_; 1474 1475 DISALLOW_COPY_AND_ASSIGN(WaitForProcessLauncherThreadToGoIdleObserver); 1476 }; 1477 1478 // Allows the automation provider to wait for acknowledgement that a drop 1479 // operation has been processed by the renderer. 1480 class DragTargetDropAckNotificationObserver 1481 : public content::NotificationObserver { 1482 public: 1483 DragTargetDropAckNotificationObserver(AutomationProvider* automation, 1484 IPC::Message* reply_message); 1485 virtual ~DragTargetDropAckNotificationObserver(); 1486 1487 // Overridden from content::NotificationObserver: 1488 virtual void Observe(int type, 1489 const content::NotificationSource& source, 1490 const content::NotificationDetails& details) OVERRIDE; 1491 1492 private: 1493 content::NotificationRegistrar registrar_; 1494 base::WeakPtr<AutomationProvider> automation_; 1495 scoped_ptr<IPC::Message> reply_message_; 1496 1497 DISALLOW_COPY_AND_ASSIGN(DragTargetDropAckNotificationObserver); 1498 }; 1499 1500 // Allows the automation provider to wait for process memory details to be 1501 // available before sending this information to the client. 1502 class ProcessInfoObserver : public MemoryDetails { 1503 public: 1504 ProcessInfoObserver(AutomationProvider* automation, 1505 IPC::Message* reply_message); 1506 1507 virtual void OnDetailsAvailable() OVERRIDE; 1508 1509 private: 1510 virtual ~ProcessInfoObserver(); 1511 base::WeakPtr<AutomationProvider> automation_; 1512 scoped_ptr<IPC::Message> reply_message_; 1513 1514 DISALLOW_COPY_AND_ASSIGN(ProcessInfoObserver); 1515 }; 1516 1517 // Observes when new v8 heap statistics are computed for a renderer process. 1518 class V8HeapStatsObserver : public content::NotificationObserver { 1519 public: 1520 V8HeapStatsObserver(AutomationProvider* automation, 1521 IPC::Message* reply_message, 1522 base::ProcessId renderer_id); 1523 virtual ~V8HeapStatsObserver(); 1524 1525 // Overridden from content::NotificationObserver: 1526 virtual void Observe(int type, 1527 const content::NotificationSource& source, 1528 const content::NotificationDetails& details) OVERRIDE; 1529 1530 private: 1531 content::NotificationRegistrar registrar_; 1532 base::WeakPtr<AutomationProvider> automation_; 1533 scoped_ptr<IPC::Message> reply_message_; 1534 base::ProcessId renderer_id_; 1535 1536 DISALLOW_COPY_AND_ASSIGN(V8HeapStatsObserver); 1537 }; 1538 1539 // Observes when a new FPS value is computed for a renderer process. 1540 class FPSObserver : public content::NotificationObserver { 1541 public: 1542 FPSObserver(AutomationProvider* automation, 1543 IPC::Message* reply_message, 1544 base::ProcessId renderer_id, 1545 int routing_id); 1546 virtual ~FPSObserver(); 1547 1548 // Overridden from content::NotificationObserver: 1549 virtual void Observe(int type, 1550 const content::NotificationSource& source, 1551 const content::NotificationDetails& details) OVERRIDE; 1552 1553 private: 1554 content::NotificationRegistrar registrar_; 1555 base::WeakPtr<AutomationProvider> automation_; 1556 scoped_ptr<IPC::Message> reply_message_; 1557 base::ProcessId renderer_id_; 1558 int routing_id_; 1559 1560 DISALLOW_COPY_AND_ASSIGN(FPSObserver); 1561 }; 1562 1563 // Manages the process of creating a new Profile and opening a new browser with 1564 // that profile. This observer should be created, and then a new Profile 1565 // should be created through the ProfileManager using 1566 // profile_manager->CreateMultiProfileAsync(). The Observer watches for profile 1567 // creation, upon which it creates a new browser window; after observing 1568 // window creation, it creates a new tab and then finally observes it finish 1569 // loading. 1570 class BrowserOpenedWithNewProfileNotificationObserver 1571 : public content::NotificationObserver { 1572 public: 1573 BrowserOpenedWithNewProfileNotificationObserver( 1574 AutomationProvider* automation, 1575 IPC::Message* reply_message); 1576 virtual ~BrowserOpenedWithNewProfileNotificationObserver(); 1577 1578 // Overridden from content::NotificationObserver: 1579 virtual void Observe(int type, 1580 const content::NotificationSource& source, 1581 const content::NotificationDetails& details) OVERRIDE; 1582 1583 private: 1584 content::NotificationRegistrar registrar_; 1585 base::WeakPtr<AutomationProvider> automation_; 1586 scoped_ptr<IPC::Message> reply_message_; 1587 int new_window_id_; 1588 1589 DISALLOW_COPY_AND_ASSIGN(BrowserOpenedWithNewProfileNotificationObserver); 1590 }; 1591 1592 // Waits for an extension popup to appear and load. 1593 class ExtensionPopupObserver : public content::NotificationObserver { 1594 public: 1595 ExtensionPopupObserver( 1596 AutomationProvider* automation, 1597 IPC::Message* reply_message, 1598 const std::string& extension_id); 1599 ~ExtensionPopupObserver(); 1600 1601 // Overridden from content::NotificationObserver: 1602 virtual void Observe(int type, 1603 const content::NotificationSource& source, 1604 const content::NotificationDetails& details) OVERRIDE; 1605 1606 private: 1607 base::WeakPtr<AutomationProvider> automation_; 1608 scoped_ptr<IPC::Message> reply_message_; 1609 std::string extension_id_; 1610 content::NotificationRegistrar registrar_; 1611 1612 DISALLOW_COPY_AND_ASSIGN(ExtensionPopupObserver); 1613 }; 1614 1615 #if defined(OS_LINUX) 1616 // Allows the automation provider to wait for a WINDOW_MAXIMIZED notification. 1617 class WindowMaximizedObserver : public content::NotificationObserver { 1618 public: 1619 WindowMaximizedObserver(AutomationProvider* automation, 1620 IPC::Message* reply_message); 1621 virtual ~WindowMaximizedObserver(); 1622 1623 // Overridden from content::NotificationObserver: 1624 virtual void Observe(int type, 1625 const content::NotificationSource& source, 1626 const content::NotificationDetails& details) OVERRIDE; 1627 1628 private: 1629 content::NotificationRegistrar registrar_; 1630 base::WeakPtr<AutomationProvider> automation_; 1631 scoped_ptr<IPC::Message> reply_message_; 1632 1633 DISALLOW_COPY_AND_ASSIGN(WindowMaximizedObserver); 1634 }; 1635 #endif // defined(OS_LINUX) 1636 1637 // Wait for a new browser window to get created (for an existing profile). 1638 // Useful when reopening a multi-profile window. 1639 class BrowserOpenedWithExistingProfileNotificationObserver 1640 : public content::NotificationObserver { 1641 public: 1642 BrowserOpenedWithExistingProfileNotificationObserver( 1643 AutomationProvider* automation, 1644 IPC::Message* reply_message, 1645 int num_loads); 1646 virtual ~BrowserOpenedWithExistingProfileNotificationObserver(); 1647 1648 virtual void Observe(int type, 1649 const content::NotificationSource& source, 1650 const content::NotificationDetails& details); 1651 private: 1652 content::NotificationRegistrar registrar_; 1653 base::WeakPtr<AutomationProvider> automation_; 1654 scoped_ptr<IPC::Message> reply_message_; 1655 int new_window_id_; 1656 int num_loads_; 1657 1658 DISALLOW_COPY_AND_ASSIGN( 1659 BrowserOpenedWithExistingProfileNotificationObserver); 1660 }; 1661 1662 #endif // CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_OBSERVERS_H_ 1663