widget/windows/WindowHook.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/windows/WindowHook.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,128 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + *
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "WindowHook.h"
    1.11 +#include "nsWindow.h"
    1.12 +#include "nsWindowDefs.h"
    1.13 +
    1.14 +namespace mozilla {
    1.15 +namespace widget {
    1.16 +
    1.17 +nsresult
    1.18 +WindowHook::AddHook(UINT nMsg, Callback callback, void *context) {
    1.19 +  MessageData *data = LookupOrCreate(nMsg);
    1.20 +
    1.21 +  if (!data)
    1.22 +    return NS_ERROR_OUT_OF_MEMORY;
    1.23 +
    1.24 +  // Ensure we don't overwrite another hook
    1.25 +  NS_ENSURE_TRUE(nullptr == data->hook.cb, NS_ERROR_UNEXPECTED);
    1.26 +
    1.27 +  data->hook = CallbackData(callback, context);
    1.28 +
    1.29 +  return NS_OK;
    1.30 +}
    1.31 +
    1.32 +nsresult
    1.33 +WindowHook::RemoveHook(UINT nMsg, Callback callback, void *context) {
    1.34 +  CallbackData cbdata(callback, context);
    1.35 +  MessageData *data = Lookup(nMsg);
    1.36 +  if (!data)
    1.37 +    return NS_ERROR_UNEXPECTED;
    1.38 +  if (data->hook != cbdata)
    1.39 +    return NS_ERROR_UNEXPECTED;
    1.40 +  data->hook = CallbackData();
    1.41 +
    1.42 +  DeleteIfEmpty(data);
    1.43 +  return NS_OK;
    1.44 +}
    1.45 +
    1.46 +nsresult
    1.47 +WindowHook::AddMonitor(UINT nMsg, Callback callback, void *context) {
    1.48 +  MessageData *data = LookupOrCreate(nMsg);
    1.49 +  return (data && data->monitors.AppendElement(CallbackData(callback, context)))
    1.50 +         ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
    1.51 +}
    1.52 +
    1.53 +nsresult
    1.54 +WindowHook::RemoveMonitor(UINT nMsg, Callback callback, void *context) {
    1.55 +  CallbackData cbdata(callback, context);
    1.56 +  MessageData *data = Lookup(nMsg);
    1.57 +  if (!data)
    1.58 +    return NS_ERROR_UNEXPECTED;
    1.59 +  CallbackDataArray::index_type idx = data->monitors.IndexOf(cbdata);
    1.60 +  if (idx == CallbackDataArray::NoIndex)
    1.61 +    return NS_ERROR_UNEXPECTED;
    1.62 +  data->monitors.RemoveElementAt(idx);
    1.63 +  DeleteIfEmpty(data);
    1.64 +  return NS_OK;
    1.65 +}
    1.66 +
    1.67 +WindowHook::MessageData *
    1.68 +WindowHook::Lookup(UINT nMsg) {
    1.69 +  MessageDataArray::index_type idx;
    1.70 +  for (idx = 0; idx < mMessageData.Length(); idx++) {
    1.71 +    MessageData &data = mMessageData[idx];
    1.72 +    if (data.nMsg == nMsg)
    1.73 +      return &data;
    1.74 +  }
    1.75 +  return nullptr;
    1.76 +}
    1.77 +
    1.78 +WindowHook::MessageData *
    1.79 +WindowHook::LookupOrCreate(UINT nMsg) {
    1.80 +  MessageData *data = Lookup(nMsg);
    1.81 +  if (!data) {
    1.82 +    data = mMessageData.AppendElement();
    1.83 +
    1.84 +    if (!data)
    1.85 +      return nullptr;
    1.86 +
    1.87 +    data->nMsg = nMsg;
    1.88 +  }
    1.89 +  return data;
    1.90 +}
    1.91 +
    1.92 +void
    1.93 +WindowHook::DeleteIfEmpty(MessageData *data) {
    1.94 +  // Never remove a MessageData that has still a hook or monitor entries.
    1.95 +  if (data->hook || !data->monitors.IsEmpty())
    1.96 +    return;
    1.97 +
    1.98 +  MessageDataArray::index_type idx;
    1.99 +  idx = data - mMessageData.Elements();
   1.100 +  NS_ASSERTION(idx >= 0 && idx < mMessageData.Length(), "Attempted to delete MessageData that doesn't belong to this array!");
   1.101 +  mMessageData.RemoveElementAt(idx);
   1.102 +}
   1.103 +
   1.104 +bool
   1.105 +WindowHook::Notify(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam,
   1.106 +                   MSGResult& aResult)
   1.107 +{
   1.108 +  MessageData *data = Lookup(nMsg);
   1.109 +  if (!data)
   1.110 +    return false;
   1.111 +
   1.112 +  uint32_t length = data->monitors.Length();
   1.113 +  for (uint32_t midx = 0; midx < length; midx++) {
   1.114 +    data->monitors[midx].Invoke(hWnd, nMsg, wParam, lParam, &aResult.mResult);
   1.115 +  }
   1.116 +
   1.117 +  aResult.mConsumed =
   1.118 +    data->hook.Invoke(hWnd, nMsg, wParam, lParam, &aResult.mResult);
   1.119 +  return aResult.mConsumed;
   1.120 +}
   1.121 +
   1.122 +bool
   1.123 +WindowHook::CallbackData::Invoke(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam,
   1.124 +                                 LRESULT *aResult) {
   1.125 +  if (!cb)
   1.126 +    return false;
   1.127 +  return cb(context, hWnd, msg, wParam, lParam, aResult);
   1.128 +}
   1.129 +} // namespace widget
   1.130 +} // namespace mozilla
   1.131 +

mercurial