-
Notifications
You must be signed in to change notification settings - Fork 1
/
usbtoken_win.cpp
1443 lines (1137 loc) · 41.9 KB
/
usbtoken_win.cpp
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************** Module Header ******************************\
* Module Name: SampleService.cpp
* Project: CppWindowsService
* Copyright (c) Microsoft Corporation.
*
* Provides a sample service class that derives from the service base class -
* CServiceBase. The sample service logs the service start and stop
* information to the Application event log, and shows how to run the main
* function of the service in a thread pool worker thread.
*
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/en-us/openness/resources/licenses.aspx#MPL.
* All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
#include "usbtoken.h"
#include <windows.h>
#include <process.h>
#include <commdlg.h>
#include <shellapi.h>
#include <commctrl.h>
#include <strsafe.h>
#include <direct.h>
#include <shlwapi.h>
#include <shlobj.h>
#include <winbase.h>
#include <windowsx.h>
#include <sstream>
#include <string>
#include "include/cef_app.h"
#include "include/cef_browser.h"
#include "include/cef_frame.h"
#include "include/cef_runnable.h"
#include "client_app.h"
#include "client_handler.h"
#include "string_util.h"
#include "inc/watcher.h"
#include "ep_pkcs11_scheme.h"
#pragma endregion
#define PREF_EPSILON_BASE L"Epsilon SARL"
#define APP_NAME L"Epsilon Token Manager"
#define PACKVERSION(major, minor) MAKELONG(minor, major)
#define MAX_LOADSTRING 100
// Global Variables
DWORD g_appStartupTime;
HINSTANCE g_hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
char szWorkingDir[MAX_PATH]; // The current working directory
#if NTDDI_VERSION >= NTDDI_VISTA
SC_HANDLE g_scm;
SC_HANDLE g_service;
#endif
HANDLE m_SvcWatcherHnd;
// The global ClientHandler reference.
extern CefRefPtr<ClientHandler> g_handler;
//// Registry access functions
//
//// Registry key strings
//#define PREF_APPSHELL_BASE L"Software"
//#define PREF_WINPOS_FOLDER L"Window Position"
//#define PREF_LEFT L"Left"
//#define PREF_TOP L"Top"
//#define PREF_WIDTH L"Width"
//#define PREF_HEIGHT L"Height"
//#define PREF_RESTORE_LEFT L"Restore Left"
//#define PREF_RESTORE_TOP L"Restore Top"
//#define PREF_RESTORE_RIGHT L"Restore Right"
//#define PREF_RESTORE_BOTTOM L"Restore Bottom"
//#define PREF_SHOWSTATE L"Show State"
//
//// Window state functions
//void SaveWindowRect(HWND hWnd);
//void RestoreWindowRect(int& left, int& top, int& width, int& height, int& showCmd);
//void RestoreWindowPlacement(HWND hWnd, int showCmd);// Registry access functions
void EnsureTrailingSeparator(LPWSTR pRet);
void GetKey(LPCWSTR pBase, LPCWSTR pGroup, LPCWSTR pApp, LPCWSTR pFolder, LPWSTR pRet);
bool GetRegistryInt(LPCWSTR pFolder, LPCWSTR pEntry, int* pDefault, int& ret);
bool WriteRegistryInt (LPCWSTR pFolder, LPCWSTR pEntry, int val);
//
//// Registry key strings
//#define PREF_APPSHELL_BASE L"Software"
//#define PREF_WINPOS_FOLDER L"Window Position"
//#define PREF_LEFT L"Left"
//#define PREF_TOP L"Top"
//#define PREF_WIDTH L"Width"
//#define PREF_HEIGHT L"Height"
//#define PREF_RESTORE_LEFT L"Restore Left"
//#define PREF_RESTORE_TOP L"Restore Top"
//#define PREF_RESTORE_RIGHT L"Restore Right"
//#define PREF_RESTORE_BOTTOM L"Restore Bottom"
//#define PREF_SHOWSTATE L"Show State"
//
//// Window state functions
//void SaveWindowRect(HWND hWnd);
//void RestoreWindowRect(int& left, int& top, int& width, int& height, int& showCmd);
//void RestoreWindowPlacement(HWND hWnd, int showCmd);
UINT const WMAPP_NOTIFYCALLBACK = WM_APP + 1;
UINT const IDM_SERVICE_STOPPED = WM_APP + 2;
UINT const NOTIFICATION_ICON_ID = WM_APP + 3;
UINT_PTR const HIDEBROWSER_TIMER_ID = 1;
#if NTDDI_VERSION > NTDDI_VISTA
// Use a guid to uniquely identify our icon
class __declspec(uuid("{3886E88A-87F9-4623-BCBD-AEB3410F1028}")) NotificationIcon;
#endif
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void ShowContextMenu(int, HWND, POINT);
BOOL AddNotificationIcon(HWND const &);
// BOOL ShowStartNotification();
BOOL DeleteNotificationIcon(HWND const &);
// BOOL ShowTokenRemovedBalloon();
// BOOL ShowPINBlockedBalloon();
// BOOL ShowTokenInsertedBalloon();
// BOOL RestoreTooltip();
BOOL DoStopSvc(SC_HANDLE);
// Formats a message string using the specified message and variable
// list of arguments.
LPWSTR GetLastErrorMessage();
#if NTDDI_VERSION >= NTDDI_VISTA
void CALLBACK ServiceNotifyCallback(void*);
#endif
#if NTDDI_VERSION < NTDDI_VISTA
HANDLE CreatePipe();
#endif
unsigned __stdcall WatchSvc(void*);
// BOOL MyPostQuitMessage(UINT message = 0x0);
void HideWnd(HWND);
void ShowWnd(HWND);
#if defined(OS_WIN)
// Add Common Controls to the application manifest because it's required to
// support the default tooltip implementation.
#pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") // NOLINT(whitespace/line_length)
#pragma comment(lib, "comctl32.lib")
#endif
int APIENTRY wWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
CefMainArgs main_args(hInstance);
CefRefPtr<ClientApp> app(new ClientApp);
// Execute the secondary process, if any.
int exit_code = CefExecuteProcess(main_args, app.get());
if (exit_code >= 0)
return exit_code;
// Retrieve the current working directory.
if (_getcwd(szWorkingDir, MAX_PATH) == NULL)
szWorkingDir[0] = 0;
// Parse command line arguments. The passed in values are ignored on Windows.
AppInitCommandLine(0, NULL);
CefSettings settings;
// Populate the settings based on command line arguments.
AppGetSettings(settings, app);
settings.multi_threaded_message_loop = true;
// Initialize CEF.
CefInitialize(main_args, settings, app.get());
// Register the scheme handler.
epsilon::schemes::InitPKCS11Scheme();
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_USBTOKEN, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization
if (!InitInstance(hInstance, SW_SHOWDEFAULT))
return FALSE;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_USBTOKEN));
int result = 0;
if (!settings.multi_threaded_message_loop) {
// Run the CEF message loop. This function will block until the application
// recieves a WM_QUIT message.
CefRunMessageLoop();
} else {
MSG msg;
// Run the application message loop.
while (GetMessage(&msg, NULL, 0, 0)) {
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
result = static_cast<int>(msg.wParam);
}
// Shut down CEF.
CefShutdown();
return result;
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
// UNREFERENCED_PARAMETER(nCmdShow);
HWND hWnd;
g_hInst = hInstance; // Store instance handle in our global variable
RECT lpRect = {600, 100, 425, 415};
AdjustWindowRect(&lpRect, (WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN) & ~WS_MAXIMIZEBOX, FALSE);
hWnd = CreateWindow(szWindowClass, szTitle,
(WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN) & ~WS_MAXIMIZEBOX,
lpRect.left, lpRect.top, lpRect.right, lpRect.bottom,
NULL, NULL, g_hInst, NULL);
//DWORD const dwStyle = WS_POPUP | WS_THICKFRAME | WS_OVERLAPPEDWINDOW | WS_MAXIMIZEBOX | WS_SYSMENU;
//// adjust the window size to take the frame into account
//AdjustWindowRectEx(&lpRect, dwStyle, FALSE, WS_EX_TOOLWINDOW);
//hWnd = CreateWindowEx(WS_EX_TOOLWINDOW, szWindowClass, szTitle, dwStyle,
// lpRect.left, lpRect.top, lpRect.right, lpRect.bottom, NULL, NULL, g_hInst, NULL);
if(!hWnd)
return FALSE;
//ShowWindow(hWnd, nCmdShow);
//UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_USBTOKEN));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL; //MAKEINTRESOURCE(IDC_USBTOKEN);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
DWORD GetAssemblyVersion(LPCTSTR lpszDllName)
{
HINSTANCE hinstDll;
static DWORD dwVersion = 0;
if(dwVersion != 0)
return dwVersion; //been here
/* For security purposes, LoadLibrary should be provided with a fully qualified
path to the DLL. The lpszDllName variable should be tested to ensure that it
is a fully qualified path before it is used. */
hinstDll = LoadLibrary(lpszDllName);
if(hinstDll)
{
DLLGETVERSIONPROC pDllGetVersion;
pDllGetVersion = (DLLGETVERSIONPROC)GetProcAddress(hinstDll, "DllGetVersion");
/* Because some DLLs might not implement this function, you must test for
it explicitly. Depending on the particular DLL, the lack of a DllGetVersion
function can be a useful indicator of the version. */
if(pDllGetVersion)
{
DLLVERSIONINFO2 dvi;
HRESULT hr;
ZeroMemory(&dvi, sizeof(dvi));
dvi.info1.cbSize = sizeof(dvi);
hr = (*pDllGetVersion)(&dvi.info1);
if(SUCCEEDED(hr))
dwVersion = PACKVERSION(dvi.info1.dwMajorVersion, dvi.info1.dwMinorVersion);
}
FreeLibrary(hinstDll);
}
return dwVersion;
}
BOOL AddNotificationIcon(HWND const &hHnd)
{
NOTIFYICONDATA nid = {0};
nid.hWnd = hHnd;
DWORD ullVersion = GetAssemblyVersion(L"shell32.dll");
if(ullVersion > MAKEDLLVERULL(6, 0, 0, 0))
nid.cbSize = sizeof (NOTIFYICONDATA);
#if NTDDI_VERSION >= NTDDI_VISTA
else if(ullVersion == MAKEDLLVERULL(6, 0, 0, 0))
nid.cbSize = sizeof(NOTIFYICONDATA_V3_SIZE);
#endif
else if (ullVersion >= MAKEDLLVERULL(5, 0, 0, 0))
nid.cbSize = NOTIFYICONDATA_V2_SIZE;
else
nid.cbSize = NOTIFYICONDATA_V1_SIZE;
#if NTDDI_VERSION > NTDDI_VISTA
nid.guidItem = __uuidof(NotificationIcon);
#else
nid.uID = NOTIFICATION_ICON_ID;
#endif
// add the icon, setting the icon, tooltip, and callback message.
// the icon will be identified with the GUID
#if NTDDI_VERSION >= NTDDI_VISTA
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_SHOWTIP | NIF_TIP;
#else
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
#endif
nid.uCallbackMessage = WMAPP_NOTIFYCALLBACK;
nid.dwState = 0;
nid.dwStateMask = NIS_HIDDEN;
#if NTDDI_VERSION >= NTDDI_VISTA
LoadIconMetric(g_hInst, MAKEINTRESOURCE(IDI_SMALL), LIM_SMALL, &nid.hIcon);
#else
nid.hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_SMALL), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
#endif
LoadString(g_hInst, IDS_TOOLTIP, nid.szTip, ARRAYSIZE(nid.szTip));
Shell_NotifyIcon(NIM_ADD, &nid);
// NOTIFYICON_VERSION_4 is prefered
#if NTDDI_VERSION >= NTDDI_VISTA
nid.uVersion = NOTIFYICON_VERSION_4;
#else
nid.uVersion = NOTIFYICON_VERSION;
#endif
return Shell_NotifyIcon(NIM_SETVERSION, &nid);
}
void ShowWnd(HWND hwnd)
{
SetForegroundWindow(hwnd);
ShowWindow(hwnd, SW_SHOW);
//UpdateWindow(hwnd);
}
void HideWnd(HWND hwnd)
{
ShowWindow(hwnd, SW_HIDE);
// immediately after hiding the flyout we don't want to allow showing it again, which will allow clicking
// on the icon to hide the flyout. If we didn't have this code, clicking on the icon when the flyout is open
// would cause the focus change (from flyout to the taskbar), which would trigger hiding the flyout
// (see the WM_ACTIVATE handler). Since the flyout would then be hidden on click, it would be shown again instead
// of hiding.
SetTimer(hwnd, HIDEBROWSER_TIMER_ID, GetDoubleClickTime(), NULL);
}
void ShowContextMenu(int type, HWND hwnd, POINT pt)
{
HMENU hMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(type));
if (hMenu)
{
HMENU hSubMenu = GetSubMenu(hMenu, 0);
if (hSubMenu)
{
// our window must be foreground before calling TrackPopupMenu or the menu will not disappear when the user clicks away
SetForegroundWindow(hwnd);
// respect menu drop alignment
UINT uFlags = TPM_RIGHTBUTTON;
if (GetSystemMetrics(SM_MENUDROPALIGNMENT) != 0)
{
uFlags |= TPM_RIGHTALIGN;
}
else
{
uFlags |= TPM_LEFTALIGN;
}
TrackPopupMenuEx(hSubMenu, uFlags, pt.x, pt.y, hwnd, NULL);
}
DestroyMenu(hMenu);
}
}
#if NTDDI_VERSION < NTDDI_VISTA
HANDLE CreatePipe()
{
union maxSize
{
UINT _1;
};
return ::CreateNamedPipe(L"\\\\.\\pipe\\epTokend",
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES, sizeof maxSize,
sizeof maxSize, NMPWAIT_USE_DEFAULT_WAIT, NULL);
}
#endif
#if NTDDI_VERSION >= NTDDI_VISTA
void CALLBACK ServiceNotifyCallback(void* param)
{
SERVICE_NOTIFY* sn = static_cast<SERVICE_NOTIFY*>(param);
// Check the notification results
if(sn->dwNotificationStatus == ERROR_SUCCESS && (sn->dwNotificationTriggered & SERVICE_NOTIFY_STOP_PENDING))
::PostMessage(g_handler->GetMainHwnd(), WM_COMMAND, IDM_EXIT, IDM_SERVICE_STOPPED);
}
#endif
unsigned __stdcall WatchSvc(void *lpParam) {
#if NTDDI_VERSION >= NTDDI_VISTA
g_scm = ::OpenSCManager(
NULL, // local SCM
SERVICES_ACTIVE_DATABASE,
SC_MANAGER_ENUMERATE_SERVICE);
g_service = ::OpenService(
g_scm,
L"epTokend",
SERVICE_STOP | SERVICE_QUERY_STATUS);
SERVICE_NOTIFY serviceNotify =
{ SERVICE_NOTIFY_STATUS_CHANGE,
ServiceNotifyCallback };
::NotifyServiceStatusChange(
g_service,
SERVICE_NOTIFY_STOP_PENDING,
&serviceNotify);
::SleepEx(INFINITE, TRUE); // Wait for the notification
#else
HANDLE hPipe = CreatePipe();
if (NULL == hPipe || hPipe == INVALID_HANDLE_VALUE)
goto terminated;
if(::ConnectNamedPipe(hPipe, NULL) || ::GetLastError() == ERROR_PIPE_CONNECTED)
{
BYTE buffer[sizeof UINT] = {0};
UINT uMessage = 0;
while (true)
{
DWORD lpNumberOfBytesRead = 0;
if (::ReadFile(hPipe, &buffer, sizeof UINT, &lpNumberOfBytesRead, 0) && sizeof UINT == lpNumberOfBytesRead)
{
uMessage = *((UINT*)&buffer[0]);
// The processing of the received data
if(uMessage == 0x0) {
::PostMessage(g_handler->GetMainHwnd(), WM_COMMAND, IDM_EXIT, IDM_SERVICE_STOPPED);
break; //Terminated from the service
}
}
else
{
}
::Sleep(1000); // Simulate some lengthy operations.
}
::DisconnectNamedPipe(hPipe);
}
else
{
}
terminated :
#endif
_endthreadex(0);
return 0;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static BOOL s_fCanShowWnd = TRUE;
switch (message)
{
case WM_CREATE: {
// add the notification icon
AddNotificationIcon(hWnd);
// ShowStartNotification();
// Create the single static handler class instance
g_handler = new ClientHandler();
g_handler->SetMainHwnd(hWnd);
// Create the child windows used for navigation
RECT rect;
GetClientRect(hWnd, &rect);
CefWindowInfo info;
CefBrowserSettings settings;
// Populate the settings based on command line arguments.
AppGetBrowserSettings(settings);
//settings.file_access_from_file_urls_allowed = true;
//settings.universal_access_from_file_urls_allowed = true;
// settings.accelerated_2d_canvas_disabled = false;
// settings.accelerated_compositing_disabled = false;
// settings.accelerated_filters_enabled = true;
//settings.accelerated_layers_disabled = false;
// settings.accelerated_painting_enabled = true;
//settings.accelerated_plugins_disabled = true;
//settings.accelerated_video_disabled = true;
//settings.application_cache_disabled = false;
// settings.databases_disabled = true;
//settings.developer_tools_disabled = true;
// settings.plugins_disabled = true;
// settings.fullscreen_enabled = false;
// settings.javascript_open_windows_disallowed = true;
// settings.plugins_disabled = true;
// settings.webgl_disabled = false;
// settings.java_disabled = true;
// Initialize window info to the defaults for a child window
info.SetAsChild(hWnd, rect);
// Creat the new child browser window
CefBrowserHost::CreateBrowser(info, g_handler.get(),
g_handler->GetStartupURL(), settings);
g_handler->app_running_ = true;
epsilon::TokenWatcher::Init(g_handler);
m_SvcWatcherHnd = (HANDLE)_beginthreadex(NULL, 0, &WatchSvc, NULL, 0, NULL);
return 0;
}
case WM_COMMAND: {
CefRefPtr<CefBrowser> browser;
if (g_handler.get())
browser = g_handler->GetBrowser();
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId) {
// case IDM_TOKEN_INSERTED:
// ShowTokenInsertedBalloon();
// break;
// case IDM_TOKEN_REMOVED:
// ShowTokenRemovedBalloon();
// break;
// case IDM_PIN_BLOCKED:
// ShowPINBlockedBalloon();
// break;
// case IDM_SHOW_TOKEN_MGR: //options
// // placeholder for an options dialog
// MessageBox(hWnd, L"Display the options dialog here.", L"Options", MB_OK);
// break;
case IDM_BROWSER_SHOW:
if (!IsWindowVisible(hWnd))
ShowWnd(hWnd);
break;
case IDM_BROWSER_HIDE:
if (IsWindowVisible(hWnd))
HideWnd(hWnd);
break;
case IDM_EXIT:{
if (g_handler.get()) {
g_handler->app_running_ = false;
CefRefPtr<CefBrowser> browser = g_handler->GetBrowser();
if (browser.get()) {
// Let the browser window know we are about to destroy it.
browser->GetHost()->ParentWindowWillClose();
//HideWnd(hWnd);
//return 0;
}
}
//if (g_handler.get()) {
//g_handler->QuittingApp(true);
//g_handler->DispatchCloseToNextBrowser();
//} else {
if(m_SvcWatcherHnd) {
TerminateThread(m_SvcWatcherHnd, 0);
CloseHandle(m_SvcWatcherHnd);
m_SvcWatcherHnd = NULL;
}
if(lParam != IDM_SERVICE_STOPPED) {
try {
DoStopSvc(
#if NTDDI_VERSION >= NTDDI_VISTA
g_service
#else
NULL
#endif
);
} catch(...) {}
}
#if NTDDI_VERSION >= NTDDI_VISTA
if(g_service)
::CloseServiceHandle(g_service);
if(g_scm)
::CloseServiceHandle(g_scm);
#endif
DestroyWindow(hWnd);
DeleteNotificationIcon(hWnd);
PostQuitMessage(0);
//}
break;
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
//case ID_WARN_CONSOLEMESSAGE:
// if (g_handler.get()) {
// std::wstringstream ss;
// ss << L"Console messages will be written to "
// << std::wstring(CefString(g_handler->GetLogFile()));
// MessageBox(hWnd, ss.str().c_str(), L"Console Messages",
// MB_OK | MB_ICONINFORMATION);
// }
// return 0;
}
}
break;
case WMAPP_NOTIFYCALLBACK: {
switch (LOWORD(lParam))
{
case NIN_SELECT:
case NIN_KEYSELECT:
case WM_LBUTTONUP:
// for NOTIFYICON_VERSION_4 clients, NIN_SELECT is prerable to listening to mouse clicks and key presses
// directly.
if (IsWindowVisible(hWnd))
{
HideWnd(hWnd);
s_fCanShowWnd = FALSE;
}
else if (s_fCanShowWnd) {
ShowWnd(hWnd);
}
break;
case NIN_BALLOONTIMEOUT:
// RestoreTooltip();
break;
case NIN_BALLOONUSERCLICK:
// RestoreTooltip();
// placeholder for the user clicking on the balloon.
// MessageBox(hWnd, L"The user clicked on the balloon.", L"User click", MB_OK);
break;
case WM_CONTEXTMENU:
case WM_RBUTTONUP:
{
POINT pt;// = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
GetCursorPos(&pt);
if (IsWindowVisible(hWnd)) {
::SetForegroundWindow(hWnd);
ShowContextMenu(IDC_CONTEXTMENU_HIDE, hWnd, pt);
}
else
ShowContextMenu(IDC_CONTEXTMENU_SHOW, hWnd, pt);
}
break;
}
break;
}
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_SETFOCUS:
if (g_handler.get() && g_handler->GetBrowser()) {
// Pass focus to the browser window
CefWindowHandle hWnd =
g_handler->GetBrowser()->GetHost()->GetWindowHandle();
if (hWnd)
PostMessage(hWnd, WM_SETFOCUS, wParam, NULL);
}
return 0;
case WM_SIZE:
{
//if (wParam == SIZE_MINIMIZED && g_handler.get() &&
// g_handler->GetBrowser()) {
// CefWindowHandle hWnd =
// g_handler->GetBrowser()->GetHost()->GetWindowHandle();
// //if (hWnd)
// // HideWnd(hWnd);
//}
//// Minimizing resizes the window to 0x0 which causes our layout to go all
//// screwy, so we just ignore it.
//if (wParam != SIZE_MINIMIZED && g_handler.get() &&
// g_handler->GetBrowser()) {
// CefWindowHandle hwnd =
// g_handler->GetBrowser()->GetHost()->GetWindowHandle();
// if (hwnd) {
// //// Resize the browser window and address bar to match the new frame
// //// window size
// //RECT rect;
// //GetClientRect(hWnd, &rect);
// ////rect.top += URLBAR_HEIGHT;
// ////int urloffset = rect.left + BUTTON_WIDTH * 4;
// //HDWP hdwp = BeginDeferWindowPos(1);
// ////hdwp = DeferWindowPos(hdwp, editWnd, NULL, urloffset,
// // //0, rect.right - urloffset, URLBAR_HEIGHT, SWP_NOZORDER);
// //hdwp = DeferWindowPos(hdwp, hwnd, NULL,
// // rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
// // SWP_NOZORDER);
// //EndDeferWindowPos(hdwp);
// }
//}
}
break;
case WM_ERASEBKGND:
if (g_handler.get() && g_handler->GetBrowser()) {
CefWindowHandle hwnd =
g_handler->GetBrowser()->GetHost()->GetWindowHandle();
if (hwnd) {
// Dont erase the background if the browser window has been loaded
// (this avoids flashing)
return 0;
}
}
break;
case WM_CLOSE:
//if (g_handler.get()) {
//CefRefPtr<CefBrowser> browser = g_handler->GetBrowser();
//if (browser.get()) {
// Let the browser window know we are about to destroy it.
//browser->GetHost()->ParentWindowWillClose();
HideWnd(hWnd);
//}
//}
break;
case WM_TIMER:
switch(wParam) {
case IDM_BROWSER_SHOW: {
PostMessage(hWnd, WM_COMMAND, IDM_BROWSER_SHOW, NULL);
KillTimer(hWnd, IDM_BROWSER_SHOW);
}
break;
case HIDEBROWSER_TIMER_ID: {
// please see the comment in HideWnd() for an explanation of this code.
KillTimer(hWnd, HIDEBROWSER_TIMER_ID);
s_fCanShowWnd = TRUE;
}
break;
}
break;
case WM_DESTROY:
// TODO: happens when we call DestroyWindow
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
BOOL IsWinVistaOrLater()
{
// Initialize the OSVERSIONINFOEX structure.
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
osvi.dwMajorVersion = 6;
osvi.dwMinorVersion = 0;
// Initialize the condition mask.
DWORDLONG dwlConditionMask = 0;
VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
// Perform the test.
return VerifyVersionInfo(&osvi,
VER_MAJORVERSION | VER_MINORVERSION,
dwlConditionMask);
}
BOOL DeleteNotificationIcon(HWND const &hWnd)
{
NOTIFYICONDATA nid = {0};
nid.hWnd = hWnd;
DWORD ullVersion = GetAssemblyVersion(L"shell32.dll");
if(ullVersion > MAKEDLLVERULL(6, 0, 0, 0))
nid.cbSize = sizeof (NOTIFYICONDATA);
#if NTDDI_VERSION >= NTDDI_VISTA
else if(ullVersion == MAKEDLLVERULL(6, 0, 0, 0))
nid.cbSize = sizeof(NOTIFYICONDATA_V3_SIZE);
#endif
else if (ullVersion >= MAKEDLLVERULL(5, 0, 0, 0))
nid.cbSize = NOTIFYICONDATA_V2_SIZE;
else
nid.cbSize = NOTIFYICONDATA_V1_SIZE;
nid.uFlags = NIF_GUID;
#if NTDDI_VERSION > NTDDI_VISTA
nid.guidItem = __uuidof(NotificationIcon);
#else
nid.uID = NOTIFICATION_ICON_ID;
#endif
return Shell_NotifyIcon(NIM_DELETE, &nid);
}
// BOOL ShowTokenRemovedBalloon()
// {
// // Display a low ink balloon message. This is a warning, so show the appropriate system icon.
// NOTIFYICONDATA nid = {0};
// DWORD ullVersion = GetAssemblyVersion(L"shell32.dll");
// if(ullVersion > MAKEDLLVERULL(6, 0, 0, 0))
// nid.cbSize = sizeof (NOTIFYICONDATA);
// else if(ullVersion == MAKEDLLVERULL(6, 0, 0, 0))
// nid.cbSize = sizeof(NOTIFYICONDATA_V3_SIZE);
// else if (ullVersion >= MAKEDLLVERULL(5, 0, 0, 0))
// nid.cbSize = NOTIFYICONDATA_V2_SIZE;
// else
// nid.cbSize = NOTIFYICONDATA_V1_SIZE;
// nid.uFlags = NIF_INFO | NIF_GUID;
// #if NTDDI_VERSION > NTDDI_VISTA
// nid.guidItem = __uuidof(NotificationIcon);
//#else
// nid.uID = NOTIFICATION_ICON_ID;