Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(21)

Side by Side Diff: gears/base/common/permissions_db.cc

Issue 800: Database2Versions table, get/set version operations implemented (Closed) SVN Base: http://google-gears.googlecode.com/svn/contrib/dimitri.glazkov/database2/
Patch Set: Created 3 months, 3 weeks ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2007, Google Inc. 1 // Copyright 2007, Google Inc.
2 // 2 //
3 // Redistribution and use in source and binary forms, with or without 3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met: 4 // modification, are permitted provided that the following conditions are met:
5 // 5 //
6 // 1. Redistributions of source code must retain the above copyright notice, 6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer. 7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice, 8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation 9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution. 10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be 11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without 12 // used to endorse or promote products derived from this software without
13 // specific prior written permission. 13 // specific prior written permission.
14 // 14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 25
26 #include "gears/base/common/permissions_db.h" 26 #include "gears/base/common/permissions_db.h"
27 #include "gears/base/common/sqlite_wrapper.h" 27 #include "gears/base/common/sqlite_wrapper.h"
28 #include "gears/base/common/thread_locals.h" 28 #include "gears/base/common/thread_locals.h"
29 #include "gears/localserver/common/localserver_db.h" 29 #include "gears/localserver/common/localserver_db.h"
30 30
31 static const char16 *kDatabaseName = STRING16(L"permissions.db"); 31 static const char16 *kDatabaseName = STRING16(L"permissions.db");
32 static const char16 *kVersionTableName = STRING16(L"VersionInfo"); 32 static const char16 *kVersionTableName = STRING16(L"VersionInfo");
33 static const char16 *kVersionKeyName = STRING16(L"Version"); 33 static const char16 *kVersionKeyName = STRING16(L"Version");
34 static const char16 *kAccessTableName = STRING16(L"Access"); 34 static const char16 *kAccessTableName = STRING16(L"Access");
35 static const int kCurrentVersion = 7; 35 static const int kCurrentVersion = 7;
36 static const int kOldestUpgradeableVersion = 1; 36 static const int kOldestUpgradeableVersion = 1;
37 37
38 38
39 const std::string PermissionsDB::kThreadLocalKey("base:permissions"); 39 const std::string PermissionsDB::kThreadLocalKey("base:permissions");
40 40
41 41
42 PermissionsDB *PermissionsDB::GetDB() { 42 PermissionsDB *PermissionsDB::GetDB() {
43 if (ThreadLocals::HasValue(kThreadLocalKey)) { 43 if (ThreadLocals::HasValue(kThreadLocalKey)) {
44 return reinterpret_cast<PermissionsDB*>( 44 return reinterpret_cast<PermissionsDB*>(
45 ThreadLocals::GetValue(kThreadLocalKey)); 45 ThreadLocals::GetValue(kThreadLocalKey));
46 } 46 }
47 47
48 PermissionsDB *db = new PermissionsDB(); 48 PermissionsDB *db = new PermissionsDB();
49 49
50 // If we can't initialize, we store NULL in the map so that we don't keep 50 // If we can't initialize, we store NULL in the map so that we don't keep
51 // trying to Init() over and over. 51 // trying to Init() over and over.
52 if (!db->Init()) { 52 if (!db->Init()) {
53 delete db; 53 delete db;
54 db = NULL; 54 db = NULL;
55 } 55 }
56 56
57 ThreadLocals::SetValue(kThreadLocalKey, db, &DestroyDB); 57 ThreadLocals::SetValue(kThreadLocalKey, db, &DestroyDB);
58 return db; 58 return db;
59 } 59 }
60 60
61 61
62 void PermissionsDB::DestroyDB(void *context) { 62 void PermissionsDB::DestroyDB(void *context) {
63 PermissionsDB *db = reinterpret_cast<PermissionsDB*>(context); 63 PermissionsDB *db = reinterpret_cast<PermissionsDB*>(context);
64 if (db) { 64 if (db) {
65 delete db; 65 delete db;
66 } 66 }
67 } 67 }
68 68
69 69
70 PermissionsDB::PermissionsDB() 70 PermissionsDB::PermissionsDB()
71 : version_table_(&db_, kVersionTableName), 71 : version_table_(&db_, kVersionTableName),
72 access_table_(&db_, kAccessTableName), 72 access_table_(&db_, kAccessTableName),
73 shortcut_table_(&db_), 73 shortcut_table_(&db_),
74 database_name_table_(&db_) { 74 database_name_table_(&db_),
75 database2_versions_table_(&db_) {
75 } 76 }
76 77
77 78
78 bool PermissionsDB::Init() { 79 bool PermissionsDB::Init() {
79 // Initialize the database and tables 80 // Initialize the database and tables
80 if (!db_.Open(kDatabaseName)) { 81 if (!db_.Open(kDatabaseName)) {
81 return false; 82 return false;
82 } 83 }
83 84
84 // Examine the contents of the database and determine if we have to 85 // Examine the contents of the database and determine if we have to
85 // instantiate or updgrade the schema. 86 // instantiate or updgrade the schema.
86 int version = 0; 87 int version = 0;
87 version_table_.GetInt(kVersionKeyName, &version); 88 version_table_.GetInt(kVersionKeyName, &version);
88 89
89 // if its the version we're expecting, great 90 // if its the version we're expecting, great
90 if (version == kCurrentVersion) { 91 if (version == kCurrentVersion) {
91 return true; 92 return true;
92 } 93 }
93 94
94 // Doing this in a transaction effectively locks the database file and 95 // Doing this in a transaction effectively locks the database file and
95 // ensures that this is synchronized across all threads and processes 96 // ensures that this is synchronized across all threads and processes
96 SQLTransaction transaction(&db_, "PermissionsDB::Init"); 97 SQLTransaction transaction(&db_, "PermissionsDB::Init");
97 if (!transaction.Begin()) { 98 if (!transaction.Begin()) {
98 return false; 99 return false;
99 } 100 }
100 101
101 // Fetch the version again in case someone else beat us to the 102 // Fetch the version again in case someone else beat us to the
102 // upgrade. 103 // upgrade.
103 version_table_.GetInt(kVersionKeyName, &version); 104 version_table_.GetInt(kVersionKeyName, &version);
104 if (version == kCurrentVersion) { 105 if (version == kCurrentVersion) {
105 return true; 106 return true;
106 } 107 }
107 108
108 if (0 == version) { 109 if (0 == version) {
109 // No database in place, create it. 110 // No database in place, create it.
110 // 111 //
111 // TODO(shess) Verify that this is true. Is it _no_ database, or 112 // TODO(shess) Verify that this is true. Is it _no_ database, or
112 // is there a database which didn't have a version? The latter 113 // is there a database which didn't have a version? The latter
113 // case would be masked by the CREATE IF NOT EXISTS statements 114 // case would be masked by the CREATE IF NOT EXISTS statements
114 // we're using. 115 // we're using.
115 if (!CreateDatabase()) { 116 if (!CreateDatabase()) {
116 return false; 117 return false;
117 } 118 }
118 } else { 119 } else {
119 if (!UpgradeToVersion7()) { 120 if (!UpgradeToVersion7()) {
120 return false; 121 return false;
121 } 122 }
122 } 123 }
123 124
124 // Double-check that we ended up with the right version. 125 // Double-check that we ended up with the right version.
(...skipping 136 matching lines...) Show 10 above Show 10 below
261 continue; 262 continue;
262 } 263 }
263 result->push_back(origin); 264 result->push_back(origin);
264 } 265 }
265 return true; 266 return true;
266 } 267 }
267 268
268 bool PermissionsDB::GetOriginShortcuts(const SecurityOrigin &origin, 269 bool PermissionsDB::GetOriginShortcuts(const SecurityOrigin &origin,
269 std::vector<std::string16> *names) { 270 std::vector<std::string16> *names) {
270 return shortcut_table_.GetOriginShortcuts(origin.url().c_str(), names); 271 return shortcut_table_.GetOriginShortcuts(origin.url().c_str(), names);
271 } 272 }
272 273
273 bool PermissionsDB::GetShortcut(const SecurityOrigin &origin, 274 bool PermissionsDB::GetShortcut(const SecurityOrigin &origin,
274 const char16 *name, std::string16 *app_url, 275 const char16 *name, std::string16 *app_url,
275 std::string16 *icon16x16_url, 276 std::string16 *icon16x16_url,
276 std::string16 *icon32x32_url, 277 std::string16 *icon32x32_url,
277 std::string16 *icon48x48_url, 278 std::string16 *icon48x48_url,
278 std::string16 *icon128x128_url, 279 std::string16 *icon128x128_url,
279 std::string16 *msg, 280 std::string16 *msg,
280 bool *allow) { 281 bool *allow) {
281 return shortcut_table_.GetShortcut(origin.url().c_str(), name, app_url, 282 return shortcut_table_.GetShortcut(origin.url().c_str(), name, app_url,
282 icon16x16_url, icon32x32_url, 283 icon16x16_url, icon32x32_url,
283 icon48x48_url, icon128x128_url, msg, 284 icon48x48_url, icon128x128_url, msg,
284 allow); 285 allow);
285 } 286 }
286 287
287 bool PermissionsDB::DeleteShortcut(const SecurityOrigin &origin, 288 bool PermissionsDB::DeleteShortcut(const SecurityOrigin &origin,
288 const char16 *name) { 289 const char16 *name) {
289 return shortcut_table_.DeleteShortcut(origin.url().c_str(), name); 290 return shortcut_table_.DeleteShortcut(origin.url().c_str(), name);
290 } 291 }
291 292
292 bool PermissionsDB::DeleteShortcuts(const SecurityOrigin &origin) { 293 bool PermissionsDB::DeleteShortcuts(const SecurityOrigin &origin) {
293 return shortcut_table_.DeleteShortcuts(origin.url().c_str()); 294 return shortcut_table_.DeleteShortcuts(origin.url().c_str());
294 } 295 }
295 296
296 // TODO(shess): Should these just be inline in the .h? 297 // TODO(shess): Should these just be inline in the .h?
297 bool PermissionsDB::GetDatabaseBasename(const SecurityOrigin &origin, 298 bool PermissionsDB::GetDatabaseBasename(const SecurityOrigin &origin,
298 const char16 *database_name, 299 const char16 *database_name,
299 std::string16 *basename) { 300 std::string16 *basename) {
300 return database_name_table_.GetDatabaseBasename(origin.url().c_str(), 301 return database_name_table_.GetDatabaseBasename(origin.url().c_str(),
301 database_name, 302 database_name,
302 basename); 303 basename);
303 } 304 }
304 305
305 bool PermissionsDB::MarkDatabaseCorrupt(const SecurityOrigin &origin, 306 bool PermissionsDB::MarkDatabaseCorrupt(const SecurityOrigin &origin,
306 const char16 *database_name, 307 const char16 *database_name,
307 const char16 *basename) { 308 const char16 *basename) {
308 return database_name_table_.MarkDatabaseCorrupt(origin.url().c_str(), 309 return database_name_table_.MarkDatabaseCorrupt(origin.url().c_str(),
309 database_name, 310 database_name,
310 basename); 311 basename);
312 }
313
314 bool PermissionsDB::GetDatabase2Version(const SecurityOrigin &origin,
Aaron 2008/05/19 15:33:50 I think we can eliminate this extra indirection by
Dimitri 2008/05/20 03:07:08 On 2008/05/19 15:33:50, Aaron wrote: > I think we
315 const std::string16 &name,
316 std::string16 *version,
317 bool *found) {
318 return database2_versions_table_.GetVersion(origin.url().c_str(), name,
319 version, found);
320 }
321
322 bool PermissionsDB::SetDatabase2Version(const SecurityOrigin &origin,
323 const std::string16 &name,
324 const std::string16 &version) {
325 return database2_versions_table_.SetVersion(origin.url().c_str(), name,
326 version);
311 } 327 }
312 328
313 bool PermissionsDB::CreateDatabase() { 329 bool PermissionsDB::CreateDatabase() {
314 ASSERT_SINGLE_THREAD(); 330 ASSERT_SINGLE_THREAD();
315 331
316 SQLTransaction transaction(&db_, "PermissionsDB::CreateDatabase"); 332 SQLTransaction transaction(&db_, "PermissionsDB::CreateDatabase");
317 if (!transaction.Begin()) { 333 if (!transaction.Begin()) {
318 return false; 334 return false;
319 } 335 }
320 336
321 if (!db_.DropAllObjects()) { 337 if (!db_.DropAllObjects()) {
322 return false; 338 return false;
323 } 339 }
324 340
325 if (!version_table_.MaybeCreateTable() || 341 if (!version_table_.MaybeCreateTable() ||
326 !access_table_.MaybeCreateTable() || 342 !access_table_.MaybeCreateTable() ||
327 !shortcut_table_.MaybeCreateTableLatestVersion() || 343 !shortcut_table_.MaybeCreateTableLatestVersion() ||
328 !database_name_table_.MaybeCreateTableLatestVersion()) { 344 !database_name_table_.MaybeCreateTableLatestVersion() ||
345 !database2_versions_table_.MaybeCreateTableLatestVersion()) {
329 return false; 346 return false;
330 } 347 }
331 348
332 // set the current version 349 // set the current version
333 if (!version_table_.SetInt(kVersionKeyName, kCurrentVersion)) { 350 if (!version_table_.SetInt(kVersionKeyName, kCurrentVersion)) {
334 return false; 351 return false;
335 } 352 }
336 353
337 return transaction.Commit(); 354 return transaction.Commit();
338 } 355 }
339 356
340 bool PermissionsDB::UpgradeToVersion2() { 357 bool PermissionsDB::UpgradeToVersion2() {
341 SQLTransaction transaction(&db_, "PermissionsDB::UpgradeToVersion2"); 358 SQLTransaction transaction(&db_, "PermissionsDB::UpgradeToVersion2");
342 if (!transaction.Begin()) { 359 if (!transaction.Begin()) {
343 return false; 360 return false;
344 } 361 }
345 362
346 int version = 0; 363 int version = 0;
347 version_table_.GetInt(kVersionKeyName, &version); 364 version_table_.GetInt(kVersionKeyName, &version);
348 365
349 if (version != 1) { 366 if (version != 1) {
350 LOG(("PermissionsDB::UpgradeToVersion2 unexpected version: %d", version)); 367 LOG(("PermissionsDB::UpgradeToVersion2 unexpected version: %d", version));
351 return false; 368 return false;
352 } 369 }
353 370
354 // There was a bug in v1 of this db where we inserted some corrupt UTF-8 371 // There was a bug in v1 of this db where we inserted some corrupt UTF-8
355 // characters into the db. This was pre-release, so it's not worth trying 372 // characters into the db. This was pre-release, so it's not worth trying
356 // to clean it up. Instead just remove old permissions. 373 // to clean it up. Instead just remove old permissions.
357 // 374 //
358 // TODO(shess) I'm inclined to say "DROP TABLE IF EXISTS 375 // TODO(shess) I'm inclined to say "DROP TABLE IF EXISTS
359 // ScourAccess". Or, since this was from a pre-release schema, 376 // ScourAccess". Or, since this was from a pre-release schema,
360 // "upgrade" version 1 by calling CreateDatabase(), which will drop 377 // "upgrade" version 1 by calling CreateDatabase(), which will drop
361 // all existing tables. 378 // all existing tables.
362 if (SQLITE_OK != db_.Execute("DELETE FROM ScourAccess")) { 379 if (SQLITE_OK != db_.Execute("DELETE FROM ScourAccess")) {
363 return false; 380 return false;
364 } 381 }
365 382
366 if (!version_table_.SetInt(kVersionKeyName, 2)) { 383 if (!version_table_.SetInt(kVersionKeyName, 2)) {
367 return false; 384 return false;
368 } 385 }
369 386
370 return transaction.Commit(); 387 return transaction.Commit();
371 } 388 }
372 389
373 bool PermissionsDB::UpgradeToVersion3() { 390 bool PermissionsDB::UpgradeToVersion3() {
374 SQLTransaction transaction(&db_, "PermissionsDB::UpgradeToVersion3"); 391 SQLTransaction transaction(&db_, "PermissionsDB::UpgradeToVersion3");
375 if (!transaction.Begin()) { 392 if (!transaction.Begin()) {
376 return false; 393 return false;
377 } 394 }
378 395
(...skipping 105 matching lines...) Show 10 above Show 10 below
484 501
485 if (version != 5) { 502 if (version != 5) {
486 LOG(("PermissionsDB::UpgradeToVersion6 unexpected version: %d", version)); 503 LOG(("PermissionsDB::UpgradeToVersion6 unexpected version: %d", version));
487 return false; 504 return false;
488 } 505 }
489 506
490 if (!shortcut_table_.UpgradeFromVersion5ToVersion6()) { 507 if (!shortcut_table_.UpgradeFromVersion5ToVersion6()) {
491 return false; 508 return false;
492 } 509 }
493 510
494 if (!version_table_.SetInt(kVersionKeyName, 6)) { 511 if (!version_table_.SetInt(kVersionKeyName, 6)) {
495 return false; 512 return false;
496 } 513 }
497 514
498 return transaction.Commit(); 515 return transaction.Commit();
499 } 516 }
500 517
501 bool PermissionsDB::UpgradeToVersion7() { 518 bool PermissionsDB::UpgradeToVersion7() {
502 SQLTransaction transaction(&db_, "PermissionsDB::UpgradeToVersion7"); 519 SQLTransaction transaction(&db_, "PermissionsDB::UpgradeToVersion7");
503 if (!transaction.Begin()) { 520 if (!transaction.Begin()) {
504 return false; 521 return false;
505 } 522 }
506 523
507 int version = 0; 524 int version = 0;
508 version_table_.GetInt(kVersionKeyName, &version); 525 version_table_.GetInt(kVersionKeyName, &version);
509 526
510 if (version < 6) { 527 if (version < 6) {
511 if (!UpgradeToVersion6()) { 528 if (!UpgradeToVersion6()) {
512 return false; 529 return false;
513 } 530 }
514 version_table_.GetInt(kVersionKeyName, &version); 531 version_table_.GetInt(kVersionKeyName, &version);
515 } 532 }
516 533
517 if (version != 6) { 534 if (version != 6) {
518 LOG(("PermissionsDB::UpgradeToVersion7 unexpected version: %d", version)); 535 LOG(("PermissionsDB::UpgradeToVersion7 unexpected version: %d", version));
519 return false; 536 return false;
520 } 537 }
521 538
522 // No changes to shortcut_table_. 539 // No changes to shortcut_table_.
523 540
524 if (!database_name_table_.UpgradeToVersion7()) { 541 if (!database_name_table_.UpgradeToVersion7()) {
525 return false; 542 return false;
526 } 543 }
527 544
528 if (!version_table_.SetInt(kVersionKeyName, 7)) { 545 if (!version_table_.SetInt(kVersionKeyName, 7)) {
529 return false; 546 return false;
530 } 547 }
531 548
532 return transaction.Commit(); 549 return transaction.Commit();
533 } 550 }
OLDNEW

Powered by Google App Engine
This is Rietveld r305