summaryrefslogtreecommitdiff
path: root/zen/notify_removal.cpp
blob: f528e81bcf8748a93d18e0b88a06f0ca26f897d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0        *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved        *
// **************************************************************************

#include "notify_removal.h"
#include <set>
#include <algorithm>
#include <dbt.h>
#include "scope_guard.h"

using namespace zen;


class MessageProvider //administrates a single dummy window to receive messages
{
public:
    static MessageProvider& instance() //throw FileError
    {
        static MessageProvider inst;
        return inst;
    }

    struct Listener
    {
        virtual ~Listener() {}
        virtual void onMessage(UINT message, WPARAM wParam, LPARAM lParam) = 0; //throw()!
    };
    void   registerListener(Listener& l) { listener.insert(&l); }
    void unregisterListener(Listener& l) { listener.erase(&l); } //don't unregister objects with static lifetime

    HWND getWnd() const { return windowHandle; } //get handle in order to register additional notifications

private:
    MessageProvider();
    ~MessageProvider();
    MessageProvider           (const MessageProvider&) = delete;
    MessageProvider& operator=(const MessageProvider&) = delete;

    static const wchar_t dummyClassName[];

    static LRESULT CALLBACK topWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

    void processMessage(UINT message, WPARAM wParam, LPARAM lParam);

    const HINSTANCE hMainModule;
    HWND windowHandle;

    std::set<Listener*> listener;
};


const wchar_t MessageProvider::dummyClassName[] = L"E6AD5EB1-527B-4EEF-AC75-27883B233380"; //random name


LRESULT CALLBACK MessageProvider::topWndProc(HWND hwnd,     //handle to window
                                             UINT uMsg,     //message identifier
                                             WPARAM wParam, //first message parameter
                                             LPARAM lParam) //second message parameter
{
    if (auto pThis = reinterpret_cast<MessageProvider*>(::GetWindowLongPtr(hwnd, GWLP_USERDATA)))
        try
        {
            pThis->processMessage(uMsg, wParam, lParam); //not supposed to throw!
        }
        catch (...) { assert(false); }

    return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
}


MessageProvider::MessageProvider() :
    hMainModule(::GetModuleHandle(nullptr)), //get program's module handle
    windowHandle(nullptr)
{
    if (!hMainModule)
        throwFileError(_("Unable to register to receive system messages."), L"GetModuleHandle", getLastError());

    //register the main window class
    WNDCLASS wc = {};
    wc.lpfnWndProc   = topWndProc;
    wc.hInstance     = hMainModule;
    wc.lpszClassName = dummyClassName;

    if (::RegisterClass(&wc) == 0)
        throwFileError(_("Unable to register to receive system messages."), L"RegisterClass", getLastError());

    ScopeGuard guardConstructor = zen::makeGuard([&] { this->~MessageProvider(); });

    //create dummy-window
    windowHandle = ::CreateWindow(dummyClassName, //_In_opt_  LPCTSTR lpClassName,
                                  nullptr,        //_In_opt_  LPCTSTR lpWindowName,
                                  0,           //_In_      DWORD dwStyle,
                                  0, 0, 0, 0,  //_In_      int x, y, nWidth, nHeight,
                                  nullptr,     //_In_opt_  HWND hWndParent; we need a toplevel window to receive device arrival events, not a message-window (HWND_MESSAGE)!
                                  nullptr,     //_In_opt_  HMENU hMenu,
                                  hMainModule, //_In_opt_  HINSTANCE hInstance,
                                  nullptr);    //_In_opt_  LPVOID lpParam
    if (!windowHandle)
        throwFileError(_("Unable to register to receive system messages."), L"CreateWindow", getLastError());

    //store this-pointer for topWndProc() to use: do this AFTER CreateWindow() to avoid processing messages while this constructor is running!!!
    //unlike: http://blogs.msdn.com/b/oldnewthing/archive/2014/02/03/10496248.aspx
    ::SetLastError(ERROR_SUCCESS); //[!] required for proper error handling, see MSDN, SetWindowLongPtr
    if (::SetWindowLongPtr(windowHandle, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)) == 0 && ::GetLastError() != ERROR_SUCCESS)
        throwFileError(_("Unable to register to receive system messages."), L"SetWindowLongPtr", ::GetLastError());

    guardConstructor.dismiss();
}


MessageProvider::~MessageProvider()
{
    //clean-up in reverse order
    if (windowHandle)
        ::DestroyWindow(windowHandle);
    ::UnregisterClass(dummyClassName, //LPCTSTR lpClassName OR ATOM in low-order word!
                      hMainModule);   //HINSTANCE hInstance
}


void MessageProvider::processMessage(UINT message, WPARAM wParam, LPARAM lParam)
{
    for (Listener* ls : listener)
        ls->onMessage(message, wParam, lParam);
}

//####################################################################################################

class NotifyRequestDeviceRemoval::Pimpl : private MessageProvider::Listener
{
public:
    Pimpl(NotifyRequestDeviceRemoval& parent, HANDLE hDir) : //throw FileError
        parent_(parent)
    {
        MessageProvider::instance().registerListener(*this); //throw FileError

        ScopeGuard guardProvider = makeGuard([&] { MessageProvider::instance().unregisterListener(*this); });

        //register handles to receive notifications
        DEV_BROADCAST_HANDLE filter = {};
        filter.dbch_size = sizeof(filter);
        filter.dbch_devicetype = DBT_DEVTYP_HANDLE;
        filter.dbch_handle = hDir;

        hNotification = ::RegisterDeviceNotification(MessageProvider::instance().getWnd(), //__in  HANDLE hRecipient,
                                                     &filter,                              //__in  LPVOID NotificationFilter,
                                                     DEVICE_NOTIFY_WINDOW_HANDLE);         //__in  DWORD Flags
        if (!hNotification)
        {
            const DWORD lastError = ::GetLastError(); //copy before directly or indirectly making other system calls!
            if (lastError != ERROR_CALL_NOT_IMPLEMENTED   && //fail on SAMBA share: this shouldn't be a showstopper!
                lastError != ERROR_SERVICE_SPECIFIC_ERROR && //neither should be fail for "Pogoplug" mapped network drives
                lastError != ERROR_INVALID_DATA)             //this seems to happen for a NetDrive-mapped FTP server
                throwFileError(_("Unable to register to receive system messages."), L"RegisterDeviceNotification", lastError);
        }

        guardProvider.dismiss();
    }

    ~Pimpl()
    {
        if (hNotification)
            ::UnregisterDeviceNotification(hNotification);
        MessageProvider::instance().unregisterListener(*this);
    }

private:
    Pimpl           (const Pimpl&) = delete;
    Pimpl& operator=(const Pimpl&) = delete;

    void onMessage(UINT message, WPARAM wParam, LPARAM lParam) override //throw()!
    {
        //DBT_DEVICEQUERYREMOVE example: http://msdn.microsoft.com/en-us/library/aa363427(v=VS.85).aspx
        if (message == WM_DEVICECHANGE)
            if (wParam == DBT_DEVICEQUERYREMOVE       ||
                wParam == DBT_DEVICEQUERYREMOVEFAILED ||
                wParam == DBT_DEVICEREMOVECOMPLETE)
            {
                PDEV_BROADCAST_HDR header = reinterpret_cast<PDEV_BROADCAST_HDR>(lParam);
                if (header->dbch_devicetype == DBT_DEVTYP_HANDLE)
                {
                    PDEV_BROADCAST_HANDLE body = reinterpret_cast<PDEV_BROADCAST_HANDLE>(lParam);

                    if (body->dbch_hdevnotify == hNotification) //is it for the notification we registered?
                        switch (wParam)
                        {
                            case DBT_DEVICEQUERYREMOVE:
                                parent_.onRequestRemoval(body->dbch_handle);
                                break;
                            case DBT_DEVICEQUERYREMOVEFAILED:
                                parent_.onRemovalFinished(body->dbch_handle, false);
                                break;
                            case DBT_DEVICEREMOVECOMPLETE:
                                parent_.onRemovalFinished(body->dbch_handle, true);
                                break;
                        }
                }
            }
    }

    NotifyRequestDeviceRemoval& parent_;
    HDEVNOTIFY hNotification;
};

//####################################################################################################

NotifyRequestDeviceRemoval::NotifyRequestDeviceRemoval(HANDLE hDir) : pimpl(zen::make_unique<Pimpl>(*this, hDir)) {}


NotifyRequestDeviceRemoval::~NotifyRequestDeviceRemoval() {} //make sure ~unique_ptr() works with complete type
bgstack15