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