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.
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "chrome/browser/automation/automation_event_observers.h"
10 #include "chrome/browser/automation/automation_event_queue.h"
11 #include "chrome/browser/automation/automation_provider_json.h"
12 #include "content/public/browser/notification_service.h"
13 #include "content/public/browser/notification_types.h"
15 AutomationEventQueue::CompareObserverId::CompareObserverId(int id
) : id_(id
) {}
17 bool AutomationEventQueue::CompareObserverId::operator()(
18 AutomationEvent
* event
) const {
19 return event
->GetId() < 0 || event
->GetId() == id_
;
22 AutomationEventQueue::AutomationEventQueue()
23 : observer_id_count_(0), wait_observer_id_(-1) {}
25 AutomationEventQueue::~AutomationEventQueue() {
29 AutomationEventQueue::AutomationEvent::AutomationEvent(
30 int observer_id
, base::DictionaryValue
* event_value
)
31 : observer_id_(observer_id
), event_value_(event_value
) {}
33 void AutomationEventQueue::GetNextEvent(AutomationJSONReply
* reply
,
36 wait_automation_reply_
.reset(reply
);
37 wait_observer_id_
= observer_id
;
38 if (!CheckReturnEvent() && !blocking
&& wait_automation_reply_
.get()) {
39 wait_automation_reply_
->SendSuccess(NULL
);
40 wait_automation_reply_
.reset();
44 void AutomationEventQueue::Clear() {
49 bool AutomationEventQueue::IsEmpty() const {
50 return event_queue_
.empty();
53 AutomationEventQueue::AutomationEvent
* AutomationEventQueue::PopEvent() {
54 if (event_queue_
.empty()) {
57 AutomationEvent
* event
= event_queue_
.back();
58 event_queue_
.pop_back();
62 AutomationEventQueue::AutomationEvent
* AutomationEventQueue::PopEvent(
64 AutomationEvent
* event
= NULL
;
65 std::list
<AutomationEvent
*>::reverse_iterator it
=
66 std::find_if(event_queue_
.rbegin(), event_queue_
.rend(),
67 CompareObserverId(observer_id
));
68 if (it
!= event_queue_
.rend()) {
70 event_queue_
.remove(event
);
75 void AutomationEventQueue::NotifyEvent(
76 AutomationEventQueue::AutomationEvent
* event
) {
78 VLOG(2) << "AutomationEventQueue::NotifyEvent id=" << event
->GetId();
79 event_queue_
.push_front(event
);
83 int AutomationEventQueue::AddObserver(AutomationEventObserver
* observer
) {
84 int id
= observer_id_count_
++;
86 observers_
[id
] = observer
;
90 bool AutomationEventQueue::RemoveObserver(int observer_id
) {
91 if (observers_
.find(observer_id
) != observers_
.end()) {
92 VLOG(2) << "AutomationEventQueue::RemoveObserver id=" << observer_id
;
93 delete observers_
[observer_id
];
94 observers_
.erase(observer_id
);
100 void AutomationEventQueue::ClearObservers() {
101 STLDeleteValues(&observers_
);
104 void AutomationEventQueue::ClearEvents() {
105 STLDeleteElements(&event_queue_
);
108 bool AutomationEventQueue::CheckReturnEvent() {
109 if (wait_automation_reply_
.get()) {
110 AutomationEventQueue::AutomationEvent
* event
= wait_observer_id_
< 0 ?
112 PopEvent(wait_observer_id_
);
114 wait_automation_reply_
->SendSuccess(event
->GetValue());
115 wait_automation_reply_
.reset();
116 wait_observer_id_
= -1;