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/callback.h" |
11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
12 #include "base/macros.h" | 13 #include "base/macros.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 //////////////////////////////////////////////////////////////////////////////// | 19 //////////////////////////////////////////////////////////////////////////////// |
20 // | 20 // |
21 // 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 |
22 // thread. Returns the icon in the form of an ImageSkia. | 22 // thread. Returns the icon in the form of an ImageSkia. |
23 // | 23 // |
24 //////////////////////////////////////////////////////////////////////////////// | 24 //////////////////////////////////////////////////////////////////////////////// |
25 class IconLoader : public base::RefCountedThreadSafe<IconLoader> { | 25 class IconLoader { |
26 public: | 26 public: |
27 // An IconGroup is a class of files that all share the same icon. For all | 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 | 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). On | 29 // (e.g. all .mp3 files share an icon, all .html files share an icon). On |
30 // Windows, for certain file types (.exe, .dll, etc), each file of that type | 30 // Windows, for certain file types (.exe, .dll, etc), each file of that type |
31 // is assumed to have a unique icon. In that case, each of those files is a | 31 // is assumed to have a unique icon. In that case, each of those files is a |
32 // group to itself. | 32 // group to itself. |
33 using IconGroup = base::FilePath::StringType; | 33 using IconGroup = base::FilePath::StringType; |
34 | 34 |
35 enum IconSize { | 35 enum IconSize { |
36 SMALL = 0, // 16x16 | 36 SMALL = 0, // 16x16 |
37 NORMAL, // 32x32 | 37 NORMAL, // 32x32 |
38 LARGE, // Windows: 32x32, Linux: 48x48, Mac: Unsupported | 38 LARGE, // Windows: 32x32, Linux: 48x48, Mac: Unsupported |
39 ALL, // All sizes available | 39 ALL, // All sizes available |
40 }; | 40 }; |
41 | 41 |
42 class Delegate { | 42 // The callback invoked when an icon has been read. The parameters are: |
43 public: | 43 // - The IconLoader. |
44 // Invoked when an icon has been read. |source| is the IconLoader. If the | 44 // - The icon that was loaded, or null if there was a failure to load it. |
45 // icon has been successfully loaded, |result| is non-null. |group| is the | 45 // - The determined group from the original requested path. |
46 // determined group from the original requested path. | 46 using IconLoadedCallback = base::OnceCallback< |
47 virtual void OnImageLoaded(IconLoader* source, | 47 void(IconLoader*, std::unique_ptr<gfx::Image>, const IconGroup&)>; |
48 std::unique_ptr<gfx::Image> result, | |
49 const IconGroup& group) = 0; | |
50 | |
51 protected: | |
52 virtual ~Delegate() {} | |
53 }; | |
54 | 48 |
55 IconLoader(const base::FilePath& file_path, | 49 IconLoader(const base::FilePath& file_path, |
56 IconSize size, | 50 IconSize size, |
57 Delegate* delegate); | 51 IconLoadedCallback callback); |
| 52 ~IconLoader(); |
58 | 53 |
59 // Start reading the icon on the file thread. | 54 // Start reading the icon on the file thread. |
60 void Start(); | 55 void Start(); |
61 | 56 |
62 private: | 57 private: |
63 friend class base::RefCountedThreadSafe<IconLoader>; | |
64 | |
65 virtual ~IconLoader(); | |
66 | |
67 // Given a file path, get the group for the given file. | 58 // Given a file path, get the group for the given file. |
68 static IconGroup GroupForFilepath(const base::FilePath& file_path); | 59 static IconGroup GroupForFilepath(const base::FilePath& file_path); |
69 | 60 |
70 // The thread ReadIcon() should be called on. | 61 // The thread ReadIcon() should be called on. |
71 static content::BrowserThread::ID ReadIconThreadID(); | 62 static content::BrowserThread::ID ReadIconThreadID(); |
72 | 63 |
73 void ReadGroup(); | 64 void ReadGroup(); |
74 void OnReadGroup(); | 65 void OnReadGroup(); |
75 void ReadIcon(); | 66 void ReadIcon(); |
76 | 67 |
77 void NotifyDelegate(); | |
78 | |
79 // The task runner object of the thread in which we notify the delegate. | 68 // The task runner object of the thread in which we notify the delegate. |
80 scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_; | 69 scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_; |
81 | 70 |
82 base::FilePath file_path_; | 71 base::FilePath file_path_; |
83 | 72 |
84 IconGroup group_; | 73 IconGroup group_; |
85 | 74 |
86 IconSize icon_size_; | 75 IconSize icon_size_; |
87 | 76 |
88 std::unique_ptr<gfx::Image> image_; | 77 std::unique_ptr<gfx::Image> image_; |
89 | 78 |
90 Delegate* delegate_; | 79 IconLoadedCallback callback_; |
91 | 80 |
92 DISALLOW_COPY_AND_ASSIGN(IconLoader); | 81 DISALLOW_COPY_AND_ASSIGN(IconLoader); |
93 }; | 82 }; |
94 | 83 |
95 #endif // CHROME_BROWSER_ICON_LOADER_H_ | 84 #endif // CHROME_BROWSER_ICON_LOADER_H_ |
OLD | NEW |