diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-01 18:20:14 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-01 18:20:14 +0000 |
commit | 0378bf4edc40686bac06c8e14bd25fdb3f9cfb28 (patch) | |
tree | ee5543b43dd9743d75e1aa8f16692dcfca6c813f /base/mac | |
parent | d028296ec1c0fdfbd40e2390ca3121de7055295d (diff) | |
download | chromium_src-0378bf4edc40686bac06c8e14bd25fdb3f9cfb28.zip chromium_src-0378bf4edc40686bac06c8e14bd25fdb3f9cfb28.tar.gz chromium_src-0378bf4edc40686bac06c8e14bd25fdb3f9cfb28.tar.bz2 |
Move base/mac_util.h to base/mac and use the base::mac namespace.
Fix up callers to use the new location & namespace. Remove includes from
files where it wasn't necessary.
TEST=it compiles
BUG=none
Review URL: http://codereview.chromium.org/6046009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@70359 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/mac')
-rw-r--r-- | base/mac/mac_util.h | 249 | ||||
-rw-r--r-- | base/mac/mac_util.mm | 716 | ||||
-rw-r--r-- | base/mac/mac_util_unittest.mm | 197 |
3 files changed, 1162 insertions, 0 deletions
diff --git a/base/mac/mac_util.h b/base/mac/mac_util.h new file mode 100644 index 0000000..7e5dddb --- /dev/null +++ b/base/mac/mac_util.h @@ -0,0 +1,249 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_MAC_MAC_UTIL_H_ +#define BASE_MAC_MAC_UTIL_H_ +#pragma once + +#include <Carbon/Carbon.h> +#include <string> +#include <vector> + +#include "base/logging.h" + +#if defined(__OBJC__) +#import <Foundation/Foundation.h> + +@class NSBundle; +@class NSWindow; +#else // __OBJC__ +class NSBundle; +class NSImage; +class NSWindow; +#endif // __OBJC__ + +class FilePath; + +// Adapted from NSPathUtilities.h and NSObjCRuntime.h. +#if __LP64__ || NS_BUILD_32_LIKE_64 +typedef unsigned long NSSearchPathDirectory; +typedef unsigned long NSSearchPathDomainMask; +#else +typedef unsigned int NSSearchPathDirectory; +typedef unsigned int NSSearchPathDomainMask; +#endif + +namespace base { +namespace mac { + +// Full screen modes, in increasing order of priority. More permissive modes +// take predecence. +enum FullScreenMode { + kFullScreenModeHideAll = 0, + kFullScreenModeHideDock = 1, + kFullScreenModeAutoHideAll = 2, + kNumFullScreenModes = 3, + + // kFullScreenModeNormal is not a valid FullScreenMode, but it is useful to + // other classes, so we include it here. + kFullScreenModeNormal = 10, +}; + +std::string PathFromFSRef(const FSRef& ref); +bool FSRefFromPath(const std::string& path, FSRef* ref); + +// Returns true if the application is running from a bundle +bool AmIBundled(); +void SetOverrideAmIBundled(bool value); + +// Returns true if this process is marked as a "Background only process". +bool IsBackgroundOnlyProcess(); + +// Returns the main bundle or the override, used for code that needs +// to fetch resources from bundles, but work within a unittest where we +// aren't a bundle. +NSBundle* MainAppBundle(); +FilePath MainAppBundlePath(); + +// Set the bundle that MainAppBundle will return, overriding the default value +// (Restore the default by calling SetOverrideAppBundle(nil)). +void SetOverrideAppBundle(NSBundle* bundle); +void SetOverrideAppBundlePath(const FilePath& file_path); + +// Returns the creator code associated with the CFBundleRef at bundle. +OSType CreatorCodeForCFBundleRef(CFBundleRef bundle); + +// Returns the creator code associated with this application, by calling +// CreatorCodeForCFBundleRef for the application's main bundle. If this +// information cannot be determined, returns kUnknownType ('????'). This +// does not respect the override app bundle because it's based on CFBundle +// instead of NSBundle, and because callers probably don't want the override +// app bundle's creator code anyway. +OSType CreatorCodeForApplication(); + +// Searches for directories for the given key in only the given |domain_mask|. +// If found, fills result (which must always be non-NULL) with the +// first found directory and returns true. Otherwise, returns false. +bool GetSearchPathDirectory(NSSearchPathDirectory directory, + NSSearchPathDomainMask domain_mask, + FilePath* result); + +// Searches for directories for the given key in only the user domain. +// If found, fills result (which must always be non-NULL) with the +// first found directory and returns true. Otherwise, returns false. +bool GetUserDirectory(NSSearchPathDirectory directory, FilePath* result); + +// Searches for directories for the given key in only the local domain. +// If found, fills result (which must always be non-NULL) with the +// first found directory and returns true. Otherwise, returns false. +bool GetLocalDirectory(NSSearchPathDirectory directory, FilePath* result); + +// Returns the ~/Library directory. +FilePath GetUserLibraryPath(); + +// Returns an sRGB color space. The return value is a static value; do not +// release it! +CGColorSpaceRef GetSRGBColorSpace(); + +// Returns the color space being used by the main display. The return value +// is a static value; do not release it! +CGColorSpaceRef GetSystemColorSpace(); + +// Add a full screen request for the given |mode|. Must be paired with a +// ReleaseFullScreen() call for the same |mode|. This does not by itself create +// a fullscreen window; rather, it manages per-application state related to +// hiding the dock and menubar. Must be called on the main thread. +void RequestFullScreen(FullScreenMode mode); + +// Release a request for full screen mode. Must be matched with a +// RequestFullScreen() call for the same |mode|. As with RequestFullScreen(), +// this does not affect windows directly, but rather manages per-application +// state. For example, if there are no other outstanding +// |kFullScreenModeAutoHideAll| requests, this will reshow the menu bar. Must +// be called on main thread. +void ReleaseFullScreen(FullScreenMode mode); + +// Convenience method to switch the current fullscreen mode. This has the same +// net effect as a ReleaseFullScreen(from_mode) call followed immediately by a +// RequestFullScreen(to_mode). Must be called on the main thread. +void SwitchFullScreenModes(FullScreenMode from_mode, FullScreenMode to_mode); + +// Set the visibility of the cursor. +void SetCursorVisibility(bool visible); + +// Should windows miniaturize on a double-click (on the title bar)? +bool ShouldWindowsMiniaturizeOnDoubleClick(); + +// Activates the process with the given PID. +void ActivateProcess(pid_t); + +// Pulls a snapshot of the entire browser into png_representation. +void GrabWindowSnapshot(NSWindow* window, + std::vector<unsigned char>* png_representation, + int* width, int* height); + +// Takes a path to an (executable) binary and tries to provide the path to an +// application bundle containing it. It takes the outermost bundle that it can +// find (so for "/Foo/Bar.app/.../Baz.app/..." it produces "/Foo/Bar.app"). +// |exec_name| - path to the binary +// returns - path to the application bundle, or empty on error +FilePath GetAppBundlePath(const FilePath& exec_name); + +// Set the Time Machine exclusion property for the given file. +bool SetFileBackupExclusion(const FilePath& file_path, bool exclude); + +// Utility function to pull out a value from a dictionary, check its type, and +// return it. Returns NULL if the key is not present or of the wrong type. +CFTypeRef GetValueFromDictionary(CFDictionaryRef dict, + CFStringRef key, + CFTypeID expected_type); + +// Sets the process name as displayed in Activity Monitor to process_name. +void SetProcessName(CFStringRef process_name); + +// Converts a NSImage to a CGImageRef. Normally, the system frameworks can do +// this fine, especially on 10.6. On 10.5, however, CGImage cannot handle +// converting a PDF-backed NSImage into a CGImageRef. This function will +// rasterize the PDF into a bitmap CGImage. The caller is responsible for +// releasing the return value. +CGImageRef CopyNSImageToCGImage(NSImage* image); + +// Checks if the current application is set as a Login Item, so it will launch +// on Login. If a non-NULL pointer to is_hidden is passed, the Login Item also +// is queried for the 'hide on launch' flag. +bool CheckLoginItemStatus(bool* is_hidden); + +// Adds current application to the set of Login Items with specified "hide" +// flag. This has the same effect as adding/removing the application in +// SystemPreferences->Accounts->LoginItems or marking Application in the Dock +// as "Options->Open on Login". +// Does nothing if the application is already set up as Login Item with +// specified hide flag. +void AddToLoginItems(bool hide_on_startup); + +// Removes the current application from the list Of Login Items. +void RemoveFromLoginItems(); + +// Returns true if the current process was automatically launched as a +// 'Login Item' with 'hide on startup' flag. Used to suppress opening windows. +bool WasLaunchedAsHiddenLoginItem(); + +// Retain/release calls for memory management in C++. +void NSObjectRetain(void* obj); +void NSObjectRelease(void* obj); + +#if defined(__OBJC__) + +// Convert toll-free bridged CFTypes to NSTypes. This does not autorelease +// |cf_val|. This is useful for the case where there is a CFType in a call that +// expects an NSType and the compiler is complaining about const casting +// problems. +// The call is used like this: +// NSString *foo = CFToNSCast(CFSTR("Hello")); +// The macro magic below is to enforce safe casting. It could possibly have +// been done using template function specialization, but template function +// specialization doesn't always work intuitively, +// (http://www.gotw.ca/publications/mill17.htm) so the trusty combination +// of macros and function overloading is used instead. + +#define CF_TO_NS_CAST(TypeCF, TypeNS) \ +inline TypeNS* CFToNSCast(TypeCF cf_val) { \ + TypeNS* ns_val = \ + const_cast<TypeNS*>(reinterpret_cast<const TypeNS*>(cf_val)); \ + DCHECK(!ns_val || [ns_val isKindOfClass:[TypeNS class]]); \ + return ns_val; \ +} + +// List of toll-free bridged types taken from: +// http://www.cocoadev.com/index.pl?TollFreeBridged + +CF_TO_NS_CAST(CFArrayRef, NSArray); +CF_TO_NS_CAST(CFMutableArrayRef, NSMutableArray); +CF_TO_NS_CAST(CFAttributedStringRef, NSAttributedString); +CF_TO_NS_CAST(CFMutableAttributedStringRef, NSMutableAttributedString); +CF_TO_NS_CAST(CFCalendarRef, NSCalendar); +CF_TO_NS_CAST(CFCharacterSetRef, NSCharacterSet); +CF_TO_NS_CAST(CFMutableCharacterSetRef, NSMutableCharacterSet); +CF_TO_NS_CAST(CFDataRef, NSData); +CF_TO_NS_CAST(CFMutableDataRef, NSMutableData); +CF_TO_NS_CAST(CFDateRef, NSDate); +CF_TO_NS_CAST(CFDictionaryRef, NSDictionary); +CF_TO_NS_CAST(CFMutableDictionaryRef, NSMutableDictionary); +CF_TO_NS_CAST(CFNumberRef, NSNumber); +CF_TO_NS_CAST(CFRunLoopTimerRef, NSTimer); +CF_TO_NS_CAST(CFSetRef, NSSet); +CF_TO_NS_CAST(CFMutableSetRef, NSMutableSet); +CF_TO_NS_CAST(CFStringRef, NSString); +CF_TO_NS_CAST(CFMutableStringRef, NSMutableString); +CF_TO_NS_CAST(CFURLRef, NSURL); +CF_TO_NS_CAST(CFTimeZoneRef, NSTimeZone); +CF_TO_NS_CAST(CFReadStreamRef, NSInputStream); +CF_TO_NS_CAST(CFWriteStreamRef, NSOutputStream); + +#endif // __OBJC__ + +} // namespace mac +} // namespace base + +#endif // BASE_MAC_MAC_UTIL_H_ diff --git a/base/mac/mac_util.mm b/base/mac/mac_util.mm new file mode 100644 index 0000000..21213ba --- /dev/null +++ b/base/mac/mac_util.mm @@ -0,0 +1,716 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/mac/mac_util.h" + +#import <Cocoa/Cocoa.h> + +#include "base/file_path.h" +#include "base/logging.h" +#include "base/mac/scoped_cftyperef.h" +#include "base/message_loop.h" +#include "base/scoped_nsobject.h" +#include "base/sys_string_conversions.h" + +namespace base { +namespace mac { + +namespace { + +// a count of currently outstanding requests for full screen mode from browser +// windows, plugins, etc. +int g_full_screen_requests[kNumFullScreenModes] = { 0, 0, 0}; + +// Sets the appropriate SystemUIMode based on the current full screen requests. +// Since only one SystemUIMode can be active at a given time, full screen +// requests are ordered by priority. If there are no outstanding full screen +// requests, reverts to normal mode. If the correct SystemUIMode is already +// set, does nothing. +void SetUIMode() { + // Get the current UI mode. + SystemUIMode current_mode; + GetSystemUIMode(¤t_mode, NULL); + + // Determine which mode should be active, based on which requests are + // currently outstanding. More permissive requests take precedence. For + // example, plugins request |kFullScreenModeAutoHideAll|, while browser + // windows request |kFullScreenModeHideDock| when the fullscreen overlay is + // down. Precedence goes to plugins in this case, so AutoHideAll wins over + // HideDock. + SystemUIMode desired_mode = kUIModeNormal; + SystemUIOptions desired_options = 0; + if (g_full_screen_requests[kFullScreenModeAutoHideAll] > 0) { + desired_mode = kUIModeAllHidden; + desired_options = kUIOptionAutoShowMenuBar; + } else if (g_full_screen_requests[kFullScreenModeHideDock] > 0) { + desired_mode = kUIModeContentHidden; + } else if (g_full_screen_requests[kFullScreenModeHideAll] > 0) { + desired_mode = kUIModeAllHidden; + } + + if (current_mode != desired_mode) + SetSystemUIMode(desired_mode, desired_options); +} + +bool WasLaunchedAsLoginItem() { + ProcessSerialNumber psn = { 0, kCurrentProcess }; + + scoped_nsobject<NSDictionary> process_info( + CFToNSCast(ProcessInformationCopyDictionary(&psn, + kProcessDictionaryIncludeAllInformationMask))); + + long long temp = [[process_info objectForKey:@"ParentPSN"] longLongValue]; + ProcessSerialNumber parent_psn = + { (temp >> 32) & 0x00000000FFFFFFFFLL, temp & 0x00000000FFFFFFFFLL }; + + scoped_nsobject<NSDictionary> parent_info( + CFToNSCast(ProcessInformationCopyDictionary(&parent_psn, + kProcessDictionaryIncludeAllInformationMask))); + + // Check that creator process code is that of loginwindow. + BOOL result = + [[parent_info objectForKey:@"FileCreator"] isEqualToString:@"lgnw"]; + + return result == YES; +} + +// Looks into Shared File Lists corresponding to Login Items for the item +// representing the current application. If such an item is found, returns +// retained reference to it. Caller is responsible for releasing the reference. +LSSharedFileListItemRef GetLoginItemForApp() { + ScopedCFTypeRef<LSSharedFileListRef> login_items(LSSharedFileListCreate( + NULL, kLSSharedFileListSessionLoginItems, NULL)); + + if (!login_items.get()) { + LOG(ERROR) << "Couldn't get a Login Items list."; + return NULL; + } + + scoped_nsobject<NSArray> login_items_array( + CFToNSCast(LSSharedFileListCopySnapshot(login_items, NULL))); + + NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; + + for(NSUInteger i = 0; i < [login_items_array count]; ++i) { + LSSharedFileListItemRef item = reinterpret_cast<LSSharedFileListItemRef>( + [login_items_array objectAtIndex:i]); + CFURLRef item_url_ref = NULL; + + if (LSSharedFileListItemResolve(item, 0, &item_url_ref, NULL) == noErr) { + ScopedCFTypeRef<CFURLRef> item_url(item_url_ref); + if (CFEqual(item_url, url)) { + CFRetain(item); + return item; + } + } + } + + return NULL; +} + +#if !defined(MAC_OS_X_VERSION_10_6) || \ + MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 +// kLSSharedFileListLoginItemHidden is supported on +// 10.5, but missing from the 10.5 headers. +// http://openradar.appspot.com/6482251 +static NSString* kLSSharedFileListLoginItemHidden = + @"com.apple.loginitem.HideOnLaunch"; +#endif + +bool IsHiddenLoginItem(LSSharedFileListItemRef item) { + ScopedCFTypeRef<CFBooleanRef> hidden(reinterpret_cast<CFBooleanRef>( + LSSharedFileListItemCopyProperty(item, + reinterpret_cast<CFStringRef>(kLSSharedFileListLoginItemHidden)))); + + return hidden && hidden == kCFBooleanTrue; +} + +} // namespace + +std::string PathFromFSRef(const FSRef& ref) { + ScopedCFTypeRef<CFURLRef> url( + CFURLCreateFromFSRef(kCFAllocatorDefault, &ref)); + NSString *path_string = [(NSURL *)url.get() path]; + return [path_string fileSystemRepresentation]; +} + +bool FSRefFromPath(const std::string& path, FSRef* ref) { + OSStatus status = FSPathMakeRef((const UInt8*)path.c_str(), + ref, nil); + return status == noErr; +} + +static bool g_override_am_i_bundled = false; +static bool g_override_am_i_bundled_value = false; + +// Adapted from http://developer.apple.com/carbon/tipsandtricks.html#AmIBundled +static bool UncachedAmIBundled() { + if (g_override_am_i_bundled) + return g_override_am_i_bundled_value; + + ProcessSerialNumber psn = {0, kCurrentProcess}; + + FSRef fsref; + OSStatus pbErr; + if ((pbErr = GetProcessBundleLocation(&psn, &fsref)) != noErr) { + LOG(ERROR) << "GetProcessBundleLocation failed: error " << pbErr; + return false; + } + + FSCatalogInfo info; + OSErr fsErr; + if ((fsErr = FSGetCatalogInfo(&fsref, kFSCatInfoNodeFlags, &info, + NULL, NULL, NULL)) != noErr) { + LOG(ERROR) << "FSGetCatalogInfo failed: error " << fsErr; + return false; + } + + return info.nodeFlags & kFSNodeIsDirectoryMask; +} + +bool AmIBundled() { + // If the return value is not cached, this function will return different + // values depending on when it's called. This confuses some client code, see + // http://crbug.com/63183 . + static bool result = UncachedAmIBundled(); + DCHECK_EQ(result, UncachedAmIBundled()) + << "The return value of AmIBundled() changed. This will confuse tests. " + << "Call SetAmIBundled() override manually if your test binary " + << "delay-loads the framework."; + return result; +} + +void SetOverrideAmIBundled(bool value) { + g_override_am_i_bundled = true; + g_override_am_i_bundled_value = value; +} + +bool IsBackgroundOnlyProcess() { + // This function really does want to examine NSBundle's idea of the main + // bundle dictionary, and not the overriden MainAppBundle. It needs to look + // at the actual running .app's Info.plist to access its LSUIElement + // property. + NSDictionary* info_dictionary = [[NSBundle mainBundle] infoDictionary]; + return [[info_dictionary objectForKey:@"LSUIElement"] boolValue] != NO; +} + +// No threading worries since NSBundle isn't thread safe. +static NSBundle* g_override_app_bundle = nil; + +NSBundle* MainAppBundle() { + if (g_override_app_bundle) + return g_override_app_bundle; + return [NSBundle mainBundle]; +} + +FilePath MainAppBundlePath() { + NSBundle* bundle = MainAppBundle(); + return FilePath([[bundle bundlePath] fileSystemRepresentation]); +} + +void SetOverrideAppBundle(NSBundle* bundle) { + if (bundle != g_override_app_bundle) { + [g_override_app_bundle release]; + g_override_app_bundle = [bundle retain]; + } +} + +void SetOverrideAppBundlePath(const FilePath& file_path) { + NSString* path = base::SysUTF8ToNSString(file_path.value()); + NSBundle* bundle = [NSBundle bundleWithPath:path]; + CHECK(bundle) << "Failed to load the bundle at " << file_path.value(); + + SetOverrideAppBundle(bundle); +} + +OSType CreatorCodeForCFBundleRef(CFBundleRef bundle) { + OSType creator = kUnknownType; + CFBundleGetPackageInfo(bundle, NULL, &creator); + return creator; +} + +OSType CreatorCodeForApplication() { + CFBundleRef bundle = CFBundleGetMainBundle(); + if (!bundle) + return kUnknownType; + + return CreatorCodeForCFBundleRef(bundle); +} + +bool GetSearchPathDirectory(NSSearchPathDirectory directory, + NSSearchPathDomainMask domain_mask, + FilePath* result) { + DCHECK(result); + NSArray* dirs = + NSSearchPathForDirectoriesInDomains(directory, domain_mask, YES); + if ([dirs count] < 1) { + return false; + } + NSString* path = [dirs objectAtIndex:0]; + *result = FilePath([path fileSystemRepresentation]); + return true; +} + +bool GetLocalDirectory(NSSearchPathDirectory directory, FilePath* result) { + return GetSearchPathDirectory(directory, NSLocalDomainMask, result); +} + +bool GetUserDirectory(NSSearchPathDirectory directory, FilePath* result) { + return GetSearchPathDirectory(directory, NSUserDomainMask, result); +} + +FilePath GetUserLibraryPath() { + FilePath user_library_path; + if (!GetUserDirectory(NSLibraryDirectory, &user_library_path)) { + LOG(WARNING) << "Could not get user library path"; + } + return user_library_path; +} + +CGColorSpaceRef GetSRGBColorSpace() { + // Leaked. That's OK, it's scoped to the lifetime of the application. + static CGColorSpaceRef g_color_space_sRGB = + CGColorSpaceCreateWithName(kCGColorSpaceSRGB); + LOG_IF(ERROR, !g_color_space_sRGB) << "Couldn't get the sRGB color space"; + return g_color_space_sRGB; +} + +CGColorSpaceRef GetSystemColorSpace() { + // Leaked. That's OK, it's scoped to the lifetime of the application. + // Try to get the main display's color space. + static CGColorSpaceRef g_system_color_space = + CGDisplayCopyColorSpace(CGMainDisplayID()); + + if (!g_system_color_space) { + // Use a generic RGB color space. This is better than nothing. + g_system_color_space = CGColorSpaceCreateDeviceRGB(); + + if (g_system_color_space) { + LOG(WARNING) << + "Couldn't get the main display's color space, using generic"; + } else { + LOG(ERROR) << "Couldn't get any color space"; + } + } + + return g_system_color_space; +} + +// Add a request for full screen mode. Must be called on the main thread. +void RequestFullScreen(FullScreenMode mode) { + DCHECK_LT(mode, kNumFullScreenModes); + if (mode >= kNumFullScreenModes) + return; + + DCHECK_GE(g_full_screen_requests[mode], 0); + g_full_screen_requests[mode] = std::max(g_full_screen_requests[mode] + 1, 1); + SetUIMode(); +} + +// Release a request for full screen mode. Must be called on the main thread. +void ReleaseFullScreen(FullScreenMode mode) { + DCHECK_LT(mode, kNumFullScreenModes); + if (mode >= kNumFullScreenModes) + return; + + DCHECK_GT(g_full_screen_requests[mode], 0); + g_full_screen_requests[mode] = std::max(g_full_screen_requests[mode] - 1, 0); + SetUIMode(); +} + +// Switches full screen modes. Releases a request for |from_mode| and adds a +// new request for |to_mode|. Must be called on the main thread. +void SwitchFullScreenModes(FullScreenMode from_mode, FullScreenMode to_mode) { + DCHECK_LT(from_mode, kNumFullScreenModes); + DCHECK_LT(to_mode, kNumFullScreenModes); + if (from_mode >= kNumFullScreenModes || to_mode >= kNumFullScreenModes) + return; + + DCHECK_GT(g_full_screen_requests[from_mode], 0); + DCHECK_GE(g_full_screen_requests[to_mode], 0); + g_full_screen_requests[from_mode] = + std::max(g_full_screen_requests[from_mode] - 1, 0); + g_full_screen_requests[to_mode] = + std::max(g_full_screen_requests[to_mode] + 1, 1); + SetUIMode(); +} + +void SetCursorVisibility(bool visible) { + if (visible) + [NSCursor unhide]; + else + [NSCursor hide]; +} + +bool ShouldWindowsMiniaturizeOnDoubleClick() { + // We use an undocumented method in Cocoa; if it doesn't exist, default to + // |true|. If it ever goes away, we can do (using an undocumented pref key): + // NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + // return ![defaults objectForKey:@"AppleMiniaturizeOnDoubleClick"] || + // [defaults boolForKey:@"AppleMiniaturizeOnDoubleClick"]; + BOOL methodImplemented = + [NSWindow respondsToSelector:@selector(_shouldMiniaturizeOnDoubleClick)]; + DCHECK(methodImplemented); + return !methodImplemented || + [NSWindow performSelector:@selector(_shouldMiniaturizeOnDoubleClick)]; +} + +void GrabWindowSnapshot(NSWindow* window, + std::vector<unsigned char>* png_representation, + int* width, int* height) { + // Make sure to grab the "window frame" view so we get current tab + + // tabstrip. + NSView* view = [[window contentView] superview]; + NSBitmapImageRep* rep = + [view bitmapImageRepForCachingDisplayInRect:[view bounds]]; + [view cacheDisplayInRect:[view bounds] toBitmapImageRep:rep]; + NSData* data = [rep representationUsingType:NSPNGFileType properties:nil]; + const unsigned char* buf = static_cast<const unsigned char*>([data bytes]); + NSUInteger length = [data length]; + if (buf != NULL && length > 0){ + *width = static_cast<int>([rep pixelsWide]); + *height = static_cast<int>([rep pixelsHigh]); + png_representation->assign(buf, buf + length); + DCHECK(png_representation->size() > 0); + } +} + +void ActivateProcess(pid_t pid) { + ProcessSerialNumber process; + OSStatus status = GetProcessForPID(pid, &process); + if (status == noErr) { + SetFrontProcess(&process); + } else { + LOG(WARNING) << "Unable to get process for pid " << pid; + } +} + +// Takes a path to an (executable) binary and tries to provide the path to an +// application bundle containing it. It takes the outermost bundle that it can +// find (so for "/Foo/Bar.app/.../Baz.app/..." it produces "/Foo/Bar.app"). +// |exec_name| - path to the binary +// returns - path to the application bundle, or empty on error +FilePath GetAppBundlePath(const FilePath& exec_name) { + const char kExt[] = ".app"; + const size_t kExtLength = arraysize(kExt) - 1; + + // Split the path into components. + std::vector<std::string> components; + exec_name.GetComponents(&components); + + // It's an error if we don't get any components. + if (!components.size()) + return FilePath(); + + // Don't prepend '/' to the first component. + std::vector<std::string>::const_iterator it = components.begin(); + std::string bundle_name = *it; + DCHECK(it->length() > 0); + // If the first component ends in ".app", we're already done. + if (it->length() > kExtLength && + !it->compare(it->length() - kExtLength, kExtLength, kExt, kExtLength)) + return FilePath(bundle_name); + + // The first component may be "/" or "//", etc. Only append '/' if it doesn't + // already end in '/'. + if (bundle_name[bundle_name.length() - 1] != '/') + bundle_name += '/'; + + // Go through the remaining components. + for (++it; it != components.end(); ++it) { + DCHECK(it->length() > 0); + + bundle_name += *it; + + // If the current component ends in ".app", we're done. + if (it->length() > kExtLength && + !it->compare(it->length() - kExtLength, kExtLength, kExt, kExtLength)) + return FilePath(bundle_name); + + // Separate this component from the next one. + bundle_name += '/'; + } + + return FilePath(); +} + +bool SetFileBackupExclusion(const FilePath& file_path, bool exclude) { + NSString* filePath = + [NSString stringWithUTF8String:file_path.value().c_str()]; + + // If being asked to exclude something in a tmp directory, just lie and say it + // was done. TimeMachine will already ignore tmp directories. This keeps the + // temporary profiles used by unittests from being added to the exclude list. + // Otherwise, as /Library/Preferences/com.apple.TimeMachine.plist grows the + // bots slow down due to reading/writing all the temporary profiles used over + // time. + + NSString* tmpDir = NSTemporaryDirectory(); + // Make sure the temp dir is terminated with a slash + if (tmpDir && ![tmpDir hasSuffix:@"/"]) + tmpDir = [tmpDir stringByAppendingString:@"/"]; + // '/var' is a link to '/private/var', make sure to check both forms. + NSString* privateTmpDir = nil; + if ([tmpDir hasPrefix:@"/var/"]) + privateTmpDir = [@"/private" stringByAppendingString:tmpDir]; + + if ((tmpDir && [filePath hasPrefix:tmpDir]) || + (privateTmpDir && [filePath hasPrefix:privateTmpDir]) || + [filePath hasPrefix:@"/tmp/"] || + [filePath hasPrefix:@"/var/tmp/"] || + [filePath hasPrefix:@"/private/tmp/"] || + [filePath hasPrefix:@"/private/var/tmp/"]) { + return true; + } + + NSURL* url = [NSURL fileURLWithPath:filePath]; + // Note that we always set CSBackupSetItemExcluded's excludeByPath param + // to true. This prevents a problem with toggling the setting: if the file + // is excluded with excludeByPath set to true then excludeByPath must + // also be true when un-excluding the file, otherwise the un-excluding + // will be ignored. + bool success = + CSBackupSetItemExcluded((CFURLRef)url, exclude, true) == noErr; + if (!success) + LOG(WARNING) << "Failed to set backup excluson for file '" + << file_path.value().c_str() << "'. Continuing."; + return success; +} + +CFTypeRef GetValueFromDictionary(CFDictionaryRef dict, + CFStringRef key, + CFTypeID expected_type) { + CFTypeRef value = CFDictionaryGetValue(dict, key); + if (!value) + return value; + + if (CFGetTypeID(value) != expected_type) { + ScopedCFTypeRef<CFStringRef> expected_type_ref( + CFCopyTypeIDDescription(expected_type)); + ScopedCFTypeRef<CFStringRef> actual_type_ref( + CFCopyTypeIDDescription(CFGetTypeID(value))); + LOG(WARNING) << "Expected value for key " + << base::SysCFStringRefToUTF8(key) + << " to be " + << base::SysCFStringRefToUTF8(expected_type_ref) + << " but it was " + << base::SysCFStringRefToUTF8(actual_type_ref) + << " instead"; + return NULL; + } + + return value; +} + +void SetProcessName(CFStringRef process_name) { + if (!process_name || CFStringGetLength(process_name) == 0) { + NOTREACHED() << "SetProcessName given bad name."; + return; + } + + if (![NSThread isMainThread]) { + NOTREACHED() << "Should only set process name from main thread."; + return; + } + + // Warning: here be dragons! This is SPI reverse-engineered from WebKit's + // plugin host, and could break at any time (although realistically it's only + // likely to break in a new major release). + // When 10.7 is available, check that this still works, and update this + // comment for 10.8. + + // Private CFType used in these LaunchServices calls. + typedef CFTypeRef PrivateLSASN; + typedef PrivateLSASN (*LSGetCurrentApplicationASNType)(); + typedef OSStatus (*LSSetApplicationInformationItemType)(int, PrivateLSASN, + CFStringRef, + CFStringRef, + CFDictionaryRef*); + + static LSGetCurrentApplicationASNType ls_get_current_application_asn_func = + NULL; + static LSSetApplicationInformationItemType + ls_set_application_information_item_func = NULL; + static CFStringRef ls_display_name_key = NULL; + + static bool did_symbol_lookup = false; + if (!did_symbol_lookup) { + did_symbol_lookup = true; + CFBundleRef launch_services_bundle = + CFBundleGetBundleWithIdentifier(CFSTR("com.apple.LaunchServices")); + if (!launch_services_bundle) { + LOG(ERROR) << "Failed to look up LaunchServices bundle"; + return; + } + + ls_get_current_application_asn_func = + reinterpret_cast<LSGetCurrentApplicationASNType>( + CFBundleGetFunctionPointerForName( + launch_services_bundle, CFSTR("_LSGetCurrentApplicationASN"))); + if (!ls_get_current_application_asn_func) + LOG(ERROR) << "Could not find _LSGetCurrentApplicationASN"; + + ls_set_application_information_item_func = + reinterpret_cast<LSSetApplicationInformationItemType>( + CFBundleGetFunctionPointerForName( + launch_services_bundle, + CFSTR("_LSSetApplicationInformationItem"))); + if (!ls_set_application_information_item_func) + LOG(ERROR) << "Could not find _LSSetApplicationInformationItem"; + + CFStringRef* key_pointer = reinterpret_cast<CFStringRef*>( + CFBundleGetDataPointerForName(launch_services_bundle, + CFSTR("_kLSDisplayNameKey"))); + ls_display_name_key = key_pointer ? *key_pointer : NULL; + if (!ls_display_name_key) + LOG(ERROR) << "Could not find _kLSDisplayNameKey"; + + // Internally, this call relies on the Mach ports that are started up by the + // Carbon Process Manager. In debug builds this usually happens due to how + // the logging layers are started up; but in release, it isn't started in as + // much of a defined order. So if the symbols had to be loaded, go ahead + // and force a call to make sure the manager has been initialized and hence + // the ports are opened. + ProcessSerialNumber psn; + GetCurrentProcess(&psn); + } + if (!ls_get_current_application_asn_func || + !ls_set_application_information_item_func || + !ls_display_name_key) { + return; + } + + PrivateLSASN asn = ls_get_current_application_asn_func(); + // Constant used by WebKit; what exactly it means is unknown. + const int magic_session_constant = -2; + OSErr err = + ls_set_application_information_item_func(magic_session_constant, asn, + ls_display_name_key, + process_name, + NULL /* optional out param */); + LOG_IF(ERROR, err) << "Call to set process name failed, err " << err; +} + +// Converts a NSImage to a CGImageRef. Normally, the system frameworks can do +// this fine, especially on 10.6. On 10.5, however, CGImage cannot handle +// converting a PDF-backed NSImage into a CGImageRef. This function will +// rasterize the PDF into a bitmap CGImage. The caller is responsible for +// releasing the return value. +CGImageRef CopyNSImageToCGImage(NSImage* image) { + // This is based loosely on http://www.cocoadev.com/index.pl?CGImageRef . + NSSize size = [image size]; + ScopedCFTypeRef<CGContextRef> context( + CGBitmapContextCreate(NULL, // Allow CG to allocate memory. + size.width, + size.height, + 8, // bitsPerComponent + 0, // bytesPerRow - CG will calculate by default. + [[NSColorSpace genericRGBColorSpace] CGColorSpace], + kCGBitmapByteOrder32Host | + kCGImageAlphaPremultipliedFirst)); + if (!context.get()) + return NULL; + + [NSGraphicsContext saveGraphicsState]; + [NSGraphicsContext setCurrentContext: + [NSGraphicsContext graphicsContextWithGraphicsPort:context.get() + flipped:NO]]; + [image drawInRect:NSMakeRect(0,0, size.width, size.height) + fromRect:NSZeroRect + operation:NSCompositeCopy + fraction:1.0]; + [NSGraphicsContext restoreGraphicsState]; + + return CGBitmapContextCreateImage(context); +} + +bool CheckLoginItemStatus(bool* is_hidden) { + ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp()); + if (!item.get()) + return false; + + if (is_hidden) + *is_hidden = IsHiddenLoginItem(item); + + return true; +} + +void AddToLoginItems(bool hide_on_startup) { + ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp()); + if (item.get() && (IsHiddenLoginItem(item) == hide_on_startup)) { + return; // Already is a login item with required hide flag. + } + + ScopedCFTypeRef<LSSharedFileListRef> login_items(LSSharedFileListCreate( + NULL, kLSSharedFileListSessionLoginItems, NULL)); + + if (!login_items.get()) { + LOG(ERROR) << "Couldn't get a Login Items list."; + return; + } + + // Remove the old item, it has wrong hide flag, we'll create a new one. + if (item.get()) { + LSSharedFileListItemRemove(login_items, item); + } + + NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; + + BOOL hide = hide_on_startup ? YES : NO; + NSDictionary* properties = + [NSDictionary + dictionaryWithObject:[NSNumber numberWithBool:hide] + forKey:(NSString*)kLSSharedFileListLoginItemHidden]; + + ScopedCFTypeRef<LSSharedFileListItemRef> new_item; + new_item.reset(LSSharedFileListInsertItemURL( + login_items, kLSSharedFileListItemLast, NULL, NULL, + reinterpret_cast<CFURLRef>(url), + reinterpret_cast<CFDictionaryRef>(properties), NULL)); + + if (!new_item.get()) { + LOG(ERROR) << "Couldn't insert current app into Login Items list."; + } +} + +void RemoveFromLoginItems() { + ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp()); + if (!item.get()) + return; + + ScopedCFTypeRef<LSSharedFileListRef> login_items(LSSharedFileListCreate( + NULL, kLSSharedFileListSessionLoginItems, NULL)); + + if (!login_items.get()) { + LOG(ERROR) << "Couldn't get a Login Items list."; + return; + } + + LSSharedFileListItemRemove(login_items, item); +} + +bool WasLaunchedAsHiddenLoginItem() { + if (!WasLaunchedAsLoginItem()) + return false; + + ScopedCFTypeRef<LSSharedFileListItemRef> item(GetLoginItemForApp()); + if (!item.get()) { + LOG(ERROR) << "Process launched at Login but can't access Login Item List."; + return false; + } + return IsHiddenLoginItem(item); +} + +void NSObjectRetain(void* obj) { + id<NSObject> nsobj = static_cast<id<NSObject> >(obj); + [nsobj retain]; +} + +void NSObjectRelease(void* obj) { + id<NSObject> nsobj = static_cast<id<NSObject> >(obj); + [nsobj release]; +} + +} // namespace mac +} // namespace base diff --git a/base/mac/mac_util_unittest.mm b/base/mac/mac_util_unittest.mm new file mode 100644 index 0000000..47ecebf --- /dev/null +++ b/base/mac/mac_util_unittest.mm @@ -0,0 +1,197 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import <Cocoa/Cocoa.h> +#include <vector> + +#include "base/mac/mac_util.h" + +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/mac/scoped_cftyperef.h" +#include "base/test/mock_chrome_application_mac.h" +#include "base/scoped_nsobject.h" +#include "base/scoped_ptr.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/platform_test.h" + +namespace base { +namespace mac { + +namespace { + +typedef PlatformTest MacUtilTest; + +TEST_F(MacUtilTest, TestFSRef) { + FSRef ref; + std::string path("/System/Library"); + + ASSERT_TRUE(FSRefFromPath(path, &ref)); + EXPECT_EQ(path, PathFromFSRef(ref)); +} + +TEST_F(MacUtilTest, GetUserDirectoryTest) { + // Try a few keys, make sure they come back with non-empty paths. + FilePath caches_dir; + EXPECT_TRUE(GetUserDirectory(NSCachesDirectory, &caches_dir)); + EXPECT_FALSE(caches_dir.empty()); + + FilePath application_support_dir; + EXPECT_TRUE(GetUserDirectory(NSApplicationSupportDirectory, + &application_support_dir)); + EXPECT_FALSE(application_support_dir.empty()); + + FilePath library_dir; + EXPECT_TRUE(GetUserDirectory(NSLibraryDirectory, &library_dir)); + EXPECT_FALSE(library_dir.empty()); +} + +TEST_F(MacUtilTest, TestLibraryPath) { + FilePath library_dir = GetUserLibraryPath(); + // Make sure the string isn't empty. + EXPECT_FALSE(library_dir.value().empty()); +} + +TEST_F(MacUtilTest, TestGrabWindowSnapshot) { + // Launch a test window so we can take a snapshot. + [MockCrApp sharedApplication]; + NSRect frame = NSMakeRect(0, 0, 400, 400); + scoped_nsobject<NSWindow> window( + [[NSWindow alloc] initWithContentRect:frame + styleMask:NSBorderlessWindowMask + backing:NSBackingStoreBuffered + defer:NO]); + [window setBackgroundColor:[NSColor whiteColor]]; + [window makeKeyAndOrderFront:NSApp]; + + scoped_ptr<std::vector<unsigned char> > png_representation( + new std::vector<unsigned char>); + int width, height; + GrabWindowSnapshot(window, png_representation.get(), + &width, &height); + + // Copy png back into NSData object so we can make sure we grabbed a png. + scoped_nsobject<NSData> image_data( + [[NSData alloc] initWithBytes:&(*png_representation)[0] + length:png_representation->size()]); + NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:image_data.get()]; + EXPECT_TRUE([rep isKindOfClass:[NSBitmapImageRep class]]); + EXPECT_TRUE(CGImageGetWidth([rep CGImage]) == 400); + NSColor* color = [rep colorAtX:200 y:200]; + CGFloat red = 0, green = 0, blue = 0, alpha = 0; + [color getRed:&red green:&green blue:&blue alpha:&alpha]; + EXPECT_GE(red + green + blue, 3.0); +} + +TEST_F(MacUtilTest, TestGetAppBundlePath) { + FilePath out; + + // Make sure it doesn't crash. + out = GetAppBundlePath(FilePath()); + EXPECT_TRUE(out.empty()); + + // Some more invalid inputs. + const char* invalid_inputs[] = { + "/", "/foo", "foo", "/foo/bar.", "foo/bar.", "/foo/bar./bazquux", + "foo/bar./bazquux", "foo/.app", "//foo", + }; + for (size_t i = 0; i < arraysize(invalid_inputs); i++) { + out = GetAppBundlePath(FilePath(invalid_inputs[i])); + EXPECT_TRUE(out.empty()) << "loop: " << i; + } + + // Some valid inputs; this and |expected_outputs| should be in sync. + struct { + const char *in; + const char *expected_out; + } valid_inputs[] = { + { "FooBar.app/", "FooBar.app" }, + { "/FooBar.app", "/FooBar.app" }, + { "/FooBar.app/", "/FooBar.app" }, + { "//FooBar.app", "//FooBar.app" }, + { "/Foo/Bar.app", "/Foo/Bar.app" }, + { "/Foo/Bar.app/", "/Foo/Bar.app" }, + { "/F/B.app", "/F/B.app" }, + { "/F/B.app/", "/F/B.app" }, + { "/Foo/Bar.app/baz", "/Foo/Bar.app" }, + { "/Foo/Bar.app/baz/", "/Foo/Bar.app" }, + { "/Foo/Bar.app/baz/quux.app/quuux", "/Foo/Bar.app" }, + { "/Applications/Google Foo.app/bar/Foo Helper.app/quux/Foo Helper", + "/Applications/Google Foo.app" }, + }; + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid_inputs); i++) { + out = GetAppBundlePath(FilePath(valid_inputs[i].in)); + EXPECT_FALSE(out.empty()) << "loop: " << i; + EXPECT_STREQ(valid_inputs[i].expected_out, + out.value().c_str()) << "loop: " << i; + } +} + +TEST_F(MacUtilTest, TestExcludeFileFromBackups) { + NSString* homeDirectory = NSHomeDirectory(); + NSString* dummyFilePath = + [homeDirectory stringByAppendingPathComponent:@"DummyFile"]; + const char* dummy_file_path = [dummyFilePath fileSystemRepresentation]; + ASSERT_TRUE(dummy_file_path); + FilePath file_path(dummy_file_path); + // It is not actually necessary to have a physical file in order to + // set its exclusion property. + NSURL* fileURL = [NSURL URLWithString:dummyFilePath]; + // Reset the exclusion in case it was set previously. + SetFileBackupExclusion(file_path, false); + Boolean excludeByPath; + // Initial state should be non-excluded. + EXPECT_FALSE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath)); + // Exclude the file. + EXPECT_TRUE(SetFileBackupExclusion(file_path, true)); + EXPECT_TRUE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath)); + // Un-exclude the file. + EXPECT_TRUE(SetFileBackupExclusion(file_path, false)); + EXPECT_FALSE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath)); +} + +TEST_F(MacUtilTest, TestGetValueFromDictionary) { + ScopedCFTypeRef<CFMutableDictionaryRef> dict( + CFDictionaryCreateMutable(0, 0, + &kCFTypeDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks)); + CFDictionarySetValue(dict.get(), CFSTR("key"), CFSTR("value")); + + EXPECT_TRUE(CFEqual(CFSTR("value"), + GetValueFromDictionary( + dict, CFSTR("key"), CFStringGetTypeID()))); + EXPECT_FALSE(GetValueFromDictionary(dict, CFSTR("key"), CFNumberGetTypeID())); + EXPECT_FALSE(GetValueFromDictionary( + dict, CFSTR("no-exist"), CFStringGetTypeID())); +} + +TEST_F(MacUtilTest, CopyNSImageToCGImage) { + scoped_nsobject<NSImage> nsImage( + [[NSImage alloc] initWithSize:NSMakeSize(20, 20)]); + [nsImage lockFocus]; + [[NSColor redColor] set]; + NSRect rect = NSZeroRect; + rect.size = [nsImage size]; + NSRectFill(rect); + [nsImage unlockFocus]; + + ScopedCFTypeRef<CGImageRef> cgImage(CopyNSImageToCGImage(nsImage.get())); + EXPECT_TRUE(cgImage.get()); +} + +TEST_F(MacUtilTest, NSObjectRetainRelease) { + scoped_nsobject<NSArray> array([[NSArray alloc] initWithObjects:@"foo", nil]); + EXPECT_EQ(1U, [array retainCount]); + + NSObjectRetain(array); + EXPECT_EQ(2U, [array retainCount]); + + NSObjectRelease(array); + EXPECT_EQ(1U, [array retainCount]); +} + +} // namespace + +} // namespace mac +} // namespace base |