Home | History | Annotate | Download | only in automation
      1 // Copyright (c) 2011 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 #pragma once
      8 
      9 #include <deque>
     10 #include <map>
     11 #include <set>
     12 #include <string>
     13 #include <vector>
     14 
     15 #include "base/compiler_specific.h"
     16 #include "base/memory/scoped_ptr.h"
     17 #include "base/memory/weak_ptr.h"
     18 #include "chrome/browser/automation/automation_provider_json.h"
     19 #include "chrome/browser/automation/automation_tab_helper.h"
     20 #include "chrome/browser/bookmarks/bookmark_model_observer.h"
     21 #include "chrome/browser/browsing_data_remover.h"
     22 #if defined(OS_CHROMEOS)
     23 #include "chrome/browser/chromeos/cros/network_library.h"
     24 #endif  // defined(OS_CHROMEOS)
     25 #include "chrome/browser/download/download_item.h"
     26 #include "chrome/browser/download/download_manager.h"
     27 #include "chrome/browser/history/history.h"
     28 #include "chrome/browser/history/history_types.h"
     29 #include "chrome/browser/importer/importer_data_types.h"
     30 #include "chrome/browser/importer/importer_progress_observer.h"
     31 #include "chrome/browser/password_manager/password_store_consumer.h"
     32 #include "chrome/browser/search_engines/template_url_model_observer.h"
     33 #include "chrome/browser/tabs/tab_strip_model.h"
     34 #include "chrome/common/automation_constants.h"
     35 #include "chrome/common/extensions/extension_constants.h"
     36 #include "content/browser/cancelable_request.h"
     37 #include "content/common/notification_observer.h"
     38 #include "content/common/notification_registrar.h"
     39 #include "content/common/notification_type.h"
     40 #include "ui/gfx/size.h"
     41 
     42 class AutocompleteEditModel;
     43 class AutomationProvider;
     44 class BalloonCollection;
     45 class Browser;
     46 class Extension;
     47 class ExtensionProcessManager;
     48 class NavigationController;
     49 class RenderViewHost;
     50 class SavePackage;
     51 class TabContents;
     52 class TranslateInfoBarDelegate;
     53 
     54 namespace history {
     55 class TopSites;
     56 }
     57 
     58 namespace IPC {
     59 class Message;
     60 }
     61 
     62 class InitialLoadObserver : public NotificationObserver {
     63  public:
     64   InitialLoadObserver(size_t tab_count, AutomationProvider* automation);
     65   virtual ~InitialLoadObserver();
     66 
     67   virtual void Observe(NotificationType type,
     68                        const NotificationSource& source,
     69                        const NotificationDetails& details);
     70 
     71   // Caller owns the return value and is responsible for deleting it.
     72   // Example return value:
     73   // {'tabs': [{'start_time_ms': 1, 'stop_time_ms': 2.5},
     74   //           {'start_time_ms': 0.5, 'stop_time_ms': 3}]}
     75   // stop_time_ms values may be null if WaitForInitialLoads has not finished.
     76   // Only includes entries for the |tab_count| tabs we are monitoring.
     77   // There is no defined ordering of the return value.
     78   DictionaryValue* GetTimingInformation() const;
     79 
     80  private:
     81   class TabTime;
     82   typedef std::map<uintptr_t, TabTime> TabTimeMap;
     83   typedef std::set<uintptr_t> TabSet;
     84 
     85   void ConditionMet();
     86 
     87   NotificationRegistrar registrar_;
     88 
     89   base::WeakPtr<AutomationProvider> automation_;
     90   size_t outstanding_tab_count_;
     91   base::TimeTicks init_time_;
     92   TabTimeMap loading_tabs_;
     93   TabSet finished_tabs_;
     94 
     95   DISALLOW_COPY_AND_ASSIGN(InitialLoadObserver);
     96 };
     97 
     98 #if defined(OS_CHROMEOS)
     99 // Watches for NetworkManager events. Because NetworkLibrary loads
    100 // asynchronously, this is used to make sure it is done before tests are run.
    101 class NetworkManagerInitObserver
    102     : public chromeos::NetworkLibrary::NetworkManagerObserver {
    103  public:
    104   explicit NetworkManagerInitObserver(AutomationProvider* automation);
    105   virtual ~NetworkManagerInitObserver();
    106   virtual bool Init();
    107   virtual void OnNetworkManagerChanged(chromeos::NetworkLibrary* obj);
    108 
    109  private:
    110   base::WeakPtr<AutomationProvider> automation_;
    111 
    112   DISALLOW_COPY_AND_ASSIGN(NetworkManagerInitObserver);
    113 };
    114 #endif  // defined(OS_CHROMEOS)
    115 
    116 // Watches for NewTabUI page loads for performance timing purposes.
    117 class NewTabUILoadObserver : public NotificationObserver {
    118  public:
    119   explicit NewTabUILoadObserver(AutomationProvider* automation);
    120   virtual ~NewTabUILoadObserver();
    121 
    122   virtual void Observe(NotificationType type,
    123                        const NotificationSource& source,
    124                        const NotificationDetails& details);
    125 
    126  private:
    127   NotificationRegistrar registrar_;
    128   base::WeakPtr<AutomationProvider> automation_;
    129 
    130   DISALLOW_COPY_AND_ASSIGN(NewTabUILoadObserver);
    131 };
    132 
    133 class NavigationControllerRestoredObserver : public NotificationObserver {
    134  public:
    135   NavigationControllerRestoredObserver(AutomationProvider* automation,
    136                                        NavigationController* controller,
    137                                        IPC::Message* reply_message);
    138   virtual ~NavigationControllerRestoredObserver();
    139 
    140   virtual void Observe(NotificationType type,
    141                        const NotificationSource& source,
    142                        const NotificationDetails& details);
    143 
    144  private:
    145   bool FinishedRestoring();
    146   void SendDone();
    147 
    148   NotificationRegistrar registrar_;
    149   base::WeakPtr<AutomationProvider> automation_;
    150   NavigationController* controller_;
    151   scoped_ptr<IPC::Message> reply_message_;
    152 
    153   DISALLOW_COPY_AND_ASSIGN(NavigationControllerRestoredObserver);
    154 };
    155 
    156 class NavigationNotificationObserver : public NotificationObserver {
    157  public:
    158   NavigationNotificationObserver(NavigationController* controller,
    159                                  AutomationProvider* automation,
    160                                  IPC::Message* reply_message,
    161                                  int number_of_navigations,
    162                                  bool include_current_navigation,
    163                                  bool use_json_interface);
    164   virtual ~NavigationNotificationObserver();
    165 
    166   virtual void Observe(NotificationType type,
    167                        const NotificationSource& source,
    168                        const NotificationDetails& details);
    169 
    170  private:
    171   void ConditionMet(AutomationMsg_NavigationResponseValues navigation_result);
    172 
    173   NotificationRegistrar registrar_;
    174   base::WeakPtr<AutomationProvider> automation_;
    175   scoped_ptr<IPC::Message> reply_message_;
    176   NavigationController* controller_;
    177   int navigations_remaining_;
    178   bool navigation_started_;
    179   bool use_json_interface_;
    180 
    181   DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver);
    182 };
    183 
    184 class TabStripNotificationObserver : public NotificationObserver {
    185  public:
    186   TabStripNotificationObserver(NotificationType notification,
    187                                AutomationProvider* automation);
    188   virtual ~TabStripNotificationObserver();
    189 
    190   virtual void Observe(NotificationType type,
    191                        const NotificationSource& source,
    192                        const NotificationDetails& details);
    193 
    194   virtual void ObserveTab(NavigationController* controller) = 0;
    195 
    196  protected:
    197   NotificationRegistrar registrar_;
    198   base::WeakPtr<AutomationProvider> automation_;
    199   NotificationType notification_;
    200 };
    201 
    202 class TabAppendedNotificationObserver : public TabStripNotificationObserver {
    203  public:
    204   TabAppendedNotificationObserver(Browser* parent,
    205                                   AutomationProvider* automation,
    206                                   IPC::Message* reply_message);
    207   virtual ~TabAppendedNotificationObserver();
    208 
    209   virtual void ObserveTab(NavigationController* controller);
    210 
    211  protected:
    212   Browser* parent_;
    213   scoped_ptr<IPC::Message> reply_message_;
    214 
    215  private:
    216   DISALLOW_COPY_AND_ASSIGN(TabAppendedNotificationObserver);
    217 };
    218 
    219 class TabClosedNotificationObserver : public TabStripNotificationObserver {
    220  public:
    221   TabClosedNotificationObserver(AutomationProvider* automation,
    222                                 bool wait_until_closed,
    223                                 IPC::Message* reply_message);
    224   virtual ~TabClosedNotificationObserver();
    225 
    226   virtual void ObserveTab(NavigationController* controller);
    227 
    228   void set_for_browser_command(bool for_browser_command);
    229 
    230  protected:
    231   scoped_ptr<IPC::Message> reply_message_;
    232   bool for_browser_command_;
    233 
    234  private:
    235   DISALLOW_COPY_AND_ASSIGN(TabClosedNotificationObserver);
    236 };
    237 
    238 // Notifies when the tab count reaches the target number.
    239 class TabCountChangeObserver : public TabStripModelObserver {
    240  public:
    241   TabCountChangeObserver(AutomationProvider* automation,
    242                          Browser* browser,
    243                          IPC::Message* reply_message,
    244                          int target_tab_count);
    245   // Implementation of TabStripModelObserver.
    246   virtual void TabInsertedAt(TabContentsWrapper* contents,
    247                              int index,
    248                              bool foreground);
    249   virtual void TabDetachedAt(TabContentsWrapper* contents, int index);
    250   virtual void TabStripModelDeleted();
    251 
    252  private:
    253   virtual ~TabCountChangeObserver();
    254 
    255   // Checks if the current tab count matches our target, and if so,
    256   // sends the reply message and deletes self.
    257   void CheckTabCount();
    258 
    259   base::WeakPtr<AutomationProvider> automation_;
    260   scoped_ptr<IPC::Message> reply_message_;
    261 
    262   TabStripModel* tab_strip_model_;
    263 
    264   const int target_tab_count_;
    265 
    266   DISALLOW_COPY_AND_ASSIGN(TabCountChangeObserver);
    267 };
    268 
    269 // Observes when an extension has finished installing or possible install
    270 // errors. This does not guarantee that the extension is ready for use.
    271 class ExtensionInstallNotificationObserver : public NotificationObserver {
    272  public:
    273   ExtensionInstallNotificationObserver(AutomationProvider* automation,
    274                                        int id,
    275                                        IPC::Message* reply_message);
    276   virtual ~ExtensionInstallNotificationObserver();
    277 
    278   // Implementation of NotificationObserver.
    279   virtual void Observe(NotificationType type,
    280                        const NotificationSource& source,
    281                        const NotificationDetails& details);
    282 
    283  private:
    284   // Send |response| back to the provider's client.
    285   void SendResponse(AutomationMsg_ExtensionResponseValues response);
    286 
    287   NotificationRegistrar registrar_;
    288   base::WeakPtr<AutomationProvider> automation_;
    289   int id_;
    290   scoped_ptr<IPC::Message> reply_message_;
    291 
    292   DISALLOW_COPY_AND_ASSIGN(ExtensionInstallNotificationObserver);
    293 };
    294 
    295 // Observes when an extension has been uninstalled.
    296 class ExtensionUninstallObserver : public NotificationObserver {
    297  public:
    298   ExtensionUninstallObserver(AutomationProvider* automation,
    299                              IPC::Message* reply_message,
    300                              const std::string& id);
    301   virtual ~ExtensionUninstallObserver();
    302 
    303   // Implementation of NotificationObserver.
    304   virtual void Observe(NotificationType type,
    305                        const NotificationSource& source,
    306                        const NotificationDetails& details);
    307 
    308  private:
    309   NotificationRegistrar registrar_;
    310   base::WeakPtr<AutomationProvider> automation_;
    311   scoped_ptr<IPC::Message> reply_message_;
    312   std::string id_;
    313 
    314   DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallObserver);
    315 };
    316 
    317 // Observes when an extension has finished loading and is ready for use. Also
    318 // checks for possible install errors.
    319 class ExtensionReadyNotificationObserver : public NotificationObserver {
    320  public:
    321   ExtensionReadyNotificationObserver(ExtensionProcessManager* manager,
    322                                      AutomationProvider* automation,
    323                                      int id,
    324                                      IPC::Message* reply_message);
    325   virtual ~ExtensionReadyNotificationObserver();
    326 
    327   // Implementation of NotificationObserver.
    328   virtual void Observe(NotificationType type,
    329                        const NotificationSource& source,
    330                        const NotificationDetails& details);
    331 
    332  private:
    333   NotificationRegistrar registrar_;
    334   ExtensionProcessManager* manager_;
    335   base::WeakPtr<AutomationProvider> automation_;
    336   int id_;
    337   scoped_ptr<IPC::Message> reply_message_;
    338   const Extension* extension_;
    339 
    340   DISALLOW_COPY_AND_ASSIGN(ExtensionReadyNotificationObserver);
    341 };
    342 
    343 class ExtensionUnloadNotificationObserver : public NotificationObserver {
    344  public:
    345   ExtensionUnloadNotificationObserver();
    346   virtual ~ExtensionUnloadNotificationObserver();
    347 
    348   // Implementation of NotificationObserver.
    349   virtual void Observe(NotificationType type,
    350                        const NotificationSource& source,
    351                        const NotificationDetails& details);
    352 
    353   bool did_receive_unload_notification() {
    354     return did_receive_unload_notification_;
    355   }
    356 
    357  private:
    358   NotificationRegistrar registrar_;
    359   bool did_receive_unload_notification_;
    360 
    361   DISALLOW_COPY_AND_ASSIGN(ExtensionUnloadNotificationObserver);
    362 };
    363 
    364 // Observes when the extensions have been fully updated.  The ExtensionUpdater
    365 // service provides notifications for each extension that gets updated, but
    366 // it does not wait for the updated extensions to be installed or loaded.  This
    367 // observer waits until all updated extensions have actually been loaded.
    368 class ExtensionsUpdatedObserver : public NotificationObserver {
    369  public:
    370   ExtensionsUpdatedObserver(ExtensionProcessManager* manager,
    371                             AutomationProvider* automation,
    372                             IPC::Message* reply_message);
    373   virtual ~ExtensionsUpdatedObserver();
    374 
    375   // Implementation of NotificationObserver.
    376   virtual void Observe(NotificationType type,
    377                        const NotificationSource& source,
    378                        const NotificationDetails& details);
    379 
    380  private:
    381   NotificationRegistrar registrar_;
    382   ExtensionProcessManager* manager_;
    383   base::WeakPtr<AutomationProvider> automation_;
    384   scoped_ptr<IPC::Message> reply_message_;
    385   std::set<std::string> in_progress_updates_;
    386   bool updater_finished_;
    387 
    388   DISALLOW_COPY_AND_ASSIGN(ExtensionsUpdatedObserver);
    389 };
    390 
    391 class ExtensionTestResultNotificationObserver : public NotificationObserver {
    392  public:
    393   explicit ExtensionTestResultNotificationObserver(
    394       AutomationProvider* automation);
    395   virtual ~ExtensionTestResultNotificationObserver();
    396 
    397   // Implementation of NotificationObserver.
    398   virtual void Observe(NotificationType type,
    399                        const NotificationSource& source,
    400                        const NotificationDetails& details);
    401 
    402   // Sends a test result back to the provider's client, if there is a pending
    403   // provider message and there is a result in the queue.
    404   void MaybeSendResult();
    405 
    406  private:
    407   NotificationRegistrar registrar_;
    408   base::WeakPtr<AutomationProvider> automation_;
    409   // Two queues containing the test results. Although typically only
    410   // one result will be in each queue, there are cases where a queue is
    411   // needed.
    412   // For example, perhaps two events occur asynchronously and their
    413   // order of completion is not guaranteed. If the test wants to make sure
    414   // both finish before continuing, a queue is needed. The test would then
    415   // need to wait twice.
    416   std::deque<bool> results_;
    417   std::deque<std::string> messages_;
    418 
    419   DISALLOW_COPY_AND_ASSIGN(ExtensionTestResultNotificationObserver);
    420 };
    421 
    422 // Observes when a new browser has been opened and a tab within it has stopped
    423 // loading.
    424 class BrowserOpenedNotificationObserver : public NotificationObserver {
    425  public:
    426   BrowserOpenedNotificationObserver(AutomationProvider* automation,
    427                                     IPC::Message* reply_message);
    428   virtual ~BrowserOpenedNotificationObserver();
    429 
    430   virtual void Observe(NotificationType type,
    431                        const NotificationSource& source,
    432                        const NotificationDetails& details);
    433 
    434   void set_for_browser_command(bool for_browser_command);
    435 
    436  private:
    437   NotificationRegistrar registrar_;
    438   base::WeakPtr<AutomationProvider> automation_;
    439   scoped_ptr<IPC::Message> reply_message_;
    440   int new_window_id_;
    441   bool for_browser_command_;
    442 
    443   DISALLOW_COPY_AND_ASSIGN(BrowserOpenedNotificationObserver);
    444 };
    445 
    446 class BrowserClosedNotificationObserver : public NotificationObserver {
    447  public:
    448   BrowserClosedNotificationObserver(Browser* browser,
    449                                     AutomationProvider* automation,
    450                                     IPC::Message* reply_message);
    451   virtual ~BrowserClosedNotificationObserver();
    452 
    453   virtual void Observe(NotificationType type,
    454                        const NotificationSource& source,
    455                        const NotificationDetails& details);
    456 
    457   void set_for_browser_command(bool for_browser_command);
    458 
    459  private:
    460   NotificationRegistrar registrar_;
    461   base::WeakPtr<AutomationProvider> automation_;
    462   scoped_ptr<IPC::Message> reply_message_;
    463   bool for_browser_command_;
    464 
    465   DISALLOW_COPY_AND_ASSIGN(BrowserClosedNotificationObserver);
    466 };
    467 
    468 class BrowserCountChangeNotificationObserver : public NotificationObserver {
    469  public:
    470   BrowserCountChangeNotificationObserver(int target_count,
    471                                          AutomationProvider* automation,
    472                                          IPC::Message* reply_message);
    473   virtual ~BrowserCountChangeNotificationObserver();
    474 
    475   virtual void Observe(NotificationType type,
    476                        const NotificationSource& source,
    477                        const NotificationDetails& details);
    478 
    479  private:
    480   int target_count_;
    481   NotificationRegistrar registrar_;
    482   base::WeakPtr<AutomationProvider> automation_;
    483   scoped_ptr<IPC::Message> reply_message_;
    484 
    485   DISALLOW_COPY_AND_ASSIGN(BrowserCountChangeNotificationObserver);
    486 };
    487 
    488 class AppModalDialogShownObserver : public NotificationObserver {
    489  public:
    490   AppModalDialogShownObserver(AutomationProvider* automation,
    491                               IPC::Message* reply_message);
    492   virtual ~AppModalDialogShownObserver();
    493 
    494   virtual void Observe(NotificationType type,
    495                        const NotificationSource& source,
    496                        const NotificationDetails& details);
    497 
    498  private:
    499   NotificationRegistrar registrar_;
    500   base::WeakPtr<AutomationProvider> automation_;
    501   scoped_ptr<IPC::Message> reply_message_;
    502 
    503   DISALLOW_COPY_AND_ASSIGN(AppModalDialogShownObserver);
    504 };
    505 
    506 class ExecuteBrowserCommandObserver : public NotificationObserver {
    507  public:
    508   virtual ~ExecuteBrowserCommandObserver();
    509 
    510   static bool CreateAndRegisterObserver(AutomationProvider* automation,
    511                                         Browser* browser,
    512                                         int command,
    513                                         IPC::Message* reply_message);
    514 
    515   virtual void Observe(NotificationType type,
    516                        const NotificationSource& source,
    517                        const NotificationDetails& details);
    518 
    519  private:
    520   ExecuteBrowserCommandObserver(AutomationProvider* automation,
    521                                 IPC::Message* reply_message);
    522 
    523   bool Register(int command);
    524 
    525   bool GetNotificationType(int command, NotificationType::Type* type);
    526 
    527   NotificationRegistrar registrar_;
    528   base::WeakPtr<AutomationProvider> automation_;
    529   NotificationType::Type notification_type_;
    530   scoped_ptr<IPC::Message> reply_message_;
    531 
    532   DISALLOW_COPY_AND_ASSIGN(ExecuteBrowserCommandObserver);
    533 };
    534 
    535 class FindInPageNotificationObserver : public NotificationObserver {
    536  public:
    537   FindInPageNotificationObserver(AutomationProvider* automation,
    538                                  TabContents* parent_tab,
    539                                  bool reply_with_json,
    540                                  IPC::Message* reply_message);
    541   virtual ~FindInPageNotificationObserver();
    542 
    543   virtual void Observe(NotificationType type,
    544                        const NotificationSource& source,
    545                        const NotificationDetails& details);
    546 
    547   // The Find mechanism is over asynchronous IPC, so a search is kicked off and
    548   // we wait for notification to find out what the results are. As the user is
    549   // typing, new search requests can be issued and the Request ID helps us make
    550   // sense of whether this is the current request or an old one. The unit tests,
    551   // however, which uses this constant issues only one search at a time, so we
    552   // don't need a rolling id to identify each search. But, we still need to
    553   // specify one, so we just use a fixed one - its value does not matter.
    554   static const int kFindInPageRequestId;
    555 
    556  private:
    557   NotificationRegistrar registrar_;
    558   base::WeakPtr<AutomationProvider> automation_;
    559   // We will at some point (before final update) be notified of the ordinal and
    560   // we need to preserve it so we can send it later.
    561   int active_match_ordinal_;
    562   // Send reply using json automation interface.
    563   bool reply_with_json_;
    564   scoped_ptr<IPC::Message> reply_message_;
    565 
    566   DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver);
    567 };
    568 
    569 class DomOperationObserver : public NotificationObserver {
    570  public:
    571   DomOperationObserver();
    572   virtual ~DomOperationObserver();
    573 
    574   virtual void Observe(NotificationType type,
    575                        const NotificationSource& source,
    576                        const NotificationDetails& details);
    577 
    578   virtual void OnDomOperationCompleted(const std::string& json) = 0;
    579 
    580  private:
    581   NotificationRegistrar registrar_;
    582 
    583   DISALLOW_COPY_AND_ASSIGN(DomOperationObserver);
    584 };
    585 
    586 // Sends a message back to the automation client with the results of the DOM
    587 // operation.
    588 class DomOperationMessageSender : public DomOperationObserver {
    589  public:
    590   explicit DomOperationMessageSender(AutomationProvider* automation,
    591                                      IPC::Message* relpy_message,
    592                                      bool use_json_interface);
    593   virtual ~DomOperationMessageSender();
    594 
    595   virtual void OnDomOperationCompleted(const std::string& json);
    596 
    597  private:
    598   base::WeakPtr<AutomationProvider> automation_;
    599   scoped_ptr<IPC::Message> reply_message_;
    600   bool use_json_interface_;
    601 
    602   DISALLOW_COPY_AND_ASSIGN(DomOperationMessageSender);
    603 };
    604 
    605 class DocumentPrintedNotificationObserver : public NotificationObserver {
    606  public:
    607   DocumentPrintedNotificationObserver(AutomationProvider* automation,
    608                                       IPC::Message* reply_message);
    609   virtual ~DocumentPrintedNotificationObserver();
    610 
    611   virtual void Observe(NotificationType type, const NotificationSource& source,
    612                        const NotificationDetails& details);
    613 
    614  private:
    615   NotificationRegistrar registrar_;
    616   base::WeakPtr<AutomationProvider> automation_;
    617   bool success_;
    618   scoped_ptr<IPC::Message> reply_message_;
    619 
    620   DISALLOW_COPY_AND_ASSIGN(DocumentPrintedNotificationObserver);
    621 };
    622 
    623 // Collects METRIC_EVENT_DURATION notifications and keep track of the times.
    624 class MetricEventDurationObserver : public NotificationObserver {
    625  public:
    626   MetricEventDurationObserver();
    627   virtual ~MetricEventDurationObserver();
    628 
    629   // Get the duration of an event.  Returns -1 if we haven't seen the event.
    630   int GetEventDurationMs(const std::string& event_name);
    631 
    632   // NotificationObserver interface.
    633   virtual void Observe(NotificationType type, const NotificationSource& source,
    634                        const NotificationDetails& details);
    635 
    636  private:
    637   NotificationRegistrar registrar_;
    638 
    639   typedef std::map<std::string, int> EventDurationMap;
    640   EventDurationMap durations_;
    641 
    642   DISALLOW_COPY_AND_ASSIGN(MetricEventDurationObserver);
    643 };
    644 
    645 class PageTranslatedObserver : public NotificationObserver {
    646  public:
    647   PageTranslatedObserver(AutomationProvider* automation,
    648                          IPC::Message* reply_message,
    649                          TabContents* tab_contents);
    650   virtual ~PageTranslatedObserver();
    651 
    652   // NotificationObserver interface.
    653   virtual void Observe(NotificationType type,
    654                        const NotificationSource& source,
    655                        const NotificationDetails& details);
    656 
    657  private:
    658   NotificationRegistrar registrar_;
    659   base::WeakPtr<AutomationProvider> automation_;
    660   scoped_ptr<IPC::Message> reply_message_;
    661 
    662   DISALLOW_COPY_AND_ASSIGN(PageTranslatedObserver);
    663 };
    664 
    665 class TabLanguageDeterminedObserver : public NotificationObserver {
    666  public:
    667   TabLanguageDeterminedObserver(AutomationProvider* automation,
    668                                 IPC::Message* reply_message,
    669                                 TabContents* tab_contents,
    670                                 TranslateInfoBarDelegate* translate_bar);
    671   virtual ~TabLanguageDeterminedObserver();
    672 
    673   // NotificationObserver interface.
    674   virtual void Observe(NotificationType type,
    675                        const NotificationSource& source,
    676                        const NotificationDetails& details);
    677 
    678  private:
    679   NotificationRegistrar registrar_;
    680   base::WeakPtr<AutomationProvider> automation_;
    681   scoped_ptr<IPC::Message> reply_message_;
    682   TabContents* tab_contents_;
    683   TranslateInfoBarDelegate* translate_bar_;
    684 
    685   DISALLOW_COPY_AND_ASSIGN(TabLanguageDeterminedObserver);
    686 };
    687 
    688 class InfoBarCountObserver : public NotificationObserver {
    689  public:
    690   InfoBarCountObserver(AutomationProvider* automation,
    691                        IPC::Message* reply_message,
    692                        TabContents* tab_contents,
    693                        size_t target_count);
    694   virtual ~InfoBarCountObserver();
    695 
    696   // NotificationObserver interface.
    697   virtual void Observe(NotificationType type,
    698                        const NotificationSource& source,
    699                        const NotificationDetails& details);
    700 
    701  private:
    702   // Checks whether the infobar count matches our target, and if so
    703   // sends the reply message and deletes itself.
    704   void CheckCount();
    705 
    706   NotificationRegistrar registrar_;
    707   base::WeakPtr<AutomationProvider> automation_;
    708   scoped_ptr<IPC::Message> reply_message_;
    709   TabContents* tab_contents_;
    710 
    711   const size_t target_count_;
    712 
    713   DISALLOW_COPY_AND_ASSIGN(InfoBarCountObserver);
    714 };
    715 
    716 #if defined(OS_CHROMEOS)
    717 // Collects LOGIN_USER_CHANGED notifications and returns
    718 // whether authentication succeeded to the automation provider.
    719 class LoginManagerObserver : public NotificationObserver {
    720  public:
    721   LoginManagerObserver(AutomationProvider* automation,
    722                        IPC::Message* reply_message);
    723   virtual ~LoginManagerObserver();
    724 
    725   // NotificationObserver interface.
    726   virtual void Observe(NotificationType type, const NotificationSource& source,
    727                        const NotificationDetails& details);
    728 
    729  private:
    730   NotificationRegistrar registrar_;
    731   base::WeakPtr<AutomationProvider> automation_;
    732   scoped_ptr<IPC::Message> reply_message_;
    733 
    734   DISALLOW_COPY_AND_ASSIGN(LoginManagerObserver);
    735 };
    736 
    737 // Collects SCREEN_LOCK_STATE_CHANGED notifications and returns
    738 // whether authentication succeeded to the automation provider.
    739 class ScreenLockUnlockObserver : public NotificationObserver {
    740  public:
    741   // Set lock_screen to true to observe lock screen events,
    742   // false for unlock screen events.
    743   ScreenLockUnlockObserver(AutomationProvider* automation,
    744                            IPC::Message* reply_message,
    745                            bool lock_screen);
    746   virtual ~ScreenLockUnlockObserver();
    747 
    748   // NotificationObserver interface.
    749   virtual void Observe(NotificationType type, const NotificationSource& source,
    750                        const NotificationDetails& details);
    751 
    752  private:
    753   NotificationRegistrar registrar_;
    754   AutomationProvider* automation_;
    755   IPC::Message* reply_message_;
    756   bool lock_screen_;
    757 
    758   DISALLOW_COPY_AND_ASSIGN(ScreenLockUnlockObserver);
    759 };
    760 
    761 class NetworkScanObserver
    762     : public chromeos::NetworkLibrary::NetworkManagerObserver {
    763  public:
    764   NetworkScanObserver(AutomationProvider* automation,
    765                       IPC::Message* reply_message);
    766 
    767   virtual ~NetworkScanObserver();
    768 
    769   // NetworkLibrary::NetworkManagerObserver implementation.
    770   virtual void OnNetworkManagerChanged(chromeos::NetworkLibrary* obj);
    771 
    772  private:
    773   AutomationProvider* automation_;
    774   IPC::Message* reply_message_;
    775 
    776   DISALLOW_COPY_AND_ASSIGN(NetworkScanObserver);
    777 };
    778 
    779 // Waits for a connection success or failure for the specified
    780 // network and returns the status to the automation provider.
    781 class NetworkConnectObserver
    782     : public chromeos::NetworkLibrary::NetworkManagerObserver {
    783  public:
    784   NetworkConnectObserver(AutomationProvider* automation,
    785                          IPC::Message* reply_message);
    786 
    787   virtual ~NetworkConnectObserver();
    788 
    789   virtual const chromeos::WifiNetwork* GetWifiNetwork(
    790       chromeos::NetworkLibrary* network_library) = 0;
    791 
    792   // NetworkLibrary::NetworkManagerObserver implementation.
    793   virtual void OnNetworkManagerChanged(chromeos::NetworkLibrary* obj);
    794 
    795  private:
    796   AutomationProvider* automation_;
    797   IPC::Message* reply_message_;
    798 
    799   DISALLOW_COPY_AND_ASSIGN(NetworkConnectObserver);
    800 };
    801 
    802 // Waits for a connection success or failure for the specified
    803 // network and returns the status to the automation provider.
    804 class ServicePathConnectObserver : public NetworkConnectObserver {
    805  public:
    806   ServicePathConnectObserver(AutomationProvider* automation,
    807                              IPC::Message* reply_message,
    808                              const std::string& service_path);
    809 
    810   virtual const chromeos::WifiNetwork* GetWifiNetwork(
    811       chromeos::NetworkLibrary* network_library);
    812 
    813  private:
    814   std::string service_path_;
    815 
    816   DISALLOW_COPY_AND_ASSIGN(ServicePathConnectObserver);
    817 };
    818 
    819 // Waits for a connection success or failure for the specified
    820 // network and returns the status to the automation provider.
    821 class SSIDConnectObserver : public NetworkConnectObserver {
    822  public:
    823   SSIDConnectObserver(AutomationProvider* automation,
    824                       IPC::Message* reply_message,
    825                       const std::string& ssid);
    826 
    827   virtual const chromeos::WifiNetwork* GetWifiNetwork(
    828       chromeos::NetworkLibrary* network_library);
    829 
    830  private:
    831   std::string ssid_;
    832 
    833   DISALLOW_COPY_AND_ASSIGN(SSIDConnectObserver);
    834 };
    835 #endif  // defined(OS_CHROMEOS)
    836 
    837 // Waits for the bookmark model to load.
    838 class AutomationProviderBookmarkModelObserver : BookmarkModelObserver {
    839  public:
    840   AutomationProviderBookmarkModelObserver(AutomationProvider* provider,
    841                                           IPC::Message* reply_message,
    842                                           BookmarkModel* model);
    843   virtual ~AutomationProviderBookmarkModelObserver();
    844 
    845   virtual void Loaded(BookmarkModel* model);
    846   virtual void BookmarkModelBeingDeleted(BookmarkModel* model);
    847   virtual void BookmarkNodeMoved(BookmarkModel* model,
    848                                  const BookmarkNode* old_parent,
    849                                  int old_index,
    850                                  const BookmarkNode* new_parent,
    851                                  int new_index) {}
    852   virtual void BookmarkNodeAdded(BookmarkModel* model,
    853                                  const BookmarkNode* parent,
    854                                  int index) {}
    855   virtual void BookmarkNodeRemoved(BookmarkModel* model,
    856                                    const BookmarkNode* parent,
    857                                    int old_index,
    858                                    const BookmarkNode* node) {}
    859   virtual void BookmarkNodeChanged(BookmarkModel* model,
    860                                    const BookmarkNode* node) {}
    861   virtual void BookmarkNodeFaviconLoaded(BookmarkModel* model,
    862                                          const BookmarkNode* node) {}
    863   virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
    864                                              const BookmarkNode* node) {}
    865 
    866  private:
    867   // Reply to the automation message with the given success value,
    868   // then delete myself (which removes myself from the bookmark model
    869   // observer list).
    870   void ReplyAndDelete(bool success);
    871 
    872   base::WeakPtr<AutomationProvider> automation_provider_;
    873   scoped_ptr<IPC::Message> reply_message_;
    874   BookmarkModel* model_;
    875 
    876   DISALLOW_COPY_AND_ASSIGN(AutomationProviderBookmarkModelObserver);
    877 };
    878 
    879 // Allows the automation provider to wait for all downloads to finish.
    880 // If any download is interrupted, it will cancel all the other downloads at
    881 // the next |OnDownloadUpdated|, and send an error when all are done.
    882 class AutomationProviderDownloadItemObserver : public DownloadItem::Observer {
    883  public:
    884   AutomationProviderDownloadItemObserver(
    885       AutomationProvider* provider,
    886       IPC::Message* reply_message,
    887       int downloads);
    888   virtual ~AutomationProviderDownloadItemObserver();
    889 
    890   virtual void OnDownloadUpdated(DownloadItem* download);
    891   virtual void OnDownloadOpened(DownloadItem* download);
    892 
    893  private:
    894   void RemoveAndCleanupOnLastEntry(DownloadItem* download);
    895 
    896   base::WeakPtr<AutomationProvider> provider_;
    897   scoped_ptr<IPC::Message> reply_message_;
    898   int downloads_;
    899   bool interrupted_;
    900 
    901   DISALLOW_COPY_AND_ASSIGN(AutomationProviderDownloadItemObserver);
    902 };
    903 
    904 // Allows the automation provider to wait until the download has been updated
    905 // or opened.
    906 class AutomationProviderDownloadUpdatedObserver
    907     : public DownloadItem::Observer {
    908  public:
    909   AutomationProviderDownloadUpdatedObserver(
    910       AutomationProvider* provider,
    911       IPC::Message* reply_message,
    912       bool wait_for_open);
    913   virtual ~AutomationProviderDownloadUpdatedObserver();
    914 
    915   virtual void OnDownloadUpdated(DownloadItem* download);
    916   virtual void OnDownloadOpened(DownloadItem* download);
    917 
    918  private:
    919   base::WeakPtr<AutomationProvider> provider_;
    920   scoped_ptr<IPC::Message> reply_message_;
    921   bool wait_for_open_;
    922 
    923   DISALLOW_COPY_AND_ASSIGN(AutomationProviderDownloadUpdatedObserver);
    924 };
    925 
    926 // Allows the automation provider to wait until the download model has changed
    927 // (because a new download has been added or removed).
    928 class AutomationProviderDownloadModelChangedObserver
    929     : public DownloadManager::Observer {
    930  public:
    931   AutomationProviderDownloadModelChangedObserver(
    932       AutomationProvider* provider,
    933       IPC::Message* reply_message,
    934       DownloadManager* download_manager);
    935   virtual ~AutomationProviderDownloadModelChangedObserver();
    936 
    937   virtual void ModelChanged();
    938 
    939  private:
    940   base::WeakPtr<AutomationProvider> provider_;
    941   scoped_ptr<IPC::Message> reply_message_;
    942   DownloadManager* download_manager_;
    943 
    944   DISALLOW_COPY_AND_ASSIGN(AutomationProviderDownloadModelChangedObserver);
    945 };
    946 
    947 // Allows automation provider to wait until TemplateURLModel has loaded
    948 // before looking up/returning search engine info.
    949 class AutomationProviderSearchEngineObserver
    950     : public TemplateURLModelObserver {
    951  public:
    952   AutomationProviderSearchEngineObserver(
    953       AutomationProvider* provider,
    954       IPC::Message* reply_message);
    955   virtual ~AutomationProviderSearchEngineObserver();
    956 
    957   virtual void OnTemplateURLModelChanged();
    958 
    959  private:
    960   base::WeakPtr<AutomationProvider> provider_;
    961   scoped_ptr<IPC::Message> reply_message_;
    962 
    963   DISALLOW_COPY_AND_ASSIGN(AutomationProviderSearchEngineObserver);
    964 };
    965 
    966 // Allows the automation provider to wait for history queries to finish.
    967 class AutomationProviderHistoryObserver {
    968  public:
    969   AutomationProviderHistoryObserver(
    970       AutomationProvider* provider,
    971       IPC::Message* reply_message);
    972   virtual ~AutomationProviderHistoryObserver();
    973 
    974   void HistoryQueryComplete(HistoryService::Handle request_handle,
    975                             history::QueryResults* results);
    976 
    977  private:
    978   base::WeakPtr<AutomationProvider> provider_;
    979   scoped_ptr<IPC::Message> reply_message_;
    980 };
    981 
    982 // Allows the automation provider to wait for import queries to finish.
    983 class AutomationProviderImportSettingsObserver
    984     : public importer::ImporterProgressObserver {
    985  public:
    986   AutomationProviderImportSettingsObserver(
    987       AutomationProvider* provider,
    988       IPC::Message* reply_message);
    989   virtual ~AutomationProviderImportSettingsObserver();
    990 
    991   // importer::ImporterProgressObserver:
    992   virtual void ImportStarted() OVERRIDE;
    993   virtual void ImportItemStarted(importer::ImportItem item) OVERRIDE;
    994   virtual void ImportItemEnded(importer::ImportItem item) OVERRIDE;
    995   virtual void ImportEnded() OVERRIDE;
    996 
    997  private:
    998   base::WeakPtr<AutomationProvider> provider_;
    999   scoped_ptr<IPC::Message> reply_message_;
   1000 };
   1001 
   1002 // Allows automation provider to wait for getting passwords to finish.
   1003 class AutomationProviderGetPasswordsObserver : public PasswordStoreConsumer {
   1004  public:
   1005   AutomationProviderGetPasswordsObserver(
   1006       AutomationProvider* provider,
   1007       IPC::Message* reply_message);
   1008   virtual ~AutomationProviderGetPasswordsObserver();
   1009 
   1010   virtual void OnPasswordStoreRequestDone(
   1011       CancelableRequestProvider::Handle handle,
   1012       const std::vector<webkit_glue::PasswordForm*>& result);
   1013 
   1014  private:
   1015   base::WeakPtr<AutomationProvider> provider_;
   1016   scoped_ptr<IPC::Message> reply_message_;
   1017 };
   1018 
   1019 // Allows the automation provider to wait for clearing browser data to finish.
   1020 class AutomationProviderBrowsingDataObserver
   1021     : public BrowsingDataRemover::Observer {
   1022  public:
   1023   AutomationProviderBrowsingDataObserver(
   1024       AutomationProvider* provider,
   1025       IPC::Message* reply_message);
   1026   virtual ~AutomationProviderBrowsingDataObserver();
   1027 
   1028   virtual void OnBrowsingDataRemoverDone();
   1029 
   1030  private:
   1031   base::WeakPtr<AutomationProvider> provider_;
   1032   scoped_ptr<IPC::Message> reply_message_;
   1033 };
   1034 
   1035 // Allows automation provider to wait until page load after selecting an item
   1036 // in the omnibox popup.
   1037 class OmniboxAcceptNotificationObserver : public NotificationObserver {
   1038  public:
   1039   OmniboxAcceptNotificationObserver(NavigationController* controller,
   1040                                  AutomationProvider* automation,
   1041                                  IPC::Message* reply_message);
   1042   virtual ~OmniboxAcceptNotificationObserver();
   1043 
   1044   virtual void Observe(NotificationType type,
   1045                        const NotificationSource& source,
   1046                        const NotificationDetails& details);
   1047 
   1048  private:
   1049   NotificationRegistrar registrar_;
   1050   base::WeakPtr<AutomationProvider> automation_;
   1051   scoped_ptr<IPC::Message> reply_message_;
   1052   NavigationController* controller_;
   1053 
   1054   DISALLOW_COPY_AND_ASSIGN(OmniboxAcceptNotificationObserver);
   1055 };
   1056 
   1057 // Allows the automation provider to wait for a save package notification.
   1058 class SavePackageNotificationObserver : public NotificationObserver {
   1059  public:
   1060   SavePackageNotificationObserver(SavePackage* save_package,
   1061                                   AutomationProvider* automation,
   1062                                   IPC::Message* reply_message);
   1063   virtual ~SavePackageNotificationObserver();
   1064 
   1065   virtual void Observe(NotificationType type,
   1066                        const NotificationSource& source,
   1067                        const NotificationDetails& details);
   1068 
   1069  private:
   1070   NotificationRegistrar registrar_;
   1071   base::WeakPtr<AutomationProvider> automation_;
   1072   scoped_ptr<IPC::Message> reply_message_;
   1073 
   1074   DISALLOW_COPY_AND_ASSIGN(SavePackageNotificationObserver);
   1075 };
   1076 
   1077 // This class manages taking a snapshot of a page. This requires waiting on
   1078 // asynchronous callbacks and notifications.
   1079 class PageSnapshotTaker : public DomOperationObserver {
   1080  public:
   1081   PageSnapshotTaker(AutomationProvider* automation,
   1082                     IPC::Message* reply_message,
   1083                     RenderViewHost* render_view,
   1084                     const FilePath& path);
   1085   virtual ~PageSnapshotTaker();
   1086 
   1087   // Start the process of taking a snapshot of the entire page.
   1088   void Start();
   1089 
   1090  private:
   1091   // Overriden from DomOperationObserver.
   1092   virtual void OnDomOperationCompleted(const std::string& json);
   1093 
   1094   // Called by the ThumbnailGenerator when the requested snapshot has been
   1095   // generated.
   1096   void OnSnapshotTaken(const SkBitmap& bitmap);
   1097 
   1098   // Helper method to send arbitrary javascript to the renderer for evaluation.
   1099   void ExecuteScript(const std::wstring& javascript);
   1100 
   1101   // Helper method to send a response back to the client. Deletes this.
   1102   void SendMessage(bool success);
   1103 
   1104   base::WeakPtr<AutomationProvider> automation_;
   1105   scoped_ptr<IPC::Message> reply_message_;
   1106   RenderViewHost* render_view_;
   1107   FilePath image_path_;
   1108   bool received_width_;
   1109   gfx::Size entire_page_size_;
   1110 
   1111   DISALLOW_COPY_AND_ASSIGN(PageSnapshotTaker);
   1112 };
   1113 
   1114 class NTPInfoObserver : public NotificationObserver {
   1115  public:
   1116   NTPInfoObserver(AutomationProvider* automation,
   1117                   IPC::Message* reply_message,
   1118                   CancelableRequestConsumer* consumer);
   1119   virtual ~NTPInfoObserver();
   1120 
   1121   virtual void Observe(NotificationType type,
   1122                        const NotificationSource& source,
   1123                        const NotificationDetails& details);
   1124 
   1125  private:
   1126   void OnTopSitesLoaded();
   1127   void OnTopSitesReceived(const history::MostVisitedURLList& visited_list);
   1128 
   1129   base::WeakPtr<AutomationProvider> automation_;
   1130   scoped_ptr<IPC::Message> reply_message_;
   1131   CancelableRequestConsumer* consumer_;
   1132   CancelableRequestProvider::Handle request_;
   1133   scoped_ptr<DictionaryValue> ntp_info_;
   1134   history::TopSites* top_sites_;
   1135   NotificationRegistrar registrar_;
   1136 
   1137   DISALLOW_COPY_AND_ASSIGN(NTPInfoObserver);
   1138 };
   1139 
   1140 // Observes when an app has been launched, as indicated by a notification that
   1141 // a content load in some tab has stopped.
   1142 class AppLaunchObserver : public NotificationObserver {
   1143  public:
   1144   AppLaunchObserver(NavigationController* controller,
   1145                     AutomationProvider* automation,
   1146                     IPC::Message* reply_message,
   1147                     extension_misc::LaunchContainer launch_container);
   1148   virtual ~AppLaunchObserver();
   1149 
   1150   virtual void Observe(NotificationType type,
   1151                        const NotificationSource& source,
   1152                        const NotificationDetails& details);
   1153 
   1154  private:
   1155   NavigationController* controller_;
   1156   base::WeakPtr<AutomationProvider> automation_;
   1157   scoped_ptr<IPC::Message> reply_message_;
   1158   NotificationRegistrar registrar_;
   1159   extension_misc::LaunchContainer launch_container_;
   1160   int new_window_id_;
   1161 
   1162   DISALLOW_COPY_AND_ASSIGN(AppLaunchObserver);
   1163 };
   1164 
   1165 // Allows automation provider to wait until the autocomplete edit
   1166 // has received focus
   1167 class AutocompleteEditFocusedObserver : public NotificationObserver {
   1168  public:
   1169   AutocompleteEditFocusedObserver(AutomationProvider* automation,
   1170                                   AutocompleteEditModel* autocomplete_edit,
   1171                                   IPC::Message* reply_message);
   1172   virtual ~AutocompleteEditFocusedObserver();
   1173 
   1174   virtual void Observe(NotificationType type,
   1175                        const NotificationSource& source,
   1176                        const NotificationDetails& details);
   1177 
   1178  private:
   1179   NotificationRegistrar registrar_;
   1180   base::WeakPtr<AutomationProvider> automation_;
   1181   scoped_ptr<IPC::Message> reply_message_;
   1182   AutocompleteEditModel* autocomplete_edit_model_;
   1183 
   1184   DISALLOW_COPY_AND_ASSIGN(AutocompleteEditFocusedObserver);
   1185 };
   1186 
   1187 // Allows the automation provider to wait until all the notification
   1188 // processes are ready.
   1189 class GetActiveNotificationsObserver : public NotificationObserver {
   1190  public:
   1191   GetActiveNotificationsObserver(AutomationProvider* automation,
   1192                                  IPC::Message* reply_message);
   1193   virtual ~GetActiveNotificationsObserver();
   1194 
   1195   virtual void Observe(NotificationType type,
   1196                        const NotificationSource& source,
   1197                        const NotificationDetails& details);
   1198 
   1199  private:
   1200   void SendMessage();
   1201 
   1202   AutomationJSONReply reply_;
   1203   NotificationRegistrar registrar_;
   1204 
   1205   DISALLOW_COPY_AND_ASSIGN(GetActiveNotificationsObserver);
   1206 };
   1207 
   1208 // Allows the automation provider to wait for a given number of
   1209 // notification balloons.
   1210 class OnNotificationBalloonCountObserver {
   1211  public:
   1212   OnNotificationBalloonCountObserver(AutomationProvider* provider,
   1213                                      IPC::Message* reply_message,
   1214                                      BalloonCollection* collection,
   1215                                      int count);
   1216 
   1217   void OnBalloonCollectionChanged();
   1218 
   1219  private:
   1220   AutomationJSONReply reply_;
   1221   BalloonCollection* collection_;
   1222   int count_;
   1223 
   1224   DISALLOW_COPY_AND_ASSIGN(OnNotificationBalloonCountObserver);
   1225 };
   1226 
   1227 // Allows the automation provider to wait for a RENDERER_PROCESS_CLOSED
   1228 // notification.
   1229 class RendererProcessClosedObserver : public NotificationObserver {
   1230  public:
   1231   RendererProcessClosedObserver(AutomationProvider* automation,
   1232                                 IPC::Message* reply_message);
   1233   virtual ~RendererProcessClosedObserver();
   1234 
   1235   virtual void Observe(NotificationType type,
   1236                        const NotificationSource& source,
   1237                        const NotificationDetails& details);
   1238 
   1239  private:
   1240   NotificationRegistrar registrar_;
   1241   base::WeakPtr<AutomationProvider> automation_;
   1242   scoped_ptr<IPC::Message> reply_message_;
   1243 
   1244   DISALLOW_COPY_AND_ASSIGN(RendererProcessClosedObserver);
   1245 };
   1246 
   1247 // Allows the automation provider to wait for acknowledgement that a input
   1248 // event has been handled.
   1249 class InputEventAckNotificationObserver : public NotificationObserver {
   1250  public:
   1251   InputEventAckNotificationObserver(AutomationProvider* automation,
   1252                                     IPC::Message* reply_message,
   1253                                     int event_type);
   1254   virtual ~InputEventAckNotificationObserver();
   1255 
   1256   virtual void Observe(NotificationType type,
   1257                        const NotificationSource& source,
   1258                        const NotificationDetails& details);
   1259 
   1260  private:
   1261   NotificationRegistrar registrar_;
   1262   base::WeakPtr<AutomationProvider> automation_;
   1263   scoped_ptr<IPC::Message> reply_message_;
   1264   int event_type_;
   1265 
   1266   DISALLOW_COPY_AND_ASSIGN(InputEventAckNotificationObserver);
   1267 };
   1268 
   1269 // Allows the automation provider to wait for all the tabs to finish any
   1270 // pending loads. This only waits for tabs that exist at the observer's
   1271 // creation. Will send a message on construction if no tabs are loading
   1272 // currently.
   1273 class AllTabsStoppedLoadingObserver : public TabEventObserver {
   1274  public:
   1275   AllTabsStoppedLoadingObserver(AutomationProvider* automation,
   1276                                 IPC::Message* reply_message);
   1277   virtual ~AllTabsStoppedLoadingObserver();
   1278 
   1279   // TabEventObserver implementation.
   1280   virtual void OnFirstPendingLoad(TabContents* tab_contents);
   1281   virtual void OnNoMorePendingLoads(TabContents* tab_contents);
   1282 
   1283  private:
   1284   typedef std::set<TabContents*> TabSet;
   1285 
   1286   // Checks if there are no pending loads. If none, it will send an automation
   1287   // relpy and delete itself.
   1288   void CheckIfNoMorePendingLoads();
   1289 
   1290   TabSet pending_tabs_;
   1291   NotificationRegistrar registrar_;
   1292   base::WeakPtr<AutomationProvider> automation_;
   1293   scoped_ptr<IPC::Message> reply_message_;
   1294 
   1295   DISALLOW_COPY_AND_ASSIGN(AllTabsStoppedLoadingObserver);
   1296 };
   1297 
   1298 // Observer used to listen for new tab creation to complete.
   1299 class NewTabObserver : public NotificationObserver {
   1300  public:
   1301   NewTabObserver(AutomationProvider* automation, IPC::Message* reply_message);
   1302 
   1303   virtual void Observe(NotificationType type,
   1304                        const NotificationSource& source,
   1305                        const NotificationDetails& details) OVERRIDE;
   1306 
   1307  private:
   1308   virtual ~NewTabObserver();
   1309 
   1310   NotificationRegistrar registrar_;
   1311   base::WeakPtr<AutomationProvider> automation_;
   1312   scoped_ptr<IPC::Message> reply_message_;
   1313 
   1314   DISALLOW_COPY_AND_ASSIGN(NewTabObserver);
   1315 };
   1316 
   1317 // Posts a task to the PROCESS_LAUNCHER thread, once processed posts a task
   1318 // back to the UI thread that notifies the provider we're done.
   1319 class WaitForProcessLauncherThreadToGoIdleObserver
   1320     : public base::RefCountedThreadSafe<
   1321           WaitForProcessLauncherThreadToGoIdleObserver,
   1322           BrowserThread::DeleteOnUIThread> {
   1323  public:
   1324   WaitForProcessLauncherThreadToGoIdleObserver(
   1325       AutomationProvider* automation, IPC::Message* reply_message);
   1326 
   1327  private:
   1328   friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
   1329   friend class DeleteTask<WaitForProcessLauncherThreadToGoIdleObserver>;
   1330 
   1331   virtual ~WaitForProcessLauncherThreadToGoIdleObserver();
   1332 
   1333   // Schedules a task on the PROCESS_LAUNCHER thread to execute
   1334   // |RunOnProcessLauncherThread2|. By the time the task is executed the
   1335   // PROCESS_LAUNCHER thread should be some what idle.
   1336   void RunOnProcessLauncherThread();
   1337 
   1338   // When executed the PROCESS_LAUNCHER thread should have processed any pending
   1339   // tasks.  Schedules a task on the UI thread that sends the message saying
   1340   // we're done.
   1341   void RunOnProcessLauncherThread2();
   1342 
   1343   // Sends the |reply_message_| to |automation_| indicating we're done.
   1344   void RunOnUIThread();
   1345 
   1346   base::WeakPtr<AutomationProvider> automation_;
   1347   scoped_ptr<IPC::Message> reply_message_;
   1348 
   1349   DISALLOW_COPY_AND_ASSIGN(WaitForProcessLauncherThreadToGoIdleObserver);
   1350 };
   1351 
   1352 #endif  // CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_OBSERVERS_H_
   1353