File: icon_loader_mac.mm

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (124 lines) | stat: -rw-r--r-- 4,182 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/icon_loader.h"

#import <AppKit/AppKit.h>
#import <CoreServices/CoreServices.h>                      // pre-macOS 11
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>  // macOS 11

#include "base/apple/foundation_util.h"
#include "base/apple/scoped_cftyperef.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/strings/sys_string_conversions.h"
#include "base/task/thread_pool.h"
#include "base/threading/thread.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_util_mac.h"

// static
IconLoader::IconGroup IconLoader::GroupForFilepath(
    const base::FilePath& file_path) {
  // The best option is to get the type directly from the file. The next best
  // option is to pull the extension from the file and get the type from that.
  // The last and worst option is to fall back to `public.content` which will
  // give a generic file icon.

  if (@available(macOS 11, *)) {
    UTType* type;
    NSURL* file_url = base::apple::FilePathToNSURL(file_path);
    if (file_url && [file_url getResourceValue:&type
                                        forKey:NSURLContentTypeKey
                                         error:nil]) {
      return base::SysNSStringToUTF8(type.identifier);
    }

    std::string extension_string = file_path.FinalExtension();
    if (!extension_string.empty()) {
      // Remove the leading dot.
      extension_string.erase(extension_string.begin());

      type = [UTType
          typeWithFilenameExtension:base::SysUTF8ToNSString(extension_string)];
      if (type) {
        return base::SysNSStringToUTF8(type.identifier);
      }
    }

    return base::SysNSStringToUTF8(UTTypeContent.identifier);
  } else {
    NSString* type;
    NSURL* file_url = base::apple::FilePathToNSURL(file_path);
    if (file_url && [file_url getResourceValue:&type
                                        forKey:NSURLTypeIdentifierKey
                                         error:nil]) {
      return base::SysNSStringToUTF8(type);
    }

    std::string extension_string = file_path.FinalExtension();
    if (!extension_string.empty()) {
      // Remove the leading dot.
      extension_string.erase(extension_string.begin());

      base::apple::ScopedCFTypeRef<CFStringRef> extension_cf =
          base::SysUTF8ToCFStringRef(extension_string);
      base::apple::ScopedCFTypeRef<CFStringRef> cftype(
          UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
                                                extension_cf,
                                                /*inConformingToUTI=*/nullptr));
      if (cftype) {
        return base::SysCFStringRefToUTF8(cftype);
      }
    }

    return base::SysCFStringRefToUTF8(kUTTypeContent);
  }
}

// static
scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() {
  // NSWorkspace is thread-safe.
  return base::ThreadPool::CreateTaskRunner(traits());
}

void IconLoader::ReadIcon() {
  NSImage* icon;
  if (@available(macOS 11, *)) {
    UTType* type = [UTType typeWithIdentifier:base::SysUTF8ToNSString(group_)];
    icon = [NSWorkspace.sharedWorkspace iconForContentType:type];
  } else {
    NSString* type = base::SysUTF8ToNSString(group_);
    icon = [NSWorkspace.sharedWorkspace iconForFileType:type];
  }

  gfx::Image image;
  if (icon_size_ == ALL) {
    // The NSImage already has all sizes.
    image = gfx::Image(icon);
  } else {
    NSSize size = NSZeroSize;
    switch (icon_size_) {
      case IconLoader::SMALL:
        size = NSMakeSize(16, 16);
        break;
      case IconLoader::NORMAL:
        size = NSMakeSize(32, 32);
        break;
      default:
        NOTREACHED();
    }

    gfx::ImageSkia image_skia = gfx::ImageSkiaFromResizedNSImage(icon, size);
    if (!image_skia.isNull()) {
      image_skia.MakeThreadSafe();
      image = gfx::Image(image_skia);
    }
  }

  target_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(std::move(callback_), std::move(image), group_));
  delete this;
}