- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / automation / automation_event_observers.h
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_AUTOMATION_AUTOMATION_EVENT_OBSERVERS_H_
6 #define CHROME_BROWSER_AUTOMATION_AUTOMATION_EVENT_OBSERVERS_H_
7
8 #include "base/compiler_specific.h"
9 #include "base/values.h"
10 #include "chrome/browser/automation/automation_event_queue.h"
11 #include "chrome/browser/automation/automation_provider.h"
12 #include "chrome/browser/automation/automation_provider_observers.h"
13 #if defined(OS_CHROMEOS)
14 #include "chrome/browser/chromeos/login/login_status_consumer.h"
15 #endif  // defined(OS_CHROMEOS)
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18
19 // AutomationEventObserver watches for a specific event, and pushes an
20 // AutomationEvent into the AutomationEventQueue for each occurance.
21 class AutomationEventObserver {
22  public:
23   explicit AutomationEventObserver(AutomationEventQueue* event_queue,
24                                    bool recurring);
25   virtual ~AutomationEventObserver();
26
27   virtual void Init(int observer_id);
28   void NotifyEvent(DictionaryValue* value);
29   int GetId() const;
30
31  protected:
32   void RemoveIfDone();  // This may delete the object.
33
34  private:
35   AutomationEventQueue* event_queue_;
36   bool recurring_;
37   int observer_id_;
38   // TODO(craigdh): Add a PyAuto hook to retrieve the number of times an event
39   // has occurred.
40   int event_count_;
41
42   DISALLOW_COPY_AND_ASSIGN(AutomationEventObserver);
43 };
44
45 // AutomationEventObserver implementation that listens for explicitly raised
46 // events. A webpage explicitly raises events by calling:
47 // window.domAutomationController.sendWithId(automation_id, event_name);
48 class DomEventObserver
49     : public AutomationEventObserver, public content::NotificationObserver {
50  public:
51   DomEventObserver(AutomationEventQueue* event_queue,
52                    const std::string& event_name,
53                    int automation_id,
54                    bool recurring);
55   virtual ~DomEventObserver();
56
57   virtual void Init(int observer_id) OVERRIDE;
58   virtual void Observe(int type,
59                        const content::NotificationSource& source,
60                        const content::NotificationDetails& details) OVERRIDE;
61
62  private:
63   std::string event_name_;
64   std::string event_name_base_;
65   int automation_id_;
66   content::NotificationRegistrar registrar_;
67
68   // The first instance of this string in an event name will be replaced with
69   // the id of this observer.
70   static const char* kSubstringReplaceWithObserverId;
71
72   DISALLOW_COPY_AND_ASSIGN(DomEventObserver);
73 };
74
75 #if defined(OS_CHROMEOS)
76
77 namespace chromeos {
78 struct UserContext;
79 }
80
81 // Event observer that listens for the completion of login.
82 class LoginEventObserver
83     : public AutomationEventObserver,
84       public chromeos::LoginStatusConsumer {
85  public:
86   LoginEventObserver(AutomationEventQueue* event_queue,
87                      AutomationProvider* automation);
88   virtual ~LoginEventObserver();
89
90   // chromeos::LoginStatusConsumer:
91   virtual void OnLoginFailure(const chromeos::LoginFailure& error) OVERRIDE;
92   virtual void OnLoginSuccess(
93       const chromeos::UserContext& user_context) OVERRIDE;
94
95  private:
96   base::WeakPtr<AutomationProvider> automation_;
97
98   void _NotifyLoginEvent(const std::string& error_string);
99
100   DISALLOW_COPY_AND_ASSIGN(LoginEventObserver);
101 };
102
103 #endif  // defined(OS_CHROMEOS)
104
105 #endif  // CHROME_BROWSER_AUTOMATION_AUTOMATION_EVENT_OBSERVERS_H_