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::CreateTablesIfNecessary() {
45 if (!db_
->DoesTableExist("web_intents")) {
46 if (!db_
->Execute("CREATE TABLE web_intents ("
47 " service_url LONGVARCHAR,"
51 " disposition VARCHAR,"
53 " UNIQUE (service_url, action, scheme, type))")) {
56 if (!db_
->Execute("CREATE INDEX IF NOT EXISTS web_intents_index"
57 " ON web_intents (action)"))
59 if (!db_
->Execute("CREATE INDEX IF NOT EXISTS web_intents_index"
60 " ON web_intents (scheme)"))
64 if (!db_
->DoesTableExist("web_intents_defaults")) {
65 if (!db_
->Execute("CREATE TABLE web_intents_defaults ("
68 " url_pattern LONGVARCHAR,"
70 " suppression INTEGER,"
71 " service_url LONGVARCHAR,"
73 " UNIQUE (action, scheme, type, url_pattern))")) {
76 if (!db_
->Execute("CREATE INDEX IF NOT EXISTS web_intents_default_index"
77 " ON web_intents_defaults (action)"))
80 if (!db_
->Execute("CREATE INDEX IF NOT EXISTS web_intents_default_index"
81 " ON web_intents_defaults (scheme)"))
88 // TODO(jhawkins): Figure out Sync story.
89 bool WebIntentsTable::IsSyncable() {
93 bool WebIntentsTable::MigrateToVersion(int version
,
94 bool* update_compatible_version
) {
96 *update_compatible_version
= true;
97 return MigrateToVersion46AddSchemeColumn();
103 // Updates the table by way of renaming the old tables, rerunning
104 // the Init method, then selecting old values into the new tables.
105 bool WebIntentsTable::MigrateToVersion46AddSchemeColumn() {
106 // If the old table doesn't exist, there's nothing to migrate.
107 if (!db_
->DoesTableExist("web_intents"))
110 if (!db_
->Execute("ALTER TABLE web_intents RENAME TO old_web_intents"))
113 if (!db_
->Execute("ALTER TABLE web_intents_defaults"
114 " RENAME TO old_web_intents_defaults"))
117 if (!CreateTablesIfNecessary())
120 int error
= db_
->ExecuteAndReturnErrorCode(
121 "INSERT INTO web_intents"
122 " (service_url, action, type, title, disposition)"
124 " service_url, action, type, title, disposition"
125 " FROM old_web_intents");
127 if (error
!= SQLITE_OK
) {
128 DLOG(WARNING
) << "Could not copy old intent data to upgraded table."
129 << db_
->GetErrorMessage();
133 error
= db_
->ExecuteAndReturnErrorCode(
134 "INSERT INTO web_intents_defaults"
135 " (service_url, action, type, url_pattern, user_date, suppression)"
137 " service_url, action, type, url_pattern, user_date, suppression"
138 " FROM old_web_intents_defaults");
140 if (error
!= SQLITE_OK
) {
141 DLOG(WARNING
) << "Could not copy old intent defaults to upgraded table."
142 << db_
->GetErrorMessage();
145 if (!db_
->Execute("DROP table old_web_intents")) {
146 LOG(WARNING
) << "Could not drop backup web_intents table.";
150 if (!db_
->Execute("DROP table old_web_intents_defaults")) {
151 DLOG(WARNING
) << "Could not drop backup web_intents_defaults table.";