| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_ICON_LOADER_H_ | 5 #ifndef CHROME_BROWSER_ICON_LOADER_H_ |
| 6 #define CHROME_BROWSER_ICON_LOADER_H_ | 6 #define CHROME_BROWSER_ICON_LOADER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "ui/gfx/image/image.h" | 17 #include "ui/gfx/image/image.h" |
| 18 | 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 //////////////////////////////////////////////////////////////////////////////// | 19 //////////////////////////////////////////////////////////////////////////////// |
| 29 // | 20 // |
| 30 // A facility to read a file containing an icon asynchronously in the IO | 21 // A facility to read a file containing an icon asynchronously in the IO |
| 31 // thread. Returns the icon in the form of an ImageSkia. | 22 // thread. Returns the icon in the form of an ImageSkia. |
| 32 // | 23 // |
| 33 //////////////////////////////////////////////////////////////////////////////// | 24 //////////////////////////////////////////////////////////////////////////////// |
| 34 class IconLoader : public base::RefCountedThreadSafe<IconLoader> { | 25 class IconLoader : public base::RefCountedThreadSafe<IconLoader> { |
| 35 public: | 26 public: |
| 27 // An IconGroup is a class of files that all share the same icon. For all |
| 28 // platforms but Windows, and for most files on Windows, it is the file type |
| 29 // (e.g. all .mp3 files share an icon, all .html files share an icon). For |
| 30 // Windows, certain file types (.exe, .dll, etc) are assumed to have a unique |
| 31 // icon. In that case, each of those files is a group to itself. |
| 32 using IconGroup = base::FilePath::StringType; |
| 33 |
| 36 enum IconSize { | 34 enum IconSize { |
| 37 SMALL = 0, // 16x16 | 35 SMALL = 0, // 16x16 |
| 38 NORMAL, // 32x32 | 36 NORMAL, // 32x32 |
| 39 LARGE, // Windows: 32x32, Linux: 48x48, Mac: Unsupported | 37 LARGE, // Windows: 32x32, Linux: 48x48, Mac: Unsupported |
| 40 ALL, // All sizes available | 38 ALL, // All sizes available |
| 41 }; | 39 }; |
| 42 | 40 |
| 43 class Delegate { | 41 class Delegate { |
| 44 public: | 42 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 | 43 // 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 | 44 // icon has been successfully loaded, |result| is non-null. |group| is the |
| 52 // return true if it is taking ownership of the returned image. | 45 // determined group from the original requested path. |
| 53 virtual bool OnImageLoaded(IconLoader* source, | 46 virtual void OnImageLoaded(IconLoader* source, |
| 54 gfx::Image* result, | 47 std::unique_ptr<gfx::Image> result, |
| 55 const IconGroupID& group) = 0; | 48 const IconGroup& group) = 0; |
| 56 | 49 |
| 57 protected: | 50 protected: |
| 58 virtual ~Delegate() {} | 51 virtual ~Delegate() {} |
| 59 }; | 52 }; |
| 60 | 53 |
| 61 IconLoader(const base::FilePath& file_path, | 54 IconLoader(const IconGroup& group, IconSize size, Delegate* delegate); |
| 62 IconSize size, | 55 |
| 63 Delegate* delegate); | 56 // Given a file path, get the group for the given file. |
| 57 static IconGroup GroupForFilepath(const base::FilePath& path); |
| 64 | 58 |
| 65 // Start reading the icon on the file thread. | 59 // Start reading the icon on the file thread. |
| 66 void Start(); | 60 void Start(); |
| 67 | 61 |
| 68 private: | 62 private: |
| 69 friend class base::RefCountedThreadSafe<IconLoader>; | 63 friend class base::RefCountedThreadSafe<IconLoader>; |
| 70 | 64 |
| 71 virtual ~IconLoader(); | 65 virtual ~IconLoader(); |
| 72 | 66 |
| 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 // The thread ReadIcon() should be called on. | 67 // The thread ReadIcon() should be called on. |
| 81 static content::BrowserThread::ID ReadIconThreadID(); | 68 static content::BrowserThread::ID ReadIconThreadID(); |
| 82 | 69 |
| 83 void ReadGroup(); | |
| 84 void OnReadGroup(); | |
| 85 void ReadIcon(); | 70 void ReadIcon(); |
| 86 | 71 |
| 87 void NotifyDelegate(); | 72 void NotifyDelegate(); |
| 88 | 73 |
| 89 // The task runner object of the thread in which we notify the delegate. | 74 // The task runner object of the thread in which we notify the delegate. |
| 90 scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_; | 75 scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_; |
| 91 | 76 |
| 92 base::FilePath file_path_; | 77 IconGroup group_; |
| 93 | |
| 94 IconGroupID group_; | |
| 95 | 78 |
| 96 IconSize icon_size_; | 79 IconSize icon_size_; |
| 97 | 80 |
| 98 std::unique_ptr<gfx::Image> image_; | 81 std::unique_ptr<gfx::Image> image_; |
| 99 | 82 |
| 100 Delegate* delegate_; | 83 Delegate* delegate_; |
| 101 | 84 |
| 102 DISALLOW_COPY_AND_ASSIGN(IconLoader); | 85 DISALLOW_COPY_AND_ASSIGN(IconLoader); |
| 103 }; | 86 }; |
| 104 | 87 |
| 105 #endif // CHROME_BROWSER_ICON_LOADER_H_ | 88 #endif // CHROME_BROWSER_ICON_LOADER_H_ |
| OLD | NEW |