sbWindowWatcher.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2009 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 #ifndef __SB_WINDOWWATCHER_H__
28 #define __SB_WINDOWWATCHER_H__
29 
35 // Songbird imports.
36 #include <sbIWindowWatcher.h>
37 
38 // Mozilla imports.
39 #include <nsClassHashtable.h>
40 #include <nsCOMArray.h>
41 #include <nsIDOMEventListener.h>
42 #include <nsIDOMEventTarget.h>
43 #include <nsIDOMWindowInternal.h>
44 #include <nsIObserver.h>
45 #include <nsIObserverService.h>
46 #include <nsIThreadManager.h>
47 #include <nsIWindowMediator.h>
48 #include <nsIWindowWatcher.h>
49 #include <nsTArray.h>
50 #include <prmon.h>
51 
52 #include <sbWeakReference.h>
53 
61 
63  public nsIObserver,
65 {
66  //----------------------------------------------------------------------------
67  //
68  // Class friends.
69  //
70  //----------------------------------------------------------------------------
71 
73 
74 
75  //----------------------------------------------------------------------------
76  //
77  // Public interface.
78  //
79  //----------------------------------------------------------------------------
80 
81 public:
82 
83  //
84  // Inherited interfaces.
85  //
86 
88  NS_DECL_SBIWINDOWWATCHER
89  NS_DECL_NSIOBSERVER
90 
91 
92  //
93  // Songbird window watcher services.
94  //
95 
97 
98  virtual ~sbWindowWatcher();
99 
100  nsresult Init();
101 
102  void Finalize();
103 
104 
105  //----------------------------------------------------------------------------
106  //
107  // Private interface.
108  //
109  //----------------------------------------------------------------------------
110 
111 private:
112 
113  //
114  // mWindowWatcher Window watcher service.
115  // mWindowMediator Window mediator service.
116  // mObserverService Observer service.
117  // mThreadManager Thread manager.
118  // mSentMainWinPresentedNotification
119  // True if the main window presented notification
120  // has been sent.
121  // mMonitor Monitor for the window watcher.
122  // mIsShuttingDown True if the window watcher service is shutting
123  // down.
124  // mWindowList List of windows.
125  // mWindowInfoTable Table of window information.
126  // mCallWithWindowList List of call with window callbacks.
127  // mServicingCallWithWindowList
128  // True if the call with window list is being
129  // serviced.
130  //
131  // The following fields must only be accessed under the window watcher
132  // monitor:
133  //
134  // mIsShuttingDown
135  // mWindowList
136  // mWindowInfoTable
137  // mCallWithWindowList
138  // mServicingCallWithWindowList
139  //
140 
141  nsCOMPtr<nsIWindowWatcher> mWindowWatcher;
142  nsCOMPtr<nsIWindowMediator> mWindowMediator;
143  nsCOMPtr<nsIObserverService> mObserverService;
144  nsCOMPtr<nsIThreadManager> mThreadManager;
145  PRBool mSentMainWinPresentedNotification;
146  PRMonitor* mMonitor;
147  PRBool mIsShuttingDown;
148  nsCOMArray<nsIDOMWindow> mWindowList;
149 
150  class WindowInfo
151  {
152  public:
153  WindowInfo() : isReady(PR_FALSE)
154  {
155  MOZ_COUNT_CTOR(WindowInfo);
156  }
157  ~WindowInfo()
158  {
159  MOZ_COUNT_DTOR(WindowInfo);
160  }
161 
162  nsCOMPtr<nsIDOMWindow> window;
163  nsCOMPtr<nsIDOMEventTarget> eventTarget;
164  nsRefPtr<sbWindowWatcherEventListener>
165  eventListener;
166  PRBool isReady;
167  };
168  nsClassHashtable<nsISupportsHashKey, WindowInfo>
169  mWindowInfoTable;
170 
171  typedef struct
172  {
173  nsString windowType;
174  nsCOMPtr<sbICallWithWindowCallback> callback;
175  } CallWithWindowInfo;
176  nsTArray<CallWithWindowInfo> mCallWithWindowList;
177  PRBool mServicingCallWithWindowList;
178 
179 
180  //
181  // Internal Songbird window watcher nsIObserver services.
182  //
183 
184  nsresult OnDOMWindowOpened(nsISupports* aSubject,
185  const PRUnichar* aData);
186 
187  nsresult OnDOMWindowClosed(nsISupports* aSubject,
188  const PRUnichar* aData);
189 
190  nsresult OnQuitApplicationGranted();
191 
192 
193  //
194  // Internal Songbird window watcher services.
195  //
196 
197  void Shutdown();
198 
199  nsresult AddWindow(nsIDOMWindow* aWindow);
200 
201  nsresult RemoveWindow(nsIDOMWindow* aWindow);
202 
203  void RemoveAllWindows();
204 
205  void OnWindowReady(nsIDOMWindow* aWindow);
206 
207  nsresult GetWindowType(nsIDOMWindow* aWindow,
208  nsAString& aWindowType);
209 
210  nsresult InvokeCallWithWindowCallbacks(nsIDOMWindow* aWindow);
211 
212  nsresult GetProxiedWindowWatcher(sbIWindowWatcher** aWindowWatcher);
213 };
214 
215 
216 //
217 // Songbird window watcher component defs.
218 //
219 
220 #define SB_WINDOWWATCHER_CLASSNAME "sbWindowWatcher"
221 #define SB_WINDOWWATCHER_CID \
222  /* {4d83ab89-f909-4e71-8d5d-d0884d0cb788} */ \
223  { \
224  0x4d83ab89, \
225  0xf909, \
226  0x4e71, \
227  { 0x8d, 0x5d, 0xd0, 0x88, 0x4d, 0x0c, 0xb7, 0x88 } \
228  }
229 
230 
231 //------------------------------------------------------------------------------
232 //------------------------------------------------------------------------------
233 //
234 // Songbird window watcher event listener class.
235 //
236 //------------------------------------------------------------------------------
237 //------------------------------------------------------------------------------
238 
244 {
245  //----------------------------------------------------------------------------
246  //
247  // Public interface.
248  //
249  //----------------------------------------------------------------------------
250 
251 public:
252 
253  //
254  // Inherited interfaces.
255  //
256 
258  NS_DECL_NSIDOMEVENTLISTENER
259 
260 
261  //
262  // Event listener services.
263  //
264 
265  static nsresult New(sbWindowWatcherEventListener** aListener,
266  sbWindowWatcher* aSBWindowWatcher,
267  nsIDOMWindow* aWindow);
268 
272  nsresult AddEventListener(const char* aEventName);
273 
277  nsresult ClearEventListeners();
278 
279  //----------------------------------------------------------------------------
280  //
281  // Private interface.
282  //
283  //----------------------------------------------------------------------------
284 
285 private:
286 
287  //
288  // mWeakSBWindowWatcher Weak Songbird window watcher reference.
289  // mSBWindowWatcher Songbird window watcher.
290  // mWindow Window for which to handle events.
291  // mEventTarget The event target to listen to
292  // mOutstandingEvents The names of events that still have to occur
293  //
294 
295  nsCOMPtr<nsIWeakReference> mWeakSBWindowWatcher;
296  sbWindowWatcher* mSBWindowWatcher;
297  nsCOMPtr<nsIDOMWindow> mWindow;
298  nsCOMPtr<nsIDOMEventTarget> mEventTarget;
299  nsTArray<nsString> mOutstandingEvents;
300 
301 
302  //
303  // Internal event listener services.
304  //
305 
306  nsresult Initialize();
307 
309  nsIDOMWindow* aWindow) :
310  mSBWindowWatcher(aSBWindowWatcher),
311  mWindow(aWindow)
312  {
313  }
314 };
315 
316 
317 //------------------------------------------------------------------------------
318 //------------------------------------------------------------------------------
319 //
320 // Songbird window watcher wait for window class.
321 //
322 //------------------------------------------------------------------------------
323 //------------------------------------------------------------------------------
324 
331 {
332  //----------------------------------------------------------------------------
333  //
334  // Public interface.
335  //
336  //----------------------------------------------------------------------------
337 
338 public:
339 
340  //
341  // Inherited interfaces.
342  //
343 
345  NS_DECL_SBICALLWITHWINDOWCALLBACK
346 
347 
348  //
349  // Get window services.
350  //
351 
352  static nsresult New(sbWindowWatcherWaitForWindow** aWaitForWindow);
353 
355 
356  nsresult Wait(const nsAString& aWindowType);
357 
358 
359  //----------------------------------------------------------------------------
360  //
361  // Private interface.
362  //
363  //----------------------------------------------------------------------------
364 
365 private:
366 
367  //
368  // Internal wait for window fields.
369  //
370  // mSBWindowWatcher Songbird window watcher service.
371  // mReadyMonitor Window ready monitor.
372  // mWindow Window to return.
373  // mReady True when window is ready.
374  //
375  // The following fields must only be accessed under the ready monitor:
376  // mWindow
377  // mReady
378  //
379 
380  nsCOMPtr<sbIWindowWatcher> mSBWindowWatcher;
381  PRMonitor* mReadyMonitor;
382  nsCOMPtr<nsIDOMWindow> mWindow;
383  PRBool mReady;
384 
385  //
386  // Internal wait for window services.
387  //
388 
389  nsresult Initialize();
390 
392 };
393 
394 
395 #endif // __SB_WINDOWWATCHER_H__
396 
A callback interface that is called with a window.
let window
const nsIDOMWindow
nsresult AddEventListener(const char *aEventName)
NS_DECL_ISUPPORTS static NS_DECL_SBICALLWITHWINDOWCALLBACK nsresult New(sbWindowWatcherWaitForWindow **aWaitForWindow)
nsresult Wait(const nsAString &aWindowType)
Wait until a window of the type specified by aWindowType is available or until shutdown.
grep callback
NS_DECL_ISUPPORTS NS_DECL_SBIWINDOWWATCHER NS_DECL_NSIOBSERVER sbWindowWatcher()
virtual ~sbWindowWatcher()
NS_DECL_ISUPPORTS static NS_DECL_NSIDOMEVENTLISTENER nsresult New(sbWindowWatcherEventListener **aListener, sbWindowWatcher *aSBWindowWatcher, nsIDOMWindow *aWindow)
nsIDOMEventListener
_updateTextAndScrollDataForFrame aData