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