| 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 #include <utility> | 10 #include <utility> |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 it = cached_icons_.erase(it); | 61 it = cached_icons_.erase(it); |
| 62 else | 62 else |
| 63 ++it; | 63 ++it; |
| 64 } | 64 } |
| 65 } | 65 } |
| 66 | 66 |
| 67 ActivityIconLoader::GetResult ActivityIconLoader::GetActivityIcons( | 67 ActivityIconLoader::GetResult ActivityIconLoader::GetActivityIcons( |
| 68 const std::vector<ActivityName>& activities, | 68 const std::vector<ActivityName>& activities, |
| 69 const OnIconsReadyCallback& cb) { | 69 const OnIconsReadyCallback& cb) { |
| 70 std::unique_ptr<ActivityToIconsMap> result(new ActivityToIconsMap); | 70 std::unique_ptr<ActivityToIconsMap> result(new ActivityToIconsMap); |
| 71 mojo::Array<mojom::ActivityNamePtr> activities_to_fetch; | 71 std::vector<mojom::ActivityNamePtr> activities_to_fetch; |
| 72 | 72 |
| 73 for (const auto& activity : activities) { | 73 for (const auto& activity : activities) { |
| 74 const auto& it = cached_icons_.find(activity); | 74 const auto& it = cached_icons_.find(activity); |
| 75 if (it == cached_icons_.end()) { | 75 if (it == cached_icons_.end()) { |
| 76 mojom::ActivityNamePtr name(mojom::ActivityName::New()); | 76 mojom::ActivityNamePtr name(mojom::ActivityName::New()); |
| 77 name->package_name = activity.package_name; | 77 name->package_name = activity.package_name; |
| 78 name->activity_name = activity.activity_name; | 78 name->activity_name = activity.activity_name; |
| 79 activities_to_fetch.push_back(std::move(name)); | 79 activities_to_fetch.push_back(std::move(name)); |
| 80 } else { | 80 } else { |
| 81 result->insert(std::make_pair(activity, it->second)); | 81 result->insert(std::make_pair(activity, it->second)); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 case GetResult::FAILED_ARC_NOT_READY: | 132 case GetResult::FAILED_ARC_NOT_READY: |
| 133 case GetResult::FAILED_ARC_NOT_SUPPORTED: | 133 case GetResult::FAILED_ARC_NOT_SUPPORTED: |
| 134 break; | 134 break; |
| 135 } | 135 } |
| 136 return true; | 136 return true; |
| 137 } | 137 } |
| 138 | 138 |
| 139 void ActivityIconLoader::OnIconsReady( | 139 void ActivityIconLoader::OnIconsReady( |
| 140 std::unique_ptr<ActivityToIconsMap> cached_result, | 140 std::unique_ptr<ActivityToIconsMap> cached_result, |
| 141 const OnIconsReadyCallback& cb, | 141 const OnIconsReadyCallback& cb, |
| 142 mojo::Array<mojom::ActivityIconPtr> icons) { | 142 std::vector<mojom::ActivityIconPtr> icons) { |
| 143 ArcServiceManager* manager = ArcServiceManager::Get(); | 143 ArcServiceManager* manager = ArcServiceManager::Get(); |
| 144 base::PostTaskAndReplyWithResult( | 144 base::PostTaskAndReplyWithResult( |
| 145 manager->blocking_task_runner().get(), FROM_HERE, | 145 manager->blocking_task_runner().get(), FROM_HERE, |
| 146 base::Bind(&ActivityIconLoader::ResizeIcons, this, base::Passed(&icons)), | 146 base::Bind(&ActivityIconLoader::ResizeIcons, this, base::Passed(&icons)), |
| 147 base::Bind(&ActivityIconLoader::OnIconsResized, this, | 147 base::Bind(&ActivityIconLoader::OnIconsResized, this, |
| 148 base::Passed(&cached_result), cb)); | 148 base::Passed(&cached_result), cb)); |
| 149 } | 149 } |
| 150 | 150 |
| 151 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> | 151 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> |
| 152 ActivityIconLoader::ResizeIcons(mojo::Array<mojom::ActivityIconPtr> icons) { | 152 ActivityIconLoader::ResizeIcons(std::vector<mojom::ActivityIconPtr> icons) { |
| 153 // Runs only on the blocking pool. | 153 // Runs only on the blocking pool. |
| 154 DCHECK(thread_checker_.CalledOnValidThread()); | 154 DCHECK(thread_checker_.CalledOnValidThread()); |
| 155 std::unique_ptr<ActivityToIconsMap> result(new ActivityToIconsMap); | 155 std::unique_ptr<ActivityToIconsMap> result(new ActivityToIconsMap); |
| 156 | 156 |
| 157 for (size_t i = 0; i < icons.size(); ++i) { | 157 for (size_t i = 0; i < icons.size(); ++i) { |
| 158 static const size_t kBytesPerPixel = 4; | 158 static const size_t kBytesPerPixel = 4; |
| 159 const mojom::ActivityIconPtr& icon = icons.at(i); | 159 const mojom::ActivityIconPtr& icon = icons.at(i); |
| 160 if (icon->width > kMaxIconSizeInPx || icon->height > kMaxIconSizeInPx || | 160 if (icon->width > kMaxIconSizeInPx || icon->height > kMaxIconSizeInPx || |
| 161 icon->width == 0 || icon->height == 0 || | 161 icon->width == 0 || icon->height == 0 || |
| 162 icon->icon.size() != (icon->width * icon->height * kBytesPerPixel)) { | 162 icon->icon.size() != (icon->width * icon->height * kBytesPerPixel)) { |
| 163 continue; | 163 continue; |
| 164 } | 164 } |
| 165 | 165 |
| 166 SkBitmap bitmap; | 166 SkBitmap bitmap; |
| 167 bitmap.allocPixels(SkImageInfo::MakeN32Premul(icon->width, icon->height)); | 167 bitmap.allocPixels(SkImageInfo::MakeN32Premul(icon->width, icon->height)); |
| 168 if (!bitmap.getPixels()) | 168 if (!bitmap.getPixels()) |
| 169 continue; | 169 continue; |
| 170 DCHECK_GE(bitmap.getSafeSize(), icon->icon.size()); | 170 DCHECK_GE(bitmap.getSafeSize(), icon->icon.size()); |
| 171 memcpy(bitmap.getPixels(), &icon->icon.front(), icon->icon.size()); | 171 memcpy(bitmap.getPixels(), &icon->icon.front(), icon->icon.size()); |
| 172 | 172 |
| 173 gfx::ImageSkia original(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); | 173 gfx::ImageSkia original(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); |
| 174 // Resize the original icon to the sizes intent_helper needs. | 174 // Resize the original icon to the sizes intent_helper needs. |
| 175 gfx::ImageSkia icon_large(gfx::ImageSkiaOperations::CreateResizedImage( | 175 gfx::ImageSkia icon_large(gfx::ImageSkiaOperations::CreateResizedImage( |
| 176 original, skia::ImageOperations::RESIZE_BEST, | 176 original, skia::ImageOperations::RESIZE_BEST, |
| 177 gfx::Size(kLargeIconSizeInDip, kLargeIconSizeInDip))); | 177 gfx::Size(kLargeIconSizeInDip, kLargeIconSizeInDip))); |
| 178 gfx::ImageSkia icon_small(gfx::ImageSkiaOperations::CreateResizedImage( | 178 gfx::ImageSkia icon_small(gfx::ImageSkiaOperations::CreateResizedImage( |
| 179 original, skia::ImageOperations::RESIZE_BEST, | 179 original, skia::ImageOperations::RESIZE_BEST, |
| 180 gfx::Size(kSmallIconSizeInDip, kSmallIconSizeInDip))); | 180 gfx::Size(kSmallIconSizeInDip, kSmallIconSizeInDip))); |
| 181 | 181 |
| 182 result->insert( | 182 const std::string activity_name = icon->activity->activity_name.has_value() |
| 183 std::make_pair(ActivityName(icon->activity->package_name, | 183 ? (*icon->activity->activity_name) |
| 184 icon->activity->activity_name), | 184 : std::string(); |
| 185 Icons(gfx::Image(icon_small), gfx::Image(icon_large)))); | 185 result->insert(std::make_pair( |
| 186 ActivityName(icon->activity->package_name, activity_name), |
| 187 Icons(gfx::Image(icon_small), gfx::Image(icon_large)))); |
| 186 } | 188 } |
| 187 | 189 |
| 188 return result; | 190 return result; |
| 189 } | 191 } |
| 190 | 192 |
| 191 void ActivityIconLoader::OnIconsResized( | 193 void ActivityIconLoader::OnIconsResized( |
| 192 std::unique_ptr<ActivityToIconsMap> cached_result, | 194 std::unique_ptr<ActivityToIconsMap> cached_result, |
| 193 const OnIconsReadyCallback& cb, | 195 const OnIconsReadyCallback& cb, |
| 194 std::unique_ptr<ActivityToIconsMap> result) { | 196 std::unique_ptr<ActivityToIconsMap> result) { |
| 195 // Update |cached_icons_|. | 197 // Update |cached_icons_|. |
| 196 for (const auto& kv : *result) { | 198 for (const auto& kv : *result) { |
| 197 cached_icons_.erase(kv.first); | 199 cached_icons_.erase(kv.first); |
| 198 cached_icons_.insert(std::make_pair(kv.first, kv.second)); | 200 cached_icons_.insert(std::make_pair(kv.first, kv.second)); |
| 199 } | 201 } |
| 200 | 202 |
| 201 // Merge the results that were obtained from cache before doing IPC. | 203 // Merge the results that were obtained from cache before doing IPC. |
| 202 result->insert(cached_result->begin(), cached_result->end()); | 204 result->insert(cached_result->begin(), cached_result->end()); |
| 203 cb.Run(std::move(result)); | 205 cb.Run(std::move(result)); |
| 204 } | 206 } |
| 205 | 207 |
| 206 } // namespace arc | 208 } // namespace arc |
| OLD | NEW |