Home | History | Annotate | Download | only in mac
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef BASE_MAC_MAC_UTIL_H_
      6 #define BASE_MAC_MAC_UTIL_H_
      7 
      8 #include <AvailabilityMacros.h>
      9 #include <Carbon/Carbon.h>
     10 #include <string>
     11 
     12 #include "base/base_export.h"
     13 #include "base/logging.h"
     14 
     15 // TODO(rohitrao): Clean up sites that include mac_util.h and remove this line.
     16 #include "base/mac/foundation_util.h"
     17 
     18 #if defined(__OBJC__)
     19 #import <Foundation/Foundation.h>
     20 #else  // __OBJC__
     21 class NSImage;
     22 #endif  // __OBJC__
     23 
     24 namespace base {
     25 
     26 class FilePath;
     27 
     28 namespace mac {
     29 
     30 // Full screen modes, in increasing order of priority.  More permissive modes
     31 // take predecence.
     32 enum FullScreenMode {
     33   kFullScreenModeHideAll = 0,
     34   kFullScreenModeHideDock = 1,
     35   kFullScreenModeAutoHideAll = 2,
     36   kNumFullScreenModes = 3,
     37 
     38   // kFullScreenModeNormal is not a valid FullScreenMode, but it is useful to
     39   // other classes, so we include it here.
     40   kFullScreenModeNormal = 10,
     41 };
     42 
     43 BASE_EXPORT std::string PathFromFSRef(const FSRef& ref);
     44 BASE_EXPORT bool FSRefFromPath(const std::string& path, FSRef* ref);
     45 
     46 // Returns an sRGB color space.  The return value is a static value; do not
     47 // release it!
     48 BASE_EXPORT CGColorSpaceRef GetSRGBColorSpace();
     49 
     50 // Returns the generic RGB color space. The return value is a static value; do
     51 // not release it!
     52 BASE_EXPORT CGColorSpaceRef GetGenericRGBColorSpace();
     53 
     54 // Returns the color space being used by the main display.  The return value
     55 // is a static value; do not release it!
     56 BASE_EXPORT CGColorSpaceRef GetSystemColorSpace();
     57 
     58 // Add a full screen request for the given |mode|.  Must be paired with a
     59 // ReleaseFullScreen() call for the same |mode|.  This does not by itself create
     60 // a fullscreen window; rather, it manages per-application state related to
     61 // hiding the dock and menubar.  Must be called on the main thread.
     62 BASE_EXPORT void RequestFullScreen(FullScreenMode mode);
     63 
     64 // Release a request for full screen mode.  Must be matched with a
     65 // RequestFullScreen() call for the same |mode|.  As with RequestFullScreen(),
     66 // this does not affect windows directly, but rather manages per-application
     67 // state.  For example, if there are no other outstanding
     68 // |kFullScreenModeAutoHideAll| requests, this will reshow the menu bar.  Must
     69 // be called on main thread.
     70 BASE_EXPORT void ReleaseFullScreen(FullScreenMode mode);
     71 
     72 // Convenience method to switch the current fullscreen mode.  This has the same
     73 // net effect as a ReleaseFullScreen(from_mode) call followed immediately by a
     74 // RequestFullScreen(to_mode).  Must be called on the main thread.
     75 BASE_EXPORT void SwitchFullScreenModes(FullScreenMode from_mode,
     76                                        FullScreenMode to_mode);
     77 
     78 // Set the visibility of the cursor.
     79 BASE_EXPORT void SetCursorVisibility(bool visible);
     80 
     81 // Should windows miniaturize on a double-click (on the title bar)?
     82 BASE_EXPORT bool ShouldWindowsMiniaturizeOnDoubleClick();
     83 
     84 // Activates the process with the given PID.
     85 BASE_EXPORT void ActivateProcess(pid_t pid);
     86 
     87 // Returns true if this process is in the foreground, meaning that it's the
     88 // frontmost process, the one whose menu bar is shown at the top of the main
     89 // display.
     90 BASE_EXPORT bool AmIForeground();
     91 
     92 // Excludes the file given by |file_path| from being backed up by Time Machine.
     93 BASE_EXPORT bool SetFileBackupExclusion(const FilePath& file_path);
     94 
     95 // Sets the process name as displayed in Activity Monitor to process_name.
     96 BASE_EXPORT void SetProcessName(CFStringRef process_name);
     97 
     98 // Converts a NSImage to a CGImageRef.  Normally, the system frameworks can do
     99 // this fine, especially on 10.6.  On 10.5, however, CGImage cannot handle
    100 // converting a PDF-backed NSImage into a CGImageRef.  This function will
    101 // rasterize the PDF into a bitmap CGImage.  The caller is responsible for
    102 // releasing the return value.
    103 BASE_EXPORT CGImageRef CopyNSImageToCGImage(NSImage* image);
    104 
    105 // Checks if the current application is set as a Login Item, so it will launch
    106 // on Login. If a non-NULL pointer to is_hidden is passed, the Login Item also
    107 // is queried for the 'hide on launch' flag.
    108 BASE_EXPORT bool CheckLoginItemStatus(bool* is_hidden);
    109 
    110 // Adds current application to the set of Login Items with specified "hide"
    111 // flag. This has the same effect as adding/removing the application in
    112 // SystemPreferences->Accounts->LoginItems or marking Application in the Dock
    113 // as "Options->Open on Login".
    114 // Does nothing if the application is already set up as Login Item with
    115 // specified hide flag.
    116 BASE_EXPORT void AddToLoginItems(bool hide_on_startup);
    117 
    118 // Removes the current application from the list Of Login Items.
    119 BASE_EXPORT void RemoveFromLoginItems();
    120 
    121 // Returns true if the current process was automatically launched as a
    122 // 'Login Item' or via Lion's Resume. Used to suppress opening windows.
    123 BASE_EXPORT bool WasLaunchedAsLoginOrResumeItem();
    124 
    125 // Returns true if the current process was automatically launched as a
    126 // 'Login Item' with 'hide on startup' flag. Used to suppress opening windows.
    127 BASE_EXPORT bool WasLaunchedAsHiddenLoginItem();
    128 
    129 // Remove the quarantine xattr from the given file. Returns false if there was
    130 // an error, or true otherwise.
    131 BASE_EXPORT bool RemoveQuarantineAttribute(const FilePath& file_path);
    132 
    133 // Run-time OS version checks. Use these instead of
    134 // base::SysInfo::OperatingSystemVersionNumbers. Prefer the "OrEarlier" and
    135 // "OrLater" variants to those that check for a specific version, unless you
    136 // know for sure that you need to check for a specific version.
    137 
    138 // Snow Leopard is Mac OS X 10.6, Darwin 10.
    139 BASE_EXPORT bool IsOSSnowLeopard();
    140 
    141 // Lion is Mac OS X 10.7, Darwin 11.
    142 BASE_EXPORT bool IsOSLion();
    143 BASE_EXPORT bool IsOSLionOrEarlier();
    144 BASE_EXPORT bool IsOSLionOrLater();
    145 
    146 // Mountain Lion is Mac OS X 10.8, Darwin 12.
    147 BASE_EXPORT bool IsOSMountainLion();
    148 BASE_EXPORT bool IsOSMountainLionOrLater();
    149 
    150 // This should be infrequently used. It only makes sense to use this to avoid
    151 // codepaths that are very likely to break on future (unreleased, untested,
    152 // unborn) OS releases, or to log when the OS is newer than any known version.
    153 BASE_EXPORT bool IsOSLaterThanMountainLion_DontCallThis();
    154 
    155 // When the deployment target is set, the code produced cannot run on earlier
    156 // OS releases. That enables some of the IsOS* family to be implemented as
    157 // constant-value inline functions. The MAC_OS_X_VERSION_MIN_REQUIRED macro
    158 // contains the value of the deployment target.
    159 
    160 #if defined(MAC_OS_X_VERSION_10_7) && \
    161     MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
    162 #define BASE_MAC_MAC_UTIL_H_INLINED_GE_10_7
    163 inline bool IsOSSnowLeopard() { return false; }
    164 inline bool IsOSLionOrLater() { return true; }
    165 #endif
    166 
    167 #if defined(MAC_OS_X_VERSION_10_7) && \
    168     MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_7
    169 #define BASE_MAC_MAC_UTIL_H_INLINED_GT_10_7
    170 inline bool IsOSLion() { return false; }
    171 inline bool IsOSLionOrEarlier() { return false; }
    172 #endif
    173 
    174 #if defined(MAC_OS_X_VERSION_10_8) && \
    175     MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_8
    176 #define BASE_MAC_MAC_UTIL_H_INLINED_GE_10_8
    177 inline bool IsOSMountainLionOrLater() { return true; }
    178 #endif
    179 
    180 #if defined(MAC_OS_X_VERSION_10_8) && \
    181     MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_8
    182 #define BASE_MAC_MAC_UTIL_H_INLINED_GT_10_8
    183 inline bool IsOSMountainLion() { return false; }
    184 inline bool IsOSLaterThanMountainLion_DontCallThis() {
    185   return true;
    186 }
    187 #endif
    188 
    189 // Retrieve the system's model identifier string from the IOKit registry:
    190 // for example, "MacPro4,1", "MacBookPro6,1". Returns empty string upon
    191 // failure.
    192 BASE_EXPORT std::string GetModelIdentifier();
    193 
    194 // Parse a model identifier string; for example, into ("MacBookPro", 6, 1).
    195 // If any error occurs, none of the input pointers are touched.
    196 BASE_EXPORT bool ParseModelIdentifier(const std::string& ident,
    197                                       std::string* type,
    198                                       int32* major,
    199                                       int32* minor);
    200 
    201 }  // namespace mac
    202 }  // namespace base
    203 
    204 #endif  // BASE_MAC_MAC_UTIL_H_
    205