| 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/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 | 78 |
| 79 struct InstallableManager::ValidManifestProperty { | 79 struct InstallableManager::ValidManifestProperty { |
| 80 InstallableStatusCode error = NO_ERROR_DETECTED; | 80 InstallableStatusCode error = NO_ERROR_DETECTED; |
| 81 bool is_valid = false; | 81 bool is_valid = false; |
| 82 bool fetched = false; | 82 bool fetched = false; |
| 83 }; | 83 }; |
| 84 | 84 |
| 85 struct InstallableManager::ServiceWorkerProperty { | 85 struct InstallableManager::ServiceWorkerProperty { |
| 86 InstallableStatusCode error = NO_ERROR_DETECTED; | 86 InstallableStatusCode error = NO_ERROR_DETECTED; |
| 87 bool has_worker = false; | 87 bool has_worker = false; |
| 88 bool is_waiting = false; | |
| 89 bool fetched = false; | 88 bool fetched = false; |
| 90 }; | 89 }; |
| 91 | 90 |
| 92 struct InstallableManager::IconProperty { | 91 struct InstallableManager::IconProperty { |
| 93 IconProperty() : | 92 IconProperty() : |
| 94 error(NO_ERROR_DETECTED), url(), icon(), fetched(false) { } | 93 error(NO_ERROR_DETECTED), url(), icon(), fetched(false) { } |
| 95 IconProperty(IconProperty&& other) = default; | 94 IconProperty(IconProperty&& other) = default; |
| 96 IconProperty& operator=(IconProperty&& other) = default; | 95 IconProperty& operator=(IconProperty&& other) = default; |
| 97 | 96 |
| 98 InstallableStatusCode error = NO_ERROR_DETECTED; | 97 InstallableStatusCode error = NO_ERROR_DETECTED; |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 | 288 |
| 290 void InstallableManager::set_valid_manifest_error( | 289 void InstallableManager::set_valid_manifest_error( |
| 291 InstallableStatusCode error_code) { | 290 InstallableStatusCode error_code) { |
| 292 valid_manifest_->error = error_code; | 291 valid_manifest_->error = error_code; |
| 293 } | 292 } |
| 294 | 293 |
| 295 InstallableStatusCode InstallableManager::worker_error() const { | 294 InstallableStatusCode InstallableManager::worker_error() const { |
| 296 return worker_->error; | 295 return worker_->error; |
| 297 } | 296 } |
| 298 | 297 |
| 299 bool InstallableManager::worker_waiting() const { | |
| 300 return worker_->is_waiting; | |
| 301 } | |
| 302 | |
| 303 InstallableStatusCode InstallableManager::icon_error( | 298 InstallableStatusCode InstallableManager::icon_error( |
| 304 const IconParams& icon_params) { | 299 const IconParams& icon_params) { |
| 305 return icons_[icon_params].error; | 300 return icons_[icon_params].error; |
| 306 } | 301 } |
| 307 | 302 |
| 308 GURL& InstallableManager::icon_url(const IconParams& icon_params) { | 303 GURL& InstallableManager::icon_url(const IconParams& icon_params) { |
| 309 return icons_[icon_params].url; | 304 return icons_[icon_params].url; |
| 310 } | 305 } |
| 311 | 306 |
| 312 const SkBitmap* InstallableManager::icon(const IconParams& icon_params) { | 307 const SkBitmap* InstallableManager::icon(const IconParams& icon_params) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 330 (!params.fetch_valid_primary_icon || | 325 (!params.fetch_valid_primary_icon || |
| 331 IsIconFetched(ParamsForPrimaryIcon(params))) && | 326 IsIconFetched(ParamsForPrimaryIcon(params))) && |
| 332 (!params.fetch_valid_badge_icon || | 327 (!params.fetch_valid_badge_icon || |
| 333 IsIconFetched(ParamsForBadgeIcon(params))); | 328 IsIconFetched(ParamsForBadgeIcon(params))); |
| 334 } | 329 } |
| 335 | 330 |
| 336 void InstallableManager::Reset() { | 331 void InstallableManager::Reset() { |
| 337 // Prevent any outstanding callbacks to or from this object from being called. | 332 // Prevent any outstanding callbacks to or from this object from being called. |
| 338 weak_factory_.InvalidateWeakPtrs(); | 333 weak_factory_.InvalidateWeakPtrs(); |
| 339 tasks_.clear(); | 334 tasks_.clear(); |
| 335 paused_tasks_.clear(); |
| 340 icons_.clear(); | 336 icons_.clear(); |
| 341 | 337 |
| 342 // We may have reset prior to completion, in which case |menu_open_count_| or | 338 // We may have reset prior to completion, in which case |menu_open_count_| or |
| 343 // |menu_item_add_to_homescreen_count_| might be nonzero and |page_status_| is | 339 // |menu_item_add_to_homescreen_count_| might be nonzero and |page_status_| is |
| 344 // one of NOT_STARTED or NOT_COMPLETED. If we completed, then these values | 340 // one of NOT_STARTED or NOT_COMPLETED. If we completed, then these values |
| 345 // cannot be anything except 0. | 341 // cannot be anything except 0. |
| 346 is_pwa_check_complete_ = false; | 342 is_pwa_check_complete_ = false; |
| 347 | 343 |
| 348 for (; menu_open_count_ > 0; --menu_open_count_) | 344 for (; menu_open_count_ > 0; --menu_open_count_) |
| 349 InstallableMetrics::RecordMenuOpenHistogram(page_status_); | 345 InstallableMetrics::RecordMenuOpenHistogram(page_status_); |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 | 537 |
| 542 switch (capability) { | 538 switch (capability) { |
| 543 case content::ServiceWorkerCapability::SERVICE_WORKER_WITH_FETCH_HANDLER: | 539 case content::ServiceWorkerCapability::SERVICE_WORKER_WITH_FETCH_HANDLER: |
| 544 worker_->has_worker = true; | 540 worker_->has_worker = true; |
| 545 break; | 541 break; |
| 546 case content::ServiceWorkerCapability::SERVICE_WORKER_NO_FETCH_HANDLER: | 542 case content::ServiceWorkerCapability::SERVICE_WORKER_NO_FETCH_HANDLER: |
| 547 worker_->has_worker = false; | 543 worker_->has_worker = false; |
| 548 worker_->error = NOT_OFFLINE_CAPABLE; | 544 worker_->error = NOT_OFFLINE_CAPABLE; |
| 549 break; | 545 break; |
| 550 case content::ServiceWorkerCapability::NO_SERVICE_WORKER: | 546 case content::ServiceWorkerCapability::NO_SERVICE_WORKER: |
| 551 InstallableParams& params = tasks_[0].first; | 547 Task& task = tasks_[0]; |
| 548 InstallableParams& params = task.first; |
| 552 if (params.wait_for_worker) { | 549 if (params.wait_for_worker) { |
| 553 // Wait for ServiceWorkerContextObserver::OnRegistrationStored. Set the | 550 // Wait for ServiceWorkerContextObserver::OnRegistrationStored. Set the |
| 554 // param |wait_for_worker| to false so we only wait once per task. | 551 // param |wait_for_worker| to false so we only wait once per task. |
| 555 params.wait_for_worker = false; | 552 params.wait_for_worker = false; |
| 556 worker_->is_waiting = true; | |
| 557 OnWaitingForServiceWorker(); | 553 OnWaitingForServiceWorker(); |
| 554 paused_tasks_.push_back(task); |
| 555 tasks_.erase(tasks_.begin()); |
| 556 StartNextTask(); |
| 558 return; | 557 return; |
| 559 } | 558 } |
| 560 worker_->has_worker = false; | 559 worker_->has_worker = false; |
| 561 worker_->error = NO_MATCHING_SERVICE_WORKER; | 560 worker_->error = NO_MATCHING_SERVICE_WORKER; |
| 562 break; | 561 break; |
| 563 } | 562 } |
| 564 | 563 |
| 565 worker_->fetched = true; | 564 worker_->fetched = true; |
| 566 WorkOnTask(); | 565 WorkOnTask(); |
| 567 } | 566 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 icon.error = NO_ICON_AVAILABLE; | 606 icon.error = NO_ICON_AVAILABLE; |
| 608 } else { | 607 } else { |
| 609 icon.url = icon_url; | 608 icon.url = icon_url; |
| 610 icon.icon.reset(new SkBitmap(bitmap)); | 609 icon.icon.reset(new SkBitmap(bitmap)); |
| 611 } | 610 } |
| 612 | 611 |
| 613 WorkOnTask(); | 612 WorkOnTask(); |
| 614 } | 613 } |
| 615 | 614 |
| 616 void InstallableManager::OnRegistrationStored(const GURL& pattern) { | 615 void InstallableManager::OnRegistrationStored(const GURL& pattern) { |
| 617 // If we aren't currently waiting, either: | 616 // If we don't have any paused tasks, that means: |
| 618 // a) we've already failed the check, or | 617 // a) we've already failed the check, or |
| 619 // b) we haven't yet called CheckHasServiceWorker. | 618 // b) we haven't yet called CheckHasServiceWorker. |
| 620 // Otherwise if the scope doesn't match we keep waiting. | 619 // Otherwise if the scope doesn't match we keep waiting. |
| 621 if (!worker_->is_waiting || !content::ServiceWorkerContext::ScopeMatches( | 620 if (paused_tasks_.empty() || !content::ServiceWorkerContext::ScopeMatches( |
| 622 pattern, manifest().start_url)) { | 621 pattern, manifest().start_url)) { |
| 623 return; | 622 return; |
| 624 } | 623 } |
| 625 | 624 |
| 626 // This will call CheckHasServiceWorker to check whether the service worker | 625 // Unpause the paused tasks. |
| 627 // controls the start_url and if it has a fetch handler. | 626 for (const auto& task : paused_tasks_) |
| 628 worker_->is_waiting = false; | 627 tasks_.push_back(task); |
| 629 WorkOnTask(); | 628 paused_tasks_.clear(); |
| 629 |
| 630 // Start the pipeline again if it is not running. This will call |
| 631 // CheckHasServiceWorker to check if the SW has a fetch handler. Otherwise, |
| 632 // adding the tasks to the end of the active queue is sufficient. |
| 633 if (!is_active_) { |
| 634 is_active_ = true; |
| 635 StartNextTask(); |
| 636 } |
| 630 } | 637 } |
| 631 | 638 |
| 632 void InstallableManager::DidFinishNavigation( | 639 void InstallableManager::DidFinishNavigation( |
| 633 content::NavigationHandle* handle) { | 640 content::NavigationHandle* handle) { |
| 634 if (handle->IsInMainFrame() && handle->HasCommitted() && | 641 if (handle->IsInMainFrame() && handle->HasCommitted() && |
| 635 !handle->IsSameDocument()) { | 642 !handle->IsSameDocument()) { |
| 636 Reset(); | 643 Reset(); |
| 637 } | 644 } |
| 638 } | 645 } |
| 639 | 646 |
| 640 void InstallableManager::WebContentsDestroyed() { | 647 void InstallableManager::WebContentsDestroyed() { |
| 641 Reset(); | 648 Reset(); |
| 642 Observe(nullptr); | 649 Observe(nullptr); |
| 643 } | 650 } |
| 644 | 651 |
| 645 const GURL& InstallableManager::manifest_url() const { | 652 const GURL& InstallableManager::manifest_url() const { |
| 646 return manifest_->url; | 653 return manifest_->url; |
| 647 } | 654 } |
| 648 | 655 |
| 649 const content::Manifest& InstallableManager::manifest() const { | 656 const content::Manifest& InstallableManager::manifest() const { |
| 650 return manifest_->manifest; | 657 return manifest_->manifest; |
| 651 } | 658 } |
| 652 | 659 |
| 653 bool InstallableManager::is_installable() const { | 660 bool InstallableManager::is_installable() const { |
| 654 return valid_manifest_->is_valid && worker_->has_worker; | 661 return valid_manifest_->is_valid && worker_->has_worker; |
| 655 } | 662 } |
| OLD | NEW |