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

Side by Side Diff: chrome/common/mach_ipc_mac.mm

Issue 460126: Mac: Proof-of-concept task manager (Closed)
Patch Set: rebase Created 10 years, 11 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 unified diff | Download patch
« chrome/common/mach_ipc_mac.h ('K') | « chrome/common/mach_ipc_mac.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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/mach_ipc_mac.h" 5 #include "chrome/common/mach_ipc_mac.h"
6 6
7 #import <Foundation/Foundation.h> 7 #import <Foundation/Foundation.h>
8 8
9 #include <stdio.h> 9 #include <stdio.h>
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 178
179 //============================================================================== 179 //==============================================================================
180 // create a new mach port for receiving messages and register a name for it 180 // create a new mach port for receiving messages and register a name for it
181 ReceivePort::ReceivePort(const char *receive_port_name) { 181 ReceivePort::ReceivePort(const char *receive_port_name) {
182 mach_port_t current_task = mach_task_self(); 182 mach_port_t current_task = mach_task_self();
183 183
184 init_result_ = mach_port_allocate(current_task, 184 init_result_ = mach_port_allocate(current_task,
185 MACH_PORT_RIGHT_RECEIVE, 185 MACH_PORT_RIGHT_RECEIVE,
186 &port_); 186 &port_);
187 187
188 if (init_result_ != KERN_SUCCESS) 188 if (init_result_ != KERN_SUCCESS) {
189 fprintf(stderr, "failed to allocate send port\n");
Mark Mentovai 2010/01/11 20:22:46 What's this decoration about?
189 return; 190 return;
191 }
190 192
191 init_result_ = mach_port_insert_right(current_task, 193 init_result_ = mach_port_insert_right(current_task,
192 port_, 194 port_,
193 port_, 195 port_,
194 MACH_MSG_TYPE_MAKE_SEND); 196 MACH_MSG_TYPE_MAKE_SEND);
195 197
196 if (init_result_ != KERN_SUCCESS) 198 if (init_result_ != KERN_SUCCESS) {
199 fprintf(stderr, "failed to insert send right\n");
Mark Mentovai 2010/01/11 20:22:46 Same
197 return; 200 return;
201 }
198 202
199 NSPort *ns_port = [NSMachPort portWithMachPort:port_]; 203 NSPort *ns_port = [NSMachPort portWithMachPort:port_];
200 NSString *port_name = [NSString stringWithUTF8String:receive_port_name]; 204 NSString *port_name = [NSString stringWithUTF8String:receive_port_name];
201 [[NSMachBootstrapServer sharedInstance] registerPort:ns_port name:port_name]; 205 if (![[NSMachBootstrapServer sharedInstance] registerPort:ns_port name:port_na me]) {
Mark Mentovai 2010/01/11 20:22:46 80
206 NSLog(@"failed to register ort %@ with name %@", ns_port, port_name);
Mark Mentovai 2010/01/11 20:22:46 NSLog? And you misspelled port. :)
207 }
202 } 208 }
203 209
204 //============================================================================== 210 //==============================================================================
205 // create a new mach port for receiving messages 211 // create a new mach port for receiving messages
206 ReceivePort::ReceivePort() { 212 ReceivePort::ReceivePort() {
207 mach_port_t current_task = mach_task_self(); 213 mach_port_t current_task = mach_task_self();
208 214
209 init_result_ = mach_port_allocate(current_task, 215 init_result_ = mach_port_allocate(current_task,
210 MACH_PORT_RIGHT_RECEIVE, 216 MACH_PORT_RIGHT_RECEIVE,
211 &port_); 217 &port_);
212 218
213 if (init_result_ != KERN_SUCCESS) 219 if (init_result_ != KERN_SUCCESS) {
220 fprintf(stderr, "failed to allocate send port2\n");
Mark Mentovai 2010/01/11 20:22:46 t’let
214 return; 221 return;
222 }
215 223
216 init_result_ = mach_port_insert_right(current_task, 224 init_result_ = mach_port_insert_right(current_task,
217 port_, 225 port_,
218 port_, 226 port_,
219 MACH_MSG_TYPE_MAKE_SEND); 227 MACH_MSG_TYPE_MAKE_SEND);
220 } 228 }
221 229
222 //============================================================================== 230 //==============================================================================
223 // Given an already existing mach port, use it. We take ownership of the 231 // Given an already existing mach port, use it. We take ownership of the
224 // port and deallocate it in our destructor. 232 // port and deallocate it in our destructor.
225 ReceivePort::ReceivePort(mach_port_t receive_port) 233 ReceivePort::ReceivePort(mach_port_t receive_port)
226 : port_(receive_port), 234 : port_(receive_port),
227 init_result_(KERN_SUCCESS) { 235 init_result_(KERN_SUCCESS) {
228 } 236 }
229 237
230 //============================================================================== 238 //==============================================================================
231 ReceivePort::~ReceivePort() { 239 ReceivePort::~ReceivePort() {
232 if (init_result_ == KERN_SUCCESS) 240 if (init_result_ == KERN_SUCCESS)
233 mach_port_deallocate(mach_task_self(), port_); 241 mach_port_deallocate(mach_task_self(), port_);
234 } 242 }
235 243
236 //============================================================================== 244 //==============================================================================
237 kern_return_t ReceivePort::WaitForMessage(MachReceiveMessage *out_message, 245 kern_return_t ReceivePort::WaitForMessage(MachReceiveMessage *out_message,
238 mach_msg_timeout_t timeout) { 246 mach_msg_timeout_t timeout) {
239 if (!out_message) { 247 if (!out_message) {
240 return KERN_INVALID_ARGUMENT; 248 return KERN_INVALID_ARGUMENT;
241 } 249 }
242 250
243 // return any error condition encountered in constructor 251 // return any error condition encountered in constructor
244 if (init_result_ != KERN_SUCCESS) 252 if (init_result_ != KERN_SUCCESS) {
253 fprintf(stderr, "failed to insert send right2\n");
Mark Mentovai 2010/01/11 20:22:46
245 return init_result_; 254 return init_result_;
255 }
246 256
247 out_message->Head()->msgh_bits = 0; 257 out_message->Head()->msgh_bits = 0;
248 out_message->Head()->msgh_local_port = port_; 258 out_message->Head()->msgh_local_port = port_;
249 out_message->Head()->msgh_remote_port = MACH_PORT_NULL; 259 out_message->Head()->msgh_remote_port = MACH_PORT_NULL;
250 out_message->Head()->msgh_reserved = 0; 260 out_message->Head()->msgh_reserved = 0;
251 out_message->Head()->msgh_id = 0; 261 out_message->Head()->msgh_id = 0;
252 262
253 kern_return_t result = mach_msg(out_message->Head(), 263 kern_return_t result = mach_msg(out_message->Head(),
254 MACH_RCV_MSG | MACH_RCV_TIMEOUT, 264 MACH_RCV_MSG | MACH_RCV_TIMEOUT,
255 0, 265 0,
256 out_message->MaxSize(), 266 out_message->MaxSize(),
257 port_, 267 port_,
258 timeout, // timeout in ms 268 timeout, // timeout in ms
259 MACH_PORT_NULL); 269 MACH_PORT_NULL);
260 270
261 return result; 271 return result;
262 } 272 }
263 273
264 #pragma mark - 274 #pragma mark -
265 275
266 //============================================================================== 276 //==============================================================================
267 // get a port with send rights corresponding to a named registered service 277 // get a port with send rights corresponding to a named registered service
268 MachPortSender::MachPortSender(const char *receive_port_name) { 278 MachPortSender::MachPortSender(const char *receive_port_name) {
269 mach_port_t bootstrap_port = 0; 279 mach_port_t bootstrap_port = 0;
270 init_result_ = task_get_bootstrap_port(mach_task_self(), &bootstrap_port); 280 init_result_ = task_get_bootstrap_port(mach_task_self(), &bootstrap_port);
271 281
272 if (init_result_ != KERN_SUCCESS) 282 if (init_result_ != KERN_SUCCESS) {
283 fprintf(stderr, "receiving bootstrap port failed\n");
Mark Mentovai 2010/01/11 20:22:46
273 return; 284 return;
285 }
274 286
275 init_result_ = bootstrap_look_up(bootstrap_port, 287 init_result_ = bootstrap_look_up(bootstrap_port,
276 const_cast<char*>(receive_port_name), 288 const_cast<char*>(receive_port_name),
277 &send_port_); 289 &send_port_);
278 } 290 }
279 291
280 //============================================================================== 292 //==============================================================================
281 MachPortSender::MachPortSender(mach_port_t send_port) 293 MachPortSender::MachPortSender(mach_port_t send_port)
282 : send_port_(send_port), 294 : send_port_(send_port),
283 init_result_(KERN_SUCCESS) { 295 init_result_(KERN_SUCCESS) {
284 } 296 }
285 297
286 //============================================================================== 298 //==============================================================================
287 kern_return_t MachPortSender::SendMessage(MachSendMessage &message, 299 kern_return_t MachPortSender::SendMessage(MachSendMessage &message,
288 mach_msg_timeout_t timeout) { 300 mach_msg_timeout_t timeout) {
289 if (message.Head()->msgh_size == 0) { 301 if (message.Head()->msgh_size == 0) {
290 NOTREACHED(); 302 NOTREACHED();
291 return KERN_INVALID_VALUE; // just for safety -- never should occur 303 return KERN_INVALID_VALUE; // just for safety -- never should occur
292 }; 304 };
293 305
294 if (init_result_ != KERN_SUCCESS) 306 if (init_result_ != KERN_SUCCESS) {
307 fprintf(stderr, "bootstrap lookup failed\n");
Mark Mentovai 2010/01/11 20:22:46
295 return init_result_; 308 return init_result_;
309 }
296 310
297 message.Head()->msgh_remote_port = send_port_; 311 message.Head()->msgh_remote_port = send_port_;
298 312
299 kern_return_t result = mach_msg(message.Head(), 313 kern_return_t result = mach_msg(message.Head(),
300 MACH_SEND_MSG | MACH_SEND_TIMEOUT, 314 MACH_SEND_MSG | MACH_SEND_TIMEOUT,
301 message.Head()->msgh_size, 315 message.Head()->msgh_size,
302 0, 316 0,
303 MACH_PORT_NULL, 317 MACH_PORT_NULL,
304 timeout, // timeout in ms 318 timeout, // timeout in ms
305 MACH_PORT_NULL); 319 MACH_PORT_NULL);
306 320
307 return result; 321 return result;
308 } 322 }
OLDNEW
« chrome/common/mach_ipc_mac.h ('K') | « chrome/common/mach_ipc_mac.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698