-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandbox.js
executable file
·103 lines (87 loc) · 3.3 KB
/
sandbox.js
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
var environ = [[NSProcessInfo processInfo] environment],
in_sandbox= (nil != [environ objectForKey:@"APP_SANDBOX_CONTAINER_ID"])
if(in_sandbox){
print("We’re sandboxed: here be dragons")
}
AppSandbox = function(){ }
AppSandbox.prototype.authorize = function(path, callback){
log("AppSandbox.authorize("+path+")")
var success = false
if (in_sandbox) {
var url = [[[NSURL fileURLWithPath:path] URLByStandardizingPath] URLByResolvingSymlinksInPath],
allowedUrl = false
// Key for bookmark data:
var bd_key = this.key_for_url(url)
// this.clear_key(bd_key) // For debug only, this clears the key we're looking for :P
// Bookmark
var bookmark = this.get_data_for_key(bd_key)
if(!bookmark){
log("– No bookmark found, let's create one")
var target = this.file_picker(url)
bookmark = [target bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
includingResourceValuesForKeys:nil
relativeToURL:nil
error:{}]
// Store bookmark
this.set_data_for_key(bookmark,bd_key)
} else {
log("– Bookmark found")
}
log(" " + bookmark)
// Thanks to @joethephish for this pointer (pun totally intended)
var bookmarkDataIsStalePtr = MOPointer.alloc().init()
var allowedURL = [NSURL URLByResolvingBookmarkData:bookmark
options:NSURLBookmarkResolutionWithSecurityScope
relativeToURL:nil
bookmarkDataIsStale:bookmarkDataIsStalePtr
error:{}]
if(bookmarkDataIsStalePtr.value() != 0){
log("— Bookmark data is stale")
log(bookmarkDataIsStalePtr.value())
}
if(allowedURL) {
success = true
}
} else {
success = true
}
// [allowedUrl startAccessingSecurityScopedResource]
callback.call(this,success)
// [allowedUrl stopAccessingSecurityScopedResource]
}
AppSandbox.prototype.key_for_url = function(url){
return "bd_" + [url absoluteString]
}
AppSandbox.prototype.clear_key = function(key){
var def = [NSUserDefaults standardUserDefaults]
[def setObject:nil forKey:key]
}
AppSandbox.prototype.file_picker = function(url){
// Panel
var openPanel = [NSOpenPanel openPanel]
[openPanel setTitle:"Sketch Authorization"]
[openPanel setMessage:"Due to Apple's Sandboxing technology, Sketch needs your permission to write to this folder."];
[openPanel setPrompt:"Authorize"];
[openPanel setCanCreateDirectories:false]
[openPanel setCanChooseFiles:true]
[openPanel setCanChooseDirectories:true]
[openPanel setAllowsMultipleSelection:false]
[openPanel setShowsHiddenFiles:false]
[openPanel setExtensionHidden:false]
[openPanel setDirectoryURL:url]
var openPanelButtonPressed = [openPanel runModal]
if (openPanelButtonPressed == NSFileHandlingPanelOKButton) {
allowedUrl = [openPanel URL]
}
return allowedUrl
}
AppSandbox.prototype.get_data_for_key = function(key){
var def = [NSUserDefaults standardUserDefaults]
return [def objectForKey:key]
}
AppSandbox.prototype.set_data_for_key = function(data,key){
var defaults = [NSUserDefaults standardUserDefaults],
default_values = [NSMutableDictionary dictionary]
[default_values setObject:data forKey:key]
[defaults registerDefaults:default_values]
}