Bumping manifests a=b2g-bump
[gecko.git] / widget / windows / WindowHook.cpp
blob2c3a533f34bfdbb083e02f1d3e74ecebda1a583a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */
7 #include "WindowHook.h"
8 #include "nsWindow.h"
9 #include "nsWindowDefs.h"
11 namespace mozilla {
12 namespace widget {
14 nsresult
15 WindowHook::AddHook(UINT nMsg, Callback callback, void *context) {
16 MessageData *data = LookupOrCreate(nMsg);
18 if (!data)
19 return NS_ERROR_OUT_OF_MEMORY;
21 // Ensure we don't overwrite another hook
22 NS_ENSURE_TRUE(nullptr == data->hook.cb, NS_ERROR_UNEXPECTED);
24 data->hook = CallbackData(callback, context);
26 return NS_OK;
29 nsresult
30 WindowHook::RemoveHook(UINT nMsg, Callback callback, void *context) {
31 CallbackData cbdata(callback, context);
32 MessageData *data = Lookup(nMsg);
33 if (!data)
34 return NS_ERROR_UNEXPECTED;
35 if (data->hook != cbdata)
36 return NS_ERROR_UNEXPECTED;
37 data->hook = CallbackData();
39 DeleteIfEmpty(data);
40 return NS_OK;
43 nsresult
44 WindowHook::AddMonitor(UINT nMsg, Callback callback, void *context) {
45 MessageData *data = LookupOrCreate(nMsg);
46 return (data && data->monitors.AppendElement(CallbackData(callback, context)))
47 ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
50 nsresult
51 WindowHook::RemoveMonitor(UINT nMsg, Callback callback, void *context) {
52 CallbackData cbdata(callback, context);
53 MessageData *data = Lookup(nMsg);
54 if (!data)
55 return NS_ERROR_UNEXPECTED;
56 CallbackDataArray::index_type idx = data->monitors.IndexOf(cbdata);
57 if (idx == CallbackDataArray::NoIndex)
58 return NS_ERROR_UNEXPECTED;
59 data->monitors.RemoveElementAt(idx);
60 DeleteIfEmpty(data);
61 return NS_OK;
64 WindowHook::MessageData *
65 WindowHook::Lookup(UINT nMsg) {
66 MessageDataArray::index_type idx;
67 for (idx = 0; idx < mMessageData.Length(); idx++) {
68 MessageData &data = mMessageData[idx];
69 if (data.nMsg == nMsg)
70 return &data;
72 return nullptr;
75 WindowHook::MessageData *
76 WindowHook::LookupOrCreate(UINT nMsg) {
77 MessageData *data = Lookup(nMsg);
78 if (!data) {
79 data = mMessageData.AppendElement();
81 if (!data)
82 return nullptr;
84 data->nMsg = nMsg;
86 return data;
89 void
90 WindowHook::DeleteIfEmpty(MessageData *data) {
91 // Never remove a MessageData that has still a hook or monitor entries.
92 if (data->hook || !data->monitors.IsEmpty())
93 return;
95 MessageDataArray::index_type idx;
96 idx = data - mMessageData.Elements();
97 NS_ASSERTION(idx >= 0 && idx < mMessageData.Length(), "Attempted to delete MessageData that doesn't belong to this array!");
98 mMessageData.RemoveElementAt(idx);
101 bool
102 WindowHook::Notify(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam,
103 MSGResult& aResult)
105 MessageData *data = Lookup(nMsg);
106 if (!data)
107 return false;
109 uint32_t length = data->monitors.Length();
110 for (uint32_t midx = 0; midx < length; midx++) {
111 data->monitors[midx].Invoke(hWnd, nMsg, wParam, lParam, &aResult.mResult);
114 aResult.mConsumed =
115 data->hook.Invoke(hWnd, nMsg, wParam, lParam, &aResult.mResult);
116 return aResult.mConsumed;
119 bool
120 WindowHook::CallbackData::Invoke(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam,
121 LRESULT *aResult) {
122 if (!cb)
123 return false;
124 return cb(context, hWnd, msg, wParam, lParam, aResult);
126 } // namespace widget
127 } // namespace mozilla