• 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 #ifndef CHROME_BROWSER_ICON_LOADER_H_
6 #define CHROME_BROWSER_ICON_LOADER_H_
7 #pragma once
8 
9 #include "build/build_config.h"
10 
11 #include <string>
12 
13 #include "base/basictypes.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop_proxy.h"
17 #include "ui/gfx/image.h"
18 
19 #if defined(TOOLKIT_USES_GTK)
20 #include "base/file_path.h"
21 #endif
22 
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.
26 typedef std::wstring IconGroupID;
27 #elif defined(OS_POSIX)
28 // On POSIX, we group files by MIME type.
29 typedef std::string IconGroupID;
30 #endif
31 
32 ////////////////////////////////////////////////////////////////////////////////
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 ////////////////////////////////////////////////////////////////////////////////
38 class 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.
51     virtual bool OnImageLoaded(IconLoader* source, gfx::Image* result) = 0;
52 
53    protected:
~Delegate()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.
72   scoped_refptr<base::MessageLoopProxy> target_message_loop_;
73 
74   IconGroupID group_;
75 
76   IconSize icon_size_;
77 
78   scoped_ptr<gfx::Image> image_;
79 
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_
95