Added owners for common login-related code that was moved recently.
[chromium-blink-merge.git] / chrome / browser / icon_loader_linux.cc
blob2ee16ead8ddc2567f66433a6f09dd54959dc720f
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"
7 #include <string>
9 #include "base/bind.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"
19 using std::string;
21 // static
22 IconGroupID IconLoader::ReadGroupIDFromFilepath(
23 const base::FilePath& filepath) {
24 return base::nix::GetFileMimeType(filepath);
27 bool IconLoader::IsIconMutableFromFilepath(const base::FilePath&) {
28 return false;
31 void IconLoader::ReadIcon() {
32 int size_pixels = 0;
33 switch (icon_size_) {
34 case IconLoader::SMALL:
35 size_pixels = 16;
36 break;
37 case IconLoader::NORMAL:
38 size_pixels = 32;
39 break;
40 case IconLoader::LARGE:
41 size_pixels = 48;
42 break;
43 default:
44 NOTREACHED();
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") {
52 string icon_data;
53 base::ReadFileToString(filename, &icon_data);
55 SkBitmap bitmap;
56 bool success = gfx::PNGCodec::Decode(
57 reinterpret_cast<const unsigned char*>(icon_data.data()),
58 icon_data.length(),
59 &bitmap);
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));
66 } else {
67 LOG(WARNING) << "Unsupported file type or load error: "
68 << filename.value();
72 target_message_loop_->PostTask(
73 FROM_HERE, base::Bind(&IconLoader::NotifyDelegate, this));