1 // Copyright (c) 2012 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 #include "chrome/browser/icon_loader.h"
10 #include "base/file_util.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/nix/mime_util_xdg.h"
14 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "ui/gfx/codec/png_codec.h"
16 #include "ui/gfx/image/image_skia.h"
17 #include "ui/gfx/size.h"
22 IconGroupID
IconLoader::ReadGroupIDFromFilepath(
23 const base::FilePath
& filepath
) {
24 return base::nix::GetFileMimeType(filepath
);
27 bool IconLoader::IsIconMutableFromFilepath(const base::FilePath
&) {
31 void IconLoader::ReadIcon() {
34 case IconLoader::SMALL
:
37 case IconLoader::NORMAL
:
40 case IconLoader::LARGE
:
47 base::FilePath filename
= base::nix::GetMimeIcon(group_
, size_pixels
);
48 // We don't support SVG or XPM icons; this just spams the terminal so fail
49 // quickly and don't try to read the file from disk first.
50 if (filename
.Extension() != ".svg" &&
51 filename
.Extension() != ".xpm") {
53 base::ReadFileToString(filename
, &icon_data
);
56 bool success
= gfx::PNGCodec::Decode(
57 reinterpret_cast<const unsigned char*>(icon_data
.data()),
60 if (success
&& !bitmap
.empty()) {
61 DCHECK_EQ(size_pixels
, bitmap
.width());
62 DCHECK_EQ(size_pixels
, bitmap
.height());
63 gfx::ImageSkia image_skia
= gfx::ImageSkia::CreateFrom1xBitmap(bitmap
);
64 image_skia
.MakeThreadSafe();
65 image_
.reset(new gfx::Image(image_skia
));
67 LOG(WARNING
) << "Unsupported file type or load error: "
72 target_message_loop_
->PostTask(
73 FROM_HERE
, base::Bind(&IconLoader::NotifyDelegate
, this));