1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef nsIJSEventListener_h__
39 : #define nsIJSEventListener_h__
40 :
41 : #include "nsIScriptContext.h"
42 : #include "jsapi.h"
43 : #include "nsIDOMEventListener.h"
44 :
45 : class nsIScriptObjectOwner;
46 : class nsIAtom;
47 :
48 : #define NS_IJSEVENTLISTENER_IID \
49 : { 0x92f9212b, 0xa6aa, 0x4867, \
50 : { 0x93, 0x8a, 0x56, 0xbe, 0x17, 0x67, 0x4f, 0xd4 } }
51 :
52 : // Implemented by script event listeners. Used to retrieve the
53 : // script object corresponding to the event target and the handler itself.
54 : // (Note this interface is now used to store script objects for all
55 : // script languages, so is no longer JS specific)
56 : //
57 : // Note, mTarget is a raw pointer and the owner of the nsIJSEventListener object
58 : // is expected to call Disconnect()!
59 : class nsIJSEventListener : public nsIDOMEventListener
60 : {
61 : public:
62 : NS_DECLARE_STATIC_IID_ACCESSOR(NS_IJSEVENTLISTENER_IID)
63 :
64 : nsIJSEventListener(nsIScriptContext* aContext, JSObject* aScopeObject,
65 : nsISupports *aTarget, JSObject *aHandler)
66 : : mContext(aContext), mScopeObject(aScopeObject), mHandler(aHandler)
67 : {
68 : nsCOMPtr<nsISupports> base = do_QueryInterface(aTarget);
69 : mTarget = base.get();
70 : }
71 :
72 0 : nsIScriptContext *GetEventContext() const
73 : {
74 0 : return mContext;
75 : }
76 :
77 : nsISupports *GetEventTarget() const
78 : {
79 : return mTarget;
80 : }
81 :
82 0 : void Disconnect()
83 : {
84 0 : mTarget = nsnull;
85 0 : }
86 :
87 0 : JSObject* GetEventScope() const
88 : {
89 0 : return mScopeObject;
90 : }
91 :
92 0 : JSObject *GetHandler() const
93 : {
94 0 : return mHandler;
95 : }
96 :
97 : // Set a handler for this event listener. Must not be called if
98 : // there is already a handler! The handler must already be bound to
99 : // the right target.
100 : virtual void SetHandler(JSObject *aHandler) = 0;
101 :
102 : // Among the sub-classes that inherit (directly or indirectly) from nsINode,
103 : // measurement of the following members may be added later if DMD finds it is
104 : // worthwhile:
105 : // - nsIJSEventListener: mEventName
106 : //
107 : virtual size_t SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const
108 : {
109 : return 0;
110 :
111 : // Measurement of the following members may be added later if DMD finds it
112 : // is worthwhile:
113 : // - mContext
114 : // - mTarget
115 : //
116 : // The following members are not measured:
117 : // - mScopeObject, mHandler: because they're measured by the JS memory
118 : // reporters
119 : }
120 :
121 : virtual size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const
122 : {
123 : return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
124 : }
125 :
126 : protected:
127 : virtual ~nsIJSEventListener()
128 : {
129 : NS_ASSERTION(!mTarget, "Should have called Disconnect()!");
130 : }
131 : nsCOMPtr<nsIScriptContext> mContext;
132 : JSObject* mScopeObject;
133 : nsISupports* mTarget;
134 : JSObject *mHandler;
135 : };
136 :
137 : NS_DEFINE_STATIC_IID_ACCESSOR(nsIJSEventListener, NS_IJSEVENTLISTENER_IID)
138 :
139 : /* factory function. aHandler must already be bound to aTarget */
140 : nsresult NS_NewJSEventListener(nsIScriptContext *aContext,
141 : JSObject* aScopeObject, nsISupports* aTarget,
142 : nsIAtom* aType, JSObject* aHandler,
143 : nsIJSEventListener **aReturn);
144 :
145 : #endif // nsIJSEventListener_h__
|