Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(398)

Side by Side Diff: components/arc/intent_helper/activity_icon_loader.cc

Issue 2655213004: Avoid retaining a reference to ActivityIconLoader in its OnIconReady() (Closed)
Patch Set: +-DCHECK(thread_checker_.COVT()); Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/arc/intent_helper/activity_icon_loader.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 return std::tie(package_name, activity_name) < 70 return std::tie(package_name, activity_name) <
71 std::tie(other.package_name, other.activity_name); 71 std::tie(other.package_name, other.activity_name);
72 } 72 }
73 73
74 ActivityIconLoader::ActivityIconLoader() 74 ActivityIconLoader::ActivityIconLoader()
75 : scale_factor_(GetSupportedScaleFactor()) {} 75 : scale_factor_(GetSupportedScaleFactor()) {}
76 76
77 ActivityIconLoader::~ActivityIconLoader() {} 77 ActivityIconLoader::~ActivityIconLoader() {}
78 78
79 void ActivityIconLoader::InvalidateIcons(const std::string& package_name) { 79 void ActivityIconLoader::InvalidateIcons(const std::string& package_name) {
80 DCHECK(thread_checker_.CalledOnValidThread());
80 for (auto it = cached_icons_.begin(); it != cached_icons_.end();) { 81 for (auto it = cached_icons_.begin(); it != cached_icons_.end();) {
81 if (it->first.package_name == package_name) 82 if (it->first.package_name == package_name)
82 it = cached_icons_.erase(it); 83 it = cached_icons_.erase(it);
83 else 84 else
84 ++it; 85 ++it;
85 } 86 }
86 } 87 }
87 88
88 ActivityIconLoader::GetResult ActivityIconLoader::GetActivityIcons( 89 ActivityIconLoader::GetResult ActivityIconLoader::GetActivityIcons(
89 const std::vector<ActivityName>& activities, 90 const std::vector<ActivityName>& activities,
90 const OnIconsReadyCallback& cb) { 91 const OnIconsReadyCallback& cb) {
92 DCHECK(thread_checker_.CalledOnValidThread());
91 std::unique_ptr<ActivityToIconsMap> result(new ActivityToIconsMap); 93 std::unique_ptr<ActivityToIconsMap> result(new ActivityToIconsMap);
92 std::vector<mojom::ActivityNamePtr> activities_to_fetch; 94 std::vector<mojom::ActivityNamePtr> activities_to_fetch;
93 95
94 for (const auto& activity : activities) { 96 for (const auto& activity : activities) {
95 const auto& it = cached_icons_.find(activity); 97 const auto& it = cached_icons_.find(activity);
96 if (it == cached_icons_.end()) { 98 if (it == cached_icons_.end()) {
97 mojom::ActivityNamePtr name(mojom::ActivityName::New()); 99 mojom::ActivityNamePtr name(mojom::ActivityName::New());
98 name->package_name = activity.package_name; 100 name->package_name = activity.package_name;
99 name->activity_name = activity.activity_name; 101 name->activity_name = activity.activity_name;
100 activities_to_fetch.push_back(std::move(name)); 102 activities_to_fetch.push_back(std::move(name));
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 case GetResult::FAILED_ARC_NOT_SUPPORTED: 155 case GetResult::FAILED_ARC_NOT_SUPPORTED:
154 break; 156 break;
155 } 157 }
156 return true; 158 return true;
157 } 159 }
158 160
159 void ActivityIconLoader::OnIconsReady( 161 void ActivityIconLoader::OnIconsReady(
160 std::unique_ptr<ActivityToIconsMap> cached_result, 162 std::unique_ptr<ActivityToIconsMap> cached_result,
161 const OnIconsReadyCallback& cb, 163 const OnIconsReadyCallback& cb,
162 std::vector<mojom::ActivityIconPtr> icons) { 164 std::vector<mojom::ActivityIconPtr> icons) {
165 DCHECK(thread_checker_.CalledOnValidThread());
163 ArcServiceManager* manager = ArcServiceManager::Get(); 166 ArcServiceManager* manager = ArcServiceManager::Get();
164 base::PostTaskAndReplyWithResult( 167 base::PostTaskAndReplyWithResult(
165 manager->blocking_task_runner().get(), FROM_HERE, 168 manager->blocking_task_runner().get(), FROM_HERE,
166 base::Bind(&ActivityIconLoader::ResizeAndEncodeIcons, this, 169 base::Bind(&ActivityIconLoader::ResizeAndEncodeIcons,
167 base::Passed(&icons)), 170 base::Passed(&icons)),
168 base::Bind(&ActivityIconLoader::OnIconsResized, this, 171 base::Bind(&ActivityIconLoader::OnIconsResized, this,
169 base::Passed(&cached_result), cb)); 172 base::Passed(&cached_result), cb));
170 } 173 }
171 174
175 // static
172 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> 176 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap>
173 ActivityIconLoader::ResizeAndEncodeIcons( 177 ActivityIconLoader::ResizeAndEncodeIcons(
174 std::vector<mojom::ActivityIconPtr> icons) { 178 std::vector<mojom::ActivityIconPtr> icons) {
175 // Runs only on the blocking pool.
176 DCHECK(thread_checker_.CalledOnValidThread());
177 std::unique_ptr<ActivityToIconsMap> result(new ActivityToIconsMap); 179 std::unique_ptr<ActivityToIconsMap> result(new ActivityToIconsMap);
178 180
179 for (size_t i = 0; i < icons.size(); ++i) { 181 for (size_t i = 0; i < icons.size(); ++i) {
180 static const size_t kBytesPerPixel = 4; 182 static const size_t kBytesPerPixel = 4;
181 const mojom::ActivityIconPtr& icon = icons.at(i); 183 const mojom::ActivityIconPtr& icon = icons.at(i);
182 if (icon->width > kMaxIconSizeInPx || icon->height > kMaxIconSizeInPx || 184 if (icon->width > kMaxIconSizeInPx || icon->height > kMaxIconSizeInPx ||
183 icon->width == 0 || icon->height == 0 || 185 icon->width == 0 || icon->height == 0 ||
184 icon->icon.size() != (icon->width * icon->height * kBytesPerPixel)) { 186 icon->icon.size() != (icon->width * icon->height * kBytesPerPixel)) {
185 continue; 187 continue;
186 } 188 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 Icons(icon16, icon20, dataurl))); 224 Icons(icon16, icon20, dataurl)));
223 } 225 }
224 226
225 return result; 227 return result;
226 } 228 }
227 229
228 void ActivityIconLoader::OnIconsResized( 230 void ActivityIconLoader::OnIconsResized(
229 std::unique_ptr<ActivityToIconsMap> cached_result, 231 std::unique_ptr<ActivityToIconsMap> cached_result,
230 const OnIconsReadyCallback& cb, 232 const OnIconsReadyCallback& cb,
231 std::unique_ptr<ActivityToIconsMap> result) { 233 std::unique_ptr<ActivityToIconsMap> result) {
234 DCHECK(thread_checker_.CalledOnValidThread());
232 // Update |cached_icons_|. 235 // Update |cached_icons_|.
233 for (const auto& kv : *result) { 236 for (const auto& kv : *result) {
234 cached_icons_.erase(kv.first); 237 cached_icons_.erase(kv.first);
235 cached_icons_.insert(std::make_pair(kv.first, kv.second)); 238 cached_icons_.insert(std::make_pair(kv.first, kv.second));
236 } 239 }
237 240
238 // Merge the results that were obtained from cache before doing IPC. 241 // Merge the results that were obtained from cache before doing IPC.
239 result->insert(cached_result->begin(), cached_result->end()); 242 result->insert(cached_result->begin(), cached_result->end());
240 cb.Run(std::move(result)); 243 cb.Run(std::move(result));
241 } 244 }
242 245
243 } // namespace arc 246 } // namespace arc
OLDNEW
« no previous file with comments | « components/arc/intent_helper/activity_icon_loader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698