Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Side by Side Diff: chrome/browser/automation/automation_event_queue.cc

Issue 9372120: Implementation of AutomationEventQueue and associated framework to support generic non-blocking aut… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed Nirnimesh's comments. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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.h"
9 #include "chrome/browser/automation/automation_provider_json.h"
10 #include "content/public/browser/notification_service.h"
11 #include "content/public/browser/notification_types.h"
12
13 AutomationEventQueue::AutomationEventQueue()
14 : observer_id_count_(0),
15 wait_reply_message_(NULL),
16 wait_observer_id_(-1) {}
17
18 AutomationEventQueue::~AutomationEventQueue() {
19 ClearObservers();
20 Clear();
21 }
22
23 AutomationEventQueue::AutomationEvent::AutomationEvent(
24 int observer_id, DictionaryValue* event_value)
25 : event_value_(event_value), observer_id_(observer_id) {}
26
27 void AutomationEventQueue::AutomationEvent::ReturnValue(
28 AutomationProvider* automation,
29 IPC::Message* reply_message) {
30 AutomationJSONReply(automation, reply_message)
Nirnimesh 2012/02/28 09:13:09 This class should not have to respond back to the
craigdh 2012/02/28 22:42:56 I agree. Changed to GetValue().
31 .SendSuccess(event_value_.get());
32 }
33
34 void AutomationEventQueue::GetEvent(AutomationProvider* automation,
35 IPC::Message* reply_message,
36 int observer_id,
37 bool blocking) {
38 SetWait(automation, reply_message, observer_id);
39 if (!CheckReturnEvent() && !blocking) {
40 ClearWait();
41 AutomationJSONReply(automation, reply_message).SendSuccess(NULL);
42 }
43 }
44
45 void AutomationEventQueue::Clear() {
46 std::list<AutomationEvent*>::iterator i;
47 for (i = event_queue_.begin(); i != event_queue_.end(); i++) {
48 delete *i;
49 }
50 event_queue_.clear();
51 }
52
53 bool AutomationEventQueue::IsEmpty() {
54 return event_queue_.empty();
55 }
56
57 AutomationEventQueue::AutomationEvent* AutomationEventQueue::PopEvent() {
58 if (event_queue_.empty()) {
59 return NULL;
60 }
61 AutomationEvent* event = event_queue_.back();
62 event_queue_.pop_back();
63 return event;
64 }
65
66 AutomationEventQueue::AutomationEvent* AutomationEventQueue::PopEvent(
67 int observer_id) {
68 AutomationEvent* event = NULL;
69 std::list<AutomationEvent*>::reverse_iterator i;
70 for (i = event_queue_.rbegin(); i != event_queue_.rend(); i++) {
71 if (observer_id < 0 || (*i)->GetId() == observer_id) {
72 event = *i;
73 break;
74 }
75 }
76 if (event) {
77 event_queue_.remove(event);
78 return event;
79 }
80 return NULL;
81 }
82
83 void AutomationEventQueue::NotifyEvent(
84 AutomationEventQueue::AutomationEvent* event) {
85 event_queue_.push_front(event);
86 CheckReturnEvent();
87 }
88
89 int AutomationEventQueue::AddObserver(AutomationEventObserver* observer) {
90 int id = observer_id_count_++;
91 observer->Init(id);
92 observers_[id] = observer;
93 return id;
94 }
95
96 void AutomationEventQueue::RemoveObserver(int observer_id) {
97 if (observers_.count(observer_id)) {
98 delete observers_[observer_id];
99 observers_.erase(observer_id);
100 }
101 }
102
103 void AutomationEventQueue::ClearObservers() {
104 std::map<int, AutomationEventObserver*>::iterator i;
Nirnimesh 2012/02/28 09:13:09 s/i/it/
craigdh 2012/02/28 22:42:56 Done.
105 for (i = observers_.begin(); i != observers_.end(); i++) {
106 delete (*i).second;
Nirnimesh 2012/02/28 09:13:09 i->second
craigdh 2012/02/28 22:42:56 Done.
107 }
108 observers_.clear();
109 }
110
111 bool AutomationEventQueue::CheckReturnEvent() {
112 if (wait_automation_ && wait_reply_message_.get()) {
113 AutomationEventQueue::AutomationEvent* event = wait_observer_id_ < 0 ?
114 PopEvent() :
115 PopEvent(wait_observer_id_);
116 if (event) {
117 event->ReturnValue(wait_automation_, wait_reply_message_.release());
118 delete event;
119 ClearWait();
120 return true;
121 }
122 }
123 return false;
124 }
125
126 void AutomationEventQueue::SetWait(AutomationProvider* automation,
127 IPC::Message* reply_message,
128 int observer_id) {
129 wait_automation_ = automation;
130 wait_reply_message_.reset(reply_message);
131 wait_observer_id_ = observer_id;
132 }
133
134 void AutomationEventQueue::ClearWait() {
135 SetWait(NULL, NULL, -1);
136 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698