Home | History | Annotate | Download | only in browser
      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.
      4 
      5 #ifndef CHROME_BROWSER_ICON_LOADER_H_
      6 #define CHROME_BROWSER_ICON_LOADER_H_
      7 
      8 #include "build/build_config.h"
      9 
     10 #include <string>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/files/file_path.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/message_loop/message_loop_proxy.h"
     17 #include "content/public/browser/browser_thread.h"
     18 #include "ui/gfx/image/image.h"
     19 
     20 #if defined(OS_WIN)
     21 // On Windows, we group files by their extension, with several exceptions:
     22 // .dll, .exe, .ico. See IconManager.h for explanation.
     23 typedef std::wstring IconGroupID;
     24 #elif defined(OS_POSIX)
     25 // On POSIX, we group files by MIME type.
     26 typedef std::string IconGroupID;
     27 #endif
     28 
     29 ////////////////////////////////////////////////////////////////////////////////
     30 //
     31 // A facility to read a file containing an icon asynchronously in the IO
     32 // thread. Returns the icon in the form of an ImageSkia.
     33 //
     34 ////////////////////////////////////////////////////////////////////////////////
     35 class IconLoader : public base::RefCountedThreadSafe<IconLoader> {
     36  public:
     37   enum IconSize {
     38     SMALL = 0,  // 16x16
     39     NORMAL,     // 32x32
     40     LARGE,      // Windows: 32x32, Linux: 48x48, Mac: Unsupported
     41     ALL,        // All sizes available
     42   };
     43 
     44   class Delegate {
     45    public:
     46     // Invoked when an icon group has been read, but before the icon data
     47     // is read. If the icon is already cached, this method should call and
     48     // return the results of OnImageLoaded with the cached image.
     49     virtual bool OnGroupLoaded(IconLoader* source,
     50                                const IconGroupID& group) = 0;
     51     // Invoked when an icon has been read. |source| is the IconLoader. If the
     52     // icon has been successfully loaded, result is non-null. This method must
     53     // return true if it is taking ownership of the returned image.
     54     virtual bool OnImageLoaded(IconLoader* source,
     55                                gfx::Image* result,
     56                                const IconGroupID& group) = 0;
     57 
     58    protected:
     59     virtual ~Delegate() {}
     60   };
     61 
     62   IconLoader(const base::FilePath& file_path,
     63              IconSize size,
     64              Delegate* delegate);
     65 
     66   // Start reading the icon on the file thread.
     67   void Start();
     68 
     69  private:
     70   friend class base::RefCountedThreadSafe<IconLoader>;
     71 
     72   virtual ~IconLoader();
     73 
     74   // Get the identifying string for the given file. The implementation
     75   // is in icon_loader_[platform].cc.
     76   static IconGroupID ReadGroupIDFromFilepath(const base::FilePath& path);
     77 
     78   // Some icons (exe's on windows) can change as they're loaded.
     79   static bool IsIconMutableFromFilepath(const base::FilePath& path);
     80 
     81   // The thread ReadIcon() should be called on.
     82   static content::BrowserThread::ID ReadIconThreadID();
     83 
     84   void ReadGroup();
     85   void OnReadGroup();
     86   void ReadIcon();
     87 
     88   void NotifyDelegate();
     89 
     90   // The message loop object of the thread in which we notify the delegate.
     91   scoped_refptr<base::MessageLoopProxy> target_message_loop_;
     92 
     93   base::FilePath file_path_;
     94 
     95   IconGroupID group_;
     96 
     97   IconSize icon_size_;
     98 
     99   scoped_ptr<gfx::Image> image_;
    100 
    101   Delegate* delegate_;
    102 
    103   DISALLOW_COPY_AND_ASSIGN(IconLoader);
    104 };
    105 
    106 #endif  // CHROME_BROWSER_ICON_LOADER_H_
    107