OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <launch.h> | 5 #include <launch.h> |
6 | 6 |
7 #include "base/mac/launchd.h" | 7 #include "base/mac/launchd.h" |
8 #include "base/mac/mac_util.h" | 8 #include "base/mac/mac_util.h" |
9 #include "base/mac/scoped_nsobject.h" | 9 #include "base/mac/scoped_nsobject.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "third_party/google_toolbox_for_mac/src/Foundation/GTMServiceManagement .h" | 11 #include "third_party/google_toolbox_for_mac/src/Foundation/GTMServiceManagement .h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // Returns the parameters needed to launch a really simple script from launchd. | 15 // Returns the parameters needed to launch a really simple script from launchd. |
16 NSDictionary* TestJobDictionary(NSString* label_ns) { | 16 NSDictionary* TestJobDictionary(NSString* label_ns) { |
17 NSString* shell_script_ns = @"sleep 10; echo TestGTMSMJobSubmitRemove"; | 17 NSString* shell_script_ns = @"echo TestGTMSMJobSubmitRemove"; |
18 base::scoped_nsobject<NSMutableArray> job_arguments( | 18 base::scoped_nsobject<NSMutableArray> job_arguments( |
19 [[NSMutableArray alloc] init]); | 19 [[NSMutableArray alloc] init]); |
20 [job_arguments addObject:@"/bin/sh"]; | 20 [job_arguments addObject:@"/bin/sh"]; |
21 [job_arguments addObject:@"-c"]; | 21 [job_arguments addObject:@"-c"]; |
22 [job_arguments addObject:shell_script_ns]; | 22 [job_arguments addObject:shell_script_ns]; |
23 | 23 |
24 NSMutableDictionary* job_dictionary_ns = [NSMutableDictionary dictionary]; | 24 NSMutableDictionary* job_dictionary_ns = [NSMutableDictionary dictionary]; |
25 [job_dictionary_ns setObject:label_ns forKey:@LAUNCH_JOBKEY_LABEL]; | 25 [job_dictionary_ns setObject:label_ns forKey:@LAUNCH_JOBKEY_LABEL]; |
26 [job_dictionary_ns setObject:[NSNumber numberWithBool:YES] | 26 [job_dictionary_ns setObject:[NSNumber numberWithBool:YES] |
27 forKey:@LAUNCH_JOBKEY_RUNATLOAD]; | 27 forKey:@LAUNCH_JOBKEY_RUNATLOAD]; |
(...skipping 21 matching lines...) Expand all Loading... | |
49 | 49 |
50 // Submit a new job. | 50 // Submit a new job. |
51 NSDictionary* job_dictionary_ns = TestJobDictionary(label_ns); | 51 NSDictionary* job_dictionary_ns = TestJobDictionary(label_ns); |
52 CFDictionaryRef job_dictionary_cf = base::mac::NSToCFCast(job_dictionary_ns); | 52 CFDictionaryRef job_dictionary_cf = base::mac::NSToCFCast(job_dictionary_ns); |
53 ASSERT_TRUE(GTMSMJobSubmit(job_dictionary_cf, &error)); | 53 ASSERT_TRUE(GTMSMJobSubmit(job_dictionary_cf, &error)); |
54 | 54 |
55 // The new job should be running. | 55 // The new job should be running. |
56 pid = base::mac::PIDForJob(label); | 56 pid = base::mac::PIDForJob(label); |
57 EXPECT_GT(pid, 0); | 57 EXPECT_GT(pid, 0); |
58 | 58 |
59 // In Yosemite, the behavior of launch_msg() has changed. Using it to remove | |
60 // a job that is still in progress succeeds, but the function still returns | |
61 // an error message: "The operation couldn’t be completed. Operation now in | |
62 // progress". This logic waits for the job to finish running to avoid | |
63 // triggering the error message. | |
64 if (base::mac::IsOSYosemiteOrLater()) { | |
65 while(true) { | |
66 pid = base::mac::PIDForJob(label); | |
67 if (pid == 0) | |
68 break; | |
69 } | |
70 } | |
71 | |
59 // Remove the job. | 72 // Remove the job. |
60 ASSERT_TRUE(GTMSMJobRemove(label_cf, &error)); | 73 ASSERT_TRUE(GTMSMJobRemove(label_cf, &error)); |
Mark Mentovai
2014/10/20 20:24:38
Shouldn’t we fix GTMSMJobRemove() instead?
erikchen
2014/10/21 00:13:25
I looked more into this. GTMSMJobRemove() is a thi
| |
61 pid = base::mac::PIDForJob(label); | 74 pid = base::mac::PIDForJob(label); |
62 EXPECT_LT(pid, 0); | 75 EXPECT_LT(pid, 0); |
63 | 76 |
64 // Attempting to remove the job again should fail. | 77 // Attempting to remove the job again should fail. |
65 EXPECT_FALSE(GTMSMJobRemove(label_cf, &error)); | 78 EXPECT_FALSE(GTMSMJobRemove(label_cf, &error)); |
66 } | 79 } |
OLD | NEW |