summaryrefslogtreecommitdiffstats
path: root/chrome_frame/np_event_listener.h
blob: bb6da666dd87833a5e6c5f03ed565c06c669f94d (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) 2009 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.

#ifndef CHROME_FRAME_NP_EVENT_LISTENER_H_
#define CHROME_FRAME_NP_EVENT_LISTENER_H_

#include "base/logging.h"

#include "chrome_frame/utils.h"
#include "chrome_frame/np_browser_functions.h"

// Avoid conflicts with basictypes and the gecko sdk.
// (different definitions of uint32).
#define NO_NSPR_10_SUPPORT
#include "third_party/xulrunner-sdk/win/include/dom/nsIDOMEventListener.h"


class nsIDOMElement;

class NpEventDelegate {
 public:
  virtual void OnEvent(const char* event_name) = 0;
};

class NpEventListener {
 public:
  NS_IMETHOD_(nsrefcnt) AddRef() = 0;
  NS_IMETHOD_(nsrefcnt) Release() = 0;
  virtual bool Subscribe(NPP instance,
                         const char* event_names[],
                         int event_name_count) = 0;
  virtual bool Unsubscribe(NPP instance,
                           const char* event_names[],
                           int event_name_count) = 0;

 protected:
  virtual ~NpEventListener() {}
};

// A little helper class to implement simple ref counting
// and assert on single threadedness.
template <class T>
class NpEventListenerBase : public NpEventListener {
 public:
  explicit NpEventListenerBase(NpEventDelegate* delegate)
      : ref_count_(0), delegate_(delegate) {
    DCHECK(delegate_);
    thread_id_ = ::GetCurrentThreadId();
  }

  ~NpEventListenerBase() {
    DCHECK(thread_id_ == ::GetCurrentThreadId());
  }

  NS_IMETHOD_(nsrefcnt) AddRef() {
    DCHECK(thread_id_ == ::GetCurrentThreadId());
    ref_count_++;
    return ref_count_;
  }

  NS_IMETHOD_(nsrefcnt) Release() {
    DCHECK(thread_id_ == ::GetCurrentThreadId());
    ref_count_--;

    if (!ref_count_) {
      T* me = static_cast<T*>(this);
      delete me;
      return 0;
    }

    return ref_count_;
  }

 protected:
  nsrefcnt ref_count_;
  NpEventDelegate* delegate_;
  AddRefModule module_ref_;
  // used to DCHECK on expected single-threaded usage
  unsigned long thread_id_;
};

// Implements nsIDOMEventListener in order to receive events from DOM
// elements inside an HTML page.
class DomEventListener
  : public nsIDOMEventListener,
  public NpEventListenerBase<DomEventListener> {
 public:
  explicit DomEventListener(NpEventDelegate* delegate);
  virtual ~DomEventListener();

  // Implementation of NpEventListener
  virtual bool Subscribe(NPP instance,
                         const char* event_names[],
                         int event_name_count);
  virtual bool Unsubscribe(NPP instance,
                           const char* event_names[],
                           int event_name_count);
 protected:
  // We implement QueryInterface etc ourselves in order to avoid
  // extra dependencies brought on by the NS_IMPL_* macros.
  NS_IMETHOD QueryInterface(REFNSIID iid, void** ptr);
  NS_IMETHOD_(nsrefcnt) AddRef() {
    return NpEventListenerBase<DomEventListener>::AddRef();
  }

  NS_IMETHOD_(nsrefcnt) Release() {
    return NpEventListenerBase<DomEventListener>::Release();
  }

  // Implementation of nsIDOMEventListener
  NS_IMETHOD HandleEvent(nsIDOMEvent *event);

 private:
  static bool GetObjectElement(NPP instance, nsIDOMElement** element);

 private:
  DISALLOW_COPY_AND_ASSIGN(DomEventListener);
};

class NPObjectEventListener
  : public NpEventListenerBase<NPObjectEventListener> {
 public:
  explicit NPObjectEventListener(NpEventDelegate* delegate);
  ~NPObjectEventListener();

  // Implementation of NpEventListener
  virtual bool Subscribe(NPP instance,
                         const char* event_names[],
                         int event_name_count);
  virtual bool Unsubscribe(NPP instance,
                           const char* event_names[],
                           int event_name_count);

 protected:
  // NPObject structure which is exposed by NPObjectEventListener.
  class Npo : public NPObject {
   public:
    explicit Npo(NPP npp) : npp_(npp), listener_(NULL) {
    }

    void Initialize(NPObjectEventListener* listener) {
      listener_ = listener;
    }

    inline NPObjectEventListener* listener() const {
      return listener_;
    }

    inline NPP npp() const {
      return npp_;
    }

   protected:
    NPP npp_;
    NPObjectEventListener* listener_;
    AddRefModule module_ref_;
  };

  static NPClass* PluginClass();

  static bool HasMethod(Npo* npo, NPIdentifier name);
  static bool Invoke(Npo* npo, NPIdentifier name, const NPVariant* args,
                     uint32_t arg_count, NPVariant* result);
  static NPObject* AllocateObject(NPP instance, NPClass* class_name);
  static void DeallocateObject(Npo* npo);

  typedef enum {
    HANDLE_EVENT,
    TYPE,
    ADD_EVENT_LISTENER,
    REMOVE_EVENT_LISTENER,
    TAG_NAME,
    PARENT_ELEMENT,
    IDENTIFIER_COUNT,
  } CachedStringIdentifiers;

  static NPIdentifier* GetCachedStringIds();

  void HandleEvent(Npo* npo, NPObject* event);
  static NPObject* GetObjectElement(NPP instance);

 private:
  // Our NPObject.
  ScopedNpObject<Npo> npo_;

  DISALLOW_COPY_AND_ASSIGN(NPObjectEventListener);
};

#endif  // CHROME_FRAME_NP_EVENT_LISTENER_H_