summaryrefslogtreecommitdiffstats
path: root/chrome/browser/automation/automation_event_observers.h
blob: daf2d5d12dabfb95ce46020ef0090c18085e9929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_AUTOMATION_AUTOMATION_EVENT_OBSERVERS_H_
#define CHROME_BROWSER_AUTOMATION_AUTOMATION_EVENT_OBSERVERS_H_

#include "base/compiler_specific.h"
#include "base/values.h"
#include "chrome/browser/automation/automation_event_queue.h"
#include "chrome/browser/automation/automation_provider.h"
#include "chrome/browser/automation/automation_provider_observers.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/login/login_status_consumer.h"
#endif  // defined(OS_CHROMEOS)
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"

// AutomationEventObserver watches for a specific event, and pushes an
// AutomationEvent into the AutomationEventQueue for each occurance.
class AutomationEventObserver {
 public:
  explicit AutomationEventObserver(AutomationEventQueue* event_queue,
                                   bool recurring);
  virtual ~AutomationEventObserver();

  virtual void Init(int observer_id);
  void NotifyEvent(DictionaryValue* value);
  int GetId() const;

 protected:
  void RemoveIfDone();  // This may delete the object.

 private:
  AutomationEventQueue* event_queue_;
  bool recurring_;
  int observer_id_;
  // TODO(craigdh): Add a PyAuto hook to retrieve the number of times an event
  // has occurred.
  int event_count_;

  DISALLOW_COPY_AND_ASSIGN(AutomationEventObserver);
};

// AutomationEventObserver implementation that listens for explicitly raised
// events. A webpage explicitly raises events by calling:
// window.domAutomationController.sendWithId(automation_id, event_name);
class DomEventObserver
    : public AutomationEventObserver, public content::NotificationObserver {
 public:
  DomEventObserver(AutomationEventQueue* event_queue,
                   const std::string& event_name,
                   int automation_id,
                   bool recurring);
  virtual ~DomEventObserver();

  virtual void Init(int observer_id) OVERRIDE;
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

 private:
  std::string event_name_;
  std::string event_name_base_;
  int automation_id_;
  content::NotificationRegistrar registrar_;

  // The first instance of this string in an event name will be replaced with
  // the id of this observer.
  static const char* kSubstringReplaceWithObserverId;

  DISALLOW_COPY_AND_ASSIGN(DomEventObserver);
};

#if defined(OS_CHROMEOS)

namespace chromeos {
struct UserContext;
}

// Event observer that listens for the completion of login.
class LoginEventObserver
    : public AutomationEventObserver,
      public chromeos::LoginStatusConsumer,
      public content::NotificationObserver {
 public:
  LoginEventObserver(AutomationEventQueue* event_queue,
                     AutomationProvider* automation);
  virtual ~LoginEventObserver();

  virtual void OnLoginFailure(const chromeos::LoginFailure& error) OVERRIDE;

  virtual void OnLoginSuccess(const chromeos::UserContext& user_context,
                              bool pending_requests, bool using_oauth) OVERRIDE;
  // Overridden from content::NotificationObserver.
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

 private:
  base::WeakPtr<AutomationProvider> automation_;
  content::NotificationRegistrar registrar_;

  void _NotifyLoginEvent(const std::string& error_string);

  DISALLOW_COPY_AND_ASSIGN(LoginEventObserver);
};

#endif  // defined(OS_CHROMEOS)

#endif  // CHROME_BROWSER_AUTOMATION_AUTOMATION_EVENT_OBSERVERS_H_