2018-02-12 18:25:06 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/api/atom_api_app.h"
|
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
#include "base/strings/sys_string_conversions.h"
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
// Callback passed to js which will stop accessing the given bookmark.
|
|
|
|
void OnStopAccessingSecurityScopedResource(NSURL* bookmarkUrl) {
|
|
|
|
[bookmarkUrl stopAccessingSecurityScopedResource];
|
|
|
|
[bookmarkUrl release];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get base64 encoded NSData, create a bookmark for it and start accessing it.
|
2019-04-24 18:29:59 +00:00
|
|
|
base::RepeatingCallback<void()> App::StartAccessingSecurityScopedResource(
|
2018-04-20 18:47:04 +00:00
|
|
|
mate::Arguments* args) {
|
2018-02-12 18:25:06 +00:00
|
|
|
std::string data;
|
|
|
|
args->GetNext(&data);
|
2018-04-20 18:47:04 +00:00
|
|
|
NSString* base64str = base::SysUTF8ToNSString(data);
|
2019-05-01 19:27:55 +00:00
|
|
|
NSData* bookmarkData = [[NSData alloc] initWithBase64EncodedString:base64str
|
|
|
|
options:0];
|
2018-02-12 18:25:06 +00:00
|
|
|
|
|
|
|
// Create bookmarkUrl from NSData.
|
|
|
|
BOOL isStale = false;
|
2018-04-20 18:47:04 +00:00
|
|
|
NSError* error = nil;
|
|
|
|
NSURL* bookmarkUrl =
|
|
|
|
[NSURL URLByResolvingBookmarkData:bookmarkData
|
|
|
|
options:NSURLBookmarkResolutionWithSecurityScope
|
|
|
|
relativeToURL:nil
|
|
|
|
bookmarkDataIsStale:&isStale
|
|
|
|
error:&error];
|
2018-02-12 18:25:06 +00:00
|
|
|
|
|
|
|
if (error != nil) {
|
2018-04-20 18:47:04 +00:00
|
|
|
NSString* err =
|
|
|
|
[NSString stringWithFormat:@"NSError: %@ %@", error, [error userInfo]];
|
2018-02-12 18:25:06 +00:00
|
|
|
args->ThrowError(base::SysNSStringToUTF8(err));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isStale) {
|
|
|
|
args->ThrowError("bookmarkDataIsStale - try recreating the bookmark");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error == nil && isStale == false) {
|
|
|
|
[bookmarkUrl startAccessingSecurityScopedResource];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the NSURL from being GC'd.
|
|
|
|
[bookmarkUrl retain];
|
|
|
|
|
|
|
|
// Return a js callback which will close the bookmark.
|
2019-04-24 18:29:59 +00:00
|
|
|
return base::BindRepeating(&OnStopAccessingSecurityScopedResource,
|
|
|
|
bookmarkUrl);
|
2018-02-12 18:25:06 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
} // namespace atom
|
2018-02-12 18:25:06 +00:00
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
} // namespace api
|