| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 775 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 786 ~UpdateJobTestHelper() override { | 786 ~UpdateJobTestHelper() override { |
| 787 if (registration_.get()) | 787 if (registration_.get()) |
| 788 registration_->RemoveListener(this); | 788 registration_->RemoveListener(this); |
| 789 } | 789 } |
| 790 | 790 |
| 791 ServiceWorkerStorage* storage() { return context()->storage(); } | 791 ServiceWorkerStorage* storage() { return context()->storage(); } |
| 792 ServiceWorkerJobCoordinator* job_coordinator() { | 792 ServiceWorkerJobCoordinator* job_coordinator() { |
| 793 return context()->job_coordinator(); | 793 return context()->job_coordinator(); |
| 794 } | 794 } |
| 795 | 795 |
| 796 bool force_bypass_cache_for_scripts() const { | |
| 797 return force_bypass_cache_for_scripts_; | |
| 798 } | |
| 799 void set_force_bypass_cache_for_scripts(bool force_bypass_cache_for_scripts) { | |
| 800 force_bypass_cache_for_scripts_ = force_bypass_cache_for_scripts; | |
| 801 } | |
| 802 | |
| 803 void set_force_start_worker_failure(bool force_start_worker_failure) { | 796 void set_force_start_worker_failure(bool force_start_worker_failure) { |
| 804 force_start_worker_failure_ = force_start_worker_failure; | 797 force_start_worker_failure_ = force_start_worker_failure; |
| 805 } | 798 } |
| 806 | 799 |
| 807 scoped_refptr<ServiceWorkerRegistration> SetupInitialRegistration( | 800 scoped_refptr<ServiceWorkerRegistration> SetupInitialRegistration( |
| 808 const GURL& test_origin) { | 801 const GURL& test_origin) { |
| 809 scoped_refptr<ServiceWorkerRegistration> registration; | 802 scoped_refptr<ServiceWorkerRegistration> registration; |
| 810 bool called = false; | 803 bool called = false; |
| 811 job_coordinator()->Register( | 804 job_coordinator()->Register( |
| 812 test_origin.Resolve(kScope), | 805 test_origin.Resolve(kScope), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 833 const uint64_t kMockScriptSize = 19284; | 826 const uint64_t kMockScriptSize = 19284; |
| 834 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); | 827 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); |
| 835 ServiceWorkerRegistration* registration = | 828 ServiceWorkerRegistration* registration = |
| 836 context()->GetLiveRegistration(version->registration_id()); | 829 context()->GetLiveRegistration(version->registration_id()); |
| 837 bool is_update = registration->active_version() && | 830 bool is_update = registration->active_version() && |
| 838 version != registration->active_version(); | 831 version != registration->active_version(); |
| 839 | 832 |
| 840 ASSERT_TRUE(version); | 833 ASSERT_TRUE(version); |
| 841 version->AddListener(this); | 834 version->AddListener(this); |
| 842 | 835 |
| 843 if (force_bypass_cache_for_scripts()) | 836 // Simulate network access. |
| 844 version->set_force_bypass_cache_for_scripts(true); | 837 base::TimeDelta time_since_last_check = |
| 838 base::Time::Now() - registration->last_update_check(); |
| 839 if (!is_update || script.GetOrigin() != kNoChangeOrigin || |
| 840 time_since_last_check > base::TimeDelta::FromHours( |
| 841 kServiceWorkerScriptMaxCacheAgeInHours)) { |
| 842 version->embedded_worker()->OnNetworkAccessedForScriptLoad(); |
| 843 } |
| 845 | 844 |
| 845 int64_t resource_id = storage()->NewResourceId(); |
| 846 version->script_cache_map()->NotifyStartedCaching(script, resource_id); |
| 846 if (!is_update) { | 847 if (!is_update) { |
| 847 // Spoof caching the script for the initial version. | 848 // Spoof caching the script for the initial version. |
| 848 int64_t resource_id = storage()->NewResourceId(); | |
| 849 version->script_cache_map()->NotifyStartedCaching(script, resource_id); | |
| 850 WriteStringResponse(storage(), resource_id, kMockScriptBody); | 849 WriteStringResponse(storage(), resource_id, kMockScriptBody); |
| 851 version->script_cache_map()->NotifyFinishedCaching( | 850 version->script_cache_map()->NotifyFinishedCaching( |
| 852 script, kMockScriptSize, net::URLRequestStatus(), std::string()); | 851 script, kMockScriptSize, net::URLRequestStatus(), std::string()); |
| 853 } else { | 852 } else { |
| 854 if (script.GetOrigin() == kNoChangeOrigin) { | 853 if (script.GetOrigin() == kNoChangeOrigin) { |
| 855 version->SetStartWorkerStatusCode(SERVICE_WORKER_ERROR_EXISTS); | 854 // Simulate fetching the updated script and finding it's identical to |
| 856 EmbeddedWorkerTestHelper::OnStopWorker(embedded_worker_id); | 855 // the incumbent. |
| 856 net::URLRequestStatus status = |
| 857 net::URLRequestStatus::FromError(net::ERR_FILE_EXISTS); |
| 858 version->script_cache_map()->NotifyFinishedCaching( |
| 859 script, kMockScriptSize, status, std::string()); |
| 860 SimulateWorkerScriptLoaded(embedded_worker_id); |
| 857 return; | 861 return; |
| 858 } | 862 } |
| 859 | 863 |
| 860 // Spoof caching the script for the new version. | 864 // Spoof caching the script for the new version. |
| 861 int64_t resource_id = storage()->NewResourceId(); | |
| 862 version->script_cache_map()->NotifyStartedCaching(script, resource_id); | |
| 863 WriteStringResponse(storage(), resource_id, "mock_different_script"); | 865 WriteStringResponse(storage(), resource_id, "mock_different_script"); |
| 864 version->script_cache_map()->NotifyFinishedCaching( | 866 version->script_cache_map()->NotifyFinishedCaching( |
| 865 script, kMockScriptSize, net::URLRequestStatus(), std::string()); | 867 script, kMockScriptSize, net::URLRequestStatus(), std::string()); |
| 866 } | 868 } |
| 867 | 869 |
| 868 EmbeddedWorkerTestHelper::OnStartWorker( | 870 EmbeddedWorkerTestHelper::OnStartWorker( |
| 869 embedded_worker_id, version_id, scope, script, pause_after_download); | 871 embedded_worker_id, version_id, scope, script, pause_after_download); |
| 870 } | 872 } |
| 871 | 873 |
| 872 void OnResumeAfterDownload(int embedded_worker_id) override { | 874 void OnResumeAfterDownload(int embedded_worker_id) override { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 904 entry.version_id = version->version_id(); | 906 entry.version_id = version->version_id(); |
| 905 entry.status = version->status(); | 907 entry.status = version->status(); |
| 906 state_change_log_.push_back(entry); | 908 state_change_log_.push_back(entry); |
| 907 } | 909 } |
| 908 | 910 |
| 909 scoped_refptr<ServiceWorkerRegistration> registration_; | 911 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 910 | 912 |
| 911 std::vector<AttributeChangeLogEntry> attribute_change_log_; | 913 std::vector<AttributeChangeLogEntry> attribute_change_log_; |
| 912 std::vector<StateChangeLogEntry> state_change_log_; | 914 std::vector<StateChangeLogEntry> state_change_log_; |
| 913 bool update_found_ = false; | 915 bool update_found_ = false; |
| 914 bool force_bypass_cache_for_scripts_ = false; | |
| 915 bool force_start_worker_failure_ = false; | 916 bool force_start_worker_failure_ = false; |
| 916 }; | 917 }; |
| 917 | 918 |
| 918 // Helper class for update tests that evicts the active version when the update | 919 // Helper class for update tests that evicts the active version when the update |
| 919 // worker is about to be started. | 920 // worker is about to be started. |
| 920 class EvictIncumbentVersionHelper : public UpdateJobTestHelper { | 921 class EvictIncumbentVersionHelper : public UpdateJobTestHelper { |
| 921 public: | 922 public: |
| 922 EvictIncumbentVersionHelper() {} | 923 EvictIncumbentVersionHelper() {} |
| 923 ~EvictIncumbentVersionHelper() override {} | 924 ~EvictIncumbentVersionHelper() override {} |
| 924 | 925 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 994 const base::Time kYesterday = | 995 const base::Time kYesterday = |
| 995 kToday - base::TimeDelta::FromDays(1) - base::TimeDelta::FromHours(1); | 996 kToday - base::TimeDelta::FromDays(1) - base::TimeDelta::FromHours(1); |
| 996 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper; | 997 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper; |
| 997 helper_.reset(update_helper); | 998 helper_.reset(update_helper); |
| 998 scoped_refptr<ServiceWorkerRegistration> registration = | 999 scoped_refptr<ServiceWorkerRegistration> registration = |
| 999 update_helper->SetupInitialRegistration(kNoChangeOrigin); | 1000 update_helper->SetupInitialRegistration(kNoChangeOrigin); |
| 1000 ASSERT_TRUE(registration.get()); | 1001 ASSERT_TRUE(registration.get()); |
| 1001 | 1002 |
| 1002 registration->AddListener(update_helper); | 1003 registration->AddListener(update_helper); |
| 1003 | 1004 |
| 1004 // Run an update that does not bypass the network cache. The check time | 1005 // Run an update where the script did not change and the network was not |
| 1005 // should not be updated. | 1006 // accessed. The check time should not be updated. |
| 1006 registration->set_last_update_check(kToday); | 1007 registration->set_last_update_check(kToday); |
| 1007 registration->active_version()->StartUpdate(); | 1008 registration->active_version()->StartUpdate(); |
| 1008 base::RunLoop().RunUntilIdle(); | 1009 base::RunLoop().RunUntilIdle(); |
| 1009 EXPECT_EQ(kToday, registration->last_update_check()); | 1010 EXPECT_EQ(kToday, registration->last_update_check()); |
| 1011 EXPECT_FALSE(update_helper->update_found_); |
| 1012 |
| 1013 // Run an update where the script did not change and the network was accessed. |
| 1014 // The check time should be updated. |
| 1015 registration->set_last_update_check(kYesterday); |
| 1016 registration->active_version()->StartUpdate(); |
| 1017 base::RunLoop().RunUntilIdle(); |
| 1018 EXPECT_LT(kYesterday, registration->last_update_check()); |
| 1019 EXPECT_FALSE(update_helper->update_found_); |
| 1010 registration->RemoveListener(update_helper); | 1020 registration->RemoveListener(update_helper); |
| 1011 | 1021 |
| 1012 registration = update_helper->SetupInitialRegistration(kNewVersionOrigin); | 1022 registration = update_helper->SetupInitialRegistration(kNewVersionOrigin); |
| 1013 ASSERT_TRUE(registration.get()); | 1023 ASSERT_TRUE(registration.get()); |
| 1014 | 1024 |
| 1015 registration->AddListener(update_helper); | 1025 registration->AddListener(update_helper); |
| 1016 | 1026 |
| 1017 // Run an update that bypasses the network cache. The check time should be | 1027 // Run an update where the script changed. The check time should be updated. |
| 1018 // updated. | |
| 1019 update_helper->set_force_bypass_cache_for_scripts(true); | |
| 1020 registration->set_last_update_check(kYesterday); | 1028 registration->set_last_update_check(kYesterday); |
| 1021 registration->active_version()->StartUpdate(); | 1029 registration->active_version()->StartUpdate(); |
| 1022 base::RunLoop().RunUntilIdle(); | 1030 base::RunLoop().RunUntilIdle(); |
| 1023 EXPECT_LT(kYesterday, registration->last_update_check()); | 1031 EXPECT_LT(kYesterday, registration->last_update_check()); |
| 1024 | 1032 |
| 1025 // Run an update to a worker that loads successfully but fails to start up | 1033 // Run an update to a worker that loads successfully but fails to start up |
| 1026 // (script evaluation failure). The check time should be updated. | 1034 // (script evaluation failure). The check time should be updated. |
| 1027 update_helper->set_force_bypass_cache_for_scripts(true); | |
| 1028 update_helper->set_force_start_worker_failure(true); | 1035 update_helper->set_force_start_worker_failure(true); |
| 1029 registration->set_last_update_check(kYesterday); | 1036 registration->set_last_update_check(kYesterday); |
| 1030 registration->active_version()->StartUpdate(); | 1037 registration->active_version()->StartUpdate(); |
| 1031 base::RunLoop().RunUntilIdle(); | 1038 base::RunLoop().RunUntilIdle(); |
| 1032 EXPECT_LT(kYesterday, registration->last_update_check()); | 1039 EXPECT_LT(kYesterday, registration->last_update_check()); |
| 1033 } | 1040 } |
| 1034 | 1041 |
| 1035 TEST_F(ServiceWorkerJobTest, Update_NewVersion) { | 1042 TEST_F(ServiceWorkerJobTest, Update_NewVersion) { |
| 1036 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper; | 1043 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper; |
| 1037 helper_.reset(update_helper); | 1044 helper_.reset(update_helper); |
| (...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1569 ASSERT_TRUE(start_msg); | 1576 ASSERT_TRUE(start_msg); |
| 1570 EmbeddedWorkerMsg_StartWorker::Param param; | 1577 EmbeddedWorkerMsg_StartWorker::Param param; |
| 1571 EmbeddedWorkerMsg_StartWorker::Read(start_msg, ¶m); | 1578 EmbeddedWorkerMsg_StartWorker::Read(start_msg, ¶m); |
| 1572 EmbeddedWorkerMsg_StartWorker_Params start_params = base::get<0>(param); | 1579 EmbeddedWorkerMsg_StartWorker_Params start_params = base::get<0>(param); |
| 1573 EXPECT_TRUE(start_params.pause_after_download); | 1580 EXPECT_TRUE(start_params.pause_after_download); |
| 1574 sink->ClearMessages(); | 1581 sink->ClearMessages(); |
| 1575 } | 1582 } |
| 1576 } | 1583 } |
| 1577 | 1584 |
| 1578 } // namespace content | 1585 } // namespace content |
| OLD | NEW |