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 "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" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "content/browser/service_worker/service_worker_database.pb.h" | 19 #include "content/browser/service_worker/service_worker_database.pb.h" |
20 #include "content/browser/service_worker/service_worker_metrics.h" | 20 #include "content/browser/service_worker/service_worker_metrics.h" |
21 #include "content/common/service_worker/service_worker_types.h" | 21 #include "content/common/service_worker/service_worker_types.h" |
22 #include "content/common/service_worker/service_worker_utils.h" | 22 #include "content/common/service_worker/service_worker_utils.h" |
23 #include "third_party/leveldatabase/env_chromium.h" | 23 #include "third_party/leveldatabase/env_chromium.h" |
24 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | 24 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" |
25 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 25 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
26 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 26 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
27 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 27 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 28 #include "url/origin.h" |
28 | 29 |
29 // LevelDB database schema | 30 // LevelDB database schema |
30 // ======================= | 31 // ======================= |
31 // | 32 // |
32 // NOTE | 33 // NOTE |
33 // - int64_t value is serialized as a string by base::Int64ToString(). | 34 // - int64_t value is serialized as a string by base::Int64ToString(). |
34 // - GURL value is serialized as a string by GURL::spec(). | 35 // - GURL value is serialized as a string by GURL::spec(). |
35 // | 36 // |
36 // Version 1 (in sorted order) | 37 // Version 1 (in sorted order) |
37 // key: "INITDATA_DB_VERSION" | 38 // key: "INITDATA_DB_VERSION" |
(...skipping 1148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1186 GURL sub_scope_url(data.foreign_fetch_scope(i)); | 1187 GURL sub_scope_url(data.foreign_fetch_scope(i)); |
1187 if (!sub_scope_url.is_valid() || | 1188 if (!sub_scope_url.is_valid() || |
1188 !ServiceWorkerUtils::ScopeMatches(scope_url, sub_scope_url)) { | 1189 !ServiceWorkerUtils::ScopeMatches(scope_url, sub_scope_url)) { |
1189 DLOG(ERROR) << "Foreign fetch scope '" << data.foreign_fetch_scope(i) | 1190 DLOG(ERROR) << "Foreign fetch scope '" << data.foreign_fetch_scope(i) |
1190 << "' is not valid or does not match Scope URL '" << scope_url | 1191 << "' is not valid or does not match Scope URL '" << scope_url |
1191 << "'."; | 1192 << "'."; |
1192 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; | 1193 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; |
1193 } | 1194 } |
1194 out->foreign_fetch_scopes.push_back(sub_scope_url); | 1195 out->foreign_fetch_scopes.push_back(sub_scope_url); |
1195 } | 1196 } |
| 1197 for (int i = 0; i < data.foreign_fetch_origin_size(); ++i) { |
| 1198 url::Origin parsed_origin(GURL(data.foreign_fetch_origin(i))); |
| 1199 if (parsed_origin.unique()) { |
| 1200 DLOG(ERROR) << "Foreign fetch origin '" << data.foreign_fetch_origin(i) |
| 1201 << "' is not valid."; |
| 1202 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; |
| 1203 } |
| 1204 out->foreign_fetch_origins.push_back(parsed_origin); |
| 1205 } |
1196 | 1206 |
1197 return ServiceWorkerDatabase::STATUS_OK; | 1207 return ServiceWorkerDatabase::STATUS_OK; |
1198 } | 1208 } |
1199 | 1209 |
1200 void ServiceWorkerDatabase::WriteRegistrationDataInBatch( | 1210 void ServiceWorkerDatabase::WriteRegistrationDataInBatch( |
1201 const RegistrationData& registration, | 1211 const RegistrationData& registration, |
1202 leveldb::WriteBatch* batch) { | 1212 leveldb::WriteBatch* batch) { |
1203 DCHECK(batch); | 1213 DCHECK(batch); |
1204 | 1214 |
1205 // The registration id and version id should be bumped before this. | 1215 // The registration id and version id should be bumped before this. |
(...skipping 11 matching lines...) Expand all Loading... |
1217 data.set_last_update_check_time( | 1227 data.set_last_update_check_time( |
1218 registration.last_update_check.ToInternalValue()); | 1228 registration.last_update_check.ToInternalValue()); |
1219 data.set_resources_total_size_bytes(registration.resources_total_size_bytes); | 1229 data.set_resources_total_size_bytes(registration.resources_total_size_bytes); |
1220 for (const GURL& url : registration.foreign_fetch_scopes) { | 1230 for (const GURL& url : registration.foreign_fetch_scopes) { |
1221 DCHECK(ServiceWorkerUtils::ScopeMatches(registration.scope, url)) | 1231 DCHECK(ServiceWorkerUtils::ScopeMatches(registration.scope, url)) |
1222 << "Foreign fetch scope '" << url | 1232 << "Foreign fetch scope '" << url |
1223 << "' does not match service worker scope '" << registration.scope | 1233 << "' does not match service worker scope '" << registration.scope |
1224 << "'."; | 1234 << "'."; |
1225 data.add_foreign_fetch_scope(url.spec()); | 1235 data.add_foreign_fetch_scope(url.spec()); |
1226 } | 1236 } |
| 1237 for (const url::Origin& origin : registration.foreign_fetch_origins) |
| 1238 data.add_foreign_fetch_origin(origin.Serialize()); |
1227 | 1239 |
1228 std::string value; | 1240 std::string value; |
1229 bool success = data.SerializeToString(&value); | 1241 bool success = data.SerializeToString(&value); |
1230 DCHECK(success); | 1242 DCHECK(success); |
1231 GURL origin = registration.scope.GetOrigin(); | 1243 GURL origin = registration.scope.GetOrigin(); |
1232 batch->Put(CreateRegistrationKey(data.registration_id(), origin), value); | 1244 batch->Put(CreateRegistrationKey(data.registration_id(), origin), value); |
1233 } | 1245 } |
1234 | 1246 |
1235 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadResourceRecords( | 1247 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadResourceRecords( |
1236 int64_t version_id, | 1248 int64_t version_id, |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1585 if (status != STATUS_OK) | 1597 if (status != STATUS_OK) |
1586 Disable(from_here, status); | 1598 Disable(from_here, status); |
1587 ServiceWorkerMetrics::CountWriteDatabaseResult(status); | 1599 ServiceWorkerMetrics::CountWriteDatabaseResult(status); |
1588 } | 1600 } |
1589 | 1601 |
1590 bool ServiceWorkerDatabase::IsDatabaseInMemory() const { | 1602 bool ServiceWorkerDatabase::IsDatabaseInMemory() const { |
1591 return path_.empty(); | 1603 return path_.empty(); |
1592 } | 1604 } |
1593 | 1605 |
1594 } // namespace content | 1606 } // namespace content |
OLD | NEW |