Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(253)

Side by Side Diff: content/browser/service_worker/service_worker_database.cc

Issue 2521793004: service worker: Persist NavigationPreloadState (Closed)
Patch Set: expect_ name Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "content/browser/service_worker/service_worker_database.h" 5 #include "content/browser/service_worker/service_worker_database.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 715 matching lines...) Expand 10 before | Expand all | Expand 10 after
726 if (status != STATUS_OK) 726 if (status != STATUS_OK)
727 return status; 727 return status;
728 728
729 registration.last_update_check = time; 729 registration.last_update_check = time;
730 730
731 leveldb::WriteBatch batch; 731 leveldb::WriteBatch batch;
732 WriteRegistrationDataInBatch(registration, &batch); 732 WriteRegistrationDataInBatch(registration, &batch);
733 return WriteBatch(&batch); 733 return WriteBatch(&batch);
734 } 734 }
735 735
736 ServiceWorkerDatabase::Status
737 ServiceWorkerDatabase::UpdateNavigationPreloadEnabled(int64_t registration_id,
738 const GURL& origin,
739 bool enable) {
740 DCHECK(sequence_checker_.CalledOnValidSequence());
741 Status status = LazyOpen(false);
742 if (IsNewOrNonexistentDatabase(status))
743 return STATUS_ERROR_NOT_FOUND;
744 if (status != STATUS_OK)
745 return status;
746 if (!origin.is_valid())
747 return STATUS_ERROR_FAILED;
748
749 RegistrationData registration;
750 status = ReadRegistrationData(registration_id, origin, &registration);
751 if (status != STATUS_OK)
752 return status;
753
754 registration.navigation_preload_state.enabled = enable;
755
756 leveldb::WriteBatch batch;
757 WriteRegistrationDataInBatch(registration, &batch);
758 return WriteBatch(&batch);
759 }
760
761 ServiceWorkerDatabase::Status
762 ServiceWorkerDatabase::UpdateNavigationPreloadHeader(int64_t registration_id,
763 const GURL& origin,
764 const std::string& value) {
765 DCHECK(sequence_checker_.CalledOnValidSequence());
766 Status status = LazyOpen(false);
767 if (IsNewOrNonexistentDatabase(status))
768 return STATUS_ERROR_NOT_FOUND;
769 if (status != STATUS_OK)
770 return status;
771 if (!origin.is_valid())
772 return STATUS_ERROR_FAILED;
773
774 RegistrationData registration;
775 status = ReadRegistrationData(registration_id, origin, &registration);
776 if (status != STATUS_OK)
777 return status;
778
779 registration.navigation_preload_state.header = value;
780
781 leveldb::WriteBatch batch;
782 WriteRegistrationDataInBatch(registration, &batch);
783 return WriteBatch(&batch);
784 }
785
736 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteRegistration( 786 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteRegistration(
737 int64_t registration_id, 787 int64_t registration_id,
738 const GURL& origin, 788 const GURL& origin,
739 RegistrationData* deleted_version, 789 RegistrationData* deleted_version,
740 std::vector<int64_t>* newly_purgeable_resources) { 790 std::vector<int64_t>* newly_purgeable_resources) {
741 DCHECK(sequence_checker_.CalledOnValidSequence()); 791 DCHECK(sequence_checker_.CalledOnValidSequence());
742 DCHECK(deleted_version); 792 DCHECK(deleted_version);
743 deleted_version->version_id = kInvalidServiceWorkerVersionId; 793 deleted_version->version_id = kInvalidServiceWorkerVersionId;
744 Status status = LazyOpen(false); 794 Status status = LazyOpen(false);
745 if (IsNewOrNonexistentDatabase(status)) 795 if (IsNewOrNonexistentDatabase(status))
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after
1226 if (data.has_origin_trial_tokens()) { 1276 if (data.has_origin_trial_tokens()) {
1227 const ServiceWorkerOriginTrialInfo& info = data.origin_trial_tokens(); 1277 const ServiceWorkerOriginTrialInfo& info = data.origin_trial_tokens();
1228 TrialTokenValidator::FeatureToTokensMap origin_trial_tokens; 1278 TrialTokenValidator::FeatureToTokensMap origin_trial_tokens;
1229 for (int i = 0; i < info.features_size(); ++i) { 1279 for (int i = 0; i < info.features_size(); ++i) {
1230 const auto& feature = info.features(i); 1280 const auto& feature = info.features(i);
1231 for (int j = 0; j < feature.tokens_size(); ++j) 1281 for (int j = 0; j < feature.tokens_size(); ++j)
1232 origin_trial_tokens[feature.name()].push_back(feature.tokens(j)); 1282 origin_trial_tokens[feature.name()].push_back(feature.tokens(j));
1233 } 1283 }
1234 out->origin_trial_tokens = origin_trial_tokens; 1284 out->origin_trial_tokens = origin_trial_tokens;
1235 } 1285 }
1286 if (data.has_navigation_preload_state()) {
1287 const ServiceWorkerNavigationPreloadState& state =
1288 data.navigation_preload_state();
1289 out->navigation_preload_state.enabled = state.enabled();
1290 if (state.has_header())
1291 out->navigation_preload_state.header = state.header();
1292 }
1236 1293
1237 return ServiceWorkerDatabase::STATUS_OK; 1294 return ServiceWorkerDatabase::STATUS_OK;
1238 } 1295 }
1239 1296
1240 void ServiceWorkerDatabase::WriteRegistrationDataInBatch( 1297 void ServiceWorkerDatabase::WriteRegistrationDataInBatch(
1241 const RegistrationData& registration, 1298 const RegistrationData& registration,
1242 leveldb::WriteBatch* batch) { 1299 leveldb::WriteBatch* batch) {
1243 DCHECK(batch); 1300 DCHECK(batch);
1244 1301
1245 // The registration id and version id should be bumped before this. 1302 // The registration id and version id should be bumped before this.
(...skipping 22 matching lines...) Expand all
1268 data.add_foreign_fetch_origin(origin.Serialize()); 1325 data.add_foreign_fetch_origin(origin.Serialize());
1269 if (registration.origin_trial_tokens) { 1326 if (registration.origin_trial_tokens) {
1270 ServiceWorkerOriginTrialInfo* info = data.mutable_origin_trial_tokens(); 1327 ServiceWorkerOriginTrialInfo* info = data.mutable_origin_trial_tokens();
1271 for (const auto& feature : *registration.origin_trial_tokens) { 1328 for (const auto& feature : *registration.origin_trial_tokens) {
1272 ServiceWorkerOriginTrialFeature* feature_out = info->add_features(); 1329 ServiceWorkerOriginTrialFeature* feature_out = info->add_features();
1273 feature_out->set_name(feature.first); 1330 feature_out->set_name(feature.first);
1274 for (const auto& token : feature.second) 1331 for (const auto& token : feature.second)
1275 feature_out->add_tokens(token); 1332 feature_out->add_tokens(token);
1276 } 1333 }
1277 } 1334 }
1335 ServiceWorkerNavigationPreloadState* state =
1336 data.mutable_navigation_preload_state();
1337 state->set_enabled(registration.navigation_preload_state.enabled);
1338 state->set_header(registration.navigation_preload_state.header);
1278 1339
1279 std::string value; 1340 std::string value;
1280 bool success = data.SerializeToString(&value); 1341 bool success = data.SerializeToString(&value);
1281 DCHECK(success); 1342 DCHECK(success);
1282 GURL origin = registration.scope.GetOrigin(); 1343 GURL origin = registration.scope.GetOrigin();
1283 batch->Put(CreateRegistrationKey(data.registration_id(), origin), value); 1344 batch->Put(CreateRegistrationKey(data.registration_id(), origin), value);
1284 } 1345 }
1285 1346
1286 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadResourceRecords( 1347 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadResourceRecords(
1287 const RegistrationData& registration, 1348 const RegistrationData& registration,
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
1653 if (status != STATUS_OK) 1714 if (status != STATUS_OK)
1654 Disable(from_here, status); 1715 Disable(from_here, status);
1655 ServiceWorkerMetrics::CountWriteDatabaseResult(status); 1716 ServiceWorkerMetrics::CountWriteDatabaseResult(status);
1656 } 1717 }
1657 1718
1658 bool ServiceWorkerDatabase::IsDatabaseInMemory() const { 1719 bool ServiceWorkerDatabase::IsDatabaseInMemory() const {
1659 return path_.empty(); 1720 return path_.empty();
1660 } 1721 }
1661 1722
1662 } // namespace content 1723 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/service_worker_database.h ('k') | content/browser/service_worker/service_worker_database.proto » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698