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