Marijn Kruisselbrink | 9aba65e | 2024-04-18 21:54:24 | [diff] [blame] | 1 | // Copyright 2024 The Chromium Authors |
| 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 CHROME_BROWSER_SHORTCUTS_PLATFORM_UTIL_MAC_H_ |
| 6 | #define CHROME_BROWSER_SHORTCUTS_PLATFORM_UTIL_MAC_H_ |
| 7 | |
Marijn Kruisselbrink | 1bfda68 | 2024-05-02 00:23:08 | [diff] [blame] | 8 | #include <optional> |
| 9 | |
| 10 | #include "base/files/safe_base_name.h" |
Marijn Kruisselbrink | 9aba65e | 2024-04-18 21:54:24 | [diff] [blame] | 11 | #include "base/functional/callback_forward.h" |
| 12 | |
| 13 | @class NSError; |
| 14 | @class NSImage; |
| 15 | @class NSURL; |
| 16 | |
| 17 | namespace base { |
| 18 | class FilePath; |
| 19 | } |
| 20 | |
| 21 | namespace shortcuts { |
| 22 | |
| 23 | // Wrapper around NSWorkspace setIcon:forFile: that can be called from any |
| 24 | // thread. While the underlying NSWorkspace method can also be called from any |
| 25 | // thread, it is not reentrancy safe (i.e. multiple calls to it need to be |
| 26 | // sequenced). This wrapper takes care of the sequencing needed. The `callback` |
| 27 | // will be called on the calling sequence. |
| 28 | void SetIconForFile(NSImage* image, |
| 29 | const base::FilePath& file, |
| 30 | base::OnceCallback<void(bool)> callback); |
| 31 | |
| 32 | // Wrapper around NSWorkspace setDefaultApplicationAtURL:toOpenFileAtURL:. The |
| 33 | // NSWorkspace method only exists on macOS 12.0 and newer; this method polyfills |
| 34 | // its implementation for older macOS versions. |
| 35 | void SetDefaultApplicationToOpenFile( |
| 36 | NSURL* file_url, |
| 37 | NSURL* application_url, |
| 38 | base::OnceCallback<void(NSError*)> callback); |
| 39 | |
Marijn Kruisselbrink | 1bfda68 | 2024-05-02 00:23:08 | [diff] [blame] | 40 | // Return a version of `title` that is safe to use as a filename on macOS. |
| 41 | std::optional<base::SafeBaseName> SanitizeTitleForFileName( |
| 42 | const std::string& title); |
| 43 | |
Marijn Kruisselbrink | 9aba65e | 2024-04-18 21:54:24 | [diff] [blame] | 44 | } // namespace shortcuts |
| 45 | |
| 46 | #endif // CHROME_BROWSER_SHORTCUTS_PLATFORM_UTIL_MAC_H_ |