What's new? | Help | Directory | Sign in
Google
             
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2008, Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "gears/geolocation/geolocation_db.h"

static const char16 *kDatabaseName = STRING16(L"geolocation.db");
static const char16 *kVersionTableName = STRING16(L"VersionInfo");
static const char16 *kAccessTokenTableName = STRING16(L"AccessTokens");
static const char16 *kVersionKey = STRING16(L"Version");
static const int kCurrentVersion = 2;

const ThreadLocals::Slot GeolocationDB::kThreadLocalKey =
ThreadLocals::Alloc();


GeolocationDB::GeolocationDB()
: version_table_(&db_, kVersionTableName),
position_table_(&db_),
access_token_table_(&db_, kAccessTokenTableName) {
}

//static
GeolocationDB *GeolocationDB::GetDB() {
if (ThreadLocals::HasValue(kThreadLocalKey)) {
return reinterpret_cast<GeolocationDB*>(
ThreadLocals::GetValue(kThreadLocalKey));
}

GeolocationDB *db = new GeolocationDB();

// If we can't initialize, we store NULL in the map so that we don't keep
// trying to Init() over and over.
if (!db->Init()) {
delete db;
db = NULL;
}

ThreadLocals::SetValue(kThreadLocalKey, db, &DestroyDB);
return db;
}

bool GeolocationDB::StorePosition(const std::string16 &name,
const Position &position) {
return position_table_.SetPosition(name, position);
}

bool GeolocationDB::RetrievePosition(const std::string16 &name,
Position *position) {
return position_table_.GetPosition(name, position);
}

bool GeolocationDB::StoreAccessToken(const std::string16 &server_url,
const std::string16 &access_token) {
return access_token_table_.SetString(server_url.c_str(),
access_token.c_str());
}

bool GeolocationDB::RetrieveAccessToken(const std::string16 &server_url,
std::string16 *access_token) {
return access_token_table_.GetString(server_url.c_str(), access_token);
}

bool GeolocationDB::Create() {
ASSERT_SINGLE_THREAD();

SQLTransaction transaction(&db_, "GeolocationDB::Create");
if (!transaction.Begin()) {
return false;
}

if (!db_.DropAllObjects()) {
return false;
}

if (!version_table_.MaybeCreateTable() ||
!position_table_.CreateTableLatestVersion() ||
!access_token_table_.MaybeCreateTable()) {
return false;
}

// set the current version
if (!version_table_.SetInt(kVersionKey, kCurrentVersion)) {
return false;
}

return transaction.Commit();
}

bool GeolocationDB::UpgradeVersion1ToVersion2() {
// Version 2 adds the access token table.
return access_token_table_.MaybeCreateTable() &&
version_table_.SetInt(kVersionKey, 2);
}

bool GeolocationDB::Init() {
// Initialize the database and tables
if (!db_.Open(kDatabaseName)) {
return false;
}

// Examine the contents of the database and determine if we have to
// instantiate or updgrade the schema.
int version = 0;
version_table_.GetInt(kVersionKey, &version);

// If it's the version we're expecting, great.
if (version == kCurrentVersion) {
return true;
}

// Doing this in a transaction effectively locks the database file and
// ensures that this is synchronized across all threads and processes.
SQLTransaction transaction(&db_, "GeolocationDB::Init");
if (!transaction.Begin()) {
return false;
}

// Fetch the version again in case someone else beat us to the
// upgrade.
version_table_.GetInt(kVersionKey, &version);
if (version == kCurrentVersion) {
return true;
}

if (0 == version) {
// No database in place, create it.
if (!Create()) {
return false;
}
} else if (1 == version) {
if (!UpgradeVersion1ToVersion2()) {
return false;
}
} else {
// This should never happen.
assert(false);
return false;
}

// Double-check that we ended up with the right version.
version_table_.GetInt(kVersionKey, &version);
if (version != kCurrentVersion) {
return false;
}

return transaction.Commit();
}

// static
void GeolocationDB::DestroyDB(void *context) {
GeolocationDB *db = reinterpret_cast<GeolocationDB*>(context);
if (db) {
delete db;
}
}
Show details Hide details

Change log

r2866 by gears.daemon on Sep 24, 2008   Diff
[Author: steveblock]

Adds access_token to Geolocation JSON
protocol.
Also updates names of accuracy fields.

R=andreip
CC=gears-eng@googlegroups.com
APPROVED=malcolmr
DELTA=237  (191 added, 6 deleted, 40
changed)
OCL=8176910
...
Go to: 
Project members, sign in to write a code review

Older revisions

r2259 by gears.daemon on Jul 07, 2008   Diff
[Author: steveblock]

Fixes a Geolocation TODO: Adds caching
of last position in the DB.

...
All revisions of this file

File info

Size: 5663 bytes, 177 lines