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

Side by Side Diff: content/browser/indexed_db/indexed_db_database.cc

Issue 2735213002: Indexed DB: Ensure large explicit keys consistently max out generator (Closed)
Patch Set: Incorporate review feedback Created 3 years, 9 months 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
« no previous file with comments | « no previous file | third_party/WebKit/LayoutTests/external/wpt/IndexedDB/keygenerator-explicit.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/indexed_db/indexed_db_database.h" 5 #include "content/browser/indexed_db/indexed_db_database.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include <algorithm>
9 #include <limits> 10 #include <limits>
10 #include <set> 11 #include <set>
11 12
12 #include "base/auto_reset.h" 13 #include "base/auto_reset.h"
13 #include "base/logging.h" 14 #include "base/logging.h"
14 #include "base/macros.h" 15 #include "base/macros.h"
15 #include "base/memory/ptr_util.h" 16 #include "base/memory/ptr_util.h"
16 #include "base/metrics/histogram_macros.h" 17 #include "base/metrics/histogram_macros.h"
17 #include "base/numerics/safe_conversions.h" 18 #include "base/numerics/safe_conversions.h"
18 #include "base/stl_util.h" 19 #include "base/stl_util.h"
(...skipping 1157 matching lines...) Expand 10 before | Expand all | Expand 10 after
1176 callbacks->OnSuccessArray(&found_values); 1177 callbacks->OnSuccessArray(&found_values);
1177 } 1178 }
1178 return s; 1179 return s;
1179 } 1180 }
1180 1181
1181 static std::unique_ptr<IndexedDBKey> GenerateKey( 1182 static std::unique_ptr<IndexedDBKey> GenerateKey(
1182 IndexedDBBackingStore* backing_store, 1183 IndexedDBBackingStore* backing_store,
1183 IndexedDBTransaction* transaction, 1184 IndexedDBTransaction* transaction,
1184 int64_t database_id, 1185 int64_t database_id,
1185 int64_t object_store_id) { 1186 int64_t object_store_id) {
1186 const int64_t max_generator_value = 1187 // Maximum integer uniquely representable as ECMAScript number.
1187 9007199254740992LL; // Maximum integer storable as ECMAScript number. 1188 const int64_t max_generator_value = 9007199254740992LL;
1188 int64_t current_number; 1189 int64_t current_number;
1189 leveldb::Status s = backing_store->GetKeyGeneratorCurrentNumber( 1190 leveldb::Status s = backing_store->GetKeyGeneratorCurrentNumber(
1190 transaction->BackingStoreTransaction(), 1191 transaction->BackingStoreTransaction(), database_id, object_store_id,
1191 database_id,
1192 object_store_id,
1193 &current_number); 1192 &current_number);
1194 if (!s.ok()) { 1193 if (!s.ok()) {
1195 LOG(ERROR) << "Failed to GetKeyGeneratorCurrentNumber"; 1194 LOG(ERROR) << "Failed to GetKeyGeneratorCurrentNumber";
1196 return base::MakeUnique<IndexedDBKey>(); 1195 return base::MakeUnique<IndexedDBKey>();
1197 } 1196 }
1198 if (current_number < 0 || current_number > max_generator_value) 1197 if (current_number < 0 || current_number > max_generator_value)
1199 return base::MakeUnique<IndexedDBKey>(); 1198 return base::MakeUnique<IndexedDBKey>();
1200 1199
1201 return base::MakeUnique<IndexedDBKey>(current_number, WebIDBKeyTypeNumber); 1200 return base::MakeUnique<IndexedDBKey>(current_number, WebIDBKeyTypeNumber);
1202 } 1201 }
1203 1202
1203 // Called at the end of a "put" operation. The key is a number that was either
1204 // generated by the generator which now needs to be incremented (so
1205 // |check_current| is false) or was user-supplied so we only conditionally use
1206 // (and |check_current| is true).
1204 static leveldb::Status UpdateKeyGenerator(IndexedDBBackingStore* backing_store, 1207 static leveldb::Status UpdateKeyGenerator(IndexedDBBackingStore* backing_store,
1205 IndexedDBTransaction* transaction, 1208 IndexedDBTransaction* transaction,
1206 int64_t database_id, 1209 int64_t database_id,
1207 int64_t object_store_id, 1210 int64_t object_store_id,
1208 const IndexedDBKey& key, 1211 const IndexedDBKey& key,
1209 bool check_current) { 1212 bool check_current) {
1210 DCHECK_EQ(WebIDBKeyTypeNumber, key.type()); 1213 DCHECK_EQ(WebIDBKeyTypeNumber, key.type());
1214 // Maximum integer uniquely representable as ECMAScript number.
1215 const double max_generator_value = 9007199254740992.0;
1216 int64_t value = base::saturated_cast<int64_t>(
1217 floor(std::min(key.number(), max_generator_value)));
1211 return backing_store->MaybeUpdateKeyGeneratorCurrentNumber( 1218 return backing_store->MaybeUpdateKeyGeneratorCurrentNumber(
1212 transaction->BackingStoreTransaction(), database_id, object_store_id, 1219 transaction->BackingStoreTransaction(), database_id, object_store_id,
1213 static_cast<int64_t>(floor(key.number())) + 1, check_current); 1220 value + 1, check_current);
1214 } 1221 }
1215 1222
1216 struct IndexedDBDatabase::PutOperationParams { 1223 struct IndexedDBDatabase::PutOperationParams {
1217 PutOperationParams() {} 1224 PutOperationParams() {}
1218 int64_t object_store_id; 1225 int64_t object_store_id;
1219 IndexedDBValue value; 1226 IndexedDBValue value;
1220 std::vector<std::unique_ptr<storage::BlobDataHandle>> handles; 1227 std::vector<std::unique_ptr<storage::BlobDataHandle>> handles;
1221 std::unique_ptr<IndexedDBKey> key; 1228 std::unique_ptr<IndexedDBKey> key;
1222 blink::WebIDBPutMode put_mode; 1229 blink::WebIDBPutMode put_mode;
1223 scoped_refptr<IndexedDBCallbacks> callbacks; 1230 scoped_refptr<IndexedDBCallbacks> callbacks;
(...skipping 724 matching lines...) Expand 10 before | Expand all | Expand 10 after
1948 if (status.IsCorruption()) { 1955 if (status.IsCorruption()) {
1949 IndexedDBDatabaseError error(blink::WebIDBDatabaseExceptionUnknownError, 1956 IndexedDBDatabaseError error(blink::WebIDBDatabaseExceptionUnknownError,
1950 message); 1957 message);
1951 factory_->HandleBackingStoreCorruption(backing_store_->origin(), error); 1958 factory_->HandleBackingStoreCorruption(backing_store_->origin(), error);
1952 } else { 1959 } else {
1953 factory_->HandleBackingStoreFailure(backing_store_->origin()); 1960 factory_->HandleBackingStoreFailure(backing_store_->origin());
1954 } 1961 }
1955 } 1962 }
1956 1963
1957 } // namespace content 1964 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/LayoutTests/external/wpt/IndexedDB/keygenerator-explicit.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698