Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2008-2009 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_UTIL_H_
      6 #define BASE_MAC_UTIL_H_
      7 
      8 #include <Carbon/Carbon.h>
      9 #include <string>
     10 #include <vector>
     11 
     12 class FilePath;
     13 
     14 #ifdef __OBJC__
     15 @class NSBundle;
     16 @class NSWindow;
     17 #else
     18 class NSBundle;
     19 class NSWindow;
     20 #endif
     21 
     22 // Adapted from NSPathUtilities.h and NSObjCRuntime.h.
     23 #if __LP64__ || NS_BUILD_32_LIKE_64
     24 typedef unsigned long NSSearchPathDirectory;
     25 #else
     26 typedef unsigned int NSSearchPathDirectory;
     27 #endif
     28 
     29 namespace mac_util {
     30 
     31 std::string PathFromFSRef(const FSRef& ref);
     32 bool FSRefFromPath(const std::string& path, FSRef* ref);
     33 
     34 // Returns true if the application is running from a bundle
     35 bool AmIBundled();
     36 
     37 // Returns true if this process is marked as a "Background only process".
     38 bool IsBackgroundOnlyProcess();
     39 
     40 // Returns the main bundle or the override, used for code that needs
     41 // to fetch resources from bundles, but work within a unittest where we
     42 // aren't a bundle.
     43 NSBundle* MainAppBundle();
     44 FilePath MainAppBundlePath();
     45 
     46 // Set the bundle that MainAppBundle will return, overriding the default value
     47 // (Restore the default by calling SetOverrideAppBundle(nil)).
     48 void SetOverrideAppBundle(NSBundle* bundle);
     49 void SetOverrideAppBundlePath(const FilePath& file_path);
     50 
     51 // Returns the creator code associated with the CFBundleRef at bundle.
     52 OSType CreatorCodeForCFBundleRef(CFBundleRef bundle);
     53 
     54 // Returns the creator code associated with this application, by calling
     55 // CreatorCodeForCFBundleRef for the application's main bundle.  If this
     56 // information cannot be determined, returns kUnknownType ('????').  This
     57 // does not respect the override app bundle because it's based on CFBundle
     58 // instead of NSBundle, and because callers probably don't want the override
     59 // app bundle's creator code anyway.
     60 OSType CreatorCodeForApplication();
     61 
     62 // Searches for directories for the given key in only the user domain.
     63 // If found, fills result (which must always be non-NULL) with the
     64 // first found directory and returns true.  Otherwise, returns false.
     65 bool GetUserDirectory(NSSearchPathDirectory directory, FilePath* result);
     66 
     67 // Returns the ~/Library directory.
     68 FilePath GetUserLibraryPath();
     69 
     70 // Returns an sRGB color space.  The return value is a static value; do not
     71 // release it!
     72 CGColorSpaceRef GetSRGBColorSpace();
     73 
     74 // Returns the color space being used by the main display.  The return value
     75 // is a static value; do not release it!
     76 CGColorSpaceRef GetSystemColorSpace();
     77 
     78 // Add a request for full screen mode.  This does not by itself create a
     79 // fullscreen window; rather, it manages per-application state related to
     80 // fullscreen windows.  For example, if the menu bar is not currently
     81 // hidden, this will hide it.  Must be called on main thread.
     82 void RequestFullScreen();
     83 
     84 // Release a request for full screen mode.  As with RequestFullScree(), this
     85 // does not affect windows directly, but rather manages per-application state.
     86 // For example, if there are no other outstanding requests for full screen,
     87 // this will show the menu bar.  Must be called on main thread.
     88 void ReleaseFullScreen();
     89 
     90 // Set the visibility of the cursor.
     91 void SetCursorVisibility(bool visible);
     92 
     93 // Activates the process with the given PID.
     94 void ActivateProcess(pid_t);
     95 
     96 // Pulls a snapshot of the entire browser into png_representation.
     97 void GrabWindowSnapshot(NSWindow* window,
     98                         std::vector<unsigned char>* png_representation);
     99 
    100 // Takes a path to an (executable) binary and tries to provide the path to an
    101 // application bundle containing it. It takes the outermost bundle that it can
    102 // find (so for "/Foo/Bar.app/.../Baz.app/..." it produces "/Foo/Bar.app").
    103 //   |exec_name| - path to the binary
    104 //   returns - path to the application bundle, or empty on error
    105 FilePath GetAppBundlePath(const FilePath& exec_name);
    106 
    107 // Set the Time Machine exclusion property for the given file.
    108 bool SetFileBackupExclusion(const FilePath& file_path, bool exclude);
    109 
    110 // Utility function to pull out a value from a dictionary, check its type, and
    111 // return it.  Returns NULL if the key is not present or of the wrong type.
    112 CFTypeRef GetValueFromDictionary(CFDictionaryRef dict,
    113                                  CFStringRef key,
    114                                  CFTypeID expected_type);
    115 
    116 }  // namespace mac_util
    117 
    118 #endif  // BASE_MAC_UTIL_H_
    119