2016-11-02 18:57:16 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// 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>
|
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "base/message_loop/message_loop.h"
|
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2018-01-08 10:21:22 +00:00
|
|
|
#include "base/task_scheduler/post_task.h"
|
2016-11-02 18:57:16 +00:00
|
|
|
#include "base/threading/thread.h"
|
|
|
|
#include "ui/gfx/image/image_skia.h"
|
|
|
|
#include "ui/gfx/image/image_skia_util_mac.h"
|
|
|
|
|
|
|
|
// static
|
2017-02-16 19:19:19 +00:00
|
|
|
IconLoader::IconGroup IconLoader::GroupForFilepath(
|
|
|
|
const base::FilePath& file_path) {
|
|
|
|
return file_path.Extension();
|
2016-11-02 18:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2018-01-08 10:21:22 +00:00
|
|
|
scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() {
|
|
|
|
// NSWorkspace is thread-safe.
|
|
|
|
return base::CreateTaskRunnerWithTraits(traits());
|
2016-11-02 18:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconLoader::ReadIcon() {
|
|
|
|
NSString* group = base::SysUTF8ToNSString(group_);
|
|
|
|
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
|
|
|
|
NSImage* icon = [workspace iconForFileType:group];
|
|
|
|
|
2018-01-08 09:47:25 +00:00
|
|
|
std::unique_ptr<gfx::Image> image;
|
|
|
|
|
2016-11-02 18:57:16 +00:00
|
|
|
if (icon_size_ == ALL) {
|
|
|
|
// The NSImage already has all sizes.
|
2018-04-12 12:48:32 +00:00
|
|
|
image = std::make_unique<gfx::Image>([icon retain]);
|
2016-11-02 18:57:16 +00:00
|
|
|
} 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();
|
2018-04-12 12:48:32 +00:00
|
|
|
image = std::make_unique<gfx::Image>(image_skia);
|
2016-11-02 18:57:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 19:19:19 +00:00
|
|
|
target_task_runner_->PostTask(
|
2018-01-08 09:47:25 +00:00
|
|
|
FROM_HERE, base::Bind(callback_, base::Passed(&image), group_));
|
2017-02-16 19:19:19 +00:00
|
|
|
delete this;
|
2016-11-02 18:57:16 +00:00
|
|
|
}
|