Make SkFontHost_fontconfig support TTC font files.
[chromium-blink-merge.git] / chrome_frame / np_event_listener.h
blobbb6da666dd87833a5e6c5f03ed565c06c669f94d
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"
19 class nsIDOMElement;
21 class NpEventDelegate {
22 public:
23 virtual void OnEvent(const char* event_name) = 0;
26 class NpEventListener {
27 public:
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;
37 protected:
38 virtual ~NpEventListener() {}
41 // A little helper class to implement simple ref counting
42 // and assert on single threadedness.
43 template <class T>
44 class NpEventListenerBase : public NpEventListener {
45 public:
46 explicit NpEventListenerBase(NpEventDelegate* delegate)
47 : ref_count_(0), delegate_(delegate) {
48 DCHECK(delegate_);
49 thread_id_ = ::GetCurrentThreadId();
52 ~NpEventListenerBase() {
53 DCHECK(thread_id_ == ::GetCurrentThreadId());
56 NS_IMETHOD_(nsrefcnt) AddRef() {
57 DCHECK(thread_id_ == ::GetCurrentThreadId());
58 ref_count_++;
59 return ref_count_;
62 NS_IMETHOD_(nsrefcnt) Release() {
63 DCHECK(thread_id_ == ::GetCurrentThreadId());
64 ref_count_--;
66 if (!ref_count_) {
67 T* me = static_cast<T*>(this);
68 delete me;
69 return 0;
72 return ref_count_;
75 protected:
76 nsrefcnt ref_count_;
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> {
88 public:
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);
99 protected:
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);
114 private:
115 static bool GetObjectElement(NPP instance, nsIDOMElement** element);
117 private:
118 DISALLOW_COPY_AND_ASSIGN(DomEventListener);
121 class NPObjectEventListener
122 : public NpEventListenerBase<NPObjectEventListener> {
123 public:
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);
135 protected:
136 // NPObject structure which is exposed by NPObjectEventListener.
137 class Npo : public NPObject {
138 public:
139 explicit Npo(NPP npp) : npp_(npp), listener_(NULL) {
142 void Initialize(NPObjectEventListener* listener) {
143 listener_ = listener;
146 inline NPObjectEventListener* listener() const {
147 return listener_;
150 inline NPP npp() const {
151 return npp_;
154 protected:
155 NPP npp_;
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);
168 typedef enum {
169 HANDLE_EVENT,
170 TYPE,
171 ADD_EVENT_LISTENER,
172 REMOVE_EVENT_LISTENER,
173 TAG_NAME,
174 PARENT_ELEMENT,
175 IDENTIFIER_COUNT,
176 } CachedStringIdentifiers;
178 static NPIdentifier* GetCachedStringIds();
180 void HandleEvent(Npo* npo, NPObject* event);
181 static NPObject* GetObjectElement(NPP instance);
183 private:
184 // Our NPObject.
185 ScopedNpObject<Npo> npo_;
187 DISALLOW_COPY_AND_ASSIGN(NPObjectEventListener);
190 #endif // CHROME_FRAME_NP_EVENT_LISTENER_H_