blob: 0b6cf2519af578d9adaa229a0e1336dd36cfd34e [file] [log] [blame]
Kristian Monsendc0f95d2011-06-09 11:47:42 +01001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
Ben Murdochc407dc52010-07-29 17:14:53 +01002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_ICON_LOADER_H_
6#define CHROME_BROWSER_ICON_LOADER_H_
Iain Merrick3345a682010-10-19 14:37:37 +01007#pragma once
8
9#include "build/build_config.h"
Ben Murdochc407dc52010-07-29 17:14:53 +010010
11#include <string>
12
13#include "base/basictypes.h"
Kristian Monsenddb351d2011-06-28 21:49:31 +010014#include "base/memory/ref_counted.h"
15#include "base/memory/scoped_ptr.h"
Ben Murdoch513209b2010-11-18 18:32:45 +000016#include "base/message_loop_proxy.h"
Kristian Monsendc0f95d2011-06-09 11:47:42 +010017#include "ui/gfx/image.h"
Ben Murdoch513209b2010-11-18 18:32:45 +000018
Iain Merrick3345a682010-10-19 14:37:37 +010019#if defined(TOOLKIT_USES_GTK)
Ben Murdochc407dc52010-07-29 17:14:53 +010020#include "base/file_path.h"
Iain Merrick3345a682010-10-19 14:37:37 +010021#endif
Ben Murdochc407dc52010-07-29 17:14:53 +010022
23#if defined(OS_WIN)
24// On Windows, we group files by their extension, with several exceptions:
25// .dll, .exe, .ico. See IconManager.h for explanation.
26typedef std::wstring IconGroupID;
27#elif defined(OS_POSIX)
28// On POSIX, we group files by MIME type.
29typedef std::string IconGroupID;
30#endif
31
Ben Murdochc407dc52010-07-29 17:14:53 +010032////////////////////////////////////////////////////////////////////////////////
33//
34// A facility to read a file containing an icon asynchronously in the IO
35// thread. Returns the icon in the form of an SkBitmap.
36//
37////////////////////////////////////////////////////////////////////////////////
38class IconLoader : public base::RefCountedThreadSafe<IconLoader> {
39 public:
40 enum IconSize {
41 SMALL = 0, // 16x16
42 NORMAL, // 32x32
43 LARGE
44 };
45
46 class Delegate {
47 public:
48 // Invoked when an icon has been read. |source| is the IconLoader. If the
49 // icon has been successfully loaded, result is non-null. This method must
50 // return true if it is taking ownership of the returned bitmap.
Kristian Monsendc0f95d2011-06-09 11:47:42 +010051 virtual bool OnImageLoaded(IconLoader* source, gfx::Image* result) = 0;
Ben Murdochc407dc52010-07-29 17:14:53 +010052
53 protected:
54 virtual ~Delegate() {}
55 };
56
57 IconLoader(const IconGroupID& group, IconSize size, Delegate* delegate);
58
59 // Start reading the icon on the file thread.
60 void Start();
61
62 private:
63 friend class base::RefCountedThreadSafe<IconLoader>;
64
65 virtual ~IconLoader();
66
67 void ReadIcon();
68
69 void NotifyDelegate();
70
71 // The message loop object of the thread in which we notify the delegate.
Ben Murdoch513209b2010-11-18 18:32:45 +000072 scoped_refptr<base::MessageLoopProxy> target_message_loop_;
Ben Murdochc407dc52010-07-29 17:14:53 +010073
74 IconGroupID group_;
75
76 IconSize icon_size_;
77
Kristian Monsendc0f95d2011-06-09 11:47:42 +010078 scoped_ptr<gfx::Image> image_;
Ben Murdochc407dc52010-07-29 17:14:53 +010079
80 Delegate* delegate_;
81
82#if defined(TOOLKIT_USES_GTK)
83 // On X11 we use gdk's pixbuf loader, which has to execute on the UI
84 // thread, so we only read the file on the background thread and parse it
85 // on the main thread.
86 void ParseIcon();
87 FilePath filename_;
88 std::string icon_data_;
89#endif
90
91 DISALLOW_COPY_AND_ASSIGN(IconLoader);
92};
93
94#endif // CHROME_BROWSER_ICON_LOADER_H_