-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform_osx.mm
236 lines (206 loc) · 6.68 KB
/
platform_osx.mm
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
#include "platform_interface.hpp"
#include "browbeaterapplication.h"
#include <QApplication>
#include <QMessageBox>
#include <Cocoa/Cocoa.h>
#include <Carbon/Carbon.h>
#include <iostream>
#include <fstream>
#include <sstream>
void throwOSXStatus(OSStatus status)
{
switch(status)
{
case noErr: break;
#define X(STATUS) case STATUS: throw std::runtime_error(#STATUS);
X(kLSAppInTrashErr)
X(kLSUnknownErr)
X(kLSNotAnApplicationErr)
X(kLSNotInitializedErr)
X(kLSDataUnavailableErr)
X(kLSApplicationNotFoundErr)
X(kLSUnknownTypeErr)
X(kLSDataTooOldErr)
X(kLSDataErr)
X(kLSLaunchInProgressErr)
X(kLSNotRegisteredErr)
X(kLSAppDoesNotClaimTypeErr)
X(kLSAppDoesNotSupportSchemeWarning)
X(kLSServerCommunicationErr)
X(kLSCannotSetInfoErr)
X(kLSNoRegistrationInfoErr)
X(kLSIncompatibleSystemVersionErr)
X(kLSNoLaunchPermissionErr)
X(kLSNoExecutableErr)
X(kLSNoClassicEnvironmentErr)
X(kLSMultipleSessionsNotSupportedErr)
#undef X
default:
std::stringstream msg;
msg << "Unrecognized status:" << status;
throw std::runtime_error(msg.str());
}
}
class OsxBrowser : public Browser
{
private:
OsxBrowser()
: m_bundle_id( nil ),
m_app_url(nil)
{ }
public:
std::string get_name() const
{
return m_name;
}
void open_urls( std::vector< std::string const > const& urls ) const
{
std::cout << "opening urls..." << urls.size() << std::endl;
NSMutableArray* itemUrls = [NSMutableArray arrayWithCapacity:urls.size()];
if (!itemUrls)
throw std::runtime_error("wtf, itemUrls?");
for( auto const& url : urls )
{
std::cout << "opening url: " << url << std::endl;
NSString* nsStringUrl = [NSString stringWithUTF8String:url.c_str()];
if (!nsStringUrl)
throw std::runtime_error("wtf, eh?");
std::cout << "2. opening url:" << url << std::endl;
NSURL* itemUrl = [NSURL URLWithString:nsStringUrl];
if (!itemUrl)
throw std::runtime_error("wtf, url?");
[itemUrls addObject:itemUrl];
}
LSLaunchURLSpec args;
args.appURL = m_app_url;
args.itemURLs = (CFArrayRef)itemUrls;
args.passThruParams = nil;
args.launchFlags = 0;
args.asyncRefCon = nil;
std::cout << "args: " << std::endl;
std::cout << "args.appURL: " << args.appURL << std::endl;
CFURLRef launchedUrl = nil;
OSStatus status = LSOpenFromURLSpec( &args, &launchedUrl );
std::cout << "status" << status << std::endl;
}
class Builder
{
private:
std::shared_ptr< OsxBrowser > result;
public:
Builder()
: result( new OsxBrowser() )
{}
std::shared_ptr< OsxBrowser > build()
{
assert( result->m_name.length() );
assert( result->m_bundle_id );
assert( result->m_app_url );
return result;
}
Builder& set_name( std::string value )
{
result->m_name = value;
return *this;
}
Builder& set_bundle_id( CFStringRef value )
{
result->m_bundle_id = value;
return *this;
}
Builder& set_app_url( CFURLRef value )
{
result->m_app_url = value;
return *this;
}
};
friend class Builder;
static Builder builder() { return Builder(); }
private:
std::string m_name;
CFStringRef m_bundle_id;
CFURLRef m_app_url;
};
std::string url_decode(std::string const& input) {
std::string result;
std::string::size_type start = 0;
while( true ) {
std::string::size_type percent = input.find('%',start);
std::string substr = input.substr(start,percent);
// std::cout << "input:" << input << " start:" << start << " percent:" << percent << " substr:" << substr << std::endl;
result += substr;
if (percent != std::string::npos) {
std::string encoded = input.substr(percent + 1, 2);
int val = atoi(encoded.c_str());
result += (char)val;
start = percent + 3;
} else {
return result;
}
}
}
class OsxBrowserRegistrar : public BrowserRegistrar
{
public:
std::vector< std::shared_ptr< Browser > > listBrowsers()
{
std::vector< std::shared_ptr< Browser > > result;
CFArrayRef handlers = LSCopyAllHandlersForURLScheme(CFSTR("http"));
if (handlers)
{
for(CFIndex i=0; i<CFArrayGetCount(handlers); i++)
{
CFStringRef nsbrowser = (CFStringRef)CFArrayGetValueAtIndex(handlers, i);
std::string browser = std::string([(id)nsbrowser UTF8String]);
std::cout << "browser:" << browser << std::endl;
if ("com.whispersoft.browbeater" == browser) {
continue;
}
CFErrorRef error = NULL;
CFArrayRef nsurls = LSCopyApplicationURLsForBundleIdentifier( nsbrowser, &error );
if (nsurls)
{
for (CFIndex j=0; j<CFArrayGetCount(nsurls); ++j)
{
CFURLRef nsurl = (CFURLRef)CFArrayGetValueAtIndex(nsurls, j);
std::string name = std::string([[[[(id)nsurl absoluteString] lastPathComponent] stringByDeletingPathExtension] UTF8String]);
name = url_decode(name);
result.push_back(OsxBrowser::builder().set_name( name ).set_bundle_id( nsbrowser ).set_app_url(nsurl).build());
}
}
}
}
std::sort( result.begin(),result.end(), [](std::shared_ptr<Browser> pBrowserL, std::shared_ptr<Browser> pBrowserR) {
return pBrowserL->get_name() < pBrowserR->get_name();
});
for(auto pBrowser : result) {
std::cout << "sorted:" << pBrowser->get_name() << std::endl;
}
return result;
}
};
std::shared_ptr< BrowserRegistrar > getBrowserRegistrar()
{
return std::shared_ptr< BrowserRegistrar >(new OsxBrowserRegistrar());
}
void registerApplication(std::string const& path)
{
CFStringRef appID = ::CFBundleGetIdentifier(::CFBundleGetMainBundle());
if (appID != nil) {
std::cout << "appId:" << [(id)appID UTF8String] << std::endl;
}
std::cout << "Registering application:" << path << std::endl;
std::string pattern = ".app/Contents";
std::string::size_type appPos = path.find(pattern);
if (appPos == std::string::npos)
{
throw std::runtime_error("Cannot register non-app path:" + path);
}
std::string appPath = path.substr(0,appPos) + ".app";
std::cout << "Registering path:" << appPath << std::endl;
NSString* nspath = [NSString stringWithUTF8String:appPath.c_str()];
NSURL* appUrl = [NSURL fileURLWithPath:nspath];
OSStatus status = LSRegisterURL( (CFURLRef) appUrl, true );
throwOSXStatus(status);
std::cout << "Registered url:" << [[appUrl absoluteString] UTF8String] << " Status:" << status << std::endl;
}