launcher_mac.mm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "platform/mac/launcher_mac.h"
  8. #include "core/crash_reports.h"
  9. #include "core/update_checker.h"
  10. #include "base/base_file_utilities.h"
  11. #include "base/platform/base_platform_file_utilities.h"
  12. #include "base/platform/mac/base_utilities_mac.h"
  13. #include <Cocoa/Cocoa.h>
  14. #include <CoreFoundation/CFURL.h>
  15. #include <sys/sysctl.h>
  16. namespace Platform {
  17. Launcher::Launcher(int argc, char *argv[])
  18. : Core::Launcher(argc, argv) {
  19. }
  20. void Launcher::initHook() {
  21. base::RegisterBundledResources(u"Telegram.rcc"_q);
  22. }
  23. bool Launcher::launchUpdater(UpdaterLaunch action) {
  24. if (cExeName().isEmpty()) {
  25. return false;
  26. }
  27. @autoreleasepool {
  28. #ifdef OS_MAC_STORE
  29. // In AppStore version we don't have Updater.
  30. // We just relaunch our app.
  31. if (action == UpdaterLaunch::JustRelaunch) {
  32. NSDictionary *conf = [NSDictionary dictionaryWithObject:[NSArray array] forKey:NSWorkspaceLaunchConfigurationArguments];
  33. [[NSWorkspace sharedWorkspace] launchApplicationAtURL:[NSURL fileURLWithPath:Q2NSString(cExeDir() + cExeName())] options:NSWorkspaceLaunchAsync | NSWorkspaceLaunchNewInstance configuration:conf error:0];
  34. return true;
  35. }
  36. #endif // OS_MAC_STORE
  37. NSString *path = @"", *args = @"";
  38. @try {
  39. path = [[NSBundle mainBundle] bundlePath];
  40. if (!path) {
  41. LOG(("Could not get bundle path!!"));
  42. return false;
  43. }
  44. path = [path stringByAppendingString:@"/Contents/Frameworks/Updater"];
  45. base::Platform::RemoveQuarantine(QFile::decodeName([path fileSystemRepresentation]));
  46. NSMutableArray *args = [[NSMutableArray alloc] initWithObjects:@"-workpath", Q2NSString(cWorkingDir()), @"-procid", nil];
  47. [args addObject:[NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]]];
  48. if (cRestartingToSettings()) [args addObject:@"-tosettings"];
  49. if (action == UpdaterLaunch::JustRelaunch) [args addObject:@"-noupdate"];
  50. if (cLaunchMode() == LaunchModeAutoStart) [args addObject:@"-autostart"];
  51. if (Logs::DebugEnabled()) [args addObject:@"-debug"];
  52. if (cStartInTray()) [args addObject:@"-startintray"];
  53. if (cDataFile() != u"data"_q) {
  54. [args addObject:@"-key"];
  55. [args addObject:Q2NSString(cDataFile())];
  56. }
  57. if (customWorkingDir()) {
  58. [args addObject:@"-workdir_custom"];
  59. }
  60. DEBUG_LOG(("Application Info: executing %1 %2").arg(NS2QString(path)).arg(NS2QString([args componentsJoinedByString:@" "])));
  61. Logs::closeMain();
  62. CrashReports::Finish();
  63. if (![NSTask launchedTaskWithLaunchPath:path arguments:args]) {
  64. DEBUG_LOG(("Task not launched while executing %1 %2").arg(NS2QString(path)).arg(NS2QString([args componentsJoinedByString:@" "])));
  65. return false;
  66. }
  67. }
  68. @catch (NSException *exception) {
  69. LOG(("Exception caught while executing %1 %2").arg(NS2QString(path)).arg(NS2QString(args)));
  70. return false;
  71. }
  72. @finally {
  73. }
  74. }
  75. return true;
  76. }
  77. } // namespace Platform