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.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) |
| 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 bool r = CheckReturnEvent(); |
| 40 if (!r && !blocking) { |
| 41 ClearWait(); |
| 42 AutomationJSONReply(automation, reply_message).SendSuccess(NULL); |
| 43 } |
| 44 } |
| 45 |
| 46 void AutomationEventQueue::Clear() { |
| 47 std::list<AutomationEvent*>::iterator i; |
| 48 for (i = event_queue_.begin(); i != event_queue_.end(); i++) { |
| 49 delete *i; |
| 50 } |
| 51 event_queue_.clear(); |
| 52 } |
| 53 |
| 54 bool AutomationEventQueue::IsEmpty() { |
| 55 return event_queue_.empty(); |
| 56 } |
| 57 |
| 58 AutomationEventQueue::AutomationEvent* AutomationEventQueue::PopEvent() { |
| 59 if (event_queue_.empty()) { |
| 60 return NULL; |
| 61 } |
| 62 AutomationEvent* ret = event_queue_.back(); |
| 63 event_queue_.pop_back(); |
| 64 return ret; |
| 65 } |
| 66 |
| 67 AutomationEventQueue::AutomationEvent* AutomationEventQueue::PopEvent( |
| 68 int observer_id) { |
| 69 AutomationEvent* ret = NULL; |
| 70 std::list<AutomationEvent*>::reverse_iterator i; |
| 71 for (i = event_queue_.rbegin(); i != event_queue_.rend(); i++) { |
| 72 if (observer_id < 0 || (*i)->GetId() == observer_id) { |
| 73 ret = *i; |
| 74 break; |
| 75 } |
| 76 } |
| 77 if (ret) { |
| 78 event_queue_.remove(ret); |
| 79 return ret; |
| 80 } |
| 81 return NULL; |
| 82 } |
| 83 |
| 84 void AutomationEventQueue::NotifyEvent( |
| 85 AutomationEventQueue::AutomationEvent* event) { |
| 86 event_queue_.push_front(event); |
| 87 CheckReturnEvent(); |
| 88 } |
| 89 |
| 90 int AutomationEventQueue::AddObserver(AutomationEventObserver* observer) { |
| 91 int id = observer_id_count_++; |
| 92 observer->Init(id); |
| 93 observers_[id] = observer; |
| 94 return id; |
| 95 } |
| 96 |
| 97 void AutomationEventQueue::RemoveObserver(int observer_id) { |
| 98 if (observers_.count(observer_id)) { |
| 99 delete observers_[observer_id]; |
| 100 observers_.erase(observer_id); |
| 101 } |
| 102 } |
| 103 |
| 104 void AutomationEventQueue::ClearObservers() { |
| 105 std::map<int, AutomationEventObserver*>::iterator i; |
| 106 for (i = observers_.begin(); i != observers_.end(); i++) { |
| 107 delete (*i).second; |
| 108 } |
| 109 observers_.clear(); |
| 110 } |
| 111 |
| 112 bool AutomationEventQueue::CheckReturnEvent() { |
| 113 if (wait_automation_ && wait_reply_message_.get()) { |
| 114 AutomationEventQueue::AutomationEvent* event = wait_observer_id_ < 0 ? |
| 115 PopEvent() : |
| 116 PopEvent(wait_observer_id_); |
| 117 if (event) { |
| 118 event->ReturnValue(wait_automation_, wait_reply_message_.release()); |
| 119 delete event; |
| 120 ClearWait(); |
| 121 return true; |
| 122 } |
| 123 } |
| 124 return false; |
| 125 } |
| 126 |
| 127 void AutomationEventQueue::SetWait(AutomationProvider* automation, |
| 128 IPC::Message* reply_message, |
| 129 int observer_id) { |
| 130 wait_automation_ = automation; |
| 131 wait_reply_message_.reset(reply_message); |
| 132 wait_observer_id_ = observer_id; |
| 133 } |
| 134 |
| 135 void AutomationEventQueue::ClearWait() { |
| 136 SetWait(NULL, NULL, -1); |
| 137 } |
OLD | NEW |