Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3266)

Unified Diff: chrome/app_shim/chrome_main_app_mode_mac.mm

Issue 2416313002: Prepare chrome_main_app_mode_mac.mm for 10.9 (Closed)
Patch Set: Cleaned up Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/app_shim/chrome_main_app_mode_mac.mm
diff --git a/chrome/app_shim/chrome_main_app_mode_mac.mm b/chrome/app_shim/chrome_main_app_mode_mac.mm
index a6d38f95182530545be4234b7abf2420ac72af76..e129ea4de9197d721516cfd98105d84e520afd2a 100644
--- a/chrome/app_shim/chrome_main_app_mode_mac.mm
+++ b/chrome/app_shim/chrome_main_app_mode_mac.mm
@@ -480,11 +480,13 @@ void AppShimController::SendSetAppHidden(bool hidden) {
base::Callback<void(bool)> onReply_;
AEDesc replyEvent_;
}
-// Sends an Apple Event to the process identified by |psn|, and calls |replyFn|
-// when the reply is received. Internally this creates a ReplyEventHandler,
-// which will delete itself once the reply event has been received.
-+ (void)pingProcess:(const ProcessSerialNumber&)psn
- andCall:(base::Callback<void(bool)>)replyFn;
+
+// Sends an Apple Event to the process identified by the outer bundle
+// identifier, and calls |replyFn| when the reply is received. Internally this
+// creates a ReplyEventHandler, which will delete itself once the reply event
+// has been received.
++ (void)pingProcessAndCall:(base::Callback<void(bool)>)replyFn;
+
@end
@interface ReplyEventHandler (PrivateMethods)
@@ -493,9 +495,12 @@ void AppShimController::SendSetAppHidden(bool hidden) {
// Apple Event reply arrives.
- (id)initWithCallback:(base::Callback<void(bool)>)replyFn;
-// Sends an Apple Event ping to the process identified by |psn| and registers
-// to listen for a reply.
-- (void)pingProcess:(const ProcessSerialNumber&)psn;
+// Returns the apple event that should be sent to the process.
+- (NSAppleEventDescriptor*)appleEventToSendToProcess;
+
+// Sends an Apple Event ping to the process identified by the bundle
+// identifier and registers to listen for a reply.
+- (void)pingProcess;
// Called when a response is received from the target process for the ping sent
// by |-pingProcess:|.
@@ -505,17 +510,19 @@ void AppShimController::SendSetAppHidden(bool hidden) {
// Calls |onReply_|, passing it |success| to specify whether the ping was
// successful.
- (void)closeWithSuccess:(bool)success;
+
@end
@implementation ReplyEventHandler
-+ (void)pingProcess:(const ProcessSerialNumber&)psn
- andCall:(base::Callback<void(bool)>)replyFn {
+
++ (void)pingProcessAndCall:(base::Callback<void(bool)>)replyFn {
// The object will release itself when the reply arrives, or possibly earlier
// if an unrecoverable error occurs.
ReplyEventHandler* handler =
[[ReplyEventHandler alloc] initWithCallback:replyFn];
- [handler pingProcess:psn];
+ [handler pingProcess];
}
+
@end
@implementation ReplyEventHandler (PrivateMethods)
@@ -526,18 +533,21 @@ void AppShimController::SendSetAppHidden(bool hidden) {
return self;
}
-- (void)pingProcess:(const ProcessSerialNumber&)psn {
+- (void)pingProcess {
// Register the reply listener.
NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager];
[em setEventHandler:self
andSelector:@selector(message:withReply:)
forEventClass:'aevt'
andEventID:'ansr'];
+
// Craft the Apple Event to send.
+ NSString* chrome_bundle_id = [base::mac::OuterBundle() bundleIdentifier];
tapted 2016/10/26 00:14:48 nit: I guess this should be `chromeBundleId ` sinc
spqchan 2016/10/26 00:45:43 Done.
NSAppleEventDescriptor* target = [NSAppleEventDescriptor
- descriptorWithDescriptorType:typeProcessSerialNumber
- bytes:&psn
- length:sizeof(psn)];
+ descriptorWithDescriptorType:typeApplicationBundleID
+ data:[chrome_bundle_id
+ dataUsingEncoding:NSUTF8StringEncoding]];
+
NSAppleEventDescriptor* initial_event =
[NSAppleEventDescriptor
appleEventWithEventClass:app_mode::kAEChromeAppClass
@@ -557,6 +567,22 @@ void AppShimController::SendSetAppHidden(bool hidden) {
}
}
+- (NSAppleEventDescriptor*)appleEventToSendToProcess {
+ // Register the reply listener.
+ NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager];
+ [em setEventHandler:self
+ andSelector:@selector(message:withReply:)
+ forEventClass:'aevt'
tapted 2016/10/26 00:14:48 kCoreEventClass ?
spqchan 2016/10/26 00:45:43 Done.
+ andEventID:'ansr'];
tapted 2016/10/26 00:14:48 kAEAnswer I guess we should update the literals i
spqchan 2016/10/26 00:45:43 Done.
+ NSAppleEventDescriptor* initial_event = [NSAppleEventDescriptor
+ appleEventWithEventClass:app_mode::kAEChromeAppClass
+ eventID:app_mode::kAEChromeAppPing
+ targetDescriptor:nil
+ returnID:kAutoGenerateReturnID
+ transactionID:kAnyTransactionID];
+ return initial_event;
+}
+
- (void)message:(NSAppleEventDescriptor*)event
withReply:(NSAppleEventDescriptor*)reply {
[self closeWithSuccess:true];
@@ -568,6 +594,7 @@ void AppShimController::SendSetAppHidden(bool hidden) {
[em removeEventHandlerForEventClass:'aevt' andEventID:'ansr'];
[self release];
}
+
@end
//-----------------------------------------------------------------------------
@@ -668,7 +695,6 @@ int ChromeAppModeStart_v4(const app_mode::ChromeAppModeInfo* info) {
!base::CommandLine::ForCurrentProcess()->HasSwitch(
app_mode::kLaunchedForTest)) {
// Launch Chrome if it isn't already running.
- ProcessSerialNumber psn;
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitch(switches::kSilentLaunch);
@@ -681,23 +707,26 @@ int ChromeAppModeStart_v4(const app_mode::ChromeAppModeInfo* info) {
info->profile_dir);
}
- base::Process app = base::mac::OpenApplicationWithPath(
- base::mac::OuterBundlePath(), command_line, NSWorkspaceLaunchDefault);
+ base::Callback<void(bool)> on_ping_chrome_reply = base::Bind(
+ &AppShimController::OnPingChromeReply, base::Unretained(&controller));
- // TODO(crbug.com/652563): Do not use deprecated GetProcessForPID. Change
- // |ReplyEventHandler| to take |pid_t| instead of |ProcessSerialNumber|.
- if (!app.IsValid() || GetProcessForPID(app.Pid(), &psn) != noErr)
- return 1;
+ // Self-owned object that will delete itself.
+ ReplyEventHandler* handler =
+ [[ReplyEventHandler alloc] initWithCallback:on_ping_chrome_reply];
+ NSAppleEventDescriptor* descriptor = [handler appleEventToSendToProcess];
- base::Callback<void(bool)> on_ping_chrome_reply =
- base::Bind(&AppShimController::OnPingChromeReply,
- base::Unretained(&controller));
+ base::Process app = base::mac::OpenApplicationWithPath(
+ base::mac::OuterBundlePath(), command_line, NSWorkspaceLaunchDefault,
+ descriptor);
+ if (!app.IsValid()) {
+ [handler closeWithSuccess:false];
+ return 1;
+ }
// This code abuses the fact that Apple Events sent before the process is
// fully initialized don't receive a reply until its run loop starts. Once
// the reply is received, Chrome will have opened its IPC port, guaranteed.
- [ReplyEventHandler pingProcess:psn
- andCall:on_ping_chrome_reply];
+ [ReplyEventHandler pingProcessAndCall:on_ping_chrome_reply];
tapted 2016/10/26 00:14:48 Can this line (and the two methods pingProcessAndC
spqchan 2016/10/26 00:45:43 I removed it at first, but this runs into a proble
main_message_loop.task_runner()->PostDelayedTask(
FROM_HERE, base::Bind(&AppShimController::OnPingChromeTimeout,

Powered by Google App Engine
This is Rietveld 408576698