OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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. | |
4 | |
5 #include "chrome/browser/icon_loader.h" | |
6 | |
7 #include <gdk-pixbuf/gdk-pixbuf.h> | |
8 #include <gio/gio.h> | |
9 #include <gtk/gtk.h> | |
10 | |
11 #include "base/file_util.h" | |
12 #include "base/logging.h" | |
13 #include "base/message_loop.h" | |
14 #include "base/mime_util.h" | |
15 #include "base/threading/thread.h" | |
16 #include "base/string_util.h" | |
17 | |
18 static int SizeToInt(IconLoader::IconSize size) { | |
19 int pixels = 0; | |
20 switch (size) { | |
21 case IconLoader::SMALL: | |
22 pixels = 16; | |
23 break; | |
24 case IconLoader::NORMAL: | |
25 pixels = 32; | |
26 break; | |
27 case IconLoader::LARGE: | |
28 pixels = 48; | |
29 break; | |
30 default: | |
31 NOTREACHED(); | |
32 } | |
33 return pixels; | |
34 } | |
35 | |
36 void IconLoader::ReadIcon() { | |
37 filename_ = mime_util::GetMimeIcon(group_, SizeToInt(icon_size_)); | |
38 file_util::ReadFileToString(filename_, &icon_data_); | |
39 target_message_loop_->PostTask(FROM_HERE, | |
40 NewRunnableMethod(this, &IconLoader::ParseIcon)); | |
41 } | |
42 | |
43 void IconLoader::ParseIcon() { | |
44 int size = SizeToInt(icon_size_); | |
45 | |
46 // It would be more convenient to use gdk_pixbuf_new_from_stream_at_scale | |
47 // but that is only available after 2.14. | |
48 GdkPixbufLoader* loader = gdk_pixbuf_loader_new(); | |
49 gdk_pixbuf_loader_set_size(loader, size, size); | |
50 gdk_pixbuf_loader_write(loader, | |
51 reinterpret_cast<const guchar*>(icon_data_.data()), | |
52 icon_data_.length(), NULL); | |
53 gdk_pixbuf_loader_close(loader, NULL); | |
54 // At this point, the pixbuf is owned by the loader. | |
55 GdkPixbuf* pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); | |
56 | |
57 if (pixbuf) { | |
58 DCHECK_EQ(size, gdk_pixbuf_get_width(pixbuf)); | |
59 DCHECK_EQ(size, gdk_pixbuf_get_height(pixbuf)); | |
60 // Takes ownership of |pixbuf|. | |
61 g_object_ref(pixbuf); | |
62 image_.reset(new gfx::Image(pixbuf)); | |
63 } else { | |
64 LOG(WARNING) << "Unsupported file type or load error: " << | |
65 filename_.value(); | |
66 } | |
67 | |
68 g_object_unref(loader); | |
69 | |
70 NotifyDelegate(); | |
71 } | |
OLD | NEW |