Repository navigation
Expand file tree
/
Copy pathprotocol_handler.cpp
More file actions
177 lines (146 loc) · 4.51 KB
/
Copy pathprotocol_handler.cpp
File metadata and controls
177 lines (146 loc) · 4.51 KB
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
#include "protocol_handler.h"
#include <atomic>
#include <deque>
#include <mutex>
#include <vector>
#include "protocol_handler_dispatch.h"
namespace nativeapi {
namespace {
constexpr size_t kMaxPendingUrls = 32;
std::atomic<bool> g_host_attached{false};
// RFC 3986: ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ), lower-cased.
std::optional<std::string> NormalizeScheme(const std::string& scheme) {
if (scheme.empty())
return std::nullopt;
std::string normalized;
normalized.reserve(scheme.size());
for (size_t i = 0; i < scheme.size(); ++i) {
char c = scheme[i];
if (c >= 'A' && c <= 'Z')
c = static_cast<char>(c - 'A' + 'a');
const bool alpha = c >= 'a' && c <= 'z';
const bool other = (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.';
if (!alpha && (i == 0 || !other))
return std::nullopt;
normalized.push_back(c);
}
return normalized;
}
} // namespace
class ProtocolHandler::Impl {
public:
explicit Impl(ProtocolHandler* owner) : owner_(owner) {}
void Deliver(const std::string& url, bool is_launch) {
if (url.empty())
return;
{
std::lock_guard<std::mutex> lock(mutex_);
if (is_launch && !initial_url_ && !received_any_) {
initial_url_ = url;
// Already answered "no launch URL": deliver it as an event as well,
// so it is not lost to a caller that read too early.
if (!initial_queried_)
return;
}
received_any_ = true;
if (!listening_) {
if (pending_.size() == kMaxPendingUrls)
pending_.pop_front();
pending_.push_back(url);
return;
}
}
owner_->EmitAsync<ProtocolUrlReceivedEvent>(url);
}
void StartListening() {
std::vector<std::string> pending;
{
std::lock_guard<std::mutex> lock(mutex_);
listening_ = true;
pending.assign(pending_.begin(), pending_.end());
pending_.clear();
}
for (auto& url : pending)
owner_->EmitAsync<ProtocolUrlReceivedEvent>(std::move(url));
}
void StopListening() {
std::lock_guard<std::mutex> lock(mutex_);
listening_ = false;
}
void SetInitialUrlIfUnset(std::optional<std::string> url) {
if (!url || url->empty())
return;
std::lock_guard<std::mutex> lock(mutex_);
if (!initial_url_ && !received_any_)
initial_url_ = std::move(url);
}
std::optional<std::string> GetInitialUrl() const {
std::lock_guard<std::mutex> lock(mutex_);
if (!initial_url_)
initial_queried_ = true;
return initial_url_;
}
private:
ProtocolHandler* owner_;
mutable std::mutex mutex_;
std::optional<std::string> initial_url_;
std::deque<std::string> pending_;
bool received_any_ = false;
bool listening_ = false;
mutable bool initial_queried_ = false;
};
ProtocolHandler& ProtocolHandler::GetInstance() {
static ProtocolHandler instance;
return instance;
}
bool ProtocolHandler::IsSupported() {
return detail::protocol_platform::IsSupported();
}
ProtocolHandler::ProtocolHandler() : pimpl_(std::make_unique<Impl>(this)) {
pimpl_->SetInitialUrlIfUnset(detail::protocol_platform::ReadLaunchUrl());
detail::protocol_platform::StartCapture();
}
ProtocolHandler::~ProtocolHandler() {
ShutdownEmitter();
}
bool ProtocolHandler::Register(const std::string& scheme) {
auto normalized = NormalizeScheme(scheme);
if (!normalized || !IsSupported())
return false;
try {
return detail::protocol_platform::Register(*normalized);
} catch (...) {
return false;
}
}
std::optional<std::string> ProtocolHandler::GetInitialUrl() const {
return pimpl_->GetInitialUrl();
}
void ProtocolHandler::StartEventListening() {
pimpl_->StartListening();
}
void ProtocolHandler::StopEventListening() {
pimpl_->StopListening();
}
namespace detail {
void ProtocolHandlerDispatch::Deliver(const std::string& url, bool is_launch) {
ProtocolHandler::GetInstance().pimpl_->Deliver(url, is_launch);
}
void ProtocolHandlerDispatch::MarkHostAttached() {
g_host_attached.store(true);
}
bool ProtocolHandlerDispatch::IsHostAttached() {
return g_host_attached.load();
}
} // namespace detail
} // namespace nativeapi
extern "C" void nativeapi_protocol_handler_host_attach(void) {
nativeapi::detail::ProtocolHandlerDispatch::MarkHostAttached();
nativeapi::ProtocolHandler::GetInstance();
}
extern "C" void nativeapi_protocol_handler_host_deliver(const char* url, int is_launch) {
if (!url)
return;
nativeapi::detail::ProtocolHandlerDispatch::MarkHostAttached();
nativeapi::detail::ProtocolHandlerDispatch::Deliver(url, is_launch != 0);
}