| 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 #include "components/arc/intent_helper/activity_icon_loader.h" | 5 #include "components/arc/intent_helper/activity_icon_loader.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <tuple> | 9 #include <tuple> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/task_runner_util.h" | 13 #include "base/task_runner_util.h" |
| 14 #include "components/arc/arc_bridge_service.h" | 14 #include "components/arc/arc_bridge_service.h" |
| 15 #include "components/arc/arc_service_manager.h" | 15 #include "components/arc/arc_service_manager.h" |
| 16 #include "ui/base/layout.h" | 16 #include "ui/base/layout.h" |
| 17 #include "ui/gfx/image/image_skia.h" | 17 #include "ui/gfx/image/image_skia.h" |
| 18 #include "ui/gfx/image/image_skia_operations.h" | 18 #include "ui/gfx/image/image_skia_operations.h" |
| 19 | 19 |
| 20 namespace arc { | 20 namespace arc { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 const size_t kSmallIconSizeInDip = 16; | 24 const size_t kSmallIconSizeInDip = 16; |
| 25 const size_t kLargeIconSizeInDip = 48; | 25 const size_t kLargeIconSizeInDip = 48; |
| 26 const size_t kMaxIconSizeInPx = 200; | 26 const size_t kMaxIconSizeInPx = 200; |
| 27 | 27 |
| 28 const int kMinInstanceVersion = 3; // see intent_helper.mojom | 28 const int kMinInstanceVersion = 3; // see intent_helper.mojom |
| 29 | 29 |
| 30 mojom::IntentHelperInstance* GetIntentHelperInstance() { | 30 mojom::IntentHelperInstance* GetIntentHelperInstance( |
| 31 ActivityIconLoader::GetResult* out_error_code) { |
| 32 DCHECK(out_error_code); |
| 31 ArcBridgeService* bridge_service = arc::ArcBridgeService::Get(); | 33 ArcBridgeService* bridge_service = arc::ArcBridgeService::Get(); |
| 32 if (!bridge_service) { | 34 if (!bridge_service) { |
| 33 VLOG(2) << "ARC bridge is not ready."; | 35 VLOG(2) << "ARC bridge is not ready."; |
| 36 *out_error_code = ActivityIconLoader::GetResult::FAILED_ARC_NOT_SUPPORTED; |
| 34 return nullptr; | 37 return nullptr; |
| 35 } | 38 } |
| 36 mojom::IntentHelperInstance* intent_helper_instance = | 39 mojom::IntentHelperInstance* intent_helper_instance = |
| 37 bridge_service->intent_helper_instance(); | 40 bridge_service->intent_helper_instance(); |
| 38 if (!intent_helper_instance) { | 41 if (!intent_helper_instance) { |
| 39 VLOG(2) << "ARC intent helper instance is not ready."; | 42 VLOG(2) << "ARC intent helper instance is not ready."; |
| 43 *out_error_code = ActivityIconLoader::GetResult::FAILED_ARC_NOT_READY; |
| 40 return nullptr; | 44 return nullptr; |
| 41 } | 45 } |
| 42 if (bridge_service->intent_helper_version() < kMinInstanceVersion) { | 46 if (bridge_service->intent_helper_version() < kMinInstanceVersion) { |
| 43 VLOG(1) << "ARC intent helper instance is too old."; | 47 VLOG(1) << "ARC intent helper instance is too old."; |
| 48 *out_error_code = ActivityIconLoader::GetResult::FAILED_ARC_NOT_SUPPORTED; |
| 44 return nullptr; | 49 return nullptr; |
| 45 } | 50 } |
| 46 return intent_helper_instance; | 51 return intent_helper_instance; |
| 47 } | 52 } |
| 48 | 53 |
| 49 ui::ScaleFactor GetSupportedScaleFactor() { | 54 ui::ScaleFactor GetSupportedScaleFactor() { |
| 50 std::vector<ui::ScaleFactor> scale_factors = ui::GetSupportedScaleFactors(); | 55 std::vector<ui::ScaleFactor> scale_factors = ui::GetSupportedScaleFactors(); |
| 51 DCHECK(!scale_factors.empty()); | 56 DCHECK(!scale_factors.empty()); |
| 52 return scale_factors.back(); | 57 return scale_factors.back(); |
| 53 } | 58 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 75 | 80 |
| 76 void ActivityIconLoader::InvalidateIcons(const std::string& package_name) { | 81 void ActivityIconLoader::InvalidateIcons(const std::string& package_name) { |
| 77 for (auto it = cached_icons_.begin(); it != cached_icons_.end();) { | 82 for (auto it = cached_icons_.begin(); it != cached_icons_.end();) { |
| 78 if (it->first.package_name == package_name) | 83 if (it->first.package_name == package_name) |
| 79 it = cached_icons_.erase(it); | 84 it = cached_icons_.erase(it); |
| 80 else | 85 else |
| 81 ++it; | 86 ++it; |
| 82 } | 87 } |
| 83 } | 88 } |
| 84 | 89 |
| 85 bool ActivityIconLoader::GetActivityIcons( | 90 ActivityIconLoader::GetResult ActivityIconLoader::GetActivityIcons( |
| 86 const std::vector<ActivityName>& activities, | 91 const std::vector<ActivityName>& activities, |
| 87 const OnIconsReadyCallback& cb) { | 92 const OnIconsReadyCallback& cb) { |
| 88 std::unique_ptr<ActivityToIconsMap> result(new ActivityToIconsMap); | 93 std::unique_ptr<ActivityToIconsMap> result(new ActivityToIconsMap); |
| 89 mojo::Array<mojom::ActivityNamePtr> activities_to_fetch; | 94 mojo::Array<mojom::ActivityNamePtr> activities_to_fetch; |
| 90 | 95 |
| 91 for (const auto& activity : activities) { | 96 for (const auto& activity : activities) { |
| 92 const auto& it = cached_icons_.find(activity); | 97 const auto& it = cached_icons_.find(activity); |
| 93 if (it == cached_icons_.end()) { | 98 if (it == cached_icons_.end()) { |
| 94 mojom::ActivityNamePtr name(mojom::ActivityName::New()); | 99 mojom::ActivityNamePtr name(mojom::ActivityName::New()); |
| 95 name->package_name = activity.package_name; | 100 name->package_name = activity.package_name; |
| 96 name->activity_name = activity.activity_name; | 101 name->activity_name = activity.activity_name; |
| 97 activities_to_fetch.push_back(std::move(name)); | 102 activities_to_fetch.push_back(std::move(name)); |
| 98 } else { | 103 } else { |
| 99 result->insert(std::make_pair(activity, it->second)); | 104 result->insert(std::make_pair(activity, it->second)); |
| 100 } | 105 } |
| 101 } | 106 } |
| 102 | 107 |
| 103 mojom::IntentHelperInstance* instance = GetIntentHelperInstance(); | 108 if (activities_to_fetch.empty()) { |
| 104 if (activities_to_fetch.empty() || !instance) { | 109 // If there's nothing to fetch, run the callback now. |
| 105 // If there's nothing to fetch or the mojo channel is not available, call | |
| 106 // back right now. | |
| 107 cb.Run(std::move(result)); | 110 cb.Run(std::move(result)); |
| 108 return true; | 111 return GetResult::SUCCEEDED_SYNC; |
| 112 } |
| 113 |
| 114 GetResult error_code = GetResult::FAILED_ARC_NOT_SUPPORTED; |
| 115 mojom::IntentHelperInstance* instance = GetIntentHelperInstance(&error_code); |
| 116 if (!instance) { |
| 117 // The mojo channel is not yet ready (or not supported at all). Run the |
| 118 // callback with |result| that could be empty. |
| 119 cb.Run(std::move(result)); |
| 120 return error_code; |
| 109 } | 121 } |
| 110 | 122 |
| 111 // Fetch icons from ARC. | 123 // Fetch icons from ARC. |
| 112 instance->RequestActivityIcons( | 124 instance->RequestActivityIcons( |
| 113 std::move(activities_to_fetch), mojom::ScaleFactor(scale_factor_), | 125 std::move(activities_to_fetch), mojom::ScaleFactor(scale_factor_), |
| 114 base::Bind(&ActivityIconLoader::OnIconsReady, this, base::Passed(&result), | 126 base::Bind(&ActivityIconLoader::OnIconsReady, this, base::Passed(&result), |
| 115 cb)); | 127 cb)); |
| 116 return false; | 128 return GetResult::SUCCEEDED_ASYNC; |
| 117 } | 129 } |
| 118 | 130 |
| 119 void ActivityIconLoader::OnIconsResizedForTesting( | 131 void ActivityIconLoader::OnIconsResizedForTesting( |
| 120 const OnIconsReadyCallback& cb, | 132 const OnIconsReadyCallback& cb, |
| 121 std::unique_ptr<ActivityToIconsMap> result) { | 133 std::unique_ptr<ActivityToIconsMap> result) { |
| 122 OnIconsResized(base::MakeUnique<ActivityToIconsMap>(), cb, std::move(result)); | 134 OnIconsResized(base::MakeUnique<ActivityToIconsMap>(), cb, std::move(result)); |
| 123 } | 135 } |
| 124 | 136 |
| 125 void ActivityIconLoader::AddIconToCacheForTesting(const ActivityName& activity, | 137 void ActivityIconLoader::AddIconToCacheForTesting(const ActivityName& activity, |
| 126 const gfx::Image& image) { | 138 const gfx::Image& image) { |
| 127 cached_icons_.insert(std::make_pair(activity, Icons(image, image))); | 139 cached_icons_.insert(std::make_pair(activity, Icons(image, image))); |
| 128 } | 140 } |
| 129 | 141 |
| 142 // static |
| 143 bool ActivityIconLoader::HasIconsReadyCallbackRun(GetResult result) { |
| 144 switch (result) { |
| 145 case GetResult::SUCCEEDED_ASYNC: |
| 146 return false; |
| 147 case GetResult::SUCCEEDED_SYNC: |
| 148 case GetResult::FAILED_ARC_NOT_READY: |
| 149 case GetResult::FAILED_ARC_NOT_SUPPORTED: |
| 150 break; |
| 151 } |
| 152 return true; |
| 153 } |
| 154 |
| 130 void ActivityIconLoader::OnIconsReady( | 155 void ActivityIconLoader::OnIconsReady( |
| 131 std::unique_ptr<ActivityToIconsMap> cached_result, | 156 std::unique_ptr<ActivityToIconsMap> cached_result, |
| 132 const OnIconsReadyCallback& cb, | 157 const OnIconsReadyCallback& cb, |
| 133 mojo::Array<mojom::ActivityIconPtr> icons) { | 158 mojo::Array<mojom::ActivityIconPtr> icons) { |
| 134 ArcServiceManager* manager = ArcServiceManager::Get(); | 159 ArcServiceManager* manager = ArcServiceManager::Get(); |
| 135 base::PostTaskAndReplyWithResult( | 160 base::PostTaskAndReplyWithResult( |
| 136 manager->blocking_task_runner().get(), FROM_HERE, | 161 manager->blocking_task_runner().get(), FROM_HERE, |
| 137 base::Bind(&ActivityIconLoader::ResizeIcons, this, base::Passed(&icons)), | 162 base::Bind(&ActivityIconLoader::ResizeIcons, this, base::Passed(&icons)), |
| 138 base::Bind(&ActivityIconLoader::OnIconsResized, this, | 163 base::Bind(&ActivityIconLoader::OnIconsResized, this, |
| 139 base::Passed(&cached_result), cb)); | 164 base::Passed(&cached_result), cb)); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 cached_icons_.erase(kv.first); | 213 cached_icons_.erase(kv.first); |
| 189 cached_icons_.insert(std::make_pair(kv.first, kv.second)); | 214 cached_icons_.insert(std::make_pair(kv.first, kv.second)); |
| 190 } | 215 } |
| 191 | 216 |
| 192 // Merge the results that were obtained from cache before doing IPC. | 217 // Merge the results that were obtained from cache before doing IPC. |
| 193 result->insert(cached_result->begin(), cached_result->end()); | 218 result->insert(cached_result->begin(), cached_result->end()); |
| 194 cb.Run(std::move(result)); | 219 cb.Run(std::move(result)); |
| 195 } | 220 } |
| 196 | 221 |
| 197 } // namespace arc | 222 } // namespace arc |
| OLD | NEW |