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
3 // found in the LICENSE file.
5 #include "chrome/browser/webdata/web_intents_table.h"
9 #include "base/i18n/case_conversion.h"
10 #include "base/logging.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "components/webdata/common/web_database.h"
14 #include "net/base/mime_util.h"
15 #include "sql/statement.h"
16 #include "third_party/sqlite/sqlite3.h"
21 WebDatabaseTable::TypeKey GetKey() {
22 // We just need a unique constant. Use the address of a static that
23 // COMDAT folding won't touch in an optimizing linker.
24 static int table_key = 0;
25 return reinterpret_cast<void*>(&table_key);
30 WebIntentsTable::WebIntentsTable() {
33 WebIntentsTable::~WebIntentsTable() {
36 WebIntentsTable* WebIntentsTable::FromWebDatabase(WebDatabase* db) {
37 return static_cast<WebIntentsTable*>(db->GetTable(GetKey()));
40 WebDatabaseTable::TypeKey WebIntentsTable::GetTypeKey() const {
44 bool WebIntentsTable::Init(sql::Connection* db, sql::MetaTable* meta_table) {
45 WebDatabaseTable::Init(db, meta_table);
47 if (!db_->DoesTableExist("web_intents")) {
48 if (!db_->Execute("CREATE TABLE web_intents ("
49 " service_url LONGVARCHAR,"
53 " disposition VARCHAR,"
55 " UNIQUE (service_url, action, scheme, type))")) {
58 if (!db_->Execute("CREATE INDEX IF NOT EXISTS web_intents_index"
59 " ON web_intents (action)"))
61 if (!db_->Execute("CREATE INDEX IF NOT EXISTS web_intents_index"
62 " ON web_intents (scheme)"))
66 if (!db_->DoesTableExist("web_intents_defaults")) {
67 if (!db_->Execute("CREATE TABLE web_intents_defaults ("
70 " url_pattern LONGVARCHAR,"
72 " suppression INTEGER,"
73 " service_url LONGVARCHAR,"
75 " UNIQUE (action, scheme, type, url_pattern))")) {
78 if (!db_->Execute("CREATE INDEX IF NOT EXISTS web_intents_default_index"
79 " ON web_intents_defaults (action)"))
82 if (!db_->Execute("CREATE INDEX IF NOT EXISTS web_intents_default_index"
83 " ON web_intents_defaults (scheme)"))
90 // TODO(jhawkins): Figure out Sync story.
91 bool WebIntentsTable::IsSyncable() {
95 bool WebIntentsTable::MigrateToVersion(int version,
96 bool* update_compatible_version) {
98 *update_compatible_version = true;
99 return MigrateToVersion46AddSchemeColumn();
105 // Updates the table by way of renaming the old tables, rerunning
106 // the Init method, then selecting old values into the new tables.
107 bool WebIntentsTable::MigrateToVersion46AddSchemeColumn() {
109 if (!db_->Execute("ALTER TABLE web_intents RENAME TO old_web_intents")) {
110 DLOG(WARNING) << "Could not backup web_intents table.";
114 if (!db_->Execute("ALTER TABLE web_intents_defaults"
115 " RENAME TO old_web_intents_defaults")) {
116 DLOG(WARNING) << "Could not backup web_intents_defaults table.";
120 if (!Init(db_, meta_table_)) return false;
122 int error = db_->ExecuteAndReturnErrorCode(
123 "INSERT INTO web_intents"
124 " (service_url, action, type, title, disposition)"
126 " service_url, action, type, title, disposition"
127 " FROM old_web_intents");
129 if (error != SQLITE_OK) {
130 DLOG(WARNING) << "Could not copy old intent data to upgraded table."
131 << db_->GetErrorMessage();
135 error = db_->ExecuteAndReturnErrorCode(
136 "INSERT INTO web_intents_defaults"
137 " (service_url, action, type, url_pattern, user_date, suppression)"
139 " service_url, action, type, url_pattern, user_date, suppression"
140 " FROM old_web_intents_defaults");
142 if (error != SQLITE_OK) {
143 DLOG(WARNING) << "Could not copy old intent defaults to upgraded table."
144 << db_->GetErrorMessage();
147 if (!db_->Execute("DROP table old_web_intents")) {
148 LOG(WARNING) << "Could not drop backup web_intents table.";
152 if (!db_->Execute("DROP table old_web_intents_defaults")) {
153 DLOG(WARNING) << "Could not drop backup web_intents_defaults table.";