• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SizeToInt(IconLoader::IconSize size)18 static int SizeToInt(IconLoader::IconSize size) {
19   int pixels = 48;
20   if (size == IconLoader::NORMAL)
21     pixels = 32;
22   else if (size == IconLoader::SMALL)
23     pixels = 16;
24 
25   return pixels;
26 }
27 
ReadIcon()28 void IconLoader::ReadIcon() {
29   filename_ = mime_util::GetMimeIcon(group_, SizeToInt(icon_size_));
30   file_util::ReadFileToString(filename_, &icon_data_);
31   target_message_loop_->PostTask(FROM_HERE,
32       NewRunnableMethod(this, &IconLoader::ParseIcon));
33 }
34 
ParseIcon()35 void IconLoader::ParseIcon() {
36   int size = SizeToInt(icon_size_);
37 
38   // It would be more convenient to use gdk_pixbuf_new_from_stream_at_scale
39   // but that is only available after 2.14.
40   GdkPixbufLoader* loader = gdk_pixbuf_loader_new();
41   gdk_pixbuf_loader_set_size(loader, size, size);
42   gdk_pixbuf_loader_write(loader,
43                           reinterpret_cast<const guchar*>(icon_data_.data()),
44                           icon_data_.length(), NULL);
45   gdk_pixbuf_loader_close(loader, NULL);
46   // At this point, the pixbuf is owned by the loader.
47   GdkPixbuf* pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
48 
49   if (pixbuf) {
50     DCHECK_EQ(size, gdk_pixbuf_get_width(pixbuf));
51     DCHECK_EQ(size, gdk_pixbuf_get_height(pixbuf));
52     // Takes ownership of |pixbuf|.
53     g_object_ref(pixbuf);
54     image_.reset(new gfx::Image(pixbuf));
55   } else {
56     LOG(WARNING) << "Unsupported file type or load error: " <<
57                     filename_.value();
58   }
59 
60   g_object_unref(loader);
61 
62   NotifyDelegate();
63 }
64