summaryrefslogtreecommitdiffstats
path: root/chrome/browser/automation/automation_event_observers.cc
blob: 8f2f5acd6d5245aed075e2003d0f3668bc13a650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 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 <string>

#include "base/logging.h"
#include "base/stringprintf.h"
#include "chrome/browser/automation/automation_event_observers.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/dom_operation_notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"

const char* DomEventObserver::kSubstringReplaceWithObserverId = "$(id)";

AutomationEventObserver::AutomationEventObserver(
    AutomationEventQueue* event_queue, bool recurring)
    : event_queue_(event_queue),
      recurring_(recurring),
      observer_id_(-1),
      event_count_(0) {
  DCHECK(event_queue_ != NULL);
}

AutomationEventObserver::~AutomationEventObserver() {}

void AutomationEventObserver::NotifyEvent(DictionaryValue* value) {
  if (event_queue_) {
    if (!value)
      value = new DictionaryValue;
    value->SetInteger("observer_id", GetId());
    event_queue_->NotifyEvent(
        new AutomationEventQueue::AutomationEvent(
            GetId(), value));
    event_count_++;
  } else if (value) {
    delete value;
  }
}

void AutomationEventObserver::Init(int observer_id) {
    observer_id_ = observer_id;
}

int AutomationEventObserver::GetId() const {
  return observer_id_;
}

void AutomationEventObserver::RemoveIfDone() {
  if (!recurring_ && event_count_ > 0 && event_queue_) {
    event_queue_->RemoveObserver(GetId());
  }
}

DomEventObserver::DomEventObserver(
    AutomationEventQueue* event_queue,
    const std::string& event_name,
    int automation_id,
    bool recurring)
    : AutomationEventObserver(event_queue, recurring),
      event_name_(event_name),
      event_name_base_(event_name),
      automation_id_(automation_id) {
  registrar_.Add(this, content::NOTIFICATION_DOM_OPERATION_RESPONSE,
                 content::NotificationService::AllSources());
}

DomEventObserver::~DomEventObserver() {}

void DomEventObserver::Init(int observer_id) {
    AutomationEventObserver::Init(observer_id);
    std::string id_string = base::StringPrintf("%d", observer_id);
    event_name_ = event_name_base_;
    int replace_pos =
        event_name_.find(kSubstringReplaceWithObserverId);
    if (replace_pos != (int)std::string::npos) {
      event_name_.replace(replace_pos,
                          ::strlen(kSubstringReplaceWithObserverId),
                          id_string);
    }
}

void DomEventObserver::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  if (type == content::NOTIFICATION_DOM_OPERATION_RESPONSE) {
    content::Details<content::DomOperationNotificationDetails> dom_op_details(
        details);
    if ((dom_op_details->automation_id == automation_id_ ||
         automation_id_ == -1) &&
        (event_name_.length() == 0 ||
         event_name_.compare(dom_op_details->json) == 0)) {
      DictionaryValue* dict = new DictionaryValue;
      dict->SetString("type", "raised_event");
      dict->SetString("name", dom_op_details->json);
      NotifyEvent(dict);
    }
  }
  // Nothing should happen after RemoveIfDone() as it may delete the object.
  RemoveIfDone();
}