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 "base/mac/mac_util.h" | 5 #include "base/mac/mac_util.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 #import <IOKit/IOKitLib.h> | 8 #import <IOKit/IOKitLib.h> |
9 | 9 |
10 #include <errno.h> | 10 #include <errno.h> |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 OSStatus os_err = | 291 OSStatus os_err = |
292 CSBackupSetItemExcluded(base::mac::NSToCFCast(file_url), TRUE, FALSE); | 292 CSBackupSetItemExcluded(base::mac::NSToCFCast(file_url), TRUE, FALSE); |
293 if (os_err != noErr) { | 293 if (os_err != noErr) { |
294 OSSTATUS_DLOG(WARNING, os_err) | 294 OSSTATUS_DLOG(WARNING, os_err) |
295 << "Failed to set backup exclusion for file '" | 295 << "Failed to set backup exclusion for file '" |
296 << file_path.value().c_str() << "'"; | 296 << file_path.value().c_str() << "'"; |
297 } | 297 } |
298 return os_err == noErr; | 298 return os_err == noErr; |
299 } | 299 } |
300 | 300 |
301 void SetProcessName(CFStringRef process_name) { | |
302 if (!process_name || CFStringGetLength(process_name) == 0) { | |
303 NOTREACHED() << "SetProcessName given bad name."; | |
304 return; | |
305 } | |
306 | |
307 if (![NSThread isMainThread]) { | |
308 NOTREACHED() << "Should only set process name from main thread."; | |
309 return; | |
310 } | |
311 | |
312 // Warning: here be dragons! This is SPI reverse-engineered from WebKit's | |
313 // plugin host, and could break at any time (although realistically it's only | |
314 // likely to break in a new major release). | |
315 // When 10.7 is available, check that this still works, and update this | |
316 // comment for 10.8. | |
317 | |
318 // Private CFType used in these LaunchServices calls. | |
319 typedef CFTypeRef PrivateLSASN; | |
320 typedef PrivateLSASN (*LSGetCurrentApplicationASNType)(); | |
321 typedef OSStatus (*LSSetApplicationInformationItemType)(int, PrivateLSASN, | |
322 CFStringRef, | |
323 CFStringRef, | |
324 CFDictionaryRef*); | |
325 | |
326 static LSGetCurrentApplicationASNType ls_get_current_application_asn_func = | |
327 NULL; | |
328 static LSSetApplicationInformationItemType | |
329 ls_set_application_information_item_func = NULL; | |
330 static CFStringRef ls_display_name_key = NULL; | |
331 | |
332 static bool did_symbol_lookup = false; | |
333 if (!did_symbol_lookup) { | |
334 did_symbol_lookup = true; | |
335 CFBundleRef launch_services_bundle = | |
336 CFBundleGetBundleWithIdentifier(CFSTR("com.apple.LaunchServices")); | |
337 if (!launch_services_bundle) { | |
338 DLOG(ERROR) << "Failed to look up LaunchServices bundle"; | |
339 return; | |
340 } | |
341 | |
342 ls_get_current_application_asn_func = | |
343 reinterpret_cast<LSGetCurrentApplicationASNType>( | |
344 CFBundleGetFunctionPointerForName( | |
345 launch_services_bundle, CFSTR("_LSGetCurrentApplicationASN"))); | |
346 if (!ls_get_current_application_asn_func) | |
347 DLOG(ERROR) << "Could not find _LSGetCurrentApplicationASN"; | |
348 | |
349 ls_set_application_information_item_func = | |
350 reinterpret_cast<LSSetApplicationInformationItemType>( | |
351 CFBundleGetFunctionPointerForName( | |
352 launch_services_bundle, | |
353 CFSTR("_LSSetApplicationInformationItem"))); | |
354 if (!ls_set_application_information_item_func) | |
355 DLOG(ERROR) << "Could not find _LSSetApplicationInformationItem"; | |
356 | |
357 CFStringRef* key_pointer = reinterpret_cast<CFStringRef*>( | |
358 CFBundleGetDataPointerForName(launch_services_bundle, | |
359 CFSTR("_kLSDisplayNameKey"))); | |
360 ls_display_name_key = key_pointer ? *key_pointer : NULL; | |
361 if (!ls_display_name_key) | |
362 DLOG(ERROR) << "Could not find _kLSDisplayNameKey"; | |
363 | |
364 // Internally, this call relies on the Mach ports that are started up by the | |
365 // Carbon Process Manager. In debug builds this usually happens due to how | |
366 // the logging layers are started up; but in release, it isn't started in as | |
367 // much of a defined order. So if the symbols had to be loaded, go ahead | |
368 // and force a call to make sure the manager has been initialized and hence | |
369 // the ports are opened. | |
370 ProcessSerialNumber psn; | |
371 GetCurrentProcess(&psn); | |
372 } | |
373 if (!ls_get_current_application_asn_func || | |
374 !ls_set_application_information_item_func || | |
375 !ls_display_name_key) { | |
376 return; | |
377 } | |
378 | |
379 PrivateLSASN asn = ls_get_current_application_asn_func(); | |
380 // Constant used by WebKit; what exactly it means is unknown. | |
381 const int magic_session_constant = -2; | |
382 OSErr err = | |
383 ls_set_application_information_item_func(magic_session_constant, asn, | |
384 ls_display_name_key, | |
385 process_name, | |
386 NULL /* optional out param */); | |
387 OSSTATUS_DLOG_IF(ERROR, err != noErr, err) | |
388 << "Call to set process name failed"; | |
389 } | |
390 | |
391 // Converts a NSImage to a CGImageRef. Normally, the system frameworks can do | 301 // Converts a NSImage to a CGImageRef. Normally, the system frameworks can do |
392 // this fine, especially on 10.6. On 10.5, however, CGImage cannot handle | 302 // this fine, especially on 10.6. On 10.5, however, CGImage cannot handle |
393 // converting a PDF-backed NSImage into a CGImageRef. This function will | 303 // converting a PDF-backed NSImage into a CGImageRef. This function will |
394 // rasterize the PDF into a bitmap CGImage. The caller is responsible for | 304 // rasterize the PDF into a bitmap CGImage. The caller is responsible for |
395 // releasing the return value. | 305 // releasing the return value. |
396 CGImageRef CopyNSImageToCGImage(NSImage* image) { | 306 CGImageRef CopyNSImageToCGImage(NSImage* image) { |
397 // This is based loosely on http://www.cocoadev.com/index.pl?CGImageRef . | 307 // This is based loosely on http://www.cocoadev.com/index.pl?CGImageRef . |
398 NSSize size = [image size]; | 308 NSSize size = [image size]; |
399 ScopedCFTypeRef<CGContextRef> context( | 309 ScopedCFTypeRef<CGContextRef> context( |
400 CGBitmapContextCreate(NULL, // Allow CG to allocate memory. | 310 CGBitmapContextCreate(NULL, // Allow CG to allocate memory. |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
701 StringPiece(begin + comma_loc + 1, ident.end()), &minor_tmp)) | 611 StringPiece(begin + comma_loc + 1, ident.end()), &minor_tmp)) |
702 return false; | 612 return false; |
703 *type = ident.substr(0, number_loc); | 613 *type = ident.substr(0, number_loc); |
704 *major = major_tmp; | 614 *major = major_tmp; |
705 *minor = minor_tmp; | 615 *minor = minor_tmp; |
706 return true; | 616 return true; |
707 } | 617 } |
708 | 618 |
709 } // namespace mac | 619 } // namespace mac |
710 } // namespace base | 620 } // namespace base |
OLD | NEW |