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 #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 Loading... | |
| 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 Loading... | |
| 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) |
|
dominickn
2017/01/27 06:40:42
Every line of this conditional needs braces now (t
F
2017/01/27 20:11:17
Done.
| |
| 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))) |
|
dominickn
2017/01/27 06:40:42
This repeated construction of the params throughou
F
2017/01/27 20:11:17
I agree that the repeated construction may seem a
dominickn
2017/01/30 00:51:27
Fair enough. Let's leave this as is for now. It's
| |
| 310 CheckAndFetchBestIcon(ParamsForPrimaryIcon(params)); | |
| 311 else if (params.fetch_valid_badge_icon && | |
| 312 !IsIconFetched(ParamsForBadgeIcon(params))) | |
| 313 CheckAndFetchBestIcon(ParamsForBadgeIcon(params)); | |
| 288 else | 314 else |
| 289 NOTREACHED(); | 315 NOTREACHED(); |
| 290 } | 316 } |
| 291 | 317 |
| 292 void InstallableManager::FetchManifest() { | 318 void InstallableManager::FetchManifest() { |
| 293 DCHECK(!manifest_->fetched); | 319 DCHECK(!manifest_->fetched); |
| 294 | 320 |
| 295 content::WebContents* web_contents = GetWebContents(); | 321 content::WebContents* web_contents = GetWebContents(); |
| 296 DCHECK(web_contents); | 322 DCHECK(web_contents); |
| 297 | 323 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 395 installable_->installable = true; | 421 installable_->installable = true; |
| 396 } else { | 422 } else { |
| 397 installable_->installable = false; | 423 installable_->installable = false; |
| 398 installable_->error = NO_MATCHING_SERVICE_WORKER; | 424 installable_->error = NO_MATCHING_SERVICE_WORKER; |
| 399 } | 425 } |
| 400 | 426 |
| 401 installable_->fetched = true; | 427 installable_->fetched = true; |
| 402 WorkOnTask(); | 428 WorkOnTask(); |
| 403 } | 429 } |
| 404 | 430 |
| 405 void InstallableManager::CheckAndFetchBestIcon() { | 431 void InstallableManager::CheckAndFetchBestIcon(const IconParams& params) { |
| 406 DCHECK(!manifest().IsEmpty()); | 432 DCHECK(!manifest().IsEmpty()); |
| 407 DCHECK(!tasks_.empty()); | |
| 408 | 433 |
| 409 const InstallableParams& params = tasks_[0].first; | 434 int ideal_icon_size_in_px = std::get<0>(params); |
| 410 IconProperty& icon = GetIcon(params); | 435 int minimum_icon_size_in_px = std::get<1>(params); |
| 436 IconPurpose icon_purpose = std::get<2>(params); | |
| 437 | |
| 438 IconProperty& icon = icons_[params]; | |
| 411 icon.fetched = true; | 439 icon.fetched = true; |
| 412 | 440 |
| 441 // Filter by icon purpose. | |
|
dominickn
2017/01/27 06:40:42
I think a better idea here is to make ManifestIcon
F
2017/01/27 20:11:17
I agree that it is a better idea. Previously I exp
dominickn
2017/01/30 00:51:26
I'm confused why ManifestIconSelector would make a
F
2017/01/30 16:50:05
At the moment, FilterIconsByType creates an extra
dominickn
2017/01/30 18:07:41
I didn't realise there was already a copy in there
| |
| 442 std::vector<content::Manifest::Icon> filtered_icons; | |
| 443 std::copy_if(manifest().icons.begin(), manifest().icons.end(), | |
| 444 std::back_inserter(filtered_icons), | |
| 445 [icon_purpose](const content::Manifest::Icon& icon) { | |
| 446 return base::ContainsValue(icon.purpose, icon_purpose); | |
| 447 }); | |
| 448 | |
| 413 GURL icon_url = ManifestIconSelector::FindBestMatchingIcon( | 449 GURL icon_url = ManifestIconSelector::FindBestMatchingIcon( |
| 414 manifest().icons, params.ideal_primary_icon_size_in_px, | 450 filtered_icons, ideal_icon_size_in_px, minimum_icon_size_in_px); |
| 415 params.minimum_primary_icon_size_in_px); | |
| 416 | 451 |
| 417 if (icon_url.is_empty()) { | 452 if (icon_url.is_empty()) { |
| 418 icon.error = NO_ACCEPTABLE_ICON; | 453 icon.error = NO_ACCEPTABLE_ICON; |
| 419 } else { | 454 } else { |
| 420 bool can_download_icon = ManifestIconDownloader::Download( | 455 bool can_download_icon = ManifestIconDownloader::Download( |
| 421 GetWebContents(), icon_url, params.ideal_primary_icon_size_in_px, | 456 GetWebContents(), icon_url, ideal_icon_size_in_px, |
| 422 params.minimum_primary_icon_size_in_px, | 457 minimum_icon_size_in_px, |
| 423 base::Bind(&InstallableManager::OnAppIconFetched, | 458 base::Bind(&InstallableManager::OnAppIconFetched, |
| 424 weak_factory_.GetWeakPtr(), icon_url)); | 459 weak_factory_.GetWeakPtr(), icon_url, params)); |
| 425 if (can_download_icon) | 460 if (can_download_icon) |
| 426 return; | 461 return; |
| 427 icon.error = CANNOT_DOWNLOAD_ICON; | 462 icon.error = CANNOT_DOWNLOAD_ICON; |
| 428 } | 463 } |
| 429 | 464 |
| 430 WorkOnTask(); | 465 WorkOnTask(); |
| 431 } | 466 } |
| 432 | 467 |
| 433 void InstallableManager::OnAppIconFetched(const GURL icon_url, | 468 void InstallableManager::OnAppIconFetched( |
| 434 const SkBitmap& bitmap) { | 469 const GURL icon_url, const IconParams& params, const SkBitmap& bitmap) { |
| 435 DCHECK(!tasks_.empty()); | 470 IconProperty& icon = icons_[params]; |
| 436 const InstallableParams& params = tasks_[0].first; | |
| 437 IconProperty& icon = GetIcon(params); | |
| 438 | 471 |
| 439 if (!GetWebContents()) | 472 if (!GetWebContents()) |
| 440 return; | 473 return; |
| 441 | 474 |
| 442 if (bitmap.drawsNothing()) { | 475 if (bitmap.drawsNothing()) { |
| 443 icon.error = NO_ICON_AVAILABLE; | 476 icon.error = NO_ICON_AVAILABLE; |
| 444 } else { | 477 } else { |
| 445 icon.url = icon_url; | 478 icon.url = icon_url; |
| 446 icon.icon.reset(new SkBitmap(bitmap)); | 479 icon.icon.reset(new SkBitmap(bitmap)); |
| 447 } | 480 } |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 466 return manifest_->url; | 499 return manifest_->url; |
| 467 } | 500 } |
| 468 | 501 |
| 469 const content::Manifest& InstallableManager::manifest() const { | 502 const content::Manifest& InstallableManager::manifest() const { |
| 470 return manifest_->manifest; | 503 return manifest_->manifest; |
| 471 } | 504 } |
| 472 | 505 |
| 473 bool InstallableManager::is_installable() const { | 506 bool InstallableManager::is_installable() const { |
| 474 return installable_->installable; | 507 return installable_->installable; |
| 475 } | 508 } |
| OLD | NEW |