Chromium Code Reviews| 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" |
| 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 { |
| 25 | 24 |
| 26 // A class which retrieves an activity icon from ARC. | 25 // A class which retrieves an activity icon from ARC. |
| 27 class ActivityIconLoader : public base::RefCounted<ActivityIconLoader> { | 26 class ActivityIconLoader { |
| 28 public: | 27 public: |
| 29 struct Icons { | 28 struct Icons { |
| 30 Icons(const gfx::Image& icon16, const gfx::Image& icon20, | 29 Icons(const gfx::Image& icon16, const gfx::Image& icon20, |
| 31 const scoped_refptr<base::RefCountedData<GURL>>& icon16_dataurl); | 30 const scoped_refptr<base::RefCountedData<GURL>>& icon16_dataurl); |
| 32 Icons(const Icons& other); | 31 Icons(const Icons& other); |
| 33 ~Icons(); | 32 ~Icons(); |
| 34 | 33 |
| 35 const gfx::Image icon16; // 16 dip | 34 const gfx::Image icon16; // 16 dip |
| 36 const gfx::Image icon20; // 20 dip | 35 const gfx::Image icon20; // 20 dip |
| 37 const scoped_refptr<base::RefCountedData<GURL>> icon16_dataurl; // as URL | 36 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 | 61 // Failed. Either ARC is not supported at all or intent_helper instance |
| 63 // version is too old. | 62 // version is too old. |
| 64 FAILED_ARC_NOT_SUPPORTED, | 63 FAILED_ARC_NOT_SUPPORTED, |
| 65 }; | 64 }; |
| 66 | 65 |
| 67 using ActivityToIconsMap = std::map<ActivityName, Icons>; | 66 using ActivityToIconsMap = std::map<ActivityName, Icons>; |
| 68 using OnIconsReadyCallback = | 67 using OnIconsReadyCallback = |
| 69 base::Callback<void(std::unique_ptr<ActivityToIconsMap>)>; | 68 base::Callback<void(std::unique_ptr<ActivityToIconsMap>)>; |
| 70 | 69 |
| 71 ActivityIconLoader(); | 70 ActivityIconLoader(); |
| 71 ~ActivityIconLoader(); | |
| 72 | 72 |
| 73 // Removes icons associated with |package_name| from the cache. | 73 // Removes icons associated with |package_name| from the cache. |
| 74 void InvalidateIcons(const std::string& package_name); | 74 void InvalidateIcons(const std::string& package_name); |
| 75 | 75 |
| 76 // Retrieves icons for the |activities| and calls |cb|. The |cb| is called | 76 // Retrieves icons for the |activities| and calls |cb|. The |cb| is called |
| 77 // back exactly once, either synchronously in the GetActivityIcons() when | 77 // back exactly once, either synchronously in the GetActivityIcons() when |
| 78 // the result is _not_ SUCCEEDED_ASYNC (i.e. all icons are already cached | 78 // 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 | 79 // locally or ARC is not ready/supported). Otherwise, the callback is run |
| 80 // later asynchronously with icons fetched from ARC side. | 80 // later asynchronously with icons fetched from ARC side. |
| 81 GetResult GetActivityIcons(const std::vector<ActivityName>& activities, | 81 GetResult GetActivityIcons(const std::vector<ActivityName>& activities, |
| 82 const OnIconsReadyCallback& cb); | 82 const OnIconsReadyCallback& cb); |
| 83 | 83 |
| 84 void OnIconsResizedForTesting(const OnIconsReadyCallback& cb, | 84 void OnIconsResizedForTesting(const OnIconsReadyCallback& cb, |
| 85 std::unique_ptr<ActivityToIconsMap> result); | 85 std::unique_ptr<ActivityToIconsMap> result); |
| 86 void AddCacheEntryForTesting(const ActivityName& activity); | 86 void AddCacheEntryForTesting(const ActivityName& activity); |
| 87 | 87 |
| 88 // Returns true if |result| indicates that the |cb| object passed to | 88 // Returns true if |result| indicates that the |cb| object passed to |
| 89 // GetActivityIcons() has already called. | 89 // GetActivityIcons() has already called. |
| 90 static bool HasIconsReadyCallbackRun(GetResult result); | 90 static bool HasIconsReadyCallbackRun(GetResult result); |
| 91 | 91 |
| 92 const ActivityToIconsMap& cached_icons_for_testing() { return cached_icons_; } | 92 const ActivityToIconsMap& cached_icons_for_testing() { return cached_icons_; } |
| 93 | 93 |
| 94 private: | 94 private: |
| 95 friend class base::RefCounted<ActivityIconLoader>; | |
| 96 ~ActivityIconLoader(); | |
| 97 | |
| 98 // A function called when the mojo IPC returns. | 95 // A function called when the mojo IPC returns. |
| 99 void OnIconsReady(std::unique_ptr<ActivityToIconsMap> cached_result, | 96 void OnIconsReady(std::unique_ptr<ActivityToIconsMap> cached_result, |
| 100 const OnIconsReadyCallback& cb, | 97 const OnIconsReadyCallback& cb, |
| 101 std::vector<mojom::ActivityIconPtr> icons); | 98 std::vector<mojom::ActivityIconPtr> icons); |
| 102 | 99 |
| 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 | 100 // A function called when ResizeIcons finishes. Append items in |result| to |
| 108 // |cached_icons_|. | 101 // |cached_icons_|. |
| 109 void OnIconsResized(std::unique_ptr<ActivityToIconsMap> cached_result, | 102 void OnIconsResized(std::unique_ptr<ActivityToIconsMap> cached_result, |
| 110 const OnIconsReadyCallback& cb, | 103 const OnIconsReadyCallback& cb, |
| 111 std::unique_ptr<ActivityToIconsMap> result); | 104 std::unique_ptr<ActivityToIconsMap> result); |
| 112 | 105 |
| 113 // The maximum scale factor the current platform supports. | 106 // The maximum scale factor the current platform supports. |
| 114 const ui::ScaleFactor scale_factor_; | 107 const ui::ScaleFactor scale_factor_; |
| 115 // A map which holds icons in a scale-factor independent form (gfx::Image). | 108 // A map which holds icons in a scale-factor independent form (gfx::Image). |
| 116 ActivityToIconsMap cached_icons_; | 109 ActivityToIconsMap cached_icons_; |
| 117 | 110 |
| 118 base::ThreadChecker thread_checker_; | 111 base::ThreadChecker thread_checker_; |
| 119 | 112 |
| 113 base::WeakPtrFactory<ActivityIconLoader> weak_ptr_factory_; | |
|
Yusuke Sato
2017/01/27 18:21:17
nit: Can you add the usual comment '// this member
hidehiko
2017/01/30 17:04:03
Done.
| |
| 120 DISALLOW_COPY_AND_ASSIGN(ActivityIconLoader); | 114 DISALLOW_COPY_AND_ASSIGN(ActivityIconLoader); |
|
Yusuke Sato
2017/01/27 18:21:17
nit: space between L113/114?
hidehiko
2017/01/30 17:04:03
Done.
| |
| 121 }; | 115 }; |
| 122 | 116 |
| 123 } // namespace arc | 117 } // namespace arc |
| 124 | 118 |
| 125 #endif // COMPONENTS_ARC_INTENT_HELPER_ACTIVITY_ICON_LOADER_H_ | 119 #endif // COMPONENTS_ARC_INTENT_HELPER_ACTIVITY_ICON_LOADER_H_ |
| OLD | NEW |