Home | History | Annotate | Download | only in mac
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef BASE_MAC_MAC_UTIL_H_
      6 #define BASE_MAC_MAC_UTIL_H_
      7 #pragma once
      8 
      9 #include <Carbon/Carbon.h>
     10 #include <string>
     11 
     12 #include "base/logging.h"
     13 
     14 // TODO(rohitrao): Clean up sites that include mac_util.h and remove this line.
     15 #include "base/mac/foundation_util.h"
     16 
     17 #if defined(__OBJC__)
     18 #import <Foundation/Foundation.h>
     19 #else  // __OBJC__
     20 class NSImage;
     21 #endif  // __OBJC__
     22 
     23 class FilePath;
     24 
     25 namespace base {
     26 namespace mac {
     27 
     28 // Full screen modes, in increasing order of priority.  More permissive modes
     29 // take predecence.
     30 enum FullScreenMode {
     31   kFullScreenModeHideAll = 0,
     32   kFullScreenModeHideDock = 1,
     33   kFullScreenModeAutoHideAll = 2,
     34   kNumFullScreenModes = 3,
     35 
     36   // kFullScreenModeNormal is not a valid FullScreenMode, but it is useful to
     37   // other classes, so we include it here.
     38   kFullScreenModeNormal = 10,
     39 };
     40 
     41 std::string PathFromFSRef(const FSRef& ref);
     42 bool FSRefFromPath(const std::string& path, FSRef* ref);
     43 
     44 // Returns an sRGB color space.  The return value is a static value; do not
     45 // release it!
     46 CGColorSpaceRef GetSRGBColorSpace();
     47 
     48 // Returns the color space being used by the main display.  The return value
     49 // is a static value; do not release it!
     50 CGColorSpaceRef GetSystemColorSpace();
     51 
     52 // Add a full screen request for the given |mode|.  Must be paired with a
     53 // ReleaseFullScreen() call for the same |mode|.  This does not by itself create
     54 // a fullscreen window; rather, it manages per-application state related to
     55 // hiding the dock and menubar.  Must be called on the main thread.
     56 void RequestFullScreen(FullScreenMode mode);
     57 
     58 // Release a request for full screen mode.  Must be matched with a
     59 // RequestFullScreen() call for the same |mode|.  As with RequestFullScreen(),
     60 // this does not affect windows directly, but rather manages per-application
     61 // state.  For example, if there are no other outstanding
     62 // |kFullScreenModeAutoHideAll| requests, this will reshow the menu bar.  Must
     63 // be called on main thread.
     64 void ReleaseFullScreen(FullScreenMode mode);
     65 
     66 // Convenience method to switch the current fullscreen mode.  This has the same
     67 // net effect as a ReleaseFullScreen(from_mode) call followed immediately by a
     68 // RequestFullScreen(to_mode).  Must be called on the main thread.
     69 void SwitchFullScreenModes(FullScreenMode from_mode, FullScreenMode to_mode);
     70 
     71 // Set the visibility of the cursor.
     72 void SetCursorVisibility(bool visible);
     73 
     74 // Should windows miniaturize on a double-click (on the title bar)?
     75 bool ShouldWindowsMiniaturizeOnDoubleClick();
     76 
     77 // Activates the process with the given PID.
     78 void ActivateProcess(pid_t pid);
     79 
     80 // Set the Time Machine exclusion property for the given file.
     81 bool SetFileBackupExclusion(const FilePath& file_path, bool exclude);
     82 
     83 // Sets the process name as displayed in Activity Monitor to process_name.
     84 void SetProcessName(CFStringRef process_name);
     85 
     86 // Converts a NSImage to a CGImageRef.  Normally, the system frameworks can do
     87 // this fine, especially on 10.6.  On 10.5, however, CGImage cannot handle
     88 // converting a PDF-backed NSImage into a CGImageRef.  This function will
     89 // rasterize the PDF into a bitmap CGImage.  The caller is responsible for
     90 // releasing the return value.
     91 CGImageRef CopyNSImageToCGImage(NSImage* image);
     92 
     93 // Checks if the current application is set as a Login Item, so it will launch
     94 // on Login. If a non-NULL pointer to is_hidden is passed, the Login Item also
     95 // is queried for the 'hide on launch' flag.
     96 bool CheckLoginItemStatus(bool* is_hidden);
     97 
     98 // Adds current application to the set of Login Items with specified "hide"
     99 // flag. This has the same effect as adding/removing the application in
    100 // SystemPreferences->Accounts->LoginItems or marking Application in the Dock
    101 // as "Options->Open on Login".
    102 // Does nothing if the application is already set up as Login Item with
    103 // specified hide flag.
    104 void AddToLoginItems(bool hide_on_startup);
    105 
    106 // Removes the current application from the list Of Login Items.
    107 void RemoveFromLoginItems();
    108 
    109 // Returns true if the current process was automatically launched as a
    110 // 'Login Item' with 'hide on startup' flag. Used to suppress opening windows.
    111 bool WasLaunchedAsHiddenLoginItem();
    112 
    113 }  // namespace mac
    114 }  // namespace base
    115 
    116 #if !defined(__OBJC__)
    117 #define OBJC_CPP_CLASS_DECL(x) class x;
    118 #else  // __OBJC__
    119 #define OBJC_CPP_CLASS_DECL(x)
    120 #endif  // __OBJC__
    121 
    122 // Convert toll-free bridged CFTypes to NSTypes and vice-versa. This does not
    123 // autorelease |cf_val|. This is useful for the case where there is a CFType in
    124 // a call that expects an NSType and the compiler is complaining about const
    125 // casting problems.
    126 // The calls are used like this:
    127 // NSString *foo = CFToNSCast(CFSTR("Hello"));
    128 // CFStringRef foo2 = NSToCFCast(@"Hello");
    129 // The macro magic below is to enforce safe casting. It could possibly have
    130 // been done using template function specialization, but template function
    131 // specialization doesn't always work intuitively,
    132 // (http://www.gotw.ca/publications/mill17.htm) so the trusty combination
    133 // of macros and function overloading is used instead.
    134 
    135 #define CF_TO_NS_CAST_DECL(TypeCF, TypeNS) \
    136 OBJC_CPP_CLASS_DECL(TypeNS) \
    137 \
    138 namespace base { \
    139 namespace mac { \
    140 TypeNS* CFToNSCast(TypeCF##Ref cf_val); \
    141 TypeCF##Ref NSToCFCast(TypeNS* ns_val); \
    142 } \
    143 } \
    144 
    145 #define CF_TO_NS_MUTABLE_CAST_DECL(name) \
    146 CF_TO_NS_CAST_DECL(CF##name, NS##name) \
    147 OBJC_CPP_CLASS_DECL(NSMutable##name) \
    148 \
    149 namespace base { \
    150 namespace mac { \
    151 NSMutable##name* CFToNSCast(CFMutable##name##Ref cf_val); \
    152 CFMutable##name##Ref NSToCFCast(NSMutable##name* ns_val); \
    153 } \
    154 } \
    155 
    156 // List of toll-free bridged types taken from:
    157 // http://www.cocoadev.com/index.pl?TollFreeBridged
    158 
    159 CF_TO_NS_MUTABLE_CAST_DECL(Array);
    160 CF_TO_NS_MUTABLE_CAST_DECL(AttributedString);
    161 CF_TO_NS_CAST_DECL(CFCalendar, NSCalendar);
    162 CF_TO_NS_MUTABLE_CAST_DECL(CharacterSet);
    163 CF_TO_NS_MUTABLE_CAST_DECL(Data);
    164 CF_TO_NS_CAST_DECL(CFDate, NSDate);
    165 CF_TO_NS_MUTABLE_CAST_DECL(Dictionary);
    166 CF_TO_NS_CAST_DECL(CFError, NSError);
    167 CF_TO_NS_CAST_DECL(CFLocale, NSLocale);
    168 CF_TO_NS_CAST_DECL(CFNumber, NSNumber);
    169 CF_TO_NS_CAST_DECL(CFRunLoopTimer, NSTimer);
    170 CF_TO_NS_CAST_DECL(CFTimeZone, NSTimeZone);
    171 CF_TO_NS_MUTABLE_CAST_DECL(Set);
    172 CF_TO_NS_CAST_DECL(CFReadStream, NSInputStream);
    173 CF_TO_NS_CAST_DECL(CFWriteStream, NSOutputStream);
    174 CF_TO_NS_MUTABLE_CAST_DECL(String);
    175 CF_TO_NS_CAST_DECL(CFURL, NSURL);
    176 
    177 // Stream operations for CFTypes. They can be used with NSTypes as well
    178 // by using the NSToCFCast methods above.
    179 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo");
    180 // Operator << can not be overloaded for ObjectiveC types as the compiler
    181 // can not distinguish between overloads for id with overloads for void*.
    182 extern std::ostream& operator<<(std::ostream& o, const CFErrorRef err);
    183 extern std::ostream& operator<<(std::ostream& o, const CFStringRef str);
    184 
    185 #endif  // BASE_MAC_MAC_UTIL_H_
    186