OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/webdata/keyword_table.h" | 5 #include "chrome/browser/webdata/keyword_table.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/json/json_reader.h" | |
10 #include "base/json/json_writer.h" | |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
11 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
12 #include "base/metrics/stats_counters.h" | 14 #include "base/metrics/stats_counters.h" |
13 #include "base/string_number_conversions.h" | 15 #include "base/string_number_conversions.h" |
14 #include "base/string_split.h" | 16 #include "base/string_split.h" |
15 #include "base/string_util.h" | 17 #include "base/string_util.h" |
16 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
17 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
20 #include "base/values.h" | |
18 #include "chrome/browser/history/history_database.h" | 21 #include "chrome/browser/history/history_database.h" |
19 #include "chrome/browser/protector/histograms.h" | 22 #include "chrome/browser/protector/histograms.h" |
20 #include "chrome/browser/protector/protector_utils.h" | 23 #include "chrome/browser/protector/protector_utils.h" |
21 #include "chrome/browser/search_engines/search_terms_data.h" | 24 #include "chrome/browser/search_engines/search_terms_data.h" |
22 #include "chrome/browser/search_engines/template_url.h" | 25 #include "chrome/browser/search_engines/template_url.h" |
23 #include "chrome/browser/search_engines/template_url_service.h" | 26 #include "chrome/browser/search_engines/template_url_service.h" |
24 #include "chrome/browser/webdata/web_database.h" | 27 #include "chrome/browser/webdata/web_database.h" |
25 #include "googleurl/src/gurl.h" | 28 #include "googleurl/src/gurl.h" |
26 #include "sql/statement.h" | 29 #include "sql/statement.h" |
27 #include "sql/transaction.h" | 30 #include "sql/transaction.h" |
28 | 31 |
29 using base::Time; | 32 using base::Time; |
30 | 33 |
31 // static | 34 // static |
32 const char KeywordTable::kDefaultSearchProviderKey[] = | 35 const char KeywordTable::kDefaultSearchProviderKey[] = |
33 "Default Search Provider ID"; | 36 "Default Search Provider ID"; |
34 const char KeywordTable::kDefaultSearchIDBackupKey[] = | 37 const char KeywordTable::kDefaultSearchIDBackupKey[] = |
35 "Default Search Provider ID Backup"; | 38 "Default Search Provider ID Backup"; |
36 const char KeywordTable::kBackupSignatureKey[] = | 39 const char KeywordTable::kBackupSignatureKey[] = |
37 "Default Search Provider ID Backup Signature"; | 40 "Default Search Provider ID Backup Signature"; |
38 const char KeywordTable::kKeywordColumns[] = "id, short_name, keyword, " | 41 const char KeywordTable::kKeywordColumns[] = "id, short_name, keyword, " |
39 "favicon_url, url, safe_for_autoreplace, originating_url, date_created, " | 42 "favicon_url, url, safe_for_autoreplace, originating_url, date_created, " |
40 "usage_count, input_encodings, show_in_default_list, suggest_url, " | 43 "usage_count, input_encodings, show_in_default_list, suggest_url, " |
41 "prepopulate_id, created_by_policy, instant_url, last_modified, sync_guid"; | 44 "prepopulate_id, created_by_policy, instant_url, last_modified, sync_guid, " |
45 "alternate_urls"; | |
42 | 46 |
43 namespace { | 47 namespace { |
44 | 48 |
45 // Keys used in the meta table. | 49 // Keys used in the meta table. |
46 const char kBuiltinKeywordVersion[] = "Builtin Keyword Version"; | 50 const char kBuiltinKeywordVersion[] = "Builtin Keyword Version"; |
47 | 51 |
48 // The set of columns up through version 44. (There were different columns | 52 // The set of columns up through version 44. (There were different columns |
49 // below version 29 but none of the code below needs to worry about that case.) | 53 // below version 29 but none of the code below needs to worry about that case.) |
50 const char kKeywordColumnsVersion44Concatenated[] = "id || short_name || " | 54 const char kKeywordColumnsVersion44Concatenated[] = "id || short_name || " |
51 "keyword || favicon_url || url || safe_for_autoreplace || " | 55 "keyword || favicon_url || url || safe_for_autoreplace || " |
52 "originating_url || date_created || usage_count || input_encodings || " | 56 "originating_url || date_created || usage_count || input_encodings || " |
53 "show_in_default_list || suggest_url || prepopulate_id || " | 57 "show_in_default_list || suggest_url || prepopulate_id || " |
54 "autogenerate_keyword || logo_id || created_by_policy || instant_url || " | 58 "autogenerate_keyword || logo_id || created_by_policy || instant_url || " |
55 "last_modified || sync_guid"; | 59 "last_modified || sync_guid"; |
56 const char kKeywordColumnsVersion44[] = "id, short_name, keyword, favicon_url, " | 60 const char kKeywordColumnsVersion44[] = "id, short_name, keyword, favicon_url, " |
57 "url, safe_for_autoreplace, originating_url, date_created, usage_count, " | 61 "url, safe_for_autoreplace, originating_url, date_created, usage_count, " |
58 "input_encodings, show_in_default_list, suggest_url, prepopulate_id, " | 62 "input_encodings, show_in_default_list, suggest_url, prepopulate_id, " |
59 "autogenerate_keyword, logo_id, created_by_policy, instant_url, " | 63 "autogenerate_keyword, logo_id, created_by_policy, instant_url, " |
60 "last_modified, sync_guid"; | 64 "last_modified, sync_guid"; |
61 // NOTE: Remember to change what |kKeywordColumnsVersion45| says if the column | 65 // The set of columns from version 45 through version 46 (inclusively). |
62 // set in |kKeywordColumns| changes, and update any code that needs to switch | 66 const char kKeywordColumnsVersion46Concatenated[] = "id || short_name || " |
63 // column sets based on a version number! | 67 "keyword || favicon_url || url || safe_for_autoreplace || " |
64 const char* const kKeywordColumnsVersion45 = KeywordTable::kKeywordColumns; | 68 "originating_url || date_created || usage_count || input_encodings || " |
69 "show_in_default_list || suggest_url || prepopulate_id || " | |
70 "created_by_policy || instant_url || last_modified || sync_guid"; | |
71 const char kKeywordColumnsVersion46[] = "id, short_name, keyword, " | |
72 "favicon_url, url, safe_for_autoreplace, originating_url, date_created, " | |
73 "usage_count, input_encodings, show_in_default_list, suggest_url, " | |
74 "prepopulate_id, created_by_policy, instant_url, last_modified, sync_guid"; | |
65 | 75 |
66 // The current columns. | 76 // The current columns. |
67 const char kKeywordColumnsConcatenated[] = "id || short_name || keyword || " | 77 const char kKeywordColumnsConcatenated[] = "id || short_name || " |
68 "favicon_url || url || safe_for_autoreplace || originating_url || " | 78 "keyword || favicon_url || url || safe_for_autoreplace || " |
69 "date_created || usage_count || input_encodings || show_in_default_list || " | 79 "originating_url || date_created || usage_count || input_encodings || " |
70 "suggest_url || prepopulate_id || created_by_policy || instant_url || " | 80 "show_in_default_list || suggest_url || prepopulate_id || " |
71 "last_modified || sync_guid"; | 81 "created_by_policy || instant_url || last_modified || sync_guid || " |
82 "alternate_urls"; | |
72 | 83 |
73 // Inserts the data from |data| into |s|. |s| is assumed to have slots for all | 84 // Inserts the data from |data| into |s|. |s| is assumed to have slots for all |
74 // the columns in the keyword table. |id_column| is the slot number to bind | 85 // the columns in the keyword table. |id_column| is the slot number to bind |
75 // |data|'s |id| to; |starting_column| is the slot number of the first of a | 86 // |data|'s |id| to; |starting_column| is the slot number of the first of a |
76 // contiguous set of slots to bind all the other fields to. | 87 // contiguous set of slots to bind all the other fields to. |
77 void BindURLToStatement(const TemplateURLData& data, | 88 void BindURLToStatement(const TemplateURLData& data, |
78 sql::Statement* s, | 89 sql::Statement* s, |
79 int id_column, | 90 int id_column, |
80 int starting_column) { | 91 int starting_column) { |
92 // Serialize |alternate_urls| to JSON. | |
dhollowa
2012/09/28 18:06:29
I am fine with serializing this list to JSON for n
beaudoin
2012/10/02 17:43:29
crbug.com/153520
Done.
| |
93 // TODO(beaudoin): Check what it would take to use a new table to store | |
94 // alternate_urls while keeping backups and table signature in a good state. | |
95 ListValue alternate_urls_value; | |
96 for (size_t i = 0; i < data.alternate_urls.size(); ++i) | |
97 alternate_urls_value.AppendString(data.alternate_urls[i]); | |
98 std::string alternate_urls; | |
99 base::JSONWriter::Write(&alternate_urls_value, &alternate_urls); | |
100 | |
81 s->BindInt64(id_column, data.id); | 101 s->BindInt64(id_column, data.id); |
82 s->BindString16(starting_column, data.short_name); | 102 s->BindString16(starting_column, data.short_name); |
83 s->BindString16(starting_column + 1, data.keyword()); | 103 s->BindString16(starting_column + 1, data.keyword()); |
84 s->BindString(starting_column + 2, data.favicon_url.is_valid() ? | 104 s->BindString(starting_column + 2, data.favicon_url.is_valid() ? |
85 history::HistoryDatabase::GURLToDatabaseURL(data.favicon_url) : | 105 history::HistoryDatabase::GURLToDatabaseURL(data.favicon_url) : |
86 std::string()); | 106 std::string()); |
87 s->BindString(starting_column + 3, data.url()); | 107 s->BindString(starting_column + 3, data.url()); |
88 s->BindBool(starting_column + 4, data.safe_for_autoreplace); | 108 s->BindBool(starting_column + 4, data.safe_for_autoreplace); |
89 s->BindString(starting_column + 5, data.originating_url.is_valid() ? | 109 s->BindString(starting_column + 5, data.originating_url.is_valid() ? |
90 history::HistoryDatabase::GURLToDatabaseURL(data.originating_url) : | 110 history::HistoryDatabase::GURLToDatabaseURL(data.originating_url) : |
91 std::string()); | 111 std::string()); |
92 s->BindInt64(starting_column + 6, data.date_created.ToTimeT()); | 112 s->BindInt64(starting_column + 6, data.date_created.ToTimeT()); |
93 s->BindInt(starting_column + 7, data.usage_count); | 113 s->BindInt(starting_column + 7, data.usage_count); |
94 s->BindString(starting_column + 8, JoinString(data.input_encodings, ';')); | 114 s->BindString(starting_column + 8, JoinString(data.input_encodings, ';')); |
95 s->BindBool(starting_column + 9, data.show_in_default_list); | 115 s->BindBool(starting_column + 9, data.show_in_default_list); |
96 s->BindString(starting_column + 10, data.suggestions_url); | 116 s->BindString(starting_column + 10, data.suggestions_url); |
97 s->BindInt(starting_column + 11, data.prepopulate_id); | 117 s->BindInt(starting_column + 11, data.prepopulate_id); |
98 s->BindBool(starting_column + 12, data.created_by_policy); | 118 s->BindBool(starting_column + 12, data.created_by_policy); |
99 s->BindString(starting_column + 13, data.instant_url); | 119 s->BindString(starting_column + 13, data.instant_url); |
100 s->BindInt64(starting_column + 14, data.last_modified.ToTimeT()); | 120 s->BindInt64(starting_column + 14, data.last_modified.ToTimeT()); |
101 s->BindString(starting_column + 15, data.sync_guid); | 121 s->BindString(starting_column + 15, data.sync_guid); |
122 s->BindString(starting_column + 16, alternate_urls); | |
102 } | 123 } |
103 | 124 |
104 // Signs search provider id and returns its signature. | 125 // Signs search provider id and returns its signature. |
105 std::string GetSearchProviderIDSignature(int64 id) { | 126 std::string GetSearchProviderIDSignature(int64 id) { |
106 return protector::SignSetting(base::Int64ToString(id)); | 127 return protector::SignSetting(base::Int64ToString(id)); |
107 } | 128 } |
108 | 129 |
109 // Checks if signature for search provider id is correct and returns the | 130 // Checks if signature for search provider id is correct and returns the |
110 // result. | 131 // result. |
111 bool IsSearchProviderIDValid(int64 id, const std::string& signature) { | 132 bool IsSearchProviderIDValid(int64 id, const std::string& signature) { |
(...skipping 21 matching lines...) Expand all Loading... | |
133 "originating_url VARCHAR," | 154 "originating_url VARCHAR," |
134 "date_created INTEGER DEFAULT 0," | 155 "date_created INTEGER DEFAULT 0," |
135 "usage_count INTEGER DEFAULT 0," | 156 "usage_count INTEGER DEFAULT 0," |
136 "input_encodings VARCHAR," | 157 "input_encodings VARCHAR," |
137 "show_in_default_list INTEGER," | 158 "show_in_default_list INTEGER," |
138 "suggest_url VARCHAR," | 159 "suggest_url VARCHAR," |
139 "prepopulate_id INTEGER DEFAULT 0," | 160 "prepopulate_id INTEGER DEFAULT 0," |
140 "created_by_policy INTEGER DEFAULT 0," | 161 "created_by_policy INTEGER DEFAULT 0," |
141 "instant_url VARCHAR," | 162 "instant_url VARCHAR," |
142 "last_modified INTEGER DEFAULT 0," | 163 "last_modified INTEGER DEFAULT 0," |
143 "sync_guid VARCHAR)") && | 164 "sync_guid VARCHAR," |
165 "alternate_urls VARCHAR)") && | |
144 UpdateBackupSignature(WebDatabase::kCurrentVersionNumber)); | 166 UpdateBackupSignature(WebDatabase::kCurrentVersionNumber)); |
145 } | 167 } |
146 | 168 |
147 bool KeywordTable::IsSyncable() { | 169 bool KeywordTable::IsSyncable() { |
148 return true; | 170 return true; |
149 } | 171 } |
150 | 172 |
151 bool KeywordTable::AddKeyword(const TemplateURLData& data) { | 173 bool KeywordTable::AddKeyword(const TemplateURLData& data) { |
152 DCHECK(data.id); | 174 DCHECK(data.id); |
153 std::string query("INSERT INTO keywords (" + std::string(kKeywordColumns) + | 175 std::string query("INSERT INTO keywords (" + std::string(kKeywordColumns) + |
154 ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); | 176 ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); |
155 sql::Statement s(db_->GetUniqueStatement(query.c_str())); | 177 sql::Statement s(db_->GetUniqueStatement(query.c_str())); |
156 BindURLToStatement(data, &s, 0, 1); | 178 BindURLToStatement(data, &s, 0, 1); |
157 | 179 |
158 return s.Run() && UpdateBackupSignature(WebDatabase::kCurrentVersionNumber); | 180 return s.Run() && UpdateBackupSignature(WebDatabase::kCurrentVersionNumber); |
159 } | 181 } |
160 | 182 |
161 bool KeywordTable::RemoveKeyword(TemplateURLID id) { | 183 bool KeywordTable::RemoveKeyword(TemplateURLID id) { |
162 DCHECK(id); | 184 DCHECK(id); |
163 sql::Statement s( | 185 sql::Statement s( |
164 db_->GetUniqueStatement("DELETE FROM keywords WHERE id = ?")); | 186 db_->GetUniqueStatement("DELETE FROM keywords WHERE id = ?")); |
(...skipping 21 matching lines...) Expand all Loading... | |
186 succeeded &= RemoveKeyword(*i); | 208 succeeded &= RemoveKeyword(*i); |
187 return succeeded; | 209 return succeeded; |
188 } | 210 } |
189 | 211 |
190 bool KeywordTable::UpdateKeyword(const TemplateURLData& data) { | 212 bool KeywordTable::UpdateKeyword(const TemplateURLData& data) { |
191 DCHECK(data.id); | 213 DCHECK(data.id); |
192 sql::Statement s(db_->GetUniqueStatement("UPDATE keywords SET short_name=?, " | 214 sql::Statement s(db_->GetUniqueStatement("UPDATE keywords SET short_name=?, " |
193 "keyword=?, favicon_url=?, url=?, safe_for_autoreplace=?, " | 215 "keyword=?, favicon_url=?, url=?, safe_for_autoreplace=?, " |
194 "originating_url=?, date_created=?, usage_count=?, input_encodings=?, " | 216 "originating_url=?, date_created=?, usage_count=?, input_encodings=?, " |
195 "show_in_default_list=?, suggest_url=?, prepopulate_id=?, " | 217 "show_in_default_list=?, suggest_url=?, prepopulate_id=?, " |
196 "created_by_policy=?, instant_url=?, last_modified=?, sync_guid=? WHERE " | 218 "created_by_policy=?, instant_url=?, last_modified=?, sync_guid=?, " |
197 "id=?")); | 219 "alternate_urls=? WHERE id=?")); |
198 BindURLToStatement(data, &s, 16, 0); // "16" binds id() as the last item. | 220 BindURLToStatement(data, &s, 17, 0); // "17" binds id() as the last item. |
199 | 221 |
200 return s.Run() && UpdateBackupSignature(WebDatabase::kCurrentVersionNumber); | 222 return s.Run() && UpdateBackupSignature(WebDatabase::kCurrentVersionNumber); |
201 } | 223 } |
202 | 224 |
203 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { | 225 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { |
204 // Added for http://crbug.com/116952. | 226 // Added for http://crbug.com/116952. |
205 UMA_HISTOGRAM_COUNTS_100("Search.DefaultSearchProviderID", | 227 UMA_HISTOGRAM_COUNTS_100("Search.DefaultSearchProviderID", |
206 static_cast<int32>(id)); | 228 static_cast<int32>(id)); |
207 return meta_table_->SetValue(kDefaultSearchProviderKey, id) && | 229 return meta_table_->SetValue(kDefaultSearchProviderKey, id) && |
208 UpdateBackupSignature(WebDatabase::kCurrentVersionNumber); | 230 UpdateBackupSignature(WebDatabase::kCurrentVersionNumber); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
313 bool KeywordTable::MigrateToVersion26AddCreatedByPolicyColumn() { | 335 bool KeywordTable::MigrateToVersion26AddCreatedByPolicyColumn() { |
314 return db_->Execute("ALTER TABLE keywords ADD COLUMN created_by_policy " | 336 return db_->Execute("ALTER TABLE keywords ADD COLUMN created_by_policy " |
315 "INTEGER DEFAULT 0"); | 337 "INTEGER DEFAULT 0"); |
316 } | 338 } |
317 | 339 |
318 bool KeywordTable::MigrateToVersion28SupportsInstantColumn() { | 340 bool KeywordTable::MigrateToVersion28SupportsInstantColumn() { |
319 return db_->Execute("ALTER TABLE keywords ADD COLUMN supports_instant " | 341 return db_->Execute("ALTER TABLE keywords ADD COLUMN supports_instant " |
320 "INTEGER DEFAULT 0"); | 342 "INTEGER DEFAULT 0"); |
321 } | 343 } |
322 | 344 |
323 bool KeywordTable::MigrateToVersion29InstantUrlToSupportsInstant() { | 345 bool KeywordTable::MigrateToVersion29InstantURLToSupportsInstant() { |
324 sql::Transaction transaction(db_); | 346 sql::Transaction transaction(db_); |
325 return transaction.Begin() && | 347 return transaction.Begin() && |
326 db_->Execute("ALTER TABLE keywords ADD COLUMN instant_url VARCHAR") && | 348 db_->Execute("ALTER TABLE keywords ADD COLUMN instant_url VARCHAR") && |
327 db_->Execute("CREATE TABLE keywords_temp (" | 349 db_->Execute("CREATE TABLE keywords_temp (" |
328 "id INTEGER PRIMARY KEY," | 350 "id INTEGER PRIMARY KEY," |
329 "short_name VARCHAR NOT NULL," | 351 "short_name VARCHAR NOT NULL," |
330 "keyword VARCHAR NOT NULL," | 352 "keyword VARCHAR NOT NULL," |
331 "favicon_url VARCHAR NOT NULL," | 353 "favicon_url VARCHAR NOT NULL," |
332 "url VARCHAR NOT NULL," | 354 "url VARCHAR NOT NULL," |
333 "safe_for_autoreplace INTEGER," | 355 "safe_for_autoreplace INTEGER," |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
390 // Old backup was invalid; drop the table entirely, which will trigger the | 412 // Old backup was invalid; drop the table entirely, which will trigger the |
391 // protector code to prompt the user and recreate the table. | 413 // protector code to prompt the user and recreate the table. |
392 if (db_->DoesTableExist("keywords_backup") && | 414 if (db_->DoesTableExist("keywords_backup") && |
393 !db_->Execute("DROP TABLE keywords_backup")) | 415 !db_->Execute("DROP TABLE keywords_backup")) |
394 return false; | 416 return false; |
395 } | 417 } |
396 | 418 |
397 return transaction.Commit(); | 419 return transaction.Commit(); |
398 } | 420 } |
399 | 421 |
422 bool KeywordTable::MigrateToVersion47AddAlternateURLsColumn() { | |
423 sql::Transaction transaction(db_); | |
424 | |
425 // Fill the |alternate_urls| column with empty strings, otherwise it breaks | |
426 // code relying on |kKeywordColumnsConcatenated|. | |
427 if (!transaction.Begin() || | |
428 !db_->Execute("ALTER TABLE keywords ADD COLUMN " | |
429 "alternate_urls VARCHAR DEFAULT ''")) | |
430 return false; | |
431 | |
432 if (db_->DoesTableExist("keywords_backup")) { | |
433 if (!db_->Execute("ALTER TABLE keywords_backup ADD COLUMN " | |
434 "alternate_urls VARCHAR DEFAULT ''") || | |
435 !SignBackup(47)) | |
436 return false; | |
437 } | |
438 | |
439 return transaction.Commit(); | |
440 } | |
441 | |
400 // static | 442 // static |
401 bool KeywordTable::GetKeywordDataFromStatement(const sql::Statement& s, | 443 bool KeywordTable::GetKeywordDataFromStatement(const sql::Statement& s, |
402 TemplateURLData* data) { | 444 TemplateURLData* data) { |
403 DCHECK(data); | 445 DCHECK(data); |
446 base::JSONReader json_reader; | |
Joao da Silva
2012/09/28 11:46:10
nit: move this next to the place where it's used
beaudoin
2012/10/02 17:43:29
Done.
| |
447 | |
404 data->short_name = s.ColumnString16(1); | 448 data->short_name = s.ColumnString16(1); |
405 data->SetKeyword(s.ColumnString16(2)); | 449 data->SetKeyword(s.ColumnString16(2)); |
406 // Due to past bugs, we might have persisted entries with empty URLs. Avoid | 450 // Due to past bugs, we might have persisted entries with empty URLs. Avoid |
407 // reading these out. (GetKeywords() will delete these entries on return.) | 451 // reading these out. (GetKeywords() will delete these entries on return.) |
408 // NOTE: This code should only be needed as long as we might be reading such | 452 // NOTE: This code should only be needed as long as we might be reading such |
409 // potentially-old data and can be removed afterward. | 453 // potentially-old data and can be removed afterward. |
410 if (s.ColumnString(4).empty()) | 454 if (s.ColumnString(4).empty()) |
411 return false; | 455 return false; |
412 data->SetURL(s.ColumnString(4)); | 456 data->SetURL(s.ColumnString(4)); |
413 data->suggestions_url = s.ColumnString(11); | 457 data->suggestions_url = s.ColumnString(11); |
414 data->instant_url = s.ColumnString(14); | 458 data->instant_url = s.ColumnString(14); |
415 data->favicon_url = GURL(s.ColumnString(3)); | 459 data->favicon_url = GURL(s.ColumnString(3)); |
416 data->originating_url = GURL(s.ColumnString(6)); | 460 data->originating_url = GURL(s.ColumnString(6)); |
417 data->show_in_default_list = s.ColumnBool(10); | 461 data->show_in_default_list = s.ColumnBool(10); |
418 data->safe_for_autoreplace = s.ColumnBool(5); | 462 data->safe_for_autoreplace = s.ColumnBool(5); |
419 base::SplitString(s.ColumnString(9), ';', &data->input_encodings); | 463 base::SplitString(s.ColumnString(9), ';', &data->input_encodings); |
420 data->id = s.ColumnInt64(0); | 464 data->id = s.ColumnInt64(0); |
421 data->date_created = Time::FromTimeT(s.ColumnInt64(7)); | 465 data->date_created = Time::FromTimeT(s.ColumnInt64(7)); |
422 data->last_modified = Time::FromTimeT(s.ColumnInt64(15)); | 466 data->last_modified = Time::FromTimeT(s.ColumnInt64(15)); |
423 data->created_by_policy = s.ColumnBool(13); | 467 data->created_by_policy = s.ColumnBool(13); |
424 data->usage_count = s.ColumnInt(8); | 468 data->usage_count = s.ColumnInt(8); |
425 data->prepopulate_id = s.ColumnInt(12); | 469 data->prepopulate_id = s.ColumnInt(12); |
426 data->sync_guid = s.ColumnString(16); | 470 data->sync_guid = s.ColumnString(16); |
471 | |
472 data->alternate_urls.clear(); | |
473 scoped_ptr<Value> value(json_reader.ReadToValue(s.ColumnString(17))); | |
474 ListValue* alternate_urls_value; | |
475 if (value->GetAsList(&alternate_urls_value)) { | |
Joao da Silva
2012/09/28 11:46:10
|value| can be NULL from the return of ReadToValue
beaudoin
2012/10/02 17:43:29
Good catch!
Done.
| |
476 std::string alternate_url; | |
477 for (size_t i = 0; i < alternate_urls_value->GetSize(); ++i) { | |
478 if (alternate_urls_value->GetString(i, &alternate_url)) | |
479 data->alternate_urls.push_back(alternate_url); | |
480 } | |
481 } | |
482 | |
427 return true; | 483 return true; |
428 } | 484 } |
429 | 485 |
430 bool KeywordTable::GetSignatureData(int table_version, std::string* backup) { | 486 bool KeywordTable::GetSignatureData(int table_version, std::string* backup) { |
431 DCHECK(backup); | 487 DCHECK(backup); |
432 | 488 |
433 int64 backup_value = kInvalidTemplateURLID; | 489 int64 backup_value = kInvalidTemplateURLID; |
434 if (!meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_value)) { | 490 if (!meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_value)) { |
435 LOG(ERROR) << "No backup id for signing."; | 491 LOG(ERROR) << "No backup id for signing."; |
436 return false; | 492 return false; |
(...skipping 11 matching lines...) Expand all Loading... | |
448 | 504 |
449 bool KeywordTable::GetTableContents(const char* table_name, | 505 bool KeywordTable::GetTableContents(const char* table_name, |
450 int table_version, | 506 int table_version, |
451 std::string* contents) { | 507 std::string* contents) { |
452 DCHECK(contents); | 508 DCHECK(contents); |
453 | 509 |
454 if (!db_->DoesTableExist(table_name)) | 510 if (!db_->DoesTableExist(table_name)) |
455 return false; | 511 return false; |
456 | 512 |
457 contents->clear(); | 513 contents->clear(); |
458 std::string query("SELECT " + | 514 const char* keywords_columns_concatenated = kKeywordColumnsConcatenated; |
459 std::string((table_version <= 44) ? | 515 if (table_version <= 44) |
460 kKeywordColumnsVersion44Concatenated : kKeywordColumnsConcatenated) + | 516 keywords_columns_concatenated = kKeywordColumnsVersion44Concatenated; |
517 else if (table_version <= 46) | |
518 keywords_columns_concatenated = kKeywordColumnsVersion46Concatenated; | |
519 std::string query("SELECT " + std::string(keywords_columns_concatenated) + | |
461 " FROM " + std::string(table_name) + " ORDER BY id ASC"); | 520 " FROM " + std::string(table_name) + " ORDER BY id ASC"); |
462 sql::Statement s((table_version == WebDatabase::kCurrentVersionNumber) ? | 521 sql::Statement s((table_version == WebDatabase::kCurrentVersionNumber) ? |
463 db_->GetCachedStatement(sql::StatementID(table_name), query.c_str()) : | 522 db_->GetCachedStatement(sql::StatementID(table_name), query.c_str()) : |
464 db_->GetUniqueStatement(query.c_str())); | 523 db_->GetUniqueStatement(query.c_str())); |
465 while (s.Step()) | 524 while (s.Step()) |
466 *contents += s.ColumnString(0); | 525 *contents += s.ColumnString(0); |
467 return s.Succeeded(); | 526 return s.Succeeded(); |
468 } | 527 } |
469 | 528 |
470 bool KeywordTable::UpdateBackupSignature(int table_version) { | 529 bool KeywordTable::UpdateBackupSignature(int table_version) { |
471 sql::Transaction transaction(db_); | 530 sql::Transaction transaction(db_); |
472 if (!transaction.Begin()) | 531 if (!transaction.Begin()) |
473 return false; | 532 return false; |
474 | 533 |
475 int64 id = kInvalidTemplateURLID; | 534 int64 id = kInvalidTemplateURLID; |
476 if (!UpdateDefaultSearchProviderIDBackup(&id)) { | 535 if (!UpdateDefaultSearchProviderIDBackup(&id)) { |
477 LOG(ERROR) << "Failed to update default search id backup."; | 536 LOG(ERROR) << "Failed to update default search id backup."; |
478 return false; | 537 return false; |
479 } | 538 } |
480 | 539 |
481 // Backup of all keywords. | 540 // Backup of all keywords. |
482 if (db_->DoesTableExist("keywords_backup") && | 541 if (db_->DoesTableExist("keywords_backup") && |
483 !db_->Execute("DROP TABLE keywords_backup")) | 542 !db_->Execute("DROP TABLE keywords_backup")) |
484 return false; | 543 return false; |
485 | 544 |
545 const char* keywords_columns = kKeywordColumns; | |
546 if (table_version <= 44) | |
547 keywords_columns = kKeywordColumnsVersion44; | |
548 else if (table_version <= 46) | |
549 keywords_columns = kKeywordColumnsVersion46; | |
486 std::string query("CREATE TABLE keywords_backup AS SELECT " + | 550 std::string query("CREATE TABLE keywords_backup AS SELECT " + |
487 std::string((table_version <= 44) ? | 551 std::string(keywords_columns) + " FROM keywords ORDER BY id ASC"); |
488 kKeywordColumnsVersion44 : kKeywordColumns) + | |
489 " FROM keywords ORDER BY id ASC"); | |
490 if (!db_->Execute(query.c_str())) { | 552 if (!db_->Execute(query.c_str())) { |
491 LOG(ERROR) << "Failed to create keywords_backup table."; | 553 LOG(ERROR) << "Failed to create keywords_backup table."; |
492 return false; | 554 return false; |
493 } | 555 } |
494 | 556 |
495 return SignBackup(table_version) && transaction.Commit(); | 557 return SignBackup(table_version) && transaction.Commit(); |
496 } | 558 } |
497 | 559 |
498 bool KeywordTable::SignBackup(int table_version) { | 560 bool KeywordTable::SignBackup(int table_version) { |
499 std::string data_to_sign; | 561 std::string data_to_sign; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
568 "input_encodings VARCHAR," | 630 "input_encodings VARCHAR," |
569 "show_in_default_list INTEGER," | 631 "show_in_default_list INTEGER," |
570 "suggest_url VARCHAR," | 632 "suggest_url VARCHAR," |
571 "prepopulate_id INTEGER DEFAULT 0," | 633 "prepopulate_id INTEGER DEFAULT 0," |
572 "created_by_policy INTEGER DEFAULT 0," | 634 "created_by_policy INTEGER DEFAULT 0," |
573 "instant_url VARCHAR," | 635 "instant_url VARCHAR," |
574 "last_modified INTEGER DEFAULT 0," | 636 "last_modified INTEGER DEFAULT 0," |
575 "sync_guid VARCHAR)")) | 637 "sync_guid VARCHAR)")) |
576 return false; | 638 return false; |
577 std::string sql("INSERT INTO keywords_temp SELECT " + | 639 std::string sql("INSERT INTO keywords_temp SELECT " + |
578 std::string(kKeywordColumnsVersion45) + " FROM " + name); | 640 std::string(kKeywordColumnsVersion46) + " FROM " + name); |
579 if (!db_->Execute(sql.c_str())) | 641 if (!db_->Execute(sql.c_str())) |
580 return false; | 642 return false; |
581 | 643 |
582 // NOTE: The ORDER BY here ensures that the uniquing process for keywords will | 644 // NOTE: The ORDER BY here ensures that the uniquing process for keywords will |
583 // happen identically on both the normal and backup tables. | 645 // happen identically on both the normal and backup tables. |
584 sql = "SELECT id, keyword, url, autogenerate_keyword FROM " + name + | 646 sql = "SELECT id, keyword, url, autogenerate_keyword FROM " + name + |
585 " ORDER BY id ASC"; | 647 " ORDER BY id ASC"; |
586 sql::Statement s(db_->GetUniqueStatement(sql.c_str())); | 648 sql::Statement s(db_->GetUniqueStatement(sql.c_str())); |
587 string16 placeholder_keyword(ASCIIToUTF16("dummy")); | 649 string16 placeholder_keyword(ASCIIToUTF16("dummy")); |
588 std::set<string16> keywords; | 650 std::set<string16> keywords; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
630 } | 692 } |
631 } | 693 } |
632 | 694 |
633 // Replace the old table with the new one. | 695 // Replace the old table with the new one. |
634 sql = "DROP TABLE " + name; | 696 sql = "DROP TABLE " + name; |
635 if (!db_->Execute(sql.c_str())) | 697 if (!db_->Execute(sql.c_str())) |
636 return false; | 698 return false; |
637 sql = "ALTER TABLE keywords_temp RENAME TO " + name; | 699 sql = "ALTER TABLE keywords_temp RENAME TO " + name; |
638 return db_->Execute(sql.c_str()); | 700 return db_->Execute(sql.c_str()); |
639 } | 701 } |
OLD | NEW |