[M120][Tizen][Onscreen] Fix build errors for TV profile
[platform/framework/web/chromium-efl.git] / chrome / browser / icon_loader_mac.mm
1 // Copyright 2012 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 #include "chrome/browser/icon_loader.h"
6
7 #import <AppKit/AppKit.h>
8 #import <CoreServices/CoreServices.h>                      // pre-macOS 11
9 #import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>  // macOS 11
10
11 #include "base/apple/foundation_util.h"
12 #include "base/apple/scoped_cftyperef.h"
13 #include "base/files/file_path.h"
14 #include "base/functional/bind.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "base/task/thread_pool.h"
17 #include "base/threading/thread.h"
18 #include "ui/gfx/image/image_skia.h"
19 #include "ui/gfx/image/image_skia_util_mac.h"
20
21 // static
22 IconLoader::IconGroup IconLoader::GroupForFilepath(
23     const base::FilePath& file_path) {
24   // The best option is to get the type directly from the file. The next best
25   // option is to pull the extension from the file and get the type from that.
26   // The last and worst option is to fall back to `public.content` which will
27   // give a generic file icon.
28
29   if (@available(macOS 11, *)) {
30     UTType* type;
31     NSURL* file_url = base::apple::FilePathToNSURL(file_path);
32     if (file_url && [file_url getResourceValue:&type
33                                         forKey:NSURLContentTypeKey
34                                          error:nil]) {
35       return base::SysNSStringToUTF8(type.identifier);
36     }
37
38     std::string extension_string = file_path.FinalExtension();
39     if (!extension_string.empty()) {
40       // Remove the leading dot.
41       extension_string.erase(extension_string.begin());
42
43       type = [UTType
44           typeWithFilenameExtension:base::SysUTF8ToNSString(extension_string)];
45       if (type) {
46         return base::SysNSStringToUTF8(type.identifier);
47       }
48     }
49
50     return base::SysNSStringToUTF8(UTTypeContent.identifier);
51   } else {
52     NSString* type;
53     NSURL* file_url = base::apple::FilePathToNSURL(file_path);
54     if (file_url && [file_url getResourceValue:&type
55                                         forKey:NSURLTypeIdentifierKey
56                                          error:nil]) {
57       return base::SysNSStringToUTF8(type);
58     }
59
60     std::string extension_string = file_path.FinalExtension();
61     if (!extension_string.empty()) {
62       // Remove the leading dot.
63       extension_string.erase(extension_string.begin());
64
65       base::apple::ScopedCFTypeRef<CFStringRef> extension_cf =
66           base::SysUTF8ToCFStringRef(extension_string);
67       base::apple::ScopedCFTypeRef<CFStringRef> cftype(
68           UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
69                                                 extension_cf,
70                                                 /*inConformingToUTI=*/nullptr));
71       if (cftype) {
72         return base::SysCFStringRefToUTF8(cftype);
73       }
74     }
75
76     return base::SysCFStringRefToUTF8(kUTTypeContent);
77   }
78 }
79
80 // static
81 scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() {
82   // NSWorkspace is thread-safe.
83   return base::ThreadPool::CreateTaskRunner(traits());
84 }
85
86 void IconLoader::ReadIcon() {
87   NSImage* icon;
88   if (@available(macOS 11, *)) {
89     UTType* type = [UTType typeWithIdentifier:base::SysUTF8ToNSString(group_)];
90     icon = [NSWorkspace.sharedWorkspace iconForContentType:type];
91   } else {
92     NSString* type = base::SysUTF8ToNSString(group_);
93     icon = [NSWorkspace.sharedWorkspace iconForFileType:type];
94   }
95
96   gfx::Image image;
97   if (icon_size_ == ALL) {
98     // The NSImage already has all sizes.
99     image = gfx::Image(icon);
100   } else {
101     NSSize size = NSZeroSize;
102     switch (icon_size_) {
103       case IconLoader::SMALL:
104         size = NSMakeSize(16, 16);
105         break;
106       case IconLoader::NORMAL:
107         size = NSMakeSize(32, 32);
108         break;
109       default:
110         NOTREACHED();
111     }
112
113     gfx::ImageSkia image_skia = gfx::ImageSkiaFromResizedNSImage(icon, size);
114     if (!image_skia.isNull()) {
115       image_skia.MakeThreadSafe();
116       image = gfx::Image(image_skia);
117     }
118   }
119
120   target_task_runner_->PostTask(
121       FROM_HERE,
122       base::BindOnce(std::move(callback_), std::move(image), group_));
123   delete this;
124 }