1 // Copyright (c) 2010 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.
5 #ifndef BASE_MAC_UTIL_H_
6 #define BASE_MAC_UTIL_H_
9 #include <Carbon/Carbon.h>
24 // Adapted from NSPathUtilities.h and NSObjCRuntime.h.
25 #if __LP64__ || NS_BUILD_32_LIKE_64
26 typedef unsigned long NSSearchPathDirectory
;
28 typedef unsigned int NSSearchPathDirectory
;
33 // Full screen modes, in increasing order of priority. More permissive modes
36 kFullScreenModeHideAll
= 0,
37 kFullScreenModeHideDock
= 1,
38 kFullScreenModeAutoHideAll
= 2,
39 kNumFullScreenModes
= 3,
41 // kFullScreenModeNormal is not a valid FullScreenMode, but it is useful to
42 // other classes, so we include it here.
43 kFullScreenModeNormal
= 10,
46 std::string
PathFromFSRef(const FSRef
& ref
);
47 bool FSRefFromPath(const std::string
& path
, FSRef
* ref
);
49 // Returns true if the application is running from a bundle
52 // Returns true if this process is marked as a "Background only process".
53 bool IsBackgroundOnlyProcess();
55 // Returns the main bundle or the override, used for code that needs
56 // to fetch resources from bundles, but work within a unittest where we
58 NSBundle
* MainAppBundle();
59 FilePath
MainAppBundlePath();
61 // Set the bundle that MainAppBundle will return, overriding the default value
62 // (Restore the default by calling SetOverrideAppBundle(nil)).
63 void SetOverrideAppBundle(NSBundle
* bundle
);
64 void SetOverrideAppBundlePath(const FilePath
& file_path
);
66 // Returns the creator code associated with the CFBundleRef at bundle.
67 OSType
CreatorCodeForCFBundleRef(CFBundleRef bundle
);
69 // Returns the creator code associated with this application, by calling
70 // CreatorCodeForCFBundleRef for the application's main bundle. If this
71 // information cannot be determined, returns kUnknownType ('????'). This
72 // does not respect the override app bundle because it's based on CFBundle
73 // instead of NSBundle, and because callers probably don't want the override
74 // app bundle's creator code anyway.
75 OSType
CreatorCodeForApplication();
77 // Searches for directories for the given key in only the user domain.
78 // If found, fills result (which must always be non-NULL) with the
79 // first found directory and returns true. Otherwise, returns false.
80 bool GetUserDirectory(NSSearchPathDirectory directory
, FilePath
* result
);
82 // Returns the ~/Library directory.
83 FilePath
GetUserLibraryPath();
85 // Returns an sRGB color space. The return value is a static value; do not
87 CGColorSpaceRef
GetSRGBColorSpace();
89 // Returns the color space being used by the main display. The return value
90 // is a static value; do not release it!
91 CGColorSpaceRef
GetSystemColorSpace();
93 // Add a full screen request for the given |mode|. Must be paired with a
94 // ReleaseFullScreen() call for the same |mode|. This does not by itself create
95 // a fullscreen window; rather, it manages per-application state related to
96 // hiding the dock and menubar. Must be called on the main thread.
97 void RequestFullScreen(FullScreenMode mode
);
99 // Release a request for full screen mode. Must be matched with a
100 // RequestFullScreen() call for the same |mode|. As with RequestFullScreen(),
101 // this does not affect windows directly, but rather manages per-application
102 // state. For example, if there are no other outstanding
103 // |kFullScreenModeAutoHideAll| requests, this will reshow the menu bar. Must
104 // be called on main thread.
105 void ReleaseFullScreen(FullScreenMode mode
);
107 // Convenience method to switch the current fullscreen mode. This has the same
108 // net effect as a ReleaseFullScreen(from_mode) call followed immediately by a
109 // RequestFullScreen(to_mode). Must be called on the main thread.
110 void SwitchFullScreenModes(FullScreenMode from_mode
, FullScreenMode to_mode
);
112 // Set the visibility of the cursor.
113 void SetCursorVisibility(bool visible
);
115 // Should windows miniaturize on a double-click (on the title bar)?
116 bool ShouldWindowsMiniaturizeOnDoubleClick();
118 // Activates the process with the given PID.
119 void ActivateProcess(pid_t
);
121 // Pulls a snapshot of the entire browser into png_representation.
122 void GrabWindowSnapshot(NSWindow
* window
,
123 std::vector
<unsigned char>* png_representation
,
124 int* width
, int* height
);
126 // Takes a path to an (executable) binary and tries to provide the path to an
127 // application bundle containing it. It takes the outermost bundle that it can
128 // find (so for "/Foo/Bar.app/.../Baz.app/..." it produces "/Foo/Bar.app").
129 // |exec_name| - path to the binary
130 // returns - path to the application bundle, or empty on error
131 FilePath
GetAppBundlePath(const FilePath
& exec_name
);
133 // Set the Time Machine exclusion property for the given file.
134 bool SetFileBackupExclusion(const FilePath
& file_path
, bool exclude
);
136 // Utility function to pull out a value from a dictionary, check its type, and
137 // return it. Returns NULL if the key is not present or of the wrong type.
138 CFTypeRef
GetValueFromDictionary(CFDictionaryRef dict
,
140 CFTypeID expected_type
);
142 // Sets the process name as displayed in Activity Monitor to process_name.
143 void SetProcessName(CFStringRef process_name
);
145 // Converts a NSImage to a CGImageRef. Normally, the system frameworks can do
146 // this fine, especially on 10.6. On 10.5, however, CGImage cannot handle
147 // converting a PDF-backed NSImage into a CGImageRef. This function will
148 // rasterize the PDF into a bitmap CGImage. The caller is responsible for
149 // releasing the return value.
150 CGImageRef
CopyNSImageToCGImage(NSImage
* image
);
152 } // namespace mac_util
154 #endif // BASE_MAC_UTIL_H_