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