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

Side by Side Diff: webkit/database/database_tracker.cc

Issue 7056025: More WebSQLDatabase and QuotaManager integration. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "webkit/database/database_tracker.h" 5 #include "webkit/database/database_tracker.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "app/sql/connection.h" 10 #include "app/sql/connection.h"
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 default_quota_ = quota; 122 default_quota_ = quota;
123 ClearAllCachedOriginInfo(); 123 ClearAllCachedOriginInfo();
124 } 124 }
125 125
126 void DatabaseTracker::DatabaseOpened(const string16& origin_identifier, 126 void DatabaseTracker::DatabaseOpened(const string16& origin_identifier,
127 const string16& database_name, 127 const string16& database_name,
128 const string16& database_description, 128 const string16& database_description,
129 int64 estimated_size, 129 int64 estimated_size,
130 int64* database_size, 130 int64* database_size,
131 int64* space_available) { 131 int64* space_available) {
132 // TODO(michaeln): remove space_available from the interface
133 *space_available = 0;
134
132 if (!LazyInit()) { 135 if (!LazyInit()) {
133 *database_size = 0; 136 *database_size = 0;
134 *space_available = 0; 137 *space_available = 0;
135 return; 138 return;
136 } 139 }
137 140
138 InsertOrUpdateDatabaseDetails(origin_identifier, database_name, 141 if (quota_manager_proxy_)
139 database_description, estimated_size);
140 database_connections_.AddConnection(origin_identifier, database_name);
141
142 CachedOriginInfo* info = GetCachedOriginInfo(origin_identifier);
143 *database_size = (info ? info->GetDatabaseSize(database_name) : 0);
144 *space_available = GetOriginSpaceAvailable(origin_identifier);
145
146 if (quota_manager_proxy_) {
147 // So we can compute deltas as modifications are made.
148 database_connections_.SetOpenDatabaseSize(
149 origin_identifier, database_name, *database_size);
150 quota_manager_proxy_->NotifyStorageAccessed( 142 quota_manager_proxy_->NotifyStorageAccessed(
151 quota::QuotaClient::kDatabase, 143 quota::QuotaClient::kDatabase,
152 DatabaseUtil::GetOriginFromIdentifier(origin_identifier), 144 DatabaseUtil::GetOriginFromIdentifier(origin_identifier),
153 quota::kStorageTypeTemporary); 145 quota::kStorageTypeTemporary);
146
147 InsertOrUpdateDatabaseDetails(origin_identifier, database_name,
148 database_description, estimated_size);
149 if (database_connections_.AddConnection(origin_identifier, database_name)) {
150 *database_size = GetDBFileSize(origin_identifier, database_name);
michaeln 2011/05/24 04:18:18 I also need to update the size in the CachedOrigin
151 database_connections_.SetOpenDatabaseSize(origin_identifier, database_name,
152 *database_size);
153 return;
154 } 154 }
155 *database_size = UpdateCachedDatabaseFileSize(origin_identifier,
michaeln 2011/05/24 04:18:18 I plan to rename this to UpdateOpenDatabaseSizeAnd
156 database_name);
155 } 157 }
156 158
157 void DatabaseTracker::DatabaseModified(const string16& origin_identifier, 159 void DatabaseTracker::DatabaseModified(const string16& origin_identifier,
158 const string16& database_name) { 160 const string16& database_name) {
159 if (!LazyInit()) 161 if (!LazyInit())
160 return; 162 return;
161 163 UpdateCachedDatabaseFileSize(origin_identifier, database_name);
162 int64 new_size =
163 UpdateCachedDatabaseFileSize(origin_identifier, database_name);
164 int64 space_available = GetOriginSpaceAvailable(origin_identifier);
165 FOR_EACH_OBSERVER(Observer, observers_, OnDatabaseSizeChanged(
166 origin_identifier, database_name, new_size, space_available));
167
168 if (quota_manager_proxy_) {
169 int64 old_size = database_connections_.GetOpenDatabaseSize(
170 origin_identifier, database_name);
171 if (old_size != new_size) {
172 database_connections_.SetOpenDatabaseSize(
173 origin_identifier, database_name, new_size);
174 quota_manager_proxy_->NotifyStorageModified(
175 quota::QuotaClient::kDatabase,
176 DatabaseUtil::GetOriginFromIdentifier(origin_identifier),
177 quota::kStorageTypeTemporary,
178 new_size - old_size);
179 }
180 }
181 } 164 }
182 165
183 void DatabaseTracker::DatabaseClosed(const string16& origin_identifier, 166 void DatabaseTracker::DatabaseClosed(const string16& origin_identifier,
184 const string16& database_name) { 167 const string16& database_name) {
185 if (database_connections_.IsEmpty()) { 168 if (database_connections_.IsEmpty()) {
186 DCHECK(!is_initialized_); 169 DCHECK(!is_initialized_);
187 return; 170 return;
188 } 171 }
189 database_connections_.RemoveConnection(origin_identifier, database_name); 172
190 if (!database_connections_.IsDatabaseOpened(origin_identifier, database_name)) 173 if (quota_manager_proxy_)
174 quota_manager_proxy_->NotifyStorageAccessed(
175 quota::QuotaClient::kDatabase,
176 DatabaseUtil::GetOriginFromIdentifier(origin_identifier),
177 quota::kStorageTypeTemporary);
178
179 UpdateCachedDatabaseFileSize(origin_identifier, database_name);
180 if (database_connections_.RemoveConnection(origin_identifier, database_name))
191 DeleteDatabaseIfNeeded(origin_identifier, database_name); 181 DeleteDatabaseIfNeeded(origin_identifier, database_name);
192 } 182 }
193 183
194 void DatabaseTracker::CloseDatabases(const DatabaseConnections& connections) { 184 void DatabaseTracker::CloseDatabases(const DatabaseConnections& connections) {
195 if (database_connections_.IsEmpty()) { 185 if (database_connections_.IsEmpty()) {
196 DCHECK(!is_initialized_ || connections.IsEmpty()); 186 DCHECK(!is_initialized_ || connections.IsEmpty());
197 return; 187 return;
198 } 188 }
199 189
200 if (quota_manager_proxy_) { 190 // When being closed by this route, there's a chance that
201 // When being closed by this route, there's a chance that 191 // the tracker missed some DatabseModified calls. This method is used
202 // the tracker missed some DatabseModified calls. This method is used 192 // when a renderer crashes to cleanup it's open resources.
203 // when a renderer crashes to cleanup it's open resources. 193 // We need to examine what we have in connections for the
204 // We need to examine what we have in connections for the 194 // size of each open databases and notify any differences between the
205 // size of each open databases and notify any differences between the 195 // actual file sizes now.
206 // actual file sizes now. 196 std::vector<std::pair<string16, string16> > open_dbs;
207 std::vector<std::pair<string16, string16> > open_dbs; 197 connections.ListConnections(&open_dbs);
208 connections.ListConnections(&open_dbs); 198 for (std::vector<std::pair<string16, string16> >::iterator it =
209 for (std::vector<std::pair<string16, string16> >::iterator it = 199 open_dbs.begin(); it != open_dbs.end(); ++it)
210 open_dbs.begin(); it != open_dbs.end(); ++it) { 200 UpdateCachedDatabaseFileSize(it->first, it->second);
211 int64 old_size = database_connections_.GetOpenDatabaseSize(
212 it->first, it->second);
213 int64 new_size = GetDBFileSize(it->first, it->second);
214 if (new_size != old_size) {
215 database_connections_.SetOpenDatabaseSize(
216 it->first, it->second, new_size);
217 quota_manager_proxy_->NotifyStorageModified(
218 quota::QuotaClient::kDatabase,
219 DatabaseUtil::GetOriginFromIdentifier(it->first),
220 quota::kStorageTypeTemporary,
221 new_size - old_size);
222 }
223 }
224 }
225 201
226 std::vector<std::pair<string16, string16> > closed_dbs; 202 std::vector<std::pair<string16, string16> > closed_dbs;
227 database_connections_.RemoveConnections(connections, &closed_dbs); 203 database_connections_.RemoveConnections(connections, &closed_dbs);
228 for (std::vector<std::pair<string16, string16> >::iterator it = 204 for (std::vector<std::pair<string16, string16> >::iterator it =
229 closed_dbs.begin(); it != closed_dbs.end(); ++it) { 205 closed_dbs.begin(); it != closed_dbs.end(); ++it) {
230 DeleteDatabaseIfNeeded(it->first, it->second); 206 DeleteDatabaseIfNeeded(it->first, it->second);
231 } 207 }
232 } 208 }
233 209
234 void DatabaseTracker::DeleteDatabaseIfNeeded(const string16& origin_identifier, 210 void DatabaseTracker::DeleteDatabaseIfNeeded(const string16& origin_identifier,
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 std::vector<DatabaseDetails> details; 535 std::vector<DatabaseDetails> details;
560 if (!databases_table_->GetAllDatabaseDetailsForOrigin( 536 if (!databases_table_->GetAllDatabaseDetailsForOrigin(
561 origin_identifier, &details)) { 537 origin_identifier, &details)) {
562 return NULL; 538 return NULL;
563 } 539 }
564 540
565 CachedOriginInfo& origin_info = origins_info_map_[origin_identifier]; 541 CachedOriginInfo& origin_info = origins_info_map_[origin_identifier];
566 origin_info.SetOrigin(origin_identifier); 542 origin_info.SetOrigin(origin_identifier);
567 for (std::vector<DatabaseDetails>::const_iterator it = details.begin(); 543 for (std::vector<DatabaseDetails>::const_iterator it = details.begin();
568 it != details.end(); it++) { 544 it != details.end(); it++) {
569 int64 db_file_size = 545 int64 db_file_size;
570 GetDBFileSize(origin_identifier, it->database_name); 546 if (database_connections_.IsDatabaseOpened(
547 origin_identifier, it->database_name)) {
548 db_file_size = database_connections_.GetOpenDatabaseSize(
549 origin_identifier, it->database_name);
550 } else {
551 db_file_size = GetDBFileSize(origin_identifier, it->database_name);
552 }
571 origin_info.SetDatabaseSize(it->database_name, db_file_size); 553 origin_info.SetDatabaseSize(it->database_name, db_file_size);
572 origin_info.SetDatabaseDescription(it->database_name, it->description); 554 origin_info.SetDatabaseDescription(it->database_name, it->description);
573 } 555 }
574 556
575 if (special_storage_policy_.get() && 557 if (special_storage_policy_.get() &&
576 special_storage_policy_->IsStorageUnlimited( 558 special_storage_policy_->IsStorageUnlimited(
577 DatabaseUtil::GetOriginFromIdentifier(origin_identifier))) { 559 DatabaseUtil::GetOriginFromIdentifier(origin_identifier))) {
578 // TODO(michaeln): handle the case where it changes status sometime after 560 // TODO(michaeln): handle the case where it changes status sometime after
579 // the cached origin_info has been established 561 // the cached origin_info has been established
580 origin_info.SetQuota(kint64max); 562 origin_info.SetQuota(kint64max);
(...skipping 13 matching lines...) Expand all
594 const string16& database_name) { 576 const string16& database_name) {
595 FilePath db_file_name = GetFullDBFilePath(origin_identifier, database_name); 577 FilePath db_file_name = GetFullDBFilePath(origin_identifier, database_name);
596 int64 db_file_size = 0; 578 int64 db_file_size = 0;
597 if (!file_util::GetFileSize(db_file_name, &db_file_size)) 579 if (!file_util::GetFileSize(db_file_name, &db_file_size))
598 db_file_size = 0; 580 db_file_size = 0;
599 return db_file_size; 581 return db_file_size;
600 } 582 }
601 583
602 int64 DatabaseTracker::GetOriginSpaceAvailable( 584 int64 DatabaseTracker::GetOriginSpaceAvailable(
603 const string16& origin_identifier) { 585 const string16& origin_identifier) {
604 // TODO(michaeln): Come up with a value according to the the QuotaMgr. 586 // TODO(michaeln): remove method from the interface
605 CachedOriginInfo* origin_info = GetCachedOriginInfo(origin_identifier); 587 return 0;
606 if (!origin_info)
607 return 0;
608 int64 space_available = origin_info->Quota() - origin_info->TotalSize();
609 return (space_available < 0 ? 0 : space_available);
610 } 588 }
611 589
590 // TODO(michaeln): rename to UpdateOpenDatabaseFileSize
612 int64 DatabaseTracker::UpdateCachedDatabaseFileSize( 591 int64 DatabaseTracker::UpdateCachedDatabaseFileSize(
613 const string16& origin_identifier, 592 const string16& origin_id, const string16& name) {
614 const string16& database_name) { 593 DCHECK(database_connections_.IsDatabaseOpened(origin_id, name));
615 int64 new_size = GetDBFileSize(origin_identifier, database_name); 594 int64 new_size = GetDBFileSize(origin_id, name);
616 CachedOriginInfo* origin_info = GetCachedOriginInfo(origin_identifier); 595 int64 old_size = database_connections_.GetOpenDatabaseSize(origin_id, name);
617 if (origin_info) 596 if (old_size != new_size) {
618 origin_info->SetDatabaseSize(database_name, new_size); 597 database_connections_.SetOpenDatabaseSize(origin_id, name, new_size);
598 if (origins_info_map_.find(origin_id) != origins_info_map_.end())
599 origins_info_map_[origin_id].SetDatabaseSize(name, new_size);
600 if (quota_manager_proxy_)
601 quota_manager_proxy_->NotifyStorageModified(
602 quota::QuotaClient::kDatabase,
603 DatabaseUtil::GetOriginFromIdentifier(origin_id),
604 quota::kStorageTypeTemporary,
605 new_size - old_size);
606 const int64 kNotUsed = 0;
607 FOR_EACH_OBSERVER(Observer, observers_, OnDatabaseSizeChanged(
608 origin_id, name, new_size, kNotUsed));
609 }
619 return new_size; 610 return new_size;
620 } 611 }
621 612
622 void DatabaseTracker::ScheduleDatabaseForDeletion( 613 void DatabaseTracker::ScheduleDatabaseForDeletion(
623 const string16& origin_identifier, 614 const string16& origin_identifier,
624 const string16& database_name) { 615 const string16& database_name) {
625 DCHECK(database_connections_.IsDatabaseOpened(origin_identifier, 616 DCHECK(database_connections_.IsDatabaseOpened(origin_identifier,
626 database_name)); 617 database_name));
627 dbs_to_be_deleted_[origin_identifier].insert(database_name); 618 dbs_to_be_deleted_[origin_identifier].insert(database_name);
628 FOR_EACH_OBSERVER(Observer, observers_, OnDatabaseScheduledForDeletion( 619 FOR_EACH_OBSERVER(Observer, observers_, OnDatabaseScheduledForDeletion(
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 std::string basename = file_path.BaseName().MaybeAsASCII(); 824 std::string basename = file_path.BaseName().MaybeAsASCII();
834 if (!basename.empty() && 825 if (!basename.empty() &&
835 !StartsWithASCII(basename, kExtensionOriginIdentifierPrefix, true)) { 826 !StartsWithASCII(basename, kExtensionOriginIdentifierPrefix, true)) {
836 file_util::Delete(file_path, true); 827 file_util::Delete(file_path, true);
837 } 828 }
838 } 829 }
839 } 830 }
840 } 831 }
841 832
842 } // namespace webkit_database 833 } // namespace webkit_database
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698