| OLD | NEW |
| 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 "chrome/browser/webdata/keyword_table.h" | 5 #include "chrome/browser/webdata/keyword_table.h" |
| 6 | 6 |
| 7 #include "app/sql/statement.h" | 7 #include "app/sql/statement.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/history/history_database.h" | 12 #include "chrome/browser/history/history_database.h" |
| 13 #include "chrome/browser/search_engines/template_url.h" | 13 #include "chrome/browser/search_engines/template_url.h" |
| 14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 15 | 15 |
| 16 using base::Time; | 16 using base::Time; |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // ID of the url column in keywords. | 20 // ID of the url column in keywords. |
| 21 const int kUrlIdPosition = 16; | 21 const int kUrlIdPosition = 17; |
| 22 | 22 |
| 23 // Keys used in the meta table. | 23 // Keys used in the meta table. |
| 24 const char* kDefaultSearchProviderKey = "Default Search Provider ID"; | 24 const char* kDefaultSearchProviderKey = "Default Search Provider ID"; |
| 25 const char* kBuiltinKeywordVersion = "Builtin Keyword Version"; | 25 const char* kBuiltinKeywordVersion = "Builtin Keyword Version"; |
| 26 | 26 |
| 27 void BindURLToStatement(const TemplateURL& url, sql::Statement* s) { | 27 void BindURLToStatement(const TemplateURL& url, sql::Statement* s) { |
| 28 s->BindString(0, UTF16ToUTF8(url.short_name())); | 28 s->BindString(0, UTF16ToUTF8(url.short_name())); |
| 29 s->BindString(1, UTF16ToUTF8(url.keyword())); | 29 s->BindString(1, UTF16ToUTF8(url.keyword())); |
| 30 GURL favicon_url = url.GetFaviconURL(); | 30 GURL favicon_url = url.GetFaviconURL(); |
| 31 if (!favicon_url.is_valid()) { | 31 if (!favicon_url.is_valid()) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 47 s->BindString(8, JoinString(url.input_encodings(), ';')); | 47 s->BindString(8, JoinString(url.input_encodings(), ';')); |
| 48 s->BindInt(9, url.show_in_default_list() ? 1 : 0); | 48 s->BindInt(9, url.show_in_default_list() ? 1 : 0); |
| 49 s->BindString(10, url.suggestions_url() ? url.suggestions_url()->url() : | 49 s->BindString(10, url.suggestions_url() ? url.suggestions_url()->url() : |
| 50 std::string()); | 50 std::string()); |
| 51 s->BindInt(11, url.prepopulate_id()); | 51 s->BindInt(11, url.prepopulate_id()); |
| 52 s->BindInt(12, url.autogenerate_keyword() ? 1 : 0); | 52 s->BindInt(12, url.autogenerate_keyword() ? 1 : 0); |
| 53 s->BindInt(13, url.logo_id()); | 53 s->BindInt(13, url.logo_id()); |
| 54 s->BindBool(14, url.created_by_policy()); | 54 s->BindBool(14, url.created_by_policy()); |
| 55 s->BindString(15, url.instant_url() ? url.instant_url()->url() : | 55 s->BindString(15, url.instant_url() ? url.instant_url()->url() : |
| 56 std::string()); | 56 std::string()); |
| 57 s->BindInt64(16, url.last_modified().ToTimeT()); |
| 57 } | 58 } |
| 58 } // anonymous namespace | 59 } // anonymous namespace |
| 59 | 60 |
| 60 KeywordTable::~KeywordTable() {} | 61 KeywordTable::~KeywordTable() {} |
| 61 | 62 |
| 62 bool KeywordTable::Init() { | 63 bool KeywordTable::Init() { |
| 63 if (!db_->DoesTableExist("keywords")) { | 64 if (!db_->DoesTableExist("keywords")) { |
| 64 if (!db_->Execute("CREATE TABLE keywords (" | 65 if (!db_->Execute("CREATE TABLE keywords (" |
| 65 "id INTEGER PRIMARY KEY," | 66 "id INTEGER PRIMARY KEY," |
| 66 "short_name VARCHAR NOT NULL," | 67 "short_name VARCHAR NOT NULL," |
| 67 "keyword VARCHAR NOT NULL," | 68 "keyword VARCHAR NOT NULL," |
| 68 "favicon_url VARCHAR NOT NULL," | 69 "favicon_url VARCHAR NOT NULL," |
| 69 "url VARCHAR NOT NULL," | 70 "url VARCHAR NOT NULL," |
| 70 "show_in_default_list INTEGER," | 71 "show_in_default_list INTEGER," |
| 71 "safe_for_autoreplace INTEGER," | 72 "safe_for_autoreplace INTEGER," |
| 72 "originating_url VARCHAR," | 73 "originating_url VARCHAR," |
| 73 "date_created INTEGER DEFAULT 0," | 74 "date_created INTEGER DEFAULT 0," |
| 74 "usage_count INTEGER DEFAULT 0," | 75 "usage_count INTEGER DEFAULT 0," |
| 75 "input_encodings VARCHAR," | 76 "input_encodings VARCHAR," |
| 76 "suggest_url VARCHAR," | 77 "suggest_url VARCHAR," |
| 77 "prepopulate_id INTEGER DEFAULT 0," | 78 "prepopulate_id INTEGER DEFAULT 0," |
| 78 "autogenerate_keyword INTEGER DEFAULT 0," | 79 "autogenerate_keyword INTEGER DEFAULT 0," |
| 79 "logo_id INTEGER DEFAULT 0," | 80 "logo_id INTEGER DEFAULT 0," |
| 80 "created_by_policy INTEGER DEFAULT 0," | 81 "created_by_policy INTEGER DEFAULT 0," |
| 81 "instant_url VARCHAR)")) { | 82 "instant_url VARCHAR," |
| 83 "last_modified INTEGER DEFAULT 0)")) { |
| 82 NOTREACHED(); | 84 NOTREACHED(); |
| 83 return false; | 85 return false; |
| 84 } | 86 } |
| 85 } | 87 } |
| 86 return true; | 88 return true; |
| 87 } | 89 } |
| 88 | 90 |
| 89 bool KeywordTable::IsSyncable() { | 91 bool KeywordTable::IsSyncable() { |
| 90 return true; | 92 return true; |
| 91 } | 93 } |
| 92 | 94 |
| 93 bool KeywordTable::AddKeyword(const TemplateURL& url) { | 95 bool KeywordTable::AddKeyword(const TemplateURL& url) { |
| 94 DCHECK(url.id()); | 96 DCHECK(url.id()); |
| 95 // Be sure to change kUrlIdPosition if you add columns | 97 // Be sure to change kUrlIdPosition if you add columns |
| 96 sql::Statement s(db_->GetCachedStatement(SQL_FROM_HERE, | 98 sql::Statement s(db_->GetCachedStatement(SQL_FROM_HERE, |
| 97 "INSERT INTO keywords " | 99 "INSERT INTO keywords " |
| 98 "(short_name, keyword, favicon_url, url, safe_for_autoreplace, " | 100 "(short_name, keyword, favicon_url, url, safe_for_autoreplace, " |
| 99 "originating_url, date_created, usage_count, input_encodings, " | 101 "originating_url, date_created, usage_count, input_encodings, " |
| 100 "show_in_default_list, suggest_url, prepopulate_id, " | 102 "show_in_default_list, suggest_url, prepopulate_id, " |
| 101 "autogenerate_keyword, logo_id, created_by_policy, instant_url, " | 103 "autogenerate_keyword, logo_id, created_by_policy, instant_url, " |
| 102 "id) VALUES " | 104 "last_modified, id) VALUES " |
| 103 "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")); | 105 "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")); |
| 104 if (!s) { | 106 if (!s) { |
| 105 NOTREACHED() << "Statement prepare failed"; | 107 NOTREACHED() << "Statement prepare failed"; |
| 106 return false; | 108 return false; |
| 107 } | 109 } |
| 108 BindURLToStatement(url, &s); | 110 BindURLToStatement(url, &s); |
| 109 s.BindInt64(kUrlIdPosition, url.id()); | 111 s.BindInt64(kUrlIdPosition, url.id()); |
| 110 if (!s.Run()) { | 112 if (!s.Run()) { |
| 111 NOTREACHED(); | 113 NOTREACHED(); |
| 112 return false; | 114 return false; |
| 113 } | 115 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 125 s.BindInt64(0, id); | 127 s.BindInt64(0, id); |
| 126 return s.Run(); | 128 return s.Run(); |
| 127 } | 129 } |
| 128 | 130 |
| 129 bool KeywordTable::GetKeywords(std::vector<TemplateURL*>* urls) { | 131 bool KeywordTable::GetKeywords(std::vector<TemplateURL*>* urls) { |
| 130 sql::Statement s(db_->GetUniqueStatement( | 132 sql::Statement s(db_->GetUniqueStatement( |
| 131 "SELECT id, short_name, keyword, favicon_url, url, " | 133 "SELECT id, short_name, keyword, favicon_url, url, " |
| 132 "safe_for_autoreplace, originating_url, date_created, " | 134 "safe_for_autoreplace, originating_url, date_created, " |
| 133 "usage_count, input_encodings, show_in_default_list, " | 135 "usage_count, input_encodings, show_in_default_list, " |
| 134 "suggest_url, prepopulate_id, autogenerate_keyword, logo_id, " | 136 "suggest_url, prepopulate_id, autogenerate_keyword, logo_id, " |
| 135 "created_by_policy, instant_url " | 137 "created_by_policy, instant_url, last_modified " |
| 136 "FROM keywords ORDER BY id ASC")); | 138 "FROM keywords ORDER BY id ASC")); |
| 137 if (!s) { | 139 if (!s) { |
| 138 NOTREACHED() << "Statement prepare failed"; | 140 NOTREACHED() << "Statement prepare failed"; |
| 139 return false; | 141 return false; |
| 140 } | 142 } |
| 141 while (s.Step()) { | 143 while (s.Step()) { |
| 142 TemplateURL* template_url = new TemplateURL(); | 144 TemplateURL* template_url = new TemplateURL(); |
| 143 template_url->set_id(s.ColumnInt64(0)); | 145 template_url->set_id(s.ColumnInt64(0)); |
| 144 | 146 |
| 145 std::string tmp; | 147 std::string tmp; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 176 template_url->set_prepopulate_id(s.ColumnInt(12)); | 178 template_url->set_prepopulate_id(s.ColumnInt(12)); |
| 177 | 179 |
| 178 template_url->set_autogenerate_keyword(s.ColumnInt(13) == 1); | 180 template_url->set_autogenerate_keyword(s.ColumnInt(13) == 1); |
| 179 | 181 |
| 180 template_url->set_logo_id(s.ColumnInt(14)); | 182 template_url->set_logo_id(s.ColumnInt(14)); |
| 181 | 183 |
| 182 template_url->set_created_by_policy(s.ColumnBool(15)); | 184 template_url->set_created_by_policy(s.ColumnBool(15)); |
| 183 | 185 |
| 184 template_url->SetInstantURL(s.ColumnString(16), 0, 0); | 186 template_url->SetInstantURL(s.ColumnString(16), 0, 0); |
| 185 | 187 |
| 188 template_url->set_last_modified(Time::FromTimeT(s.ColumnInt64(17))); |
| 189 |
| 186 urls->push_back(template_url); | 190 urls->push_back(template_url); |
| 187 } | 191 } |
| 188 return s.Succeeded(); | 192 return s.Succeeded(); |
| 189 } | 193 } |
| 190 | 194 |
| 191 bool KeywordTable::UpdateKeyword(const TemplateURL& url) { | 195 bool KeywordTable::UpdateKeyword(const TemplateURL& url) { |
| 192 DCHECK(url.id()); | 196 DCHECK(url.id()); |
| 193 // Be sure to change kUrlIdPosition if you add columns | 197 // Be sure to change kUrlIdPosition if you add columns |
| 194 sql::Statement s(db_->GetUniqueStatement( | 198 sql::Statement s(db_->GetUniqueStatement( |
| 195 "UPDATE keywords " | 199 "UPDATE keywords " |
| 196 "SET short_name=?, keyword=?, favicon_url=?, url=?, " | 200 "SET short_name=?, keyword=?, favicon_url=?, url=?, " |
| 197 "safe_for_autoreplace=?, originating_url=?, date_created=?, " | 201 "safe_for_autoreplace=?, originating_url=?, date_created=?, " |
| 198 "usage_count=?, input_encodings=?, show_in_default_list=?, " | 202 "usage_count=?, input_encodings=?, show_in_default_list=?, " |
| 199 "suggest_url=?, prepopulate_id=?, autogenerate_keyword=?, " | 203 "suggest_url=?, prepopulate_id=?, autogenerate_keyword=?, " |
| 200 "logo_id=?, created_by_policy=?, instant_url=? WHERE id=?")); | 204 "logo_id=?, created_by_policy=?, instant_url=?, last_modified=? " |
| 205 "WHERE id=?")); |
| 201 if (!s) { | 206 if (!s) { |
| 202 NOTREACHED() << "Statement prepare failed"; | 207 NOTREACHED() << "Statement prepare failed"; |
| 203 return false; | 208 return false; |
| 204 } | 209 } |
| 205 BindURLToStatement(url, &s); | 210 BindURLToStatement(url, &s); |
| 206 s.BindInt64(kUrlIdPosition, url.id()); | 211 s.BindInt64(kUrlIdPosition, url.id()); |
| 207 return s.Run(); | 212 return s.Run(); |
| 208 } | 213 } |
| 209 | 214 |
| 210 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { | 215 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 } | 288 } |
| 284 | 289 |
| 285 if (!db_->Execute("DROP TABLE keywords")) | 290 if (!db_->Execute("DROP TABLE keywords")) |
| 286 return false; | 291 return false; |
| 287 | 292 |
| 288 if (!db_->Execute("ALTER TABLE keywords_temp RENAME TO keywords")) | 293 if (!db_->Execute("ALTER TABLE keywords_temp RENAME TO keywords")) |
| 289 return false; | 294 return false; |
| 290 | 295 |
| 291 return true; | 296 return true; |
| 292 } | 297 } |
| 298 |
| 299 bool KeywordTable::MigrateToVersion38AddLastModifiedColumn() { |
| 300 return db_->Execute( |
| 301 "ALTER TABLE keywords ADD COLUMN last_modified INTEGER DEFAULT 0"); |
| 302 } |
| OLD | NEW |