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

Side by Side Diff: chrome/browser/installable/installable_manager.cc

Issue 2641413002: Allow InstallableManager to fetch optional badge icon (Closed)
Patch Set: Fix rebase 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
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 "chrome/browser/installable/installable_manager.h" 5 #include "chrome/browser/installable/installable_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 // Return immediately if we're already working on a task. The new task will be 135 // Return immediately if we're already working on a task. The new task will be
136 // looked at once the current task is finished. 136 // looked at once the current task is finished.
137 tasks_.push_back({params, callback}); 137 tasks_.push_back({params, callback});
138 if (is_active_) 138 if (is_active_)
139 return; 139 return;
140 140
141 is_active_ = true; 141 is_active_ = true;
142 StartNextTask(); 142 StartNextTask();
143 } 143 }
144 144
145 InstallableManager::IconProperty& InstallableManager::GetIcon( 145 InstallableManager::IconParams InstallableManager::ParamsForPrimaryIcon(
146 const InstallableParams& params) { 146 const InstallableParams& params) const {
147 return icons_[{params.ideal_primary_icon_size_in_px, 147 return std::make_tuple(params.ideal_primary_icon_size_in_px,
148 params.minimum_primary_icon_size_in_px}]; 148 params.minimum_primary_icon_size_in_px,
149 IconPurpose::ANY);
149 } 150 }
150 151
151 bool InstallableManager::IsIconFetched(const InstallableParams& params) const { 152 InstallableManager::IconParams InstallableManager::ParamsForBadgeIcon(
152 const auto it = icons_.find({params.ideal_primary_icon_size_in_px, 153 const InstallableParams& params) const {
153 params.minimum_primary_icon_size_in_px}); 154 return std::make_tuple(params.ideal_badge_icon_size_in_px,
155 params.minimum_badge_icon_size_in_px,
156 IconPurpose::BADGE);
157 }
158
159 bool InstallableManager::IsIconFetched(const IconParams& params) const {
160 const auto it = icons_.find(params);
154 return it != icons_.end() && it->second.fetched; 161 return it != icons_.end() && it->second.fetched;
155 } 162 }
156 163
157 void InstallableManager::SetIconFetched(const InstallableParams& params) { 164 void InstallableManager::SetIconFetched(const IconParams& params) {
158 GetIcon(params).fetched = true; 165 icons_[params].fetched = true;
159 } 166 }
160 167
161 InstallableStatusCode InstallableManager::GetErrorCode( 168 InstallableStatusCode InstallableManager::GetErrorCode(
162 const InstallableParams& params) { 169 const InstallableParams& params) {
163 if (manifest_->error != NO_ERROR_DETECTED) 170 if (manifest_->error != NO_ERROR_DETECTED)
164 return manifest_->error; 171 return manifest_->error;
165 172
166 if (params.check_installable && installable_->error != NO_ERROR_DETECTED) 173 if (params.check_installable && installable_->error != NO_ERROR_DETECTED)
167 return installable_->error; 174 return installable_->error;
168 175
169 if (params.fetch_valid_primary_icon) { 176 if (params.fetch_valid_primary_icon) {
170 IconProperty& icon = GetIcon(params); 177 IconProperty& icon = icons_[ParamsForPrimaryIcon(params)];
171 if (icon.error != NO_ERROR_DETECTED) 178 if (icon.error != NO_ERROR_DETECTED)
172 return icon.error; 179 return icon.error;
173 } 180 }
174 181
182 // Do not report badge icon's error because badge icon is optional.
175 return NO_ERROR_DETECTED; 183 return NO_ERROR_DETECTED;
176 } 184 }
177 185
178 InstallableStatusCode InstallableManager::manifest_error() const { 186 InstallableStatusCode InstallableManager::manifest_error() const {
179 return manifest_->error; 187 return manifest_->error;
180 } 188 }
181 189
182 InstallableStatusCode InstallableManager::installable_error() const { 190 InstallableStatusCode InstallableManager::installable_error() const {
183 return installable_->error; 191 return installable_->error;
184 } 192 }
185 193
186 void InstallableManager::set_installable_error( 194 void InstallableManager::set_installable_error(
187 InstallableStatusCode error_code) { 195 InstallableStatusCode error_code) {
188 installable_->error = error_code; 196 installable_->error = error_code;
189 } 197 }
190 198
191 InstallableStatusCode InstallableManager::icon_error( 199 InstallableStatusCode InstallableManager::icon_error(
192 const InstallableManager::IconParams& icon_params) { 200 const IconParams& icon_params) {
193 return icons_[icon_params].error; 201 return icons_[icon_params].error;
194 } 202 }
195 203
196 GURL& InstallableManager::icon_url( 204 GURL& InstallableManager::icon_url(const IconParams& icon_params) {
197 const InstallableManager::IconParams& icon_params) {
198 return icons_[icon_params].url; 205 return icons_[icon_params].url;
199 } 206 }
200 207
201 const SkBitmap* InstallableManager::icon( 208 const SkBitmap* InstallableManager::icon(const IconParams& icon_params) {
202 const InstallableManager::IconParams& icon_params) {
203 return icons_[icon_params].icon.get(); 209 return icons_[icon_params].icon.get();
204 } 210 }
205 211
206 content::WebContents* InstallableManager::GetWebContents() { 212 content::WebContents* InstallableManager::GetWebContents() {
207 content::WebContents* contents = web_contents(); 213 content::WebContents* contents = web_contents();
208 if (!contents || contents->IsBeingDestroyed()) 214 if (!contents || contents->IsBeingDestroyed())
209 return nullptr; 215 return nullptr;
210 return contents; 216 return contents;
211 } 217 }
212 218
213 bool InstallableManager::IsComplete(const InstallableParams& params) const { 219 bool InstallableManager::IsComplete(const InstallableParams& params) const {
214 // Returns true if for all resources: 220 // Returns true if for all resources:
215 // a. the params did not request it, OR 221 // a. the params did not request it, OR
216 // b. the resource has been fetched/checked. 222 // b. the resource has been fetched/checked.
217 return manifest_->fetched && 223 return manifest_->fetched &&
218 (!params.check_installable || installable_->fetched) && 224 (!params.check_installable || installable_->fetched) &&
219 (!params.fetch_valid_primary_icon || IsIconFetched(params)); 225 (!params.fetch_valid_primary_icon ||
226 IsIconFetched(ParamsForPrimaryIcon(params))) &&
227 (!params.fetch_valid_badge_icon ||
228 IsIconFetched(ParamsForBadgeIcon(params)));
220 } 229 }
221 230
222 void InstallableManager::Reset() { 231 void InstallableManager::Reset() {
223 // Prevent any outstanding callbacks to or from this object from being called. 232 // Prevent any outstanding callbacks to or from this object from being called.
224 weak_factory_.InvalidateWeakPtrs(); 233 weak_factory_.InvalidateWeakPtrs();
225 tasks_.clear(); 234 tasks_.clear();
226 icons_.clear(); 235 icons_.clear();
227 236
228 manifest_.reset(new ManifestProperty()); 237 manifest_.reset(new ManifestProperty());
229 installable_.reset(new InstallableProperty()); 238 installable_.reset(new InstallableProperty());
230 239
231 is_active_ = false; 240 is_active_ = false;
232 } 241 }
233 242
234 void InstallableManager::SetManifestDependentTasksComplete() { 243 void InstallableManager::SetManifestDependentTasksComplete() {
235 DCHECK(!tasks_.empty()); 244 DCHECK(!tasks_.empty());
236 const InstallableParams& params = tasks_[0].first; 245 const InstallableParams& params = tasks_[0].first;
237 246
238 installable_->fetched = true; 247 installable_->fetched = true;
239 SetIconFetched(params); 248 SetIconFetched(ParamsForPrimaryIcon(params));
249 SetIconFetched(ParamsForBadgeIcon(params));
240 } 250 }
241 251
242 void InstallableManager::RunCallback(const Task& task, 252 void InstallableManager::RunCallback(const Task& task,
243 InstallableStatusCode code) { 253 InstallableStatusCode code) {
244 const InstallableParams& params = task.first; 254 const InstallableParams& params = task.first;
245 IconProperty& icon = GetIcon(params); 255 IconProperty null_icon;
256 IconProperty* primary_icon = &null_icon;
257 IconProperty* badge_icon = &null_icon;
258
259 if (params.fetch_valid_primary_icon &&
260 base::ContainsKey(icons_, ParamsForPrimaryIcon(params)))
261 primary_icon = &icons_[ParamsForPrimaryIcon(params)];
262 if (params.fetch_valid_badge_icon &&
263 base::ContainsKey(icons_, ParamsForBadgeIcon(params)))
264 badge_icon = &icons_[ParamsForBadgeIcon(params)];
265
246 InstallableData data = { 266 InstallableData data = {
247 code, 267 code,
248 manifest_url(), 268 manifest_url(),
249 manifest(), 269 manifest(),
250 params.fetch_valid_primary_icon ? icon.url : GURL::EmptyGURL(), 270 primary_icon->url,
251 params.fetch_valid_primary_icon ? icon.icon.get() : nullptr, 271 primary_icon->icon.get(),
272 badge_icon->url,
273 badge_icon->icon.get(),
252 params.check_installable ? is_installable() : false}; 274 params.check_installable ? is_installable() : false};
253 275
254 task.second.Run(data); 276 task.second.Run(data);
255 } 277 }
256 278
257 void InstallableManager::StartNextTask() { 279 void InstallableManager::StartNextTask() {
258 // If there's nothing to do, exit. Resources remain cached so any future calls 280 // If there's nothing to do, exit. Resources remain cached so any future calls
259 // won't re-fetch anything that has already been retrieved. 281 // won't re-fetch anything that has already been retrieved.
260 if (tasks_.empty()) { 282 if (tasks_.empty()) {
261 is_active_ = false; 283 is_active_ = false;
(...skipping 10 matching lines...) Expand all
272 const InstallableParams& params = task.first; 294 const InstallableParams& params = task.first;
273 295
274 InstallableStatusCode code = GetErrorCode(params); 296 InstallableStatusCode code = GetErrorCode(params);
275 if (code != NO_ERROR_DETECTED || IsComplete(params)) { 297 if (code != NO_ERROR_DETECTED || IsComplete(params)) {
276 RunCallback(task, code); 298 RunCallback(task, code);
277 tasks_.erase(tasks_.begin()); 299 tasks_.erase(tasks_.begin());
278 StartNextTask(); 300 StartNextTask();
279 return; 301 return;
280 } 302 }
281 303
282 if (!manifest_->fetched) 304 if (!manifest_->fetched) {
283 FetchManifest(); 305 FetchManifest();
284 else if (params.check_installable && !installable_->fetched) 306 } else if (params.check_installable && !installable_->fetched) {
285 CheckInstallable(); 307 CheckInstallable();
286 else if (params.fetch_valid_primary_icon && !IsIconFetched(params)) 308 } else if (params.fetch_valid_primary_icon &&
287 CheckAndFetchBestIcon(); 309 !IsIconFetched(ParamsForPrimaryIcon(params))) {
288 else 310 CheckAndFetchBestIcon(ParamsForPrimaryIcon(params));
311 } else if (params.fetch_valid_badge_icon &&
312 !IsIconFetched(ParamsForBadgeIcon(params))) {
313 CheckAndFetchBestIcon(ParamsForBadgeIcon(params));
314 } else {
289 NOTREACHED(); 315 NOTREACHED();
316 }
290 } 317 }
291 318
292 void InstallableManager::FetchManifest() { 319 void InstallableManager::FetchManifest() {
293 DCHECK(!manifest_->fetched); 320 DCHECK(!manifest_->fetched);
294 321
295 content::WebContents* web_contents = GetWebContents(); 322 content::WebContents* web_contents = GetWebContents();
296 DCHECK(web_contents); 323 DCHECK(web_contents);
297 324
298 web_contents->GetManifest(base::Bind(&InstallableManager::OnDidGetManifest, 325 web_contents->GetManifest(base::Bind(&InstallableManager::OnDidGetManifest,
299 weak_factory_.GetWeakPtr())); 326 weak_factory_.GetWeakPtr()));
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 installable_->installable = true; 422 installable_->installable = true;
396 } else { 423 } else {
397 installable_->installable = false; 424 installable_->installable = false;
398 installable_->error = NO_MATCHING_SERVICE_WORKER; 425 installable_->error = NO_MATCHING_SERVICE_WORKER;
399 } 426 }
400 427
401 installable_->fetched = true; 428 installable_->fetched = true;
402 WorkOnTask(); 429 WorkOnTask();
403 } 430 }
404 431
405 void InstallableManager::CheckAndFetchBestIcon() { 432 void InstallableManager::CheckAndFetchBestIcon(const IconParams& params) {
406 DCHECK(!manifest().IsEmpty()); 433 DCHECK(!manifest().IsEmpty());
407 DCHECK(!tasks_.empty());
408 434
409 const InstallableParams& params = tasks_[0].first; 435 int ideal_icon_size_in_px = std::get<0>(params);
410 IconProperty& icon = GetIcon(params); 436 int minimum_icon_size_in_px = std::get<1>(params);
437 IconPurpose icon_purpose = std::get<2>(params);
438
439 IconProperty& icon = icons_[params];
411 icon.fetched = true; 440 icon.fetched = true;
412 441
442 // Filter by icon purpose.
443 std::vector<content::Manifest::Icon> filtered_icons;
444 std::copy_if(manifest().icons.begin(), manifest().icons.end(),
445 std::back_inserter(filtered_icons),
446 [icon_purpose](const content::Manifest::Icon& icon) {
447 return base::ContainsValue(icon.purpose, icon_purpose);
448 });
449
413 GURL icon_url = ManifestIconSelector::FindBestMatchingIcon( 450 GURL icon_url = ManifestIconSelector::FindBestMatchingIcon(
414 manifest().icons, params.ideal_primary_icon_size_in_px, 451 filtered_icons, ideal_icon_size_in_px, minimum_icon_size_in_px);
415 params.minimum_primary_icon_size_in_px);
416 452
417 if (icon_url.is_empty()) { 453 if (icon_url.is_empty()) {
418 icon.error = NO_ACCEPTABLE_ICON; 454 icon.error = NO_ACCEPTABLE_ICON;
419 } else { 455 } else {
420 bool can_download_icon = ManifestIconDownloader::Download( 456 bool can_download_icon = ManifestIconDownloader::Download(
421 GetWebContents(), icon_url, params.ideal_primary_icon_size_in_px, 457 GetWebContents(), icon_url, ideal_icon_size_in_px,
422 params.minimum_primary_icon_size_in_px, 458 minimum_icon_size_in_px,
423 base::Bind(&InstallableManager::OnAppIconFetched, 459 base::Bind(&InstallableManager::OnIconFetched,
424 weak_factory_.GetWeakPtr(), icon_url)); 460 weak_factory_.GetWeakPtr(), icon_url, params));
425 if (can_download_icon) 461 if (can_download_icon)
426 return; 462 return;
427 icon.error = CANNOT_DOWNLOAD_ICON; 463 icon.error = CANNOT_DOWNLOAD_ICON;
428 } 464 }
429 465
430 WorkOnTask(); 466 WorkOnTask();
431 } 467 }
432 468
433 void InstallableManager::OnAppIconFetched(const GURL icon_url, 469 void InstallableManager::OnIconFetched(
434 const SkBitmap& bitmap) { 470 const GURL icon_url, const IconParams& params, const SkBitmap& bitmap) {
435 DCHECK(!tasks_.empty()); 471 IconProperty& icon = icons_[params];
436 const InstallableParams& params = tasks_[0].first;
437 IconProperty& icon = GetIcon(params);
438 472
439 if (!GetWebContents()) 473 if (!GetWebContents())
440 return; 474 return;
441 475
442 if (bitmap.drawsNothing()) { 476 if (bitmap.drawsNothing()) {
443 icon.error = NO_ICON_AVAILABLE; 477 icon.error = NO_ICON_AVAILABLE;
444 } else { 478 } else {
445 icon.url = icon_url; 479 icon.url = icon_url;
446 icon.icon.reset(new SkBitmap(bitmap)); 480 icon.icon.reset(new SkBitmap(bitmap));
447 } 481 }
(...skipping 18 matching lines...) Expand all
466 return manifest_->url; 500 return manifest_->url;
467 } 501 }
468 502
469 const content::Manifest& InstallableManager::manifest() const { 503 const content::Manifest& InstallableManager::manifest() const {
470 return manifest_->manifest; 504 return manifest_->manifest;
471 } 505 }
472 506
473 bool InstallableManager::is_installable() const { 507 bool InstallableManager::is_installable() const {
474 return installable_->installable; 508 return installable_->installable;
475 } 509 }
OLDNEW
« no previous file with comments | « chrome/browser/installable/installable_manager.h ('k') | chrome/browser/installable/installable_manager_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698