OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 COMPONENTS_ARC_INTENT_HELPER_ACTIVITY_ICON_LOADER_H_ | 5 #ifndef COMPONENTS_ARC_INTENT_HELPER_ACTIVITY_ICON_LOADER_H_ |
6 #define COMPONENTS_ARC_INTENT_HELPER_ACTIVITY_ICON_LOADER_H_ | 6 #define COMPONENTS_ARC_INTENT_HELPER_ACTIVITY_ICON_LOADER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/weak_ptr.h" |
Yusuke Sato
2017/01/30 20:38:26
refptr still in use at L37?
hidehiko
2017/01/31 05:54:08
Good catch. Revived.
| |
16 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
17 #include "components/arc/arc_service.h" | 17 #include "components/arc/arc_service.h" |
18 #include "components/arc/common/intent_helper.mojom.h" | 18 #include "components/arc/common/intent_helper.mojom.h" |
19 #include "components/arc/intent_helper/arc_intent_helper_bridge.h" | |
20 #include "ui/base/layout.h" | 19 #include "ui/base/layout.h" |
21 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
22 #include "url/gurl.h" | 21 #include "url/gurl.h" |
23 | 22 |
24 namespace arc { | 23 namespace arc { |
24 namespace internal { | |
25 | 25 |
26 // A class which retrieves an activity icon from ARC. | 26 // A class which retrieves an activity icon from ARC. |
27 class ActivityIconLoader : public base::RefCounted<ActivityIconLoader> { | 27 class ActivityIconLoader { |
28 public: | 28 public: |
29 struct Icons { | 29 struct Icons { |
30 Icons(const gfx::Image& icon16, const gfx::Image& icon20, | 30 Icons(const gfx::Image& icon16, const gfx::Image& icon20, |
31 const scoped_refptr<base::RefCountedData<GURL>>& icon16_dataurl); | 31 const scoped_refptr<base::RefCountedData<GURL>>& icon16_dataurl); |
32 Icons(const Icons& other); | 32 Icons(const Icons& other); |
33 ~Icons(); | 33 ~Icons(); |
34 | 34 |
35 const gfx::Image icon16; // 16 dip | 35 const gfx::Image icon16; // 16 dip |
36 const gfx::Image icon20; // 20 dip | 36 const gfx::Image icon20; // 20 dip |
37 const scoped_refptr<base::RefCountedData<GURL>> icon16_dataurl; // as URL | 37 const scoped_refptr<base::RefCountedData<GURL>> icon16_dataurl; // as URL |
(...skipping 24 matching lines...) Expand all Loading... | |
62 // Failed. Either ARC is not supported at all or intent_helper instance | 62 // Failed. Either ARC is not supported at all or intent_helper instance |
63 // version is too old. | 63 // version is too old. |
64 FAILED_ARC_NOT_SUPPORTED, | 64 FAILED_ARC_NOT_SUPPORTED, |
65 }; | 65 }; |
66 | 66 |
67 using ActivityToIconsMap = std::map<ActivityName, Icons>; | 67 using ActivityToIconsMap = std::map<ActivityName, Icons>; |
68 using OnIconsReadyCallback = | 68 using OnIconsReadyCallback = |
69 base::Callback<void(std::unique_ptr<ActivityToIconsMap>)>; | 69 base::Callback<void(std::unique_ptr<ActivityToIconsMap>)>; |
70 | 70 |
71 ActivityIconLoader(); | 71 ActivityIconLoader(); |
72 ~ActivityIconLoader(); | |
72 | 73 |
73 // Removes icons associated with |package_name| from the cache. | 74 // Removes icons associated with |package_name| from the cache. |
74 void InvalidateIcons(const std::string& package_name); | 75 void InvalidateIcons(const std::string& package_name); |
75 | 76 |
76 // Retrieves icons for the |activities| and calls |cb|. The |cb| is called | 77 // Retrieves icons for the |activities| and calls |cb|. The |cb| is called |
77 // back exactly once, either synchronously in the GetActivityIcons() when | 78 // back exactly once, either synchronously in the GetActivityIcons() when |
78 // the result is _not_ SUCCEEDED_ASYNC (i.e. all icons are already cached | 79 // the result is _not_ SUCCEEDED_ASYNC (i.e. all icons are already cached |
79 // locally or ARC is not ready/supported). Otherwise, the callback is run | 80 // locally or ARC is not ready/supported). Otherwise, the callback is run |
80 // later asynchronously with icons fetched from ARC side. | 81 // later asynchronously with icons fetched from ARC side. |
81 GetResult GetActivityIcons(const std::vector<ActivityName>& activities, | 82 GetResult GetActivityIcons(const std::vector<ActivityName>& activities, |
82 const OnIconsReadyCallback& cb); | 83 const OnIconsReadyCallback& cb); |
83 | 84 |
84 void OnIconsResizedForTesting(const OnIconsReadyCallback& cb, | 85 void OnIconsResizedForTesting(const OnIconsReadyCallback& cb, |
85 std::unique_ptr<ActivityToIconsMap> result); | 86 std::unique_ptr<ActivityToIconsMap> result); |
86 void AddCacheEntryForTesting(const ActivityName& activity); | 87 void AddCacheEntryForTesting(const ActivityName& activity); |
87 | 88 |
88 // Returns true if |result| indicates that the |cb| object passed to | 89 // Returns true if |result| indicates that the |cb| object passed to |
89 // GetActivityIcons() has already called. | 90 // GetActivityIcons() has already called. |
90 static bool HasIconsReadyCallbackRun(GetResult result); | 91 static bool HasIconsReadyCallbackRun(GetResult result); |
91 | 92 |
92 const ActivityToIconsMap& cached_icons_for_testing() { return cached_icons_; } | 93 const ActivityToIconsMap& cached_icons_for_testing() { return cached_icons_; } |
93 | 94 |
94 private: | 95 private: |
95 friend class base::RefCounted<ActivityIconLoader>; | |
96 ~ActivityIconLoader(); | |
97 | |
98 // A function called when the mojo IPC returns. | 96 // A function called when the mojo IPC returns. |
99 void OnIconsReady(std::unique_ptr<ActivityToIconsMap> cached_result, | 97 void OnIconsReady(std::unique_ptr<ActivityToIconsMap> cached_result, |
100 const OnIconsReadyCallback& cb, | 98 const OnIconsReadyCallback& cb, |
101 std::vector<mojom::ActivityIconPtr> icons); | 99 std::vector<mojom::ActivityIconPtr> icons); |
102 | 100 |
103 // Resizes and encodes |icons| and returns the results as ActivityToIconsMap. | |
104 static std::unique_ptr<ActivityToIconsMap> ResizeAndEncodeIcons( | |
105 std::vector<mojom::ActivityIconPtr> icons); | |
106 | |
107 // A function called when ResizeIcons finishes. Append items in |result| to | 101 // A function called when ResizeIcons finishes. Append items in |result| to |
108 // |cached_icons_|. | 102 // |cached_icons_|. |
109 void OnIconsResized(std::unique_ptr<ActivityToIconsMap> cached_result, | 103 void OnIconsResized(std::unique_ptr<ActivityToIconsMap> cached_result, |
110 const OnIconsReadyCallback& cb, | 104 const OnIconsReadyCallback& cb, |
111 std::unique_ptr<ActivityToIconsMap> result); | 105 std::unique_ptr<ActivityToIconsMap> result); |
112 | 106 |
113 // The maximum scale factor the current platform supports. | 107 // The maximum scale factor the current platform supports. |
114 const ui::ScaleFactor scale_factor_; | 108 const ui::ScaleFactor scale_factor_; |
115 // A map which holds icons in a scale-factor independent form (gfx::Image). | 109 // A map which holds icons in a scale-factor independent form (gfx::Image). |
116 ActivityToIconsMap cached_icons_; | 110 ActivityToIconsMap cached_icons_; |
117 | 111 |
118 base::ThreadChecker thread_checker_; | 112 base::ThreadChecker thread_checker_; |
119 | 113 |
114 // This must come last to make sure weak pointers are invalidated first. | |
115 base::WeakPtrFactory<ActivityIconLoader> weak_ptr_factory_; | |
116 | |
120 DISALLOW_COPY_AND_ASSIGN(ActivityIconLoader); | 117 DISALLOW_COPY_AND_ASSIGN(ActivityIconLoader); |
121 }; | 118 }; |
122 | 119 |
120 } // namespace internal | |
123 } // namespace arc | 121 } // namespace arc |
124 | 122 |
125 #endif // COMPONENTS_ARC_INTENT_HELPER_ACTIVITY_ICON_LOADER_H_ | 123 #endif // COMPONENTS_ARC_INTENT_HELPER_ACTIVITY_ICON_LOADER_H_ |
OLD | NEW |