atom_api_app_mas.mm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/api/atom_api_app.h"
  5. #import <Cocoa/Cocoa.h>
  6. #include "base/strings/sys_string_conversions.h"
  7. namespace atom {
  8. namespace api {
  9. // Callback passed to js which will stop accessing the given bookmark.
  10. void OnStopAccessingSecurityScopedResource(NSURL* bookmarkUrl) {
  11. [bookmarkUrl stopAccessingSecurityScopedResource];
  12. [bookmarkUrl release];
  13. }
  14. // Get base64 encoded NSData, create a bookmark for it and start accessing it.
  15. base::Callback<void()> App::StartAccessingSecurityScopedResource(
  16. mate::Arguments* args) {
  17. std::string data;
  18. args->GetNext(&data);
  19. NSString* base64str = base::SysUTF8ToNSString(data);
  20. NSData* bookmarkData =
  21. [[NSData alloc] initWithBase64EncodedString:base64str options:0];
  22. // Create bookmarkUrl from NSData.
  23. BOOL isStale = false;
  24. NSError* error = nil;
  25. NSURL* bookmarkUrl =
  26. [NSURL URLByResolvingBookmarkData:bookmarkData
  27. options:NSURLBookmarkResolutionWithSecurityScope
  28. relativeToURL:nil
  29. bookmarkDataIsStale:&isStale
  30. error:&error];
  31. if (error != nil) {
  32. NSString* err =
  33. [NSString stringWithFormat:@"NSError: %@ %@", error, [error userInfo]];
  34. args->ThrowError(base::SysNSStringToUTF8(err));
  35. }
  36. if (isStale) {
  37. args->ThrowError("bookmarkDataIsStale - try recreating the bookmark");
  38. }
  39. if (error == nil && isStale == false) {
  40. [bookmarkUrl startAccessingSecurityScopedResource];
  41. }
  42. // Stop the NSURL from being GC'd.
  43. [bookmarkUrl retain];
  44. // Return a js callback which will close the bookmark.
  45. return base::Bind(&OnStopAccessingSecurityScopedResource, bookmarkUrl);
  46. }
  47. } // namespace atom
  48. } // namespace api