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 icon that was loaded, or null if there was a failure to load it. |
44 // Invoked when an icon has been read. |source| is the IconLoader. If the | 44 // - The determined group from the original requested path. |
45 // icon has been successfully loaded, |result| is non-null. |group| is the | 45 using IconLoadedCallback = |
46 // determined group from the original requested path. | 46 base::Callback<void(std::unique_ptr<gfx::Image>, const IconGroup&)>; |
47 virtual void OnImageLoaded(IconLoader* source, | |
48 std::unique_ptr<gfx::Image> result, | |
49 const IconGroup& group) = 0; | |
50 | 47 |
51 protected: | 48 // Creates an IconLoader, which owns itself. If the IconLoader might outlive |
52 virtual ~Delegate() {} | 49 // the caller, be sure to use a weak pointer in the |callback|. |
53 }; | 50 static IconLoader* Create(const base::FilePath& file_path, |
| 51 IconSize size, |
| 52 IconLoadedCallback callback); |
54 | 53 |
55 IconLoader(const base::FilePath& file_path, | 54 // Starts the process of reading the icon. When the reading of the icon is |
56 IconSize size, | 55 // complete, the IconLoadedCallback callback will be fulfilled, and the |
57 Delegate* delegate); | 56 // IconLoader will delete itself. |
58 | |
59 // Start reading the icon on the file thread. | |
60 void Start(); | 57 void Start(); |
61 | 58 |
62 private: | 59 private: |
63 friend class base::RefCountedThreadSafe<IconLoader>; | 60 IconLoader(const base::FilePath& file_path, |
| 61 IconSize size, |
| 62 IconLoadedCallback callback); |
64 | 63 |
65 virtual ~IconLoader(); | 64 ~IconLoader(); |
66 | 65 |
67 // Given a file path, get the group for the given file. | 66 // Given a file path, get the group for the given file. |
68 static IconGroup GroupForFilepath(const base::FilePath& file_path); | 67 static IconGroup GroupForFilepath(const base::FilePath& file_path); |
69 | 68 |
70 // The thread ReadIcon() should be called on. | 69 // The thread ReadIcon() should be called on. |
71 static content::BrowserThread::ID ReadIconThreadID(); | 70 static content::BrowserThread::ID ReadIconThreadID(); |
72 | 71 |
73 void ReadGroup(); | 72 void ReadGroup(); |
74 void OnReadGroup(); | 73 void OnReadGroup(); |
75 void ReadIcon(); | 74 void ReadIcon(); |
76 | 75 |
77 void NotifyDelegate(); | |
78 | |
79 // The task runner object of the thread in which we notify the delegate. | 76 // The task runner object of the thread in which we notify the delegate. |
80 scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_; | 77 scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_; |
81 | 78 |
82 base::FilePath file_path_; | 79 base::FilePath file_path_; |
83 | 80 |
84 IconGroup group_; | 81 IconGroup group_; |
85 | 82 |
86 IconSize icon_size_; | 83 IconSize icon_size_; |
87 | 84 |
88 std::unique_ptr<gfx::Image> image_; | 85 std::unique_ptr<gfx::Image> image_; |
89 | 86 |
90 Delegate* delegate_; | 87 IconLoadedCallback callback_; |
91 | 88 |
92 DISALLOW_COPY_AND_ASSIGN(IconLoader); | 89 DISALLOW_COPY_AND_ASSIGN(IconLoader); |
93 }; | 90 }; |
94 | 91 |
95 #endif // CHROME_BROWSER_ICON_LOADER_H_ | 92 #endif // CHROME_BROWSER_ICON_LOADER_H_ |
OLD | NEW |