1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  *
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef __mozilla_WindowHook_h__
8 #define __mozilla_WindowHook_h__
9 
10 #include <windows.h>
11 
12 #include <nsHashKeys.h>
13 #include <nsClassHashtable.h>
14 #include <nsTArray.h>
15 
16 #include "nsAppShell.h"
17 
18 class nsWindow;
19 
20 namespace mozilla {
21 namespace widget {
22 
23 struct MSGResult;
24 
25 class WindowHook {
26  public:
27   // It is expected that most callbacks will return false
28   typedef bool (*Callback)(void *aContext, HWND hWnd, UINT nMsg, WPARAM wParam,
29                            LPARAM lParam, LRESULT *aResult);
30 
31   nsresult AddHook(UINT nMsg, Callback callback, void *context);
32   nsresult RemoveHook(UINT nMsg, Callback callback, void *context);
33   nsresult AddMonitor(UINT nMsg, Callback callback, void *context);
34   nsresult RemoveMonitor(UINT nMsg, Callback callback, void *context);
35 
36  private:
37   struct CallbackData {
38     Callback cb;
39     void *context;
40 
CallbackDataCallbackData41     CallbackData() : cb(nullptr), context(nullptr) {}
CallbackDataCallbackData42     CallbackData(Callback cb, void *ctx) : cb(cb), context(ctx) {}
43     bool Invoke(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam,
44                 LRESULT *aResult);
45     bool operator==(const CallbackData &rhs) const {
46       return cb == rhs.cb && context == rhs.context;
47     }
48     bool operator!=(const CallbackData &rhs) const { return !(*this == rhs); }
49     explicit operator bool() const { return !!cb; }
50   };
51 
52   typedef nsTArray<CallbackData> CallbackDataArray;
53   struct MessageData {
54     UINT nMsg;
55     CallbackData hook;
56     CallbackDataArray monitors;
57   };
58 
59   bool Notify(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam,
60               MSGResult &aResult);
61 
62   MessageData *Lookup(UINT nMsg);
63   MessageData *LookupOrCreate(UINT nMsg);
64   void DeleteIfEmpty(MessageData *data);
65 
66   typedef nsTArray<MessageData> MessageDataArray;
67   MessageDataArray mMessageData;
68 
69   // For Notify
70   friend class ::nsWindow;
71 };
72 
73 }  // namespace widget
74 }  // namespace mozilla
75 
76 #endif  // __mozilla_WindowHook_h__
77