OLD | NEW |
---|---|
(Empty) | |
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 #include <algorithm> | |
6 | |
7 #include "chrome/browser/automation/automation_event_observer.h" | |
8 #include "chrome/browser/automation/automation_event_queue.h" | |
9 | |
Nirnimesh
2012/03/01 10:34:12
why the blank line?
| |
10 #include "chrome/browser/automation/automation_provider_json.h" | |
11 #include "content/public/browser/notification_service.h" | |
12 #include "content/public/browser/notification_types.h" | |
13 | |
14 AutomationEventQueue::CompareEventId::CompareEventId(int id) : id_(id) {} | |
15 | |
16 bool AutomationEventQueue::CompareEventId::operator()( | |
17 AutomationEvent* event) const { | |
18 return event->GetId() < 0 || event->GetId() == id_; | |
19 } | |
20 | |
21 AutomationEventQueue::AutomationEventQueue() | |
22 : observer_id_count_(0), | |
23 wait_automation_reply_(NULL), | |
24 wait_observer_id_(-1) {} | |
25 | |
26 AutomationEventQueue::~AutomationEventQueue() { | |
27 Clear(); | |
28 } | |
29 | |
30 AutomationEventQueue::AutomationEvent::AutomationEvent( | |
31 int observer_id, DictionaryValue* event_value) | |
32 : observer_id_(observer_id), event_value_(event_value) {} | |
33 | |
34 void AutomationEventQueue::GetNextEvent(AutomationJSONReply* reply, | |
35 int observer_id, | |
36 bool blocking) { | |
37 wait_automation_reply_.reset(reply); | |
38 wait_observer_id_ = observer_id; | |
39 if (!CheckReturnEvent() && !blocking && wait_automation_reply_.get()) { | |
40 wait_automation_reply_->SendSuccess(NULL); | |
41 wait_automation_reply_.reset(); | |
42 } | |
43 } | |
44 | |
45 void AutomationEventQueue::Clear() { | |
46 ClearObservers(); | |
47 ClearEvents(); | |
48 } | |
49 | |
50 bool AutomationEventQueue::IsEmpty() const { | |
51 return event_queue_.empty(); | |
52 } | |
53 | |
54 AutomationEventQueue::AutomationEvent* AutomationEventQueue::PopEvent() { | |
55 if (event_queue_.empty()) { | |
56 return NULL; | |
57 } | |
58 AutomationEvent* event = event_queue_.back(); | |
59 event_queue_.pop_back(); | |
60 return event; | |
61 } | |
62 | |
63 AutomationEventQueue::AutomationEvent* AutomationEventQueue::PopEvent( | |
64 int observer_id) { | |
65 AutomationEvent* event = NULL; | |
66 std::list<AutomationEvent*>::reverse_iterator it = | |
67 std::find_if(event_queue_.rbegin(), event_queue_.rend(), | |
68 CompareEventId(observer_id)); | |
69 if (it != event_queue_.rend()) { | |
70 event = *it; | |
71 event_queue_.remove(event); | |
72 } | |
73 return event; | |
74 } | |
75 | |
76 void AutomationEventQueue::NotifyEvent( | |
77 AutomationEventQueue::AutomationEvent* event) { | |
78 event_queue_.push_front(event); | |
79 CheckReturnEvent(); | |
80 } | |
81 | |
82 int AutomationEventQueue::AddObserver(AutomationEventObserver* observer) { | |
83 int id = observer_id_count_++; | |
84 observer->Init(id); | |
85 observers_[id] = observer; | |
86 return id; | |
87 } | |
88 | |
89 bool AutomationEventQueue::RemoveObserver(int observer_id) { | |
90 if (observers_.find(observer_id) != observers_.end()) { | |
91 delete observers_[observer_id]; | |
92 observers_.erase(observer_id); | |
93 return true; | |
94 } | |
95 return false; | |
96 } | |
97 | |
98 void AutomationEventQueue::ClearObservers() { | |
99 std::map<int, AutomationEventObserver*>::iterator it; | |
100 for (it = observers_.begin(); it != observers_.end(); it++) { | |
101 delete it->second; | |
102 } | |
103 observers_.clear(); | |
104 } | |
105 | |
106 void AutomationEventQueue::ClearEvents() { | |
107 std::list<AutomationEvent*>::iterator it; | |
108 for (it = event_queue_.begin(); it != event_queue_.end(); it++) { | |
109 delete *it; | |
110 } | |
111 event_queue_.clear(); | |
112 } | |
113 | |
114 bool AutomationEventQueue::CheckReturnEvent() { | |
115 if (wait_automation_reply_.get()) { | |
116 AutomationEventQueue::AutomationEvent* event = wait_observer_id_ < 0 ? | |
117 PopEvent() : | |
118 PopEvent(wait_observer_id_); | |
119 if (event) { | |
120 wait_automation_reply_->SendSuccess(event->GetValue()); | |
121 wait_automation_reply_.reset(); | |
122 wait_observer_id_ = -1; | |
123 delete event; | |
124 return true; | |
125 } | |
126 } | |
127 return false; | |
128 } | |
OLD | NEW |