Index: chrome/browser/automation/automation_event_queue.cc |
diff --git a/chrome/browser/automation/automation_event_queue.cc b/chrome/browser/automation/automation_event_queue.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..20315f3e03037b0132b62c79ac777b3d07c7b37b |
--- /dev/null |
+++ b/chrome/browser/automation/automation_event_queue.cc |
@@ -0,0 +1,137 @@ |
+// 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. |
+ |
+#include "chrome/browser/automation/automation_event_observer.h" |
+#include "chrome/browser/automation/automation_event_queue.h" |
+ |
+#include "chrome/browser/automation/automation_provider.h" |
+#include "chrome/browser/automation/automation_provider_json.h" |
+#include "content/public/browser/notification_service.h" |
+#include "content/public/browser/notification_types.h" |
+ |
+AutomationEventQueue::AutomationEventQueue() |
+ : observer_id_count_(0), |
+ wait_reply_message_(NULL), |
+ wait_observer_id_(-1) {} |
+ |
+AutomationEventQueue::~AutomationEventQueue() { |
+ ClearObservers(); |
+ Clear(); |
+} |
+ |
+AutomationEventQueue::AutomationEvent::AutomationEvent( |
+ int observer_id, DictionaryValue* event_value) |
+ : event_value_(event_value), observer_id_(observer_id) {} |
+ |
+void AutomationEventQueue::AutomationEvent::ReturnValue( |
+ AutomationProvider* automation, |
+ IPC::Message* reply_message) { |
+ AutomationJSONReply(automation, reply_message) |
+ .SendSuccess(event_value_.get()); |
+} |
+ |
+void AutomationEventQueue::GetEvent(AutomationProvider* automation, |
+ IPC::Message* reply_message, |
+ int observer_id, |
+ bool blocking) { |
+ SetWait(automation, reply_message, observer_id); |
+ bool r = CheckReturnEvent(); |
Nirnimesh
2012/02/24 23:18:09
use better varnames
craigdh
2012/02/27 22:43:38
Done.
|
+ if (!r && !blocking) { |
+ ClearWait(); |
+ AutomationJSONReply(automation, reply_message).SendSuccess(NULL); |
+ } |
+} |
+ |
+void AutomationEventQueue::Clear() { |
+ std::list<AutomationEvent*>::iterator i; |
+ for (i = event_queue_.begin(); i != event_queue_.end(); i++) { |
+ delete *i; |
+ } |
+ event_queue_.clear(); |
+} |
+ |
+bool AutomationEventQueue::IsEmpty() { |
+ return event_queue_.empty(); |
+} |
+ |
+AutomationEventQueue::AutomationEvent* AutomationEventQueue::PopEvent() { |
+ if (event_queue_.empty()) { |
+ return NULL; |
+ } |
+ AutomationEvent* ret = event_queue_.back(); |
+ event_queue_.pop_back(); |
+ return ret; |
+} |
+ |
+AutomationEventQueue::AutomationEvent* AutomationEventQueue::PopEvent( |
+ int observer_id) { |
+ AutomationEvent* ret = NULL; |
+ std::list<AutomationEvent*>::reverse_iterator i; |
+ for (i = event_queue_.rbegin(); i != event_queue_.rend(); i++) { |
+ if (observer_id < 0 || (*i)->GetId() == observer_id) { |
+ ret = *i; |
+ break; |
+ } |
+ } |
+ if (ret) { |
+ event_queue_.remove(ret); |
+ return ret; |
+ } |
+ return NULL; |
+} |
+ |
+void AutomationEventQueue::NotifyEvent( |
+ AutomationEventQueue::AutomationEvent* event) { |
+ event_queue_.push_front(event); |
+ CheckReturnEvent(); |
+} |
+ |
+int AutomationEventQueue::AddObserver(AutomationEventObserver* observer) { |
+ int id = observer_id_count_++; |
+ observer->Init(id); |
+ observers_[id] = observer; |
+ return id; |
+} |
+ |
+void AutomationEventQueue::RemoveObserver(int observer_id) { |
+ if (observers_.count(observer_id)) { |
+ delete observers_[observer_id]; |
+ observers_.erase(observer_id); |
+ } |
+} |
+ |
+void AutomationEventQueue::ClearObservers() { |
+ std::map<int, AutomationEventObserver*>::iterator i; |
+ for (i = observers_.begin(); i != observers_.end(); i++) { |
+ delete (*i).second; |
+ } |
+ observers_.clear(); |
+} |
+ |
+bool AutomationEventQueue::CheckReturnEvent() { |
+ if (wait_automation_ && wait_reply_message_.get()) { |
+ AutomationEventQueue::AutomationEvent* event = wait_observer_id_ < 0 ? |
+ PopEvent() : |
+ PopEvent(wait_observer_id_); |
+ if (event) { |
+ event->ReturnValue(wait_automation_, wait_reply_message_.release()); |
+ delete event; |
+ ClearWait(); |
+ return true; |
+ } |
+ } |
+ return false; |
+} |
+ |
+void AutomationEventQueue::SetWait(AutomationProvider* automation, |
+ IPC::Message* reply_message, |
+ int observer_id) { |
+ wait_automation_ = automation; |
+ wait_reply_message_.reset(reply_message); |
+ wait_observer_id_ = observer_id; |
+} |
+ |
+void AutomationEventQueue::ClearWait() { |
+ SetWait(NULL, NULL, -1); |
+} |