1 // Copyright (c) 2009 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 #ifndef CHROME_FRAME_NP_EVENT_LISTENER_H_
6 #define CHROME_FRAME_NP_EVENT_LISTENER_H_
8 #include "base/logging.h"
10 #include "chrome_frame/utils.h"
11 #include "chrome_frame/np_browser_functions.h"
13 // Avoid conflicts with basictypes and the gecko sdk.
14 // (different definitions of uint32).
15 #define NO_NSPR_10_SUPPORT
16 #include "third_party/xulrunner-sdk/win/include/dom/nsIDOMEventListener.h"
21 class NpEventDelegate
{
23 virtual void OnEvent(const char* event_name
) = 0;
26 class NpEventListener
{
28 NS_IMETHOD_(nsrefcnt
) AddRef() = 0;
29 NS_IMETHOD_(nsrefcnt
) Release() = 0;
30 virtual bool Subscribe(NPP instance
,
31 const char* event_names
[],
32 int event_name_count
) = 0;
33 virtual bool Unsubscribe(NPP instance
,
34 const char* event_names
[],
35 int event_name_count
) = 0;
38 virtual ~NpEventListener() {}
41 // A little helper class to implement simple ref counting
42 // and assert on single threadedness.
44 class NpEventListenerBase
: public NpEventListener
{
46 explicit NpEventListenerBase(NpEventDelegate
* delegate
)
47 : ref_count_(0), delegate_(delegate
) {
49 thread_id_
= ::GetCurrentThreadId();
52 ~NpEventListenerBase() {
53 DCHECK(thread_id_
== ::GetCurrentThreadId());
56 NS_IMETHOD_(nsrefcnt
) AddRef() {
57 DCHECK(thread_id_
== ::GetCurrentThreadId());
62 NS_IMETHOD_(nsrefcnt
) Release() {
63 DCHECK(thread_id_
== ::GetCurrentThreadId());
67 T
* me
= static_cast<T
*>(this);
77 NpEventDelegate
* delegate_
;
78 AddRefModule module_ref_
;
79 // used to DCHECK on expected single-threaded usage
80 unsigned long thread_id_
;
83 // Implements nsIDOMEventListener in order to receive events from DOM
84 // elements inside an HTML page.
85 class DomEventListener
86 : public nsIDOMEventListener
,
87 public NpEventListenerBase
<DomEventListener
> {
89 explicit DomEventListener(NpEventDelegate
* delegate
);
90 virtual ~DomEventListener();
92 // Implementation of NpEventListener
93 virtual bool Subscribe(NPP instance
,
94 const char* event_names
[],
95 int event_name_count
);
96 virtual bool Unsubscribe(NPP instance
,
97 const char* event_names
[],
98 int event_name_count
);
100 // We implement QueryInterface etc ourselves in order to avoid
101 // extra dependencies brought on by the NS_IMPL_* macros.
102 NS_IMETHOD
QueryInterface(REFNSIID iid
, void** ptr
);
103 NS_IMETHOD_(nsrefcnt
) AddRef() {
104 return NpEventListenerBase
<DomEventListener
>::AddRef();
107 NS_IMETHOD_(nsrefcnt
) Release() {
108 return NpEventListenerBase
<DomEventListener
>::Release();
111 // Implementation of nsIDOMEventListener
112 NS_IMETHOD
HandleEvent(nsIDOMEvent
*event
);
115 static bool GetObjectElement(NPP instance
, nsIDOMElement
** element
);
118 DISALLOW_COPY_AND_ASSIGN(DomEventListener
);
121 class NPObjectEventListener
122 : public NpEventListenerBase
<NPObjectEventListener
> {
124 explicit NPObjectEventListener(NpEventDelegate
* delegate
);
125 ~NPObjectEventListener();
127 // Implementation of NpEventListener
128 virtual bool Subscribe(NPP instance
,
129 const char* event_names
[],
130 int event_name_count
);
131 virtual bool Unsubscribe(NPP instance
,
132 const char* event_names
[],
133 int event_name_count
);
136 // NPObject structure which is exposed by NPObjectEventListener.
137 class Npo
: public NPObject
{
139 explicit Npo(NPP npp
) : npp_(npp
), listener_(NULL
) {
142 void Initialize(NPObjectEventListener
* listener
) {
143 listener_
= listener
;
146 inline NPObjectEventListener
* listener() const {
150 inline NPP
npp() const {
156 NPObjectEventListener
* listener_
;
157 AddRefModule module_ref_
;
160 static NPClass
* PluginClass();
162 static bool HasMethod(Npo
* npo
, NPIdentifier name
);
163 static bool Invoke(Npo
* npo
, NPIdentifier name
, const NPVariant
* args
,
164 uint32_t arg_count
, NPVariant
* result
);
165 static NPObject
* AllocateObject(NPP instance
, NPClass
* class_name
);
166 static void DeallocateObject(Npo
* npo
);
172 REMOVE_EVENT_LISTENER
,
176 } CachedStringIdentifiers
;
178 static NPIdentifier
* GetCachedStringIds();
180 void HandleEvent(Npo
* npo
, NPObject
* event
);
181 static NPObject
* GetObjectElement(NPP instance
);
185 ScopedNpObject
<Npo
> npo_
;
187 DISALLOW_COPY_AND_ASSIGN(NPObjectEventListener
);
190 #endif // CHROME_FRAME_NP_EVENT_LISTENER_H_