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.
5 #ifndef CHROME_BROWSER_AUTOMATION_AUTOMATION_EVENT_OBSERVERS_H_
6 #define CHROME_BROWSER_AUTOMATION_AUTOMATION_EVENT_OBSERVERS_H_
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"
19 // AutomationEventObserver watches for a specific event, and pushes an
20 // AutomationEvent into the AutomationEventQueue for each occurance.
21 class AutomationEventObserver
{
23 explicit AutomationEventObserver(AutomationEventQueue
* event_queue
,
25 virtual ~AutomationEventObserver();
27 virtual void Init(int observer_id
);
28 void NotifyEvent(base::DictionaryValue
* value
);
32 void RemoveIfDone(); // This may delete the object.
35 AutomationEventQueue
* event_queue_
;
38 // TODO(craigdh): Add a PyAuto hook to retrieve the number of times an event
42 DISALLOW_COPY_AND_ASSIGN(AutomationEventObserver
);
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
{
51 DomEventObserver(AutomationEventQueue
* event_queue
,
52 const std::string
& event_name
,
55 virtual ~DomEventObserver();
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
;
63 std::string event_name_
;
64 std::string event_name_base_
;
66 content::NotificationRegistrar registrar_
;
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
;
72 DISALLOW_COPY_AND_ASSIGN(DomEventObserver
);
75 #if defined(OS_CHROMEOS)
81 // Event observer that listens for the completion of login.
82 class LoginEventObserver
83 : public AutomationEventObserver
,
84 public chromeos::LoginStatusConsumer
{
86 LoginEventObserver(AutomationEventQueue
* event_queue
,
87 AutomationProvider
* automation
);
88 virtual ~LoginEventObserver();
90 // chromeos::LoginStatusConsumer:
91 virtual void OnLoginFailure(const chromeos::LoginFailure
& error
) OVERRIDE
;
92 virtual void OnLoginSuccess(
93 const chromeos::UserContext
& user_context
) OVERRIDE
;
96 base::WeakPtr
<AutomationProvider
> automation_
;
98 void _NotifyLoginEvent(const std::string
& error_string
);
100 DISALLOW_COPY_AND_ASSIGN(LoginEventObserver
);
103 #endif // defined(OS_CHROMEOS)
105 #endif // CHROME_BROWSER_AUTOMATION_AUTOMATION_EVENT_OBSERVERS_H_