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 "chrome/browser/automation/automation_event_observer.h" | |
6 #include "chrome/browser/automation/automation_event_queue.h" | |
7 | |
8 #include "chrome/browser/automation/automation_provider_json.h" | |
9 #include "content/public/browser/notification_service.h" | |
10 #include "content/public/browser/notification_types.h" | |
11 | |
12 AutomationEventQueue::AutomationEventQueue() | |
13 : observer_id_count_(0), | |
14 wait_automation_reply_(NULL), | |
15 wait_observer_id_(-1) {} | |
16 | |
17 AutomationEventQueue::~AutomationEventQueue() { | |
18 Clear(); | |
19 } | |
20 | |
21 AutomationEventQueue::AutomationEvent::AutomationEvent( | |
22 int observer_id, DictionaryValue* event_value) | |
23 : event_value_(event_value), observer_id_(observer_id) {} | |
frankf
2012/02/29 02:29:00
order of member value initialization should match
craigdh
2012/02/29 22:53:43
Whoops. Done.
| |
24 | |
25 void AutomationEventQueue::GetNextEvent(AutomationJSONReply* reply, | |
frankf
2012/02/29 02:29:00
You're making a copy of reply, why can't you make
craigdh
2012/02/29 22:53:43
I'm not making a copy, a shared_ptr is taking owne
| |
26 int observer_id, | |
27 bool blocking) { | |
28 wait_automation_reply_.reset(reply); | |
29 wait_observer_id_ = observer_id; | |
30 if (!CheckReturnEvent() && !blocking && wait_automation_reply_.get()) { | |
31 wait_automation_reply_->SendSuccess(NULL); | |
32 wait_automation_reply_.reset(); | |
33 } | |
34 } | |
35 | |
36 void AutomationEventQueue::Clear() { | |
37 ClearObservers(); | |
38 ClearEvents(); | |
39 } | |
40 | |
41 bool AutomationEventQueue::IsEmpty() { | |
42 return event_queue_.empty(); | |
43 } | |
44 | |
45 AutomationEventQueue::AutomationEvent* AutomationEventQueue::PopEvent() { | |
46 if (event_queue_.empty()) { | |
47 return NULL; | |
48 } | |
49 AutomationEvent* event = event_queue_.back(); | |
50 event_queue_.pop_back(); | |
51 return event; | |
52 } | |
53 | |
54 AutomationEventQueue::AutomationEvent* AutomationEventQueue::PopEvent( | |
55 int observer_id) { | |
56 AutomationEvent* event = NULL; | |
57 std::list<AutomationEvent*>::reverse_iterator it; | |
58 for (it = event_queue_.rbegin(); it != event_queue_.rend(); it++) { | |
59 if (observer_id < 0 || (*it)->GetId() == observer_id) { | |
60 event = *it; | |
61 break; | |
frankf
2012/02/29 02:29:00
you could use find_if here.
Nirnimesh
2012/02/29 03:22:08
You could directly call .erase(it) here, instead o
craigdh
2012/02/29 22:53:43
Actually, erase() won't take a reverse_iterator an
craigdh
2012/02/29 22:53:43
I'd have to write a predicate class to check the i
| |
62 } | |
63 } | |
64 if (event) { | |
Nirnimesh
2012/02/29 03:22:08
event wil already be NULL if this if is not fired,
| |
65 event_queue_.remove(event); | |
66 return event; | |
67 } | |
68 return NULL; | |
69 } | |
70 | |
71 void AutomationEventQueue::NotifyEvent( | |
72 AutomationEventQueue::AutomationEvent* event) { | |
73 event_queue_.push_front(event); | |
74 CheckReturnEvent(); | |
75 } | |
76 | |
77 int AutomationEventQueue::AddObserver(AutomationEventObserver* observer) { | |
78 int id = observer_id_count_++; | |
79 observer->Init(id); | |
80 observers_[id] = observer; | |
81 return id; | |
82 } | |
83 | |
84 bool AutomationEventQueue::RemoveObserver(int observer_id) { | |
85 if (observers_.count(observer_id)) { | |
Nirnimesh
2012/02/29 03:22:08
You don't really need the count.
Use observers.fin
craigdh
2012/02/29 22:53:43
observers_ is a map so count just returns whether
| |
86 delete observers_[observer_id]; | |
87 observers_.erase(observer_id); | |
88 return true; | |
89 } | |
90 return false; | |
91 } | |
92 | |
93 void AutomationEventQueue::ClearObservers() { | |
94 std::map<int, AutomationEventObserver*>::iterator it; | |
95 for (it = observers_.begin(); it != observers_.end(); it++) { | |
96 delete it->second; | |
97 } | |
98 observers_.clear(); | |
99 } | |
100 | |
101 void AutomationEventQueue::ClearEvents() { | |
102 std::list<AutomationEvent*>::iterator i; | |
Nirnimesh
2012/02/29 03:22:08
s/i/it/
craigdh
2012/02/29 22:53:43
Done.
| |
103 for (i = event_queue_.begin(); i != event_queue_.end(); i++) { | |
104 delete *i; | |
105 } | |
106 event_queue_.clear(); | |
107 } | |
108 | |
109 bool AutomationEventQueue::CheckReturnEvent() { | |
110 if (wait_automation_reply_.get()) { | |
111 AutomationEventQueue::AutomationEvent* event = wait_observer_id_ < 0 ? | |
112 PopEvent() : | |
113 PopEvent(wait_observer_id_); | |
frankf
2012/02/29 02:29:00
alignment issue
craigdh
2012/02/29 22:53:43
Done.
| |
114 if (event) { | |
115 wait_automation_reply_->SendSuccess(event->GetValue()); | |
116 wait_automation_reply_.reset(); | |
117 wait_observer_id_ = -1; | |
118 delete event; | |
119 return true; | |
120 } | |
121 } | |
122 return false; | |
123 } | |
OLD | NEW |