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::Callback< |
47 virtual void OnImageLoaded(IconLoader* source, | 47 void(IconLoader*, std::unique_ptr<gfx::Image>, const IconGroup&)>; |
sky
2016/12/16 23:27:27
Given that the IconLoader may be deleted by the ti
Avi (use Gerrit)
2016/12/17 02:28:53
Or drop it, and have callers pre-bind the info the
| |
48 std::unique_ptr<gfx::Image> result, | |
49 const IconGroup& group) = 0; | |
50 | 48 |
51 protected: | 49 // Creates an IconLoader, which owns itself. If the IconLoader might outlive |
52 virtual ~Delegate() {} | 50 // the caller, be sure to use a weak pointer in the |callback|. |
53 }; | 51 static IconLoader* Create(const base::FilePath& file_path, |
52 IconSize size, | |
53 IconLoadedCallback callback); | |
54 | 54 |
55 IconLoader(const base::FilePath& file_path, | 55 // Starts the process of reading the icon. When the reading of the icon is |
56 IconSize size, | 56 // complete, the IconLoadedCallback callback will be fulfilled, and the |
57 Delegate* delegate); | 57 // IconLoader will delete itself. |
58 | |
59 // Start reading the icon on the file thread. | |
60 void Start(); | 58 void Start(); |
61 | 59 |
62 private: | 60 private: |
63 friend class base::RefCountedThreadSafe<IconLoader>; | 61 IconLoader(const base::FilePath& file_path, |
62 IconSize size, | |
63 IconLoadedCallback callback); | |
64 | 64 |
65 virtual ~IconLoader(); | 65 ~IconLoader(); |
66 | 66 |
67 // Given a file path, get the group for the given file. | 67 // Given a file path, get the group for the given file. |
68 static IconGroup GroupForFilepath(const base::FilePath& file_path); | 68 static IconGroup GroupForFilepath(const base::FilePath& file_path); |
69 | 69 |
70 // The thread ReadIcon() should be called on. | 70 // The thread ReadIcon() should be called on. |
71 static content::BrowserThread::ID ReadIconThreadID(); | 71 static content::BrowserThread::ID ReadIconThreadID(); |
72 | 72 |
73 void ReadGroup(); | 73 void ReadGroup(); |
74 void OnReadGroup(); | 74 void OnReadGroup(); |
75 void ReadIcon(); | 75 void ReadIcon(); |
76 | 76 |
77 void NotifyDelegate(); | |
78 | |
79 // The task runner object of the thread in which we notify the delegate. | 77 // The task runner object of the thread in which we notify the delegate. |
80 scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_; | 78 scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_; |
81 | 79 |
82 base::FilePath file_path_; | 80 base::FilePath file_path_; |
83 | 81 |
84 IconGroup group_; | 82 IconGroup group_; |
85 | 83 |
86 IconSize icon_size_; | 84 IconSize icon_size_; |
87 | 85 |
88 std::unique_ptr<gfx::Image> image_; | 86 std::unique_ptr<gfx::Image> image_; |
89 | 87 |
90 Delegate* delegate_; | 88 IconLoadedCallback callback_; |
91 | 89 |
92 DISALLOW_COPY_AND_ASSIGN(IconLoader); | 90 DISALLOW_COPY_AND_ASSIGN(IconLoader); |
93 }; | 91 }; |
94 | 92 |
95 #endif // CHROME_BROWSER_ICON_LOADER_H_ | 93 #endif // CHROME_BROWSER_ICON_LOADER_H_ |
OLD | NEW |