Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / automation / automation_event_queue.cc
blob0e7574712c420cc1370de02ffd858f4af508a8a1
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.
5 #include <algorithm>
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "chrome/browser/automation/automation_event_observers.h"
10 #include "chrome/browser/automation/automation_event_queue.h"
11 #include "chrome/browser/automation/automation_provider_json.h"
12 #include "content/public/browser/notification_service.h"
13 #include "content/public/browser/notification_types.h"
15 AutomationEventQueue::CompareObserverId::CompareObserverId(int id) : id_(id) {}
17 bool AutomationEventQueue::CompareObserverId::operator()(
18 AutomationEvent* event) const {
19 return event->GetId() < 0 || event->GetId() == id_;
22 AutomationEventQueue::AutomationEventQueue()
23 : observer_id_count_(0), wait_observer_id_(-1) {}
25 AutomationEventQueue::~AutomationEventQueue() {
26 Clear();
29 AutomationEventQueue::AutomationEvent::AutomationEvent(
30 int observer_id, base::DictionaryValue* event_value)
31 : observer_id_(observer_id), event_value_(event_value) {}
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();
44 void AutomationEventQueue::Clear() {
45 ClearObservers();
46 ClearEvents();
49 bool AutomationEventQueue::IsEmpty() const {
50 return event_queue_.empty();
53 AutomationEventQueue::AutomationEvent* AutomationEventQueue::PopEvent() {
54 if (event_queue_.empty()) {
55 return NULL;
57 AutomationEvent* event = event_queue_.back();
58 event_queue_.pop_back();
59 return event;
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);
72 return event;
75 void AutomationEventQueue::NotifyEvent(
76 AutomationEventQueue::AutomationEvent* event) {
77 DCHECK(event);
78 VLOG(2) << "AutomationEventQueue::NotifyEvent id=" << event->GetId();
79 event_queue_.push_front(event);
80 CheckReturnEvent();
83 int AutomationEventQueue::AddObserver(AutomationEventObserver* observer) {
84 int id = observer_id_count_++;
85 observer->Init(id);
86 observers_[id] = observer;
87 return id;
90 bool AutomationEventQueue::RemoveObserver(int observer_id) {
91 if (observers_.find(observer_id) != observers_.end()) {
92 VLOG(2) << "AutomationEventQueue::RemoveObserver id=" << observer_id;
93 delete observers_[observer_id];
94 observers_.erase(observer_id);
95 return true;
97 return false;
100 void AutomationEventQueue::ClearObservers() {
101 STLDeleteValues(&observers_);
104 void AutomationEventQueue::ClearEvents() {
105 STLDeleteElements(&event_queue_);
108 bool AutomationEventQueue::CheckReturnEvent() {
109 if (wait_automation_reply_.get()) {
110 AutomationEventQueue::AutomationEvent* event = wait_observer_id_ < 0 ?
111 PopEvent() :
112 PopEvent(wait_observer_id_);
113 if (event) {
114 wait_automation_reply_->SendSuccess(event->GetValue());
115 wait_automation_reply_.reset();
116 wait_observer_id_ = -1;
117 delete event;
118 return true;
121 return false;