| 
									
										
										
										
											2014-10-31 11:17:05 -07:00
										 |  |  | // Copyright (c) 2013 GitHub, Inc. | 
					
						
							| 
									
										
										
										
											2014-04-25 17:49:37 +08:00
										 |  |  | // Use of this source code is governed by the MIT license that can be | 
					
						
							| 
									
										
										
										
											2013-05-02 20:09:19 +08:00
										 |  |  | // found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-04 12:19:40 -08:00
										 |  |  | #import "shell/browser/mac/electron_application.h" | 
					
						
							| 
									
										
										
										
											2013-05-02 20:09:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 14:05:37 +02:00
										 |  |  | #include <memory> | 
					
						
							|  |  |  | #include <string> | 
					
						
							|  |  |  | #include <utility> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 20:09:19 +08:00
										 |  |  | #include "base/auto_reset.h" | 
					
						
							| 
									
										
										
										
											2019-01-10 21:50:45 +05:30
										 |  |  | #include "base/observer_list.h" | 
					
						
							| 
									
										
										
										
											2013-07-10 16:10:38 +08:00
										 |  |  | #include "base/strings/sys_string_conversions.h" | 
					
						
							| 
									
										
										
										
											2015-04-12 10:44:27 -07:00
										 |  |  | #include "content/public/browser/browser_accessibility_state.h" | 
					
						
							| 
									
										
										
										
											2019-01-10 21:50:45 +05:30
										 |  |  | #include "content/public/browser/native_event_processor_mac.h" | 
					
						
							|  |  |  | #include "content/public/browser/native_event_processor_observer_mac.h" | 
					
						
							| 
									
										
										
										
											2019-06-19 13:46:59 -07:00
										 |  |  | #include "shell/browser/browser.h" | 
					
						
							|  |  |  | #include "shell/browser/mac/dict_util.h" | 
					
						
							| 
									
										
										
										
											2020-02-04 12:19:40 -08:00
										 |  |  | #import "shell/browser/mac/electron_application_delegate.h" | 
					
						
							| 
									
										
										
										
											2015-04-12 10:44:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 16:12:34 +09:00
										 |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | inline void dispatch_sync_main(dispatch_block_t block) { | 
					
						
							|  |  |  |   if ([NSThread isMainThread]) | 
					
						
							|  |  |  |     block(); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     dispatch_sync(dispatch_get_main_queue(), block); | 
					
						
							| 
									
										
										
										
											2017-08-09 14:28:43 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 16:12:34 +09:00
										 |  |  | }  // namespace | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-21 11:05:03 -08:00
										 |  |  | @interface AtomApplication () <NativeEventProcessor> { | 
					
						
							| 
									
										
										
										
											2019-01-10 21:50:45 +05:30
										 |  |  |   base::ObserverList<content::NativeEventProcessorObserver>::Unchecked | 
					
						
							|  |  |  |       observers_; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | @end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-21 11:05:03 -08:00
										 |  |  | @implementation AtomApplication | 
					
						
							| 
									
										
										
										
											2013-05-02 20:09:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-21 11:05:03 -08:00
										 |  |  | + (AtomApplication*)sharedApplication { | 
					
						
							|  |  |  |   return (AtomApplication*)[super sharedApplication]; | 
					
						
							| 
									
										
										
										
											2013-05-30 16:03:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-15 18:58:28 -07:00
										 |  |  | - (void)willPowerOff:(NSNotification*)notify { | 
					
						
							|  |  |  |   userStoppedShutdown_ = shouldShutdown_ && !shouldShutdown_.Run(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 14:50:35 -08:00
										 |  |  | - (void)terminate:(id)sender { | 
					
						
							| 
									
										
										
										
											2020-06-15 18:58:28 -07:00
										 |  |  |   // User will call Quit later. | 
					
						
							|  |  |  |   if (userStoppedShutdown_) | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2018-02-05 16:25:50 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // We simply try to close the browser, which in turn will try to close the | 
					
						
							|  |  |  |   // windows. Termination can proceed if all windows are closed or window close | 
					
						
							|  |  |  |   // can be cancelled which will abort termination. | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  |   electron::Browser::Get()->Quit(); | 
					
						
							| 
									
										
										
										
											2018-01-11 14:50:35 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 16:13:35 +09:00
										 |  |  | - (void)setShutdownHandler:(base::Callback<bool()>)handler { | 
					
						
							|  |  |  |   shouldShutdown_ = std::move(handler); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 20:09:19 +08:00
										 |  |  | - (BOOL)isHandlingSendEvent { | 
					
						
							|  |  |  |   return handlingSendEvent_; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - (void)sendEvent:(NSEvent*)event { | 
					
						
							|  |  |  |   base::AutoReset<BOOL> scoper(&handlingSendEvent_, YES); | 
					
						
							| 
									
										
										
										
											2019-01-10 21:50:45 +05:30
										 |  |  |   content::ScopedNotifyNativeEventProcessorObserver scopedObserverNotifier( | 
					
						
							|  |  |  |       &observers_, event); | 
					
						
							| 
									
										
										
										
											2013-05-02 20:09:19 +08:00
										 |  |  |   [super sendEvent:event]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - (void)setHandlingSendEvent:(BOOL)handlingSendEvent { | 
					
						
							|  |  |  |   handlingSendEvent_ = handlingSendEvent; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-05 16:38:47 +09:00
										 |  |  | - (void)setCurrentActivity:(NSString*)type | 
					
						
							| 
									
										
										
										
											2016-05-23 11:49:46 -04:00
										 |  |  |               withUserInfo:(NSDictionary*)userInfo | 
					
						
							|  |  |  |             withWebpageURL:(NSURL*)webpageURL { | 
					
						
							| 
									
										
										
										
											2019-02-25 18:21:57 +01:00
										 |  |  |   currentActivity_ = base::scoped_nsobject<NSUserActivity>( | 
					
						
							|  |  |  |       [[NSUserActivity alloc] initWithActivityType:type]); | 
					
						
							|  |  |  |   [currentActivity_ setUserInfo:userInfo]; | 
					
						
							|  |  |  |   [currentActivity_ setWebpageURL:webpageURL]; | 
					
						
							|  |  |  |   [currentActivity_ setDelegate:self]; | 
					
						
							|  |  |  |   [currentActivity_ becomeCurrent]; | 
					
						
							|  |  |  |   [currentActivity_ setNeedsSave:YES]; | 
					
						
							| 
									
										
										
										
											2016-05-03 14:57:16 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - (NSUserActivity*)getCurrentActivity { | 
					
						
							| 
									
										
										
										
											2016-05-04 11:28:49 -07:00
										 |  |  |   return currentActivity_.get(); | 
					
						
							| 
									
										
										
										
											2016-05-03 14:57:16 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-26 16:14:44 -03:00
										 |  |  | - (void)invalidateCurrentActivity { | 
					
						
							| 
									
										
										
										
											2017-09-14 16:12:34 +09:00
										 |  |  |   if (currentActivity_) { | 
					
						
							|  |  |  |     [currentActivity_ invalidate]; | 
					
						
							| 
									
										
										
										
											2017-06-26 16:14:44 -03:00
										 |  |  |     currentActivity_.reset(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 22:16:03 -07:00
										 |  |  | - (void)resignCurrentActivity { | 
					
						
							|  |  |  |   if (@available(macOS 10.11, *)) { | 
					
						
							|  |  |  |     if (currentActivity_) | 
					
						
							|  |  |  |       [currentActivity_ resignCurrent]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 16:12:34 +09:00
										 |  |  | - (void)updateCurrentActivity:(NSString*)type | 
					
						
							| 
									
										
										
										
											2017-06-26 16:14:44 -03:00
										 |  |  |                  withUserInfo:(NSDictionary*)userInfo { | 
					
						
							| 
									
										
										
										
											2017-09-14 16:12:34 +09:00
										 |  |  |   if (currentActivity_) { | 
					
						
							|  |  |  |     [currentActivity_ addUserInfoEntriesFromDictionary:userInfo]; | 
					
						
							| 
									
										
										
										
											2017-06-26 16:14:44 -03:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-08-09 12:09:47 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   [handoffLock_ lock]; | 
					
						
							|  |  |  |   updateReceived_ = YES; | 
					
						
							|  |  |  |   [handoffLock_ signal]; | 
					
						
							|  |  |  |   [handoffLock_ unlock]; | 
					
						
							| 
									
										
										
										
											2017-06-26 16:14:44 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-25 18:21:57 +01:00
										 |  |  | - (void)userActivityWillSave:(NSUserActivity*)userActivity { | 
					
						
							| 
									
										
										
										
											2017-08-09 12:09:47 -03:00
										 |  |  |   __block BOOL shouldWait = NO; | 
					
						
							| 
									
										
										
										
											2017-08-09 14:28:43 -03:00
										 |  |  |   dispatch_sync_main(^{ | 
					
						
							| 
									
										
										
										
											2018-04-20 14:47:04 -04:00
										 |  |  |     std::string activity_type( | 
					
						
							|  |  |  |         base::SysNSStringToUTF8(userActivity.activityType)); | 
					
						
							| 
									
										
										
										
											2019-10-30 14:30:59 +09:00
										 |  |  |     base::DictionaryValue user_info = | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  |         electron::NSDictionaryToDictionaryValue(userActivity.userInfo); | 
					
						
							| 
									
										
										
										
											2017-06-26 16:14:44 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  |     electron::Browser* browser = electron::Browser::Get(); | 
					
						
							| 
									
										
										
										
											2018-04-20 14:47:04 -04:00
										 |  |  |     shouldWait = | 
					
						
							| 
									
										
										
										
											2019-10-30 14:30:59 +09:00
										 |  |  |         browser->UpdateUserActivityState(activity_type, std::move(user_info)) | 
					
						
							|  |  |  |             ? YES | 
					
						
							|  |  |  |             : NO; | 
					
						
							| 
									
										
										
										
											2017-08-07 16:28:00 -03:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-08-09 12:09:47 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (shouldWait) { | 
					
						
							|  |  |  |     [handoffLock_ lock]; | 
					
						
							|  |  |  |     updateReceived_ = NO; | 
					
						
							|  |  |  |     while (!updateReceived_) { | 
					
						
							| 
									
										
										
										
											2018-04-20 14:47:04 -04:00
										 |  |  |       BOOL isSignaled = | 
					
						
							|  |  |  |           [handoffLock_ waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]]; | 
					
						
							|  |  |  |       if (!isSignaled) | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2017-08-09 12:09:47 -03:00
										 |  |  |     } | 
					
						
							|  |  |  |     [handoffLock_ unlock]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-02 14:41:48 -03:00
										 |  |  |   [userActivity setNeedsSave:YES]; | 
					
						
							| 
									
										
										
										
											2017-06-26 16:14:44 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-25 18:21:57 +01:00
										 |  |  | - (void)userActivityWasContinued:(NSUserActivity*)userActivity { | 
					
						
							| 
									
										
										
										
											2017-08-07 16:28:00 -03:00
										 |  |  |   dispatch_async(dispatch_get_main_queue(), ^{ | 
					
						
							| 
									
										
										
										
											2018-04-20 14:47:04 -04:00
										 |  |  |     std::string activity_type( | 
					
						
							|  |  |  |         base::SysNSStringToUTF8(userActivity.activityType)); | 
					
						
							| 
									
										
										
										
											2019-10-30 14:30:59 +09:00
										 |  |  |     base::DictionaryValue user_info = | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  |         electron::NSDictionaryToDictionaryValue(userActivity.userInfo); | 
					
						
							| 
									
										
										
										
											2017-06-26 16:14:44 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  |     electron::Browser* browser = electron::Browser::Get(); | 
					
						
							| 
									
										
										
										
											2019-10-30 14:30:59 +09:00
										 |  |  |     browser->UserActivityWasContinued(activity_type, std::move(user_info)); | 
					
						
							| 
									
										
										
										
											2017-08-07 16:28:00 -03:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-06-26 16:14:44 -03:00
										 |  |  |   [userActivity setNeedsSave:YES]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-10 17:54:03 +02:00
										 |  |  | - (void)registerURLHandler { | 
					
						
							| 
									
										
										
										
											2013-07-10 16:10:38 +08:00
										 |  |  |   [[NSAppleEventManager sharedAppleEventManager] | 
					
						
							|  |  |  |       setEventHandler:self | 
					
						
							|  |  |  |           andSelector:@selector(handleURLEvent:withReplyEvent:) | 
					
						
							|  |  |  |         forEventClass:kInternetEventClass | 
					
						
							|  |  |  |            andEventID:kAEGetURL]; | 
					
						
							| 
									
										
										
										
											2017-08-09 12:09:47 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   handoffLock_ = [NSCondition new]; | 
					
						
							| 
									
										
										
										
											2013-07-10 16:10:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - (void)handleURLEvent:(NSAppleEventDescriptor*)event | 
					
						
							|  |  |  |         withReplyEvent:(NSAppleEventDescriptor*)replyEvent { | 
					
						
							| 
									
										
										
										
											2018-04-20 14:47:04 -04:00
										 |  |  |   NSString* url = | 
					
						
							|  |  |  |       [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  |   electron::Browser::Get()->OpenURL(base::SysNSStringToUTF8(url)); | 
					
						
							| 
									
										
										
										
											2013-07-10 16:10:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-20 14:47:04 -04:00
										 |  |  | - (void)accessibilitySetValue:(id)value forAttribute:(NSString*)attribute { | 
					
						
							| 
									
										
										
										
											2020-10-28 20:24:37 -07:00
										 |  |  |   // Undocumented attribute that screen reader related functionality | 
					
						
							|  |  |  |   // sets when running. | 
					
						
							|  |  |  |   if ([attribute isEqualToString:@"AXEnhancedUserInterface"] || | 
					
						
							|  |  |  |       [attribute isEqualToString:@"AXManualAccessibility"]) { | 
					
						
							|  |  |  |     auto* ax_state = content::BrowserAccessibilityState::GetInstance(); | 
					
						
							|  |  |  |     if ([value boolValue]) { | 
					
						
							|  |  |  |       ax_state->OnScreenReaderDetected(); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       ax_state->DisableAccessibility(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-04-12 10:44:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-28 20:24:37 -07:00
										 |  |  |     electron::Browser::Get()->OnAccessibilitySupportChanged(); | 
					
						
							| 
									
										
										
										
											2015-04-12 10:44:27 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-07-11 14:09:01 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-28 20:24:37 -07:00
										 |  |  |   return [super accessibilitySetValue:value forAttribute:attribute]; | 
					
						
							| 
									
										
										
										
											2015-04-12 10:44:27 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 13:30:58 -07:00
										 |  |  | - (void)orderFrontStandardAboutPanel:(id)sender { | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  |   electron::Browser::Get()->ShowAboutPanel(); | 
					
						
							| 
									
										
										
										
											2016-10-10 13:30:58 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-10 21:50:45 +05:30
										 |  |  | - (void)addNativeEventProcessorObserver: | 
					
						
							|  |  |  |     (content::NativeEventProcessorObserver*)observer { | 
					
						
							|  |  |  |   observers_.AddObserver(observer); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - (void)removeNativeEventProcessorObserver: | 
					
						
							|  |  |  |     (content::NativeEventProcessorObserver*)observer { | 
					
						
							|  |  |  |   observers_.RemoveObserver(observer); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 20:09:19 +08:00
										 |  |  | @end |