OLD | NEW |
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 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
(...skipping 1214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1225 object_store_metadata.name + ASCIIToUTF16("'."); | 1225 object_store_metadata.name + ASCIIToUTF16("'."); |
1226 transaction->Abort(IndexedDBDatabaseError( | 1226 transaction->Abort(IndexedDBDatabaseError( |
1227 blink::WebIDBDatabaseExceptionUnknownError, error_string)); | 1227 blink::WebIDBDatabaseExceptionUnknownError, error_string)); |
1228 } | 1228 } |
1229 } | 1229 } |
1230 | 1230 |
1231 void IndexedDBDatabase::VersionChangeOperation( | 1231 void IndexedDBDatabase::VersionChangeOperation( |
1232 int64 version, | 1232 int64 version, |
1233 scoped_refptr<IndexedDBCallbacks> callbacks, | 1233 scoped_refptr<IndexedDBCallbacks> callbacks, |
1234 scoped_ptr<IndexedDBConnection> connection, | 1234 scoped_ptr<IndexedDBConnection> connection, |
1235 blink::WebIDBDataLoss data_loss, | |
1236 std::string data_loss_message, | |
1237 IndexedDBTransaction* transaction) { | 1235 IndexedDBTransaction* transaction) { |
1238 IDB_TRACE("IndexedDBDatabase::VersionChangeOperation"); | 1236 IDB_TRACE("IndexedDBDatabase::VersionChangeOperation"); |
1239 int64 old_version = metadata_.int_version; | 1237 int64 old_version = metadata_.int_version; |
1240 DCHECK_GT(version, old_version); | 1238 DCHECK_GT(version, old_version); |
1241 metadata_.int_version = version; | 1239 metadata_.int_version = version; |
1242 if (!backing_store_->UpdateIDBDatabaseIntVersion( | 1240 if (!backing_store_->UpdateIDBDatabaseIntVersion( |
1243 transaction->BackingStoreTransaction(), | 1241 transaction->BackingStoreTransaction(), |
1244 id(), | 1242 id(), |
1245 metadata_.int_version)) { | 1243 metadata_.int_version)) { |
1246 IndexedDBDatabaseError error( | 1244 IndexedDBDatabaseError error( |
1247 blink::WebIDBDatabaseExceptionUnknownError, | 1245 blink::WebIDBDatabaseExceptionUnknownError, |
1248 ASCIIToUTF16( | 1246 ASCIIToUTF16( |
1249 "Internal error writing data to stable storage when " | 1247 "Internal error writing data to stable storage when " |
1250 "updating version.")); | 1248 "updating version.")); |
1251 callbacks->OnError(error); | 1249 callbacks->OnError(error); |
1252 transaction->Abort(error); | 1250 transaction->Abort(error); |
1253 return; | 1251 return; |
1254 } | 1252 } |
1255 DCHECK(!pending_second_half_open_); | 1253 DCHECK(!pending_second_half_open_); |
1256 pending_second_half_open_.reset( | 1254 pending_second_half_open_.reset( |
1257 new PendingSuccessCall(callbacks, connection.get(), version)); | 1255 new PendingSuccessCall(callbacks, connection.get(), version)); |
1258 callbacks->OnUpgradeNeeded( | 1256 callbacks->OnUpgradeNeeded(old_version, connection.Pass(), metadata()); |
1259 old_version, connection.Pass(), metadata(), data_loss, data_loss_message); | |
1260 } | 1257 } |
1261 | 1258 |
1262 void IndexedDBDatabase::TransactionStarted(IndexedDBTransaction* transaction) { | 1259 void IndexedDBDatabase::TransactionStarted(IndexedDBTransaction* transaction) { |
1263 | 1260 |
1264 if (transaction->mode() == indexed_db::TRANSACTION_VERSION_CHANGE) { | 1261 if (transaction->mode() == indexed_db::TRANSACTION_VERSION_CHANGE) { |
1265 DCHECK(!running_version_change_transaction_); | 1262 DCHECK(!running_version_change_transaction_); |
1266 running_version_change_transaction_ = transaction; | 1263 running_version_change_transaction_ = transaction; |
1267 } | 1264 } |
1268 } | 1265 } |
1269 | 1266 |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1407 return !pending_delete_calls_.empty() || | 1404 return !pending_delete_calls_.empty() || |
1408 running_version_change_transaction_ || | 1405 running_version_change_transaction_ || |
1409 pending_run_version_change_transaction_call_; | 1406 pending_run_version_change_transaction_call_; |
1410 } | 1407 } |
1411 | 1408 |
1412 void IndexedDBDatabase::OpenConnection( | 1409 void IndexedDBDatabase::OpenConnection( |
1413 scoped_refptr<IndexedDBCallbacks> callbacks, | 1410 scoped_refptr<IndexedDBCallbacks> callbacks, |
1414 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks, | 1411 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks, |
1415 int64 transaction_id, | 1412 int64 transaction_id, |
1416 int64 version) { | 1413 int64 version) { |
1417 const blink::WebIDBDataLoss kDataLoss = | |
1418 blink::WebIDBDataLossNone; | |
1419 OpenConnection( | |
1420 callbacks, database_callbacks, transaction_id, version, kDataLoss, ""); | |
1421 } | |
1422 | |
1423 void IndexedDBDatabase::OpenConnection( | |
1424 scoped_refptr<IndexedDBCallbacks> callbacks, | |
1425 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks, | |
1426 int64 transaction_id, | |
1427 int64 version, | |
1428 blink::WebIDBDataLoss data_loss, | |
1429 std::string data_loss_message) { | |
1430 DCHECK(backing_store_); | 1414 DCHECK(backing_store_); |
1431 | 1415 |
1432 // TODO(jsbell): Should have a priority queue so that higher version | 1416 // TODO(jsbell): Should have a priority queue so that higher version |
1433 // requests are processed first. http://crbug.com/225850 | 1417 // requests are processed first. http://crbug.com/225850 |
1434 if (IsOpenConnectionBlocked()) { | 1418 if (IsOpenConnectionBlocked()) { |
1435 // The backing store only detects data loss when it is first opened. The | 1419 // The backing store only detects data loss when it is first opened. The |
1436 // presence of existing connections means we didn't even check for data loss | 1420 // presence of existing connections means we didn't even check for data loss |
1437 // so there'd better not be any. | 1421 // so there'd better not be any. |
1438 DCHECK_NE(blink::WebIDBDataLossTotal, data_loss); | 1422 DCHECK_NE(blink::WebIDBDataLossTotal, callbacks->data_loss()); |
1439 pending_open_calls_.push_back(new PendingOpenCall( | 1423 pending_open_calls_.push_back(new PendingOpenCall( |
1440 callbacks, database_callbacks, transaction_id, version)); | 1424 callbacks, database_callbacks, transaction_id, version)); |
1441 return; | 1425 return; |
1442 } | 1426 } |
1443 | 1427 |
1444 if (metadata_.id == kInvalidId) { | 1428 if (metadata_.id == kInvalidId) { |
1445 // The database was deleted then immediately re-opened; OpenInternal() | 1429 // The database was deleted then immediately re-opened; OpenInternal() |
1446 // recreates it in the backing store. | 1430 // recreates it in the backing store. |
1447 if (OpenInternal()) { | 1431 if (OpenInternal()) { |
1448 DCHECK_EQ(IndexedDBDatabaseMetadata::NO_INT_VERSION, | 1432 DCHECK_EQ(IndexedDBDatabaseMetadata::NO_INT_VERSION, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1488 callbacks->OnSuccess(connection.Pass(), this->metadata()); | 1472 callbacks->OnSuccess(connection.Pass(), this->metadata()); |
1489 return; | 1473 return; |
1490 } | 1474 } |
1491 // Spec says: If no version is specified and no database exists, set | 1475 // Spec says: If no version is specified and no database exists, set |
1492 // database version to 1. | 1476 // database version to 1. |
1493 version = 1; | 1477 version = 1; |
1494 } | 1478 } |
1495 | 1479 |
1496 if (version > metadata_.int_version) { | 1480 if (version > metadata_.int_version) { |
1497 connections_.insert(connection.get()); | 1481 connections_.insert(connection.get()); |
1498 RunVersionChangeTransaction(callbacks, | 1482 RunVersionChangeTransaction( |
1499 connection.Pass(), | 1483 callbacks, connection.Pass(), transaction_id, version); |
1500 transaction_id, | |
1501 version, | |
1502 data_loss, | |
1503 data_loss_message); | |
1504 return; | 1484 return; |
1505 } | 1485 } |
1506 if (version < metadata_.int_version) { | 1486 if (version < metadata_.int_version) { |
1507 callbacks->OnError(IndexedDBDatabaseError( | 1487 callbacks->OnError(IndexedDBDatabaseError( |
1508 blink::WebIDBDatabaseExceptionVersionError, | 1488 blink::WebIDBDatabaseExceptionVersionError, |
1509 ASCIIToUTF16("The requested version (") + Int64ToString16(version) + | 1489 ASCIIToUTF16("The requested version (") + Int64ToString16(version) + |
1510 ASCIIToUTF16(") is less than the existing version (") + | 1490 ASCIIToUTF16(") is less than the existing version (") + |
1511 Int64ToString16(metadata_.int_version) + ASCIIToUTF16(")."))); | 1491 Int64ToString16(metadata_.int_version) + ASCIIToUTF16(")."))); |
1512 return; | 1492 return; |
1513 } | 1493 } |
1514 DCHECK_EQ(version, metadata_.int_version); | 1494 DCHECK_EQ(version, metadata_.int_version); |
1515 connections_.insert(connection.get()); | 1495 connections_.insert(connection.get()); |
1516 callbacks->OnSuccess(connection.Pass(), this->metadata()); | 1496 callbacks->OnSuccess(connection.Pass(), this->metadata()); |
1517 } | 1497 } |
1518 | 1498 |
1519 void IndexedDBDatabase::RunVersionChangeTransaction( | 1499 void IndexedDBDatabase::RunVersionChangeTransaction( |
1520 scoped_refptr<IndexedDBCallbacks> callbacks, | 1500 scoped_refptr<IndexedDBCallbacks> callbacks, |
1521 scoped_ptr<IndexedDBConnection> connection, | 1501 scoped_ptr<IndexedDBConnection> connection, |
1522 int64 transaction_id, | 1502 int64 transaction_id, |
1523 int64 requested_version, | 1503 int64 requested_version) { |
1524 blink::WebIDBDataLoss data_loss, | |
1525 std::string data_loss_message) { | |
1526 | 1504 |
1527 DCHECK(callbacks); | 1505 DCHECK(callbacks); |
1528 DCHECK(connections_.count(connection.get())); | 1506 DCHECK(connections_.count(connection.get())); |
1529 if (ConnectionCount() > 1) { | 1507 if (ConnectionCount() > 1) { |
1530 DCHECK_NE(blink::WebIDBDataLossTotal, data_loss); | 1508 DCHECK_NE(blink::WebIDBDataLossTotal, callbacks->data_loss()); |
1531 // Front end ensures the event is not fired at connections that have | 1509 // Front end ensures the event is not fired at connections that have |
1532 // close_pending set. | 1510 // close_pending set. |
1533 for (ConnectionSet::const_iterator it = connections_.begin(); | 1511 for (ConnectionSet::const_iterator it = connections_.begin(); |
1534 it != connections_.end(); | 1512 it != connections_.end(); |
1535 ++it) { | 1513 ++it) { |
1536 if (*it != connection.get()) { | 1514 if (*it != connection.get()) { |
1537 (*it)->callbacks()->OnVersionChange(metadata_.int_version, | 1515 (*it)->callbacks()->OnVersionChange(metadata_.int_version, |
1538 requested_version); | 1516 requested_version); |
1539 } | 1517 } |
1540 } | 1518 } |
1541 // TODO(jsbell): Remove the call to OnBlocked and instead wait | 1519 // TODO(jsbell): Remove the call to OnBlocked and instead wait |
1542 // until the frontend tells us that all the "versionchange" events | 1520 // until the frontend tells us that all the "versionchange" events |
1543 // have been delivered. http://crbug.com/100123 | 1521 // have been delivered. http://crbug.com/100123 |
1544 callbacks->OnBlocked(metadata_.int_version); | 1522 callbacks->OnBlocked(metadata_.int_version); |
1545 | 1523 |
1546 DCHECK(!pending_run_version_change_transaction_call_); | 1524 DCHECK(!pending_run_version_change_transaction_call_); |
1547 pending_run_version_change_transaction_call_.reset(new PendingUpgradeCall( | 1525 pending_run_version_change_transaction_call_.reset(new PendingUpgradeCall( |
1548 callbacks, connection.Pass(), transaction_id, requested_version)); | 1526 callbacks, connection.Pass(), transaction_id, requested_version)); |
1549 return; | 1527 return; |
1550 } | 1528 } |
1551 RunVersionChangeTransactionFinal(callbacks, | 1529 RunVersionChangeTransactionFinal( |
1552 connection.Pass(), | 1530 callbacks, connection.Pass(), transaction_id, requested_version); |
1553 transaction_id, | |
1554 requested_version, | |
1555 data_loss, | |
1556 data_loss_message); | |
1557 } | 1531 } |
1558 | 1532 |
1559 void IndexedDBDatabase::RunVersionChangeTransactionFinal( | 1533 void IndexedDBDatabase::RunVersionChangeTransactionFinal( |
1560 scoped_refptr<IndexedDBCallbacks> callbacks, | 1534 scoped_refptr<IndexedDBCallbacks> callbacks, |
1561 scoped_ptr<IndexedDBConnection> connection, | 1535 scoped_ptr<IndexedDBConnection> connection, |
1562 int64 transaction_id, | 1536 int64 transaction_id, |
1563 int64 requested_version) { | 1537 int64 requested_version) { |
1564 const blink::WebIDBDataLoss kDataLoss = | |
1565 blink::WebIDBDataLossNone; | |
1566 RunVersionChangeTransactionFinal(callbacks, | |
1567 connection.Pass(), | |
1568 transaction_id, | |
1569 requested_version, | |
1570 kDataLoss, | |
1571 ""); | |
1572 } | |
1573 | |
1574 void IndexedDBDatabase::RunVersionChangeTransactionFinal( | |
1575 scoped_refptr<IndexedDBCallbacks> callbacks, | |
1576 scoped_ptr<IndexedDBConnection> connection, | |
1577 int64 transaction_id, | |
1578 int64 requested_version, | |
1579 blink::WebIDBDataLoss data_loss, | |
1580 std::string data_loss_message) { | |
1581 | 1538 |
1582 std::vector<int64> object_store_ids; | 1539 std::vector<int64> object_store_ids; |
1583 CreateTransaction(transaction_id, | 1540 CreateTransaction(transaction_id, |
1584 connection.get(), | 1541 connection.get(), |
1585 object_store_ids, | 1542 object_store_ids, |
1586 indexed_db::TRANSACTION_VERSION_CHANGE); | 1543 indexed_db::TRANSACTION_VERSION_CHANGE); |
1587 | 1544 |
1588 transactions_[transaction_id] | 1545 transactions_[transaction_id] |
1589 ->ScheduleTask(base::Bind(&IndexedDBDatabase::VersionChangeOperation, | 1546 ->ScheduleTask(base::Bind(&IndexedDBDatabase::VersionChangeOperation, |
1590 this, | 1547 this, |
1591 requested_version, | 1548 requested_version, |
1592 callbacks, | 1549 callbacks, |
1593 base::Passed(&connection), | 1550 base::Passed(&connection)), |
1594 data_loss, | |
1595 data_loss_message), | |
1596 base::Bind(&IndexedDBDatabase::VersionChangeAbortOperation, | 1551 base::Bind(&IndexedDBDatabase::VersionChangeAbortOperation, |
1597 this, | 1552 this, |
1598 metadata_.version, | 1553 metadata_.version, |
1599 metadata_.int_version)); | 1554 metadata_.int_version)); |
1600 | 1555 |
1601 DCHECK(!pending_second_half_open_); | 1556 DCHECK(!pending_second_half_open_); |
1602 } | 1557 } |
1603 | 1558 |
1604 void IndexedDBDatabase::DeleteDatabase( | 1559 void IndexedDBDatabase::DeleteDatabase( |
1605 scoped_refptr<IndexedDBCallbacks> callbacks) { | 1560 scoped_refptr<IndexedDBCallbacks> callbacks) { |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1717 const base::string16& previous_version, | 1672 const base::string16& previous_version, |
1718 int64 previous_int_version, | 1673 int64 previous_int_version, |
1719 IndexedDBTransaction* transaction) { | 1674 IndexedDBTransaction* transaction) { |
1720 IDB_TRACE("IndexedDBDatabase::VersionChangeAbortOperation"); | 1675 IDB_TRACE("IndexedDBDatabase::VersionChangeAbortOperation"); |
1721 DCHECK(!transaction); | 1676 DCHECK(!transaction); |
1722 metadata_.version = previous_version; | 1677 metadata_.version = previous_version; |
1723 metadata_.int_version = previous_int_version; | 1678 metadata_.int_version = previous_int_version; |
1724 } | 1679 } |
1725 | 1680 |
1726 } // namespace content | 1681 } // namespace content |
OLD | NEW |