platform_util_mac.mm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/common/platform_util.h"
  5. #import <Carbon/Carbon.h>
  6. #import <Cocoa/Cocoa.h>
  7. #import <ServiceManagement/ServiceManagement.h>
  8. #include "base/callback.h"
  9. #include "base/files/file_path.h"
  10. #include "base/files/file_util.h"
  11. #include "base/logging.h"
  12. #include "base/mac/foundation_util.h"
  13. #include "base/mac/mac_logging.h"
  14. #include "base/mac/scoped_aedesc.h"
  15. #include "base/strings/stringprintf.h"
  16. #include "base/strings/sys_string_conversions.h"
  17. #include "net/base/mac/url_conversions.h"
  18. #include "url/gurl.h"
  19. namespace {
  20. // This may be called from a global dispatch queue, the methods used here are
  21. // thread safe, including LSGetApplicationForURL (> 10.2) and
  22. // NSWorkspace#openURLs.
  23. std::string OpenURL(NSURL* ns_url, bool activate) {
  24. CFURLRef ref = LSCopyDefaultApplicationURLForURL(
  25. base::mac::NSToCFCast(ns_url), kLSRolesAll, nullptr);
  26. // If no application could be found, NULL is returned and outError
  27. // (if not NULL) is populated with kLSApplicationNotFoundErr.
  28. if (ref == NULL)
  29. return "No application in the Launch Services database matches the input "
  30. "criteria.";
  31. NSUInteger launchOptions = NSWorkspaceLaunchDefault;
  32. if (!activate)
  33. launchOptions |= NSWorkspaceLaunchWithoutActivation;
  34. bool opened = [[NSWorkspace sharedWorkspace] openURLs:@[ ns_url ]
  35. withAppBundleIdentifier:nil
  36. options:launchOptions
  37. additionalEventParamDescriptor:nil
  38. launchIdentifiers:nil];
  39. if (!opened)
  40. return "Failed to open URL";
  41. return "";
  42. }
  43. NSString* GetLoginHelperBundleIdentifier() {
  44. return [[[NSBundle mainBundle] bundleIdentifier]
  45. stringByAppendingString:@".loginhelper"];
  46. }
  47. } // namespace
  48. namespace platform_util {
  49. bool ShowItemInFolder(const base::FilePath& path) {
  50. // The API only takes absolute path.
  51. base::FilePath full_path =
  52. path.IsAbsolute() ? path : base::MakeAbsoluteFilePath(path);
  53. DCHECK([NSThread isMainThread]);
  54. NSString* path_string = base::SysUTF8ToNSString(full_path.value());
  55. if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string
  56. inFileViewerRootedAtPath:@""]) {
  57. LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value();
  58. return false;
  59. }
  60. return true;
  61. }
  62. bool OpenItem(const base::FilePath& full_path) {
  63. DCHECK([NSThread isMainThread]);
  64. NSString* path_string = base::SysUTF8ToNSString(full_path.value());
  65. if (!path_string)
  66. return false;
  67. NSURL* url = [NSURL fileURLWithPath:path_string];
  68. if (!url)
  69. return false;
  70. const NSWorkspaceLaunchOptions launch_options =
  71. NSWorkspaceLaunchAsync | NSWorkspaceLaunchWithErrorPresentation;
  72. return [[NSWorkspace sharedWorkspace] openURLs:@[ url ]
  73. withAppBundleIdentifier:nil
  74. options:launch_options
  75. additionalEventParamDescriptor:nil
  76. launchIdentifiers:NULL];
  77. }
  78. bool OpenExternal(const GURL& url, const OpenExternalOptions& options) {
  79. DCHECK([NSThread isMainThread]);
  80. NSURL* ns_url = net::NSURLWithGURL(url);
  81. if (ns_url)
  82. return OpenURL(ns_url, options.activate).empty();
  83. return false;
  84. }
  85. void OpenExternal(const GURL& url,
  86. const OpenExternalOptions& options,
  87. OpenExternalCallback callback) {
  88. NSURL* ns_url = net::NSURLWithGURL(url);
  89. if (!ns_url) {
  90. std::move(callback).Run("Invalid URL");
  91. return;
  92. }
  93. bool activate = options.activate;
  94. __block OpenExternalCallback c = std::move(callback);
  95. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
  96. ^{
  97. __block std::string error = OpenURL(ns_url, activate);
  98. dispatch_async(dispatch_get_main_queue(), ^{
  99. std::move(c).Run(error);
  100. });
  101. });
  102. }
  103. bool MoveItemToTrash(const base::FilePath& full_path) {
  104. NSString* path_string = base::SysUTF8ToNSString(full_path.value());
  105. BOOL status = [[NSFileManager defaultManager]
  106. trashItemAtURL:[NSURL fileURLWithPath:path_string]
  107. resultingItemURL:nil
  108. error:nil];
  109. if (!path_string || !status)
  110. LOG(WARNING) << "NSWorkspace failed to move file " << full_path.value()
  111. << " to trash";
  112. return status;
  113. }
  114. void Beep() {
  115. NSBeep();
  116. }
  117. bool GetLoginItemEnabled() {
  118. BOOL enabled = NO;
  119. #pragma clang diagnostic push
  120. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  121. // SMJobCopyDictionary does not work in sandbox (see rdar://13626319)
  122. CFArrayRef jobs = SMCopyAllJobDictionaries(kSMDomainUserLaunchd);
  123. #pragma clang diagnostic pop
  124. NSArray* jobs_ = CFBridgingRelease(jobs);
  125. NSString* identifier = GetLoginHelperBundleIdentifier();
  126. if (jobs_ && [jobs_ count] > 0) {
  127. for (NSDictionary* job in jobs_) {
  128. if ([identifier isEqualToString:[job objectForKey:@"Label"]]) {
  129. enabled = [[job objectForKey:@"OnDemand"] boolValue];
  130. break;
  131. }
  132. }
  133. }
  134. return enabled;
  135. }
  136. bool SetLoginItemEnabled(bool enabled) {
  137. NSString* identifier = GetLoginHelperBundleIdentifier();
  138. return SMLoginItemSetEnabled((__bridge CFStringRef)identifier, enabled);
  139. }
  140. } // namespace platform_util