-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathHttpProxyWatcher.h
92 lines (72 loc) · 2.21 KB
/
HttpProxyWatcher.h
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
#pragma once
#include "HttpProxyWatcher.g.h"
#include <ppl.h>
using namespace concurrency;
namespace winrt::Telegram::Native::implementation
{
struct HttpProxyWatcher : HttpProxyWatcherT<HttpProxyWatcher>
{
static winrt::Telegram::Native::HttpProxyWatcher Current()
{
std::lock_guard const guard(s_criticalSection);
if (s_current == nullptr)
{
s_current = winrt::make_self<HttpProxyWatcher>();
}
return s_current.as<winrt::Telegram::Native::HttpProxyWatcher>();
}
HttpProxyWatcher();
void Close()
{
SetEvent(m_shutdownEvent);
m_thread.join();
CloseHandle(m_shutdownEvent);
}
hstring Server()
{
return m_server;
}
bool IsEnabled()
{
return m_isEnabled;
}
winrt::event_token Changed(Windows::Foundation::TypedEventHandler<
winrt::Telegram::Native::HttpProxyWatcher,
bool> const& value)
{
if (!m_changed)
{
m_shutdownEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
m_thread = std::thread(ThreadLoop, this);
}
return m_changed.add(value);
}
void Changed(winrt::event_token const& token)
{
m_changed.remove(token);
if (!m_changed)
{
SetEvent(m_shutdownEvent);
m_thread.join();
}
}
private:
static std::mutex s_criticalSection;
static winrt::com_ptr<HttpProxyWatcher> s_current;
static void ThreadLoop(HttpProxyWatcher* watcher);
void UpdateValues(HKEY internetSettings, bool notify);
std::thread m_thread;
HANDLE m_shutdownEvent;
hstring m_server;
bool m_isEnabled;
winrt::event<Windows::Foundation::TypedEventHandler<
winrt::Telegram::Native::HttpProxyWatcher,
bool>> m_changed;
};
}
namespace winrt::Telegram::Native::factory_implementation
{
struct HttpProxyWatcher : HttpProxyWatcherT<HttpProxyWatcher, implementation::HttpProxyWatcher>
{
};
}