| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "chrome/browser/manifest/manifest_icon_downloader.h" | 9 #include "chrome/browser/manifest/manifest_icon_downloader.h" |
| 10 #include "chrome/browser/manifest/manifest_icon_selector.h" | 10 #include "chrome/browser/manifest/manifest_icon_selector.h" |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 tasks_.push_back({params, callback}); | 108 tasks_.push_back({params, callback}); |
| 109 if (is_active_) | 109 if (is_active_) |
| 110 return; | 110 return; |
| 111 | 111 |
| 112 is_active_ = true; | 112 is_active_ = true; |
| 113 StartNextTask(); | 113 StartNextTask(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 InstallableManager::IconProperty& InstallableManager::GetIcon( | 116 InstallableManager::IconProperty& InstallableManager::GetIcon( |
| 117 const InstallableParams& params) { | 117 const InstallableParams& params) { |
| 118 return icons_[{params.ideal_icon_size_in_px, params.minimum_icon_size_in_px}]; | 118 return icons_[{params.ideal_primary_icon_size_in_px, |
| 119 params.minimum_primary_icon_size_in_px}]; |
| 119 } | 120 } |
| 120 | 121 |
| 121 bool InstallableManager::IsIconFetched(const InstallableParams& params) const { | 122 bool InstallableManager::IsIconFetched(const InstallableParams& params) const { |
| 122 const auto it = icons_.find( | 123 const auto it = icons_.find({params.ideal_primary_icon_size_in_px, |
| 123 {params.ideal_icon_size_in_px, params.minimum_icon_size_in_px}); | 124 params.minimum_primary_icon_size_in_px}); |
| 124 return it != icons_.end() && it->second.fetched; | 125 return it != icons_.end() && it->second.fetched; |
| 125 } | 126 } |
| 126 | 127 |
| 127 void InstallableManager::SetIconFetched(const InstallableParams& params) { | 128 void InstallableManager::SetIconFetched(const InstallableParams& params) { |
| 128 GetIcon(params).fetched = true; | 129 GetIcon(params).fetched = true; |
| 129 } | 130 } |
| 130 | 131 |
| 131 InstallableStatusCode InstallableManager::GetErrorCode( | 132 InstallableStatusCode InstallableManager::GetErrorCode( |
| 132 const InstallableParams& params) { | 133 const InstallableParams& params) { |
| 133 if (manifest_->error != NO_ERROR_DETECTED) | 134 if (manifest_->error != NO_ERROR_DETECTED) |
| 134 return manifest_->error; | 135 return manifest_->error; |
| 135 | 136 |
| 136 if (params.check_installable && installable_->error != NO_ERROR_DETECTED) | 137 if (params.check_installable && installable_->error != NO_ERROR_DETECTED) |
| 137 return installable_->error; | 138 return installable_->error; |
| 138 | 139 |
| 139 if (params.fetch_valid_icon) { | 140 if (params.fetch_valid_primary_icon) { |
| 140 IconProperty& icon = GetIcon(params); | 141 IconProperty& icon = GetIcon(params); |
| 141 if (icon.error != NO_ERROR_DETECTED) | 142 if (icon.error != NO_ERROR_DETECTED) |
| 142 return icon.error; | 143 return icon.error; |
| 143 } | 144 } |
| 144 | 145 |
| 145 return NO_ERROR_DETECTED; | 146 return NO_ERROR_DETECTED; |
| 146 } | 147 } |
| 147 | 148 |
| 148 InstallableStatusCode InstallableManager::manifest_error() const { | 149 InstallableStatusCode InstallableManager::manifest_error() const { |
| 149 return manifest_->error; | 150 return manifest_->error; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 179 return nullptr; | 180 return nullptr; |
| 180 return contents; | 181 return contents; |
| 181 } | 182 } |
| 182 | 183 |
| 183 bool InstallableManager::IsComplete(const InstallableParams& params) const { | 184 bool InstallableManager::IsComplete(const InstallableParams& params) const { |
| 184 // Returns true if for all resources: | 185 // Returns true if for all resources: |
| 185 // a. the params did not request it, OR | 186 // a. the params did not request it, OR |
| 186 // b. the resource has been fetched/checked. | 187 // b. the resource has been fetched/checked. |
| 187 return manifest_->fetched && | 188 return manifest_->fetched && |
| 188 (!params.check_installable || installable_->fetched) && | 189 (!params.check_installable || installable_->fetched) && |
| 189 (!params.fetch_valid_icon || IsIconFetched(params)); | 190 (!params.fetch_valid_primary_icon || IsIconFetched(params)); |
| 190 } | 191 } |
| 191 | 192 |
| 192 void InstallableManager::Reset() { | 193 void InstallableManager::Reset() { |
| 193 // Prevent any outstanding callbacks to or from this object from being called. | 194 // Prevent any outstanding callbacks to or from this object from being called. |
| 194 weak_factory_.InvalidateWeakPtrs(); | 195 weak_factory_.InvalidateWeakPtrs(); |
| 195 tasks_.clear(); | 196 tasks_.clear(); |
| 196 icons_.clear(); | 197 icons_.clear(); |
| 197 | 198 |
| 198 manifest_.reset(new ManifestProperty()); | 199 manifest_.reset(new ManifestProperty()); |
| 199 installable_.reset(new InstallableProperty()); | 200 installable_.reset(new InstallableProperty()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 210 } | 211 } |
| 211 | 212 |
| 212 void InstallableManager::RunCallback(const Task& task, | 213 void InstallableManager::RunCallback(const Task& task, |
| 213 InstallableStatusCode code) { | 214 InstallableStatusCode code) { |
| 214 const InstallableParams& params = task.first; | 215 const InstallableParams& params = task.first; |
| 215 IconProperty& icon = GetIcon(params); | 216 IconProperty& icon = GetIcon(params); |
| 216 InstallableData data = { | 217 InstallableData data = { |
| 217 code, | 218 code, |
| 218 manifest_url(), | 219 manifest_url(), |
| 219 manifest(), | 220 manifest(), |
| 220 params.fetch_valid_icon ? icon.url : GURL::EmptyGURL(), | 221 params.fetch_valid_primary_icon ? icon.url : GURL::EmptyGURL(), |
| 221 params.fetch_valid_icon ? icon.icon.get() : nullptr, | 222 params.fetch_valid_primary_icon ? icon.icon.get() : nullptr, |
| 222 params.check_installable ? is_installable() : false}; | 223 params.check_installable ? is_installable() : false}; |
| 223 | 224 |
| 224 task.second.Run(data); | 225 task.second.Run(data); |
| 225 } | 226 } |
| 226 | 227 |
| 227 void InstallableManager::StartNextTask() { | 228 void InstallableManager::StartNextTask() { |
| 228 // If there's nothing to do, exit. Resources remain cached so any future calls | 229 // If there's nothing to do, exit. Resources remain cached so any future calls |
| 229 // won't re-fetch anything that has already been retrieved. | 230 // won't re-fetch anything that has already been retrieved. |
| 230 if (tasks_.empty()) { | 231 if (tasks_.empty()) { |
| 231 is_active_ = false; | 232 is_active_ = false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 246 RunCallback(task, code); | 247 RunCallback(task, code); |
| 247 tasks_.erase(tasks_.begin()); | 248 tasks_.erase(tasks_.begin()); |
| 248 StartNextTask(); | 249 StartNextTask(); |
| 249 return; | 250 return; |
| 250 } | 251 } |
| 251 | 252 |
| 252 if (!manifest_->fetched) | 253 if (!manifest_->fetched) |
| 253 FetchManifest(); | 254 FetchManifest(); |
| 254 else if (params.check_installable && !installable_->fetched) | 255 else if (params.check_installable && !installable_->fetched) |
| 255 CheckInstallable(); | 256 CheckInstallable(); |
| 256 else if (params.fetch_valid_icon && !IsIconFetched(params)) | 257 else if (params.fetch_valid_primary_icon && !IsIconFetched(params)) |
| 257 CheckAndFetchBestIcon(); | 258 CheckAndFetchBestIcon(); |
| 258 else | 259 else |
| 259 NOTREACHED(); | 260 NOTREACHED(); |
| 260 } | 261 } |
| 261 | 262 |
| 262 void InstallableManager::FetchManifest() { | 263 void InstallableManager::FetchManifest() { |
| 263 DCHECK(!manifest_->fetched); | 264 DCHECK(!manifest_->fetched); |
| 264 | 265 |
| 265 content::WebContents* web_contents = GetWebContents(); | 266 content::WebContents* web_contents = GetWebContents(); |
| 266 DCHECK(web_contents); | 267 DCHECK(web_contents); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 | 375 |
| 375 void InstallableManager::CheckAndFetchBestIcon() { | 376 void InstallableManager::CheckAndFetchBestIcon() { |
| 376 DCHECK(!manifest().IsEmpty()); | 377 DCHECK(!manifest().IsEmpty()); |
| 377 DCHECK(!tasks_.empty()); | 378 DCHECK(!tasks_.empty()); |
| 378 | 379 |
| 379 const InstallableParams& params = tasks_[0].first; | 380 const InstallableParams& params = tasks_[0].first; |
| 380 IconProperty& icon = GetIcon(params); | 381 IconProperty& icon = GetIcon(params); |
| 381 icon.fetched = true; | 382 icon.fetched = true; |
| 382 | 383 |
| 383 GURL icon_url = ManifestIconSelector::FindBestMatchingIcon( | 384 GURL icon_url = ManifestIconSelector::FindBestMatchingIcon( |
| 384 manifest().icons, params.ideal_icon_size_in_px, | 385 manifest().icons, params.ideal_primary_icon_size_in_px, |
| 385 params.minimum_icon_size_in_px); | 386 params.minimum_primary_icon_size_in_px); |
| 386 | 387 |
| 387 if (icon_url.is_empty()) { | 388 if (icon_url.is_empty()) { |
| 388 icon.error = NO_ACCEPTABLE_ICON; | 389 icon.error = NO_ACCEPTABLE_ICON; |
| 389 } else { | 390 } else { |
| 390 bool can_download_icon = ManifestIconDownloader::Download( | 391 bool can_download_icon = ManifestIconDownloader::Download( |
| 391 GetWebContents(), icon_url, params.ideal_icon_size_in_px, | 392 GetWebContents(), icon_url, params.ideal_primary_icon_size_in_px, |
| 392 params.minimum_icon_size_in_px, | 393 params.minimum_primary_icon_size_in_px, |
| 393 base::Bind(&InstallableManager::OnAppIconFetched, | 394 base::Bind(&InstallableManager::OnAppIconFetched, |
| 394 weak_factory_.GetWeakPtr(), icon_url)); | 395 weak_factory_.GetWeakPtr(), icon_url)); |
| 395 if (can_download_icon) | 396 if (can_download_icon) |
| 396 return; | 397 return; |
| 397 icon.error = CANNOT_DOWNLOAD_ICON; | 398 icon.error = CANNOT_DOWNLOAD_ICON; |
| 398 } | 399 } |
| 399 | 400 |
| 400 WorkOnTask(); | 401 WorkOnTask(); |
| 401 } | 402 } |
| 402 | 403 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 return manifest_->url; | 437 return manifest_->url; |
| 437 } | 438 } |
| 438 | 439 |
| 439 const content::Manifest& InstallableManager::manifest() const { | 440 const content::Manifest& InstallableManager::manifest() const { |
| 440 return manifest_->manifest; | 441 return manifest_->manifest; |
| 441 } | 442 } |
| 442 | 443 |
| 443 bool InstallableManager::is_installable() const { | 444 bool InstallableManager::is_installable() const { |
| 444 return installable_->installable; | 445 return installable_->installable; |
| 445 } | 446 } |
| OLD | NEW |