atom_api_app_mas.mm 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 "shell/browser/api/atom_api_app.h"
  5. #include <string>
  6. #import <Cocoa/Cocoa.h>
  7. #include "base/strings/sys_string_conversions.h"
  8. namespace electron {
  9. namespace api {
  10. // Callback passed to js which will stop accessing the given bookmark.
  11. void OnStopAccessingSecurityScopedResource(NSURL* bookmarkUrl) {
  12. [bookmarkUrl stopAccessingSecurityScopedResource];
  13. [bookmarkUrl release];
  14. }
  15. // Get base64 encoded NSData, create a bookmark for it and start accessing it.
  16. base::RepeatingCallback<void()> App::StartAccessingSecurityScopedResource(
  17. mate::Arguments* args) {
  18. std::string data;
  19. args->GetNext(&data);
  20. NSString* base64str = base::SysUTF8ToNSString(data);
  21. NSData* bookmarkData = [[NSData alloc] initWithBase64EncodedString:base64str
  22. options:0];
  23. // Create bookmarkUrl from NSData.
  24. BOOL isStale = false;
  25. NSError* error = nil;
  26. NSURL* bookmarkUrl =
  27. [NSURL URLByResolvingBookmarkData:bookmarkData
  28. options:NSURLBookmarkResolutionWithSecurityScope
  29. relativeToURL:nil
  30. bookmarkDataIsStale:&isStale
  31. error:&error];
  32. if (error != nil) {
  33. NSString* err =
  34. [NSString stringWithFormat:@"NSError: %@ %@", error, [error userInfo]];
  35. args->ThrowError(base::SysNSStringToUTF8(err));
  36. }
  37. if (isStale) {
  38. args->ThrowError("bookmarkDataIsStale - try recreating the bookmark");
  39. }
  40. if (error == nil && isStale == false) {
  41. [bookmarkUrl startAccessingSecurityScopedResource];
  42. }
  43. // Stop the NSURL from being GC'd.
  44. [bookmarkUrl retain];
  45. // Return a js callback which will close the bookmark.
  46. return base::BindRepeating(&OnStopAccessingSecurityScopedResource,
  47. bookmarkUrl);
  48. }
  49. } // namespace api
  50. } // namespace electron