OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/common/mac/mock_launchd.h" | 5 #include "chrome/common/mac/mock_launchd.h" |
6 | 6 |
7 #include <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> |
8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
9 #include <sys/un.h> | 9 #include <sys/un.h> |
10 | 10 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 base::ScopedCFTypeRef<CFBundleRef> bundle( | 90 base::ScopedCFTypeRef<CFBundleRef> bundle( |
91 CFBundleCreate(kCFAllocatorDefault, url)); | 91 CFBundleCreate(kCFAllocatorDefault, url)); |
92 return bundle.get(); | 92 return bundle.get(); |
93 } | 93 } |
94 | 94 |
95 MockLaunchd::MockLaunchd(const base::FilePath& file, | 95 MockLaunchd::MockLaunchd(const base::FilePath& file, |
96 base::MessageLoop* loop, | 96 base::MessageLoop* loop, |
97 bool create_socket, | 97 bool create_socket, |
98 bool as_service) | 98 bool as_service) |
99 : file_(file), | 99 : file_(file), |
| 100 pipe_name_(GetServiceProcessChannel().name), |
100 message_loop_(loop), | 101 message_loop_(loop), |
101 create_socket_(create_socket), | 102 create_socket_(create_socket), |
102 as_service_(as_service), | 103 as_service_(as_service), |
103 restart_called_(false), | 104 restart_called_(false), |
104 remove_called_(false), | 105 remove_called_(false), |
105 checkin_called_(false), | 106 checkin_called_(false), |
106 write_called_(false), | 107 write_called_(false), |
107 delete_called_(false) { | 108 delete_called_(false) { |
108 std::string pipe_suffix("_SOCKET"); | |
109 base::FilePath socket_path = file_; | |
110 while (socket_path.value().length() + pipe_suffix.length() > | |
111 kMaxPipeNameLength - 2) { | |
112 socket_path = socket_path.DirName(); | |
113 } | |
114 pipe_name_ = socket_path.value() + pipe_suffix; | |
115 } | 109 } |
116 | 110 |
117 MockLaunchd::~MockLaunchd() { | 111 MockLaunchd::~MockLaunchd() { |
118 } | 112 } |
119 | 113 |
120 CFDictionaryRef MockLaunchd::CopyExports() { | |
121 if (!create_socket_) { | |
122 ADD_FAILURE(); | |
123 return NULL; | |
124 } | |
125 | |
126 CFStringRef env_var = | |
127 base::mac::NSToCFCast(GetServiceProcessLaunchDSocketEnvVar()); | |
128 base::ScopedCFTypeRef<CFStringRef> socket_path(CFStringCreateWithCString( | |
129 kCFAllocatorDefault, pipe_name_.c_str(), kCFStringEncodingUTF8)); | |
130 const void *keys[] = { env_var }; | |
131 const void *values[] = { socket_path }; | |
132 static_assert(arraysize(keys) == arraysize(values), | |
133 "keys must have the same number of elements as values"); | |
134 return CFDictionaryCreate(kCFAllocatorDefault, | |
135 keys, | |
136 values, | |
137 arraysize(keys), | |
138 &kCFTypeDictionaryKeyCallBacks, | |
139 &kCFTypeDictionaryValueCallBacks); | |
140 } | |
141 | |
142 CFDictionaryRef MockLaunchd::CopyJobDictionary(CFStringRef label) { | 114 CFDictionaryRef MockLaunchd::CopyJobDictionary(CFStringRef label) { |
143 if (!as_service_) { | 115 if (!as_service_) { |
144 scoped_ptr<MultiProcessLock> running_lock( | 116 scoped_ptr<MultiProcessLock> running_lock( |
145 TakeNamedLock(pipe_name_, false)); | 117 TakeNamedLock(pipe_name_, false)); |
146 if (running_lock.get()) | 118 if (running_lock.get()) |
147 return NULL; | 119 return NULL; |
148 } | 120 } |
149 | 121 |
150 CFStringRef program = CFSTR(LAUNCH_JOBKEY_PROGRAM); | 122 CFStringRef program = CFSTR(LAUNCH_JOBKEY_PROGRAM); |
151 CFStringRef program_pid = CFSTR(LAUNCH_JOBKEY_PID); | 123 CFStringRef program_pid = CFSTR(LAUNCH_JOBKEY_PID); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 Type type, | 261 Type type, |
290 CFStringRef name) { | 262 CFStringRef name) { |
291 delete_called_ = true; | 263 delete_called_ = true; |
292 return true; | 264 return true; |
293 } | 265 } |
294 | 266 |
295 void MockLaunchd::SignalReady() { | 267 void MockLaunchd::SignalReady() { |
296 ASSERT_TRUE(as_service_); | 268 ASSERT_TRUE(as_service_); |
297 running_lock_.reset(TakeNamedLock(pipe_name_, true)); | 269 running_lock_.reset(TakeNamedLock(pipe_name_, true)); |
298 } | 270 } |
OLD | NEW |