summaryrefslogtreecommitdiffstats
path: root/chrome/browser/webdata/web_intents_table.cc
blob: 127c840d39a920e4fd7c0558997d161d43500492 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/webdata/web_intents_table.h"
#include <string>

#include "base/i18n/case_conversion.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/intents/default_web_intent_service.h"
#include "googleurl/src/gurl.h"
#include "net/base/mime_util.h"
#include "sql/statement.h"
#include "third_party/sqlite/sqlite3.h"

using webkit_glue::WebIntentServiceData;

namespace {

bool ExtractIntents(sql::Statement* s,
                    std::vector<WebIntentServiceData>* services) {
  DCHECK(s);
  if (!s->is_valid())
    return false;

  while (s->Step()) {
    WebIntentServiceData service;
    string16 tmp = s->ColumnString16(0);
    service.service_url = GURL(tmp);

    service.action = s->ColumnString16(1);
    service.type = s->ColumnString16(2);
    service.title = s->ColumnString16(3);
    tmp = s->ColumnString16(4);
     // Default to window disposition.
    service.disposition = WebIntentServiceData::DISPOSITION_WINDOW;
    if (tmp == ASCIIToUTF16("inline"))
      service.disposition = WebIntentServiceData::DISPOSITION_INLINE;
    services->push_back(service);
  }
  return s->Succeeded();
}

}  // namespace

WebIntentsTable::WebIntentsTable(sql::Connection* db,
                                 sql::MetaTable* meta_table)
    : WebDatabaseTable(db, meta_table) {
}

WebIntentsTable::~WebIntentsTable() {
}

bool WebIntentsTable::Init() {
  if (!db_->DoesTableExist("web_intents")) {
    if (!db_->Execute("CREATE TABLE web_intents ("
                      " service_url LONGVARCHAR,"
                      " action VARCHAR,"
                      " type VARCHAR,"
                      " title LONGVARCHAR,"
                      " disposition VARCHAR,"
                      " scheme VARCHAR,"
                      " UNIQUE (service_url, action, scheme, type))")) {
      return false;
    }
    if (!db_->Execute("CREATE INDEX IF NOT EXISTS web_intents_index"
                      " ON web_intents (action)"))
      return false;
    if (!db_->Execute("CREATE INDEX IF NOT EXISTS web_intents_index"
                      " ON web_intents (scheme)"))
      return false;
  }

  if (!db_->DoesTableExist("web_intents_defaults")) {
    if (!db_->Execute("CREATE TABLE web_intents_defaults ("
                      " action VARCHAR,"
                      " type VARCHAR,"
                      " url_pattern LONGVARCHAR,"
                      " user_date INTEGER,"
                      " suppression INTEGER,"
                      " service_url LONGVARCHAR,"
                      " scheme VARCHAR,"
                      " UNIQUE (action, scheme, type, url_pattern))")) {
      return false;
    }
    if (!db_->Execute("CREATE INDEX IF NOT EXISTS web_intents_default_index"
                      " ON web_intents_defaults (action)"))
      return false;

    if (!db_->Execute("CREATE INDEX IF NOT EXISTS web_intents_default_index"
                      " ON web_intents_defaults (scheme)"))
      return false;
  }

  return true;
}

// TODO(jhawkins): Figure out Sync story.
bool WebIntentsTable::IsSyncable() {
  return false;
}

// Updates the table by way of renaming the old tables, rerunning
// the Init method, then selecting old values into the new tables.
bool WebIntentsTable::MigrateToVersion46AddSchemeColumn() {

  if (!db_->Execute("ALTER TABLE web_intents RENAME TO old_web_intents")) {
    DLOG(WARNING) << "Could not backup web_intents table.";
    return false;
  }

  if (!db_->Execute("ALTER TABLE web_intents_defaults"
                    " RENAME TO old_web_intents_defaults")) {
    DLOG(WARNING) << "Could not backup web_intents_defaults table.";
    return false;
  }

  if (!Init()) return false;

  int error = db_->ExecuteAndReturnErrorCode(
      "INSERT INTO web_intents"
      " (service_url, action, type, title, disposition)"
      " SELECT "
      " service_url, action, type, title, disposition"
      " FROM old_web_intents");

  if (error != SQLITE_OK) {
    DLOG(WARNING) << "Could not copy old intent data to upgraded table."
        << db_->GetErrorMessage();
  }


  error = db_->ExecuteAndReturnErrorCode(
      "INSERT INTO web_intents_defaults"
      " (service_url, action, type, url_pattern, user_date, suppression)"
      " SELECT "
      " service_url, action, type, url_pattern, user_date, suppression"
      " FROM old_web_intents_defaults");

  if (error != SQLITE_OK) {
    DLOG(WARNING) << "Could not copy old intent defaults to upgraded table."
        << db_->GetErrorMessage();
  }

  if (!db_->Execute("DROP table old_web_intents")) {
    LOG(WARNING) << "Could not drop backup web_intents table.";
    return false;
  }

  if (!db_->Execute("DROP table old_web_intents_defaults")) {
    DLOG(WARNING) << "Could not drop backup web_intents_defaults table.";
    return false;
  }

  return true;
}

bool WebIntentsTable::GetWebIntentServices(
    const string16& action,
    std::vector<WebIntentServiceData>* services) {
  DCHECK(services);
  sql::Statement s(db_->GetUniqueStatement(
      "SELECT service_url, action, type, title, disposition FROM web_intents "
      "WHERE action=?"));
  s.BindString16(0, action);

  return ExtractIntents(&s, services);
}

// TODO(gbillock): This currently does a full-table scan. Eventually we will
// store registrations by domain, and so have an indexed origin. At that time,
// this should just change to do lookup by origin instead of URL.
bool WebIntentsTable::GetWebIntentServicesForURL(
    const string16& service_url,
    std::vector<WebIntentServiceData>* services) {
  DCHECK(services);
  sql::Statement s(db_->GetUniqueStatement(
      "SELECT service_url, action, type, title, disposition FROM web_intents "
      "WHERE service_url=?"));
  s.BindString16(0, service_url);

  return ExtractIntents(&s, services);
}

bool WebIntentsTable::GetAllWebIntentServices(
    std::vector<WebIntentServiceData>* services) {
  DCHECK(services);
  sql::Statement s(db_->GetUniqueStatement(
      "SELECT service_url, action, type, title, disposition FROM web_intents"));

  return ExtractIntents(&s, services);
}

bool WebIntentsTable::SetWebIntentService(const WebIntentServiceData& service) {
  sql::Statement s(db_->GetUniqueStatement(
      "INSERT OR REPLACE INTO web_intents "
      "(service_url, type, action, title, disposition) "
      "VALUES (?, ?, ?, ?, ?)"));

  // Default to window disposition.
  string16 disposition = ASCIIToUTF16("window");
  if (service.disposition == WebIntentServiceData::DISPOSITION_INLINE)
    disposition = ASCIIToUTF16("inline");
  s.BindString(0, service.service_url.spec());
  s.BindString16(1, service.type);
  s.BindString16(2, service.action);
  s.BindString16(3, service.title);
  s.BindString16(4, disposition);

  return s.Run();
}

// TODO(jhawkins): Investigate the need to remove rows matching only
// |service.service_url|. It's unlikely the user will be given the ability to
// remove at the granularity of actions or types.
bool WebIntentsTable::RemoveWebIntentService(
    const WebIntentServiceData& service) {
  sql::Statement s(db_->GetUniqueStatement(
      "DELETE FROM web_intents "
      "WHERE service_url = ? AND action = ? AND type = ?"));
  s.BindString(0, service.service_url.spec());
  s.BindString16(1, service.action);
  s.BindString16(2, service.type);

  return s.Run();
}

bool WebIntentsTable::GetDefaultServices(
    const string16& action,
    std::vector<DefaultWebIntentService>* default_services) {
  sql::Statement s(db_->GetUniqueStatement(
      "SELECT action, type, url_pattern, user_date, suppression, "
      "service_url FROM web_intents_defaults "
      "WHERE action=?"));
  s.BindString16(0, action);

  while (s.Step()) {
    DefaultWebIntentService entry;
    entry.action = s.ColumnString16(0);
    entry.type = s.ColumnString16(1);
    if (entry.url_pattern.Parse(s.ColumnString(2)) !=
        URLPattern::PARSE_SUCCESS) {
      return false;
    }
    entry.user_date = s.ColumnInt(3);
    entry.suppression = s.ColumnInt(4);
    entry.service_url = s.ColumnString(5);

    default_services->push_back(entry);
  }

  return s.Succeeded();
}

bool WebIntentsTable::GetAllDefaultServices(
    std::vector<DefaultWebIntentService>* default_services) {
  sql::Statement s(db_->GetUniqueStatement(
      "SELECT action, type, url_pattern, user_date, suppression, "
      "service_url FROM web_intents_defaults"));

  while (s.Step()) {
    DefaultWebIntentService entry;
    entry.action = s.ColumnString16(0);
    entry.type = s.ColumnString16(1);
    if (entry.url_pattern.Parse(s.ColumnString(2)) !=
        URLPattern::PARSE_SUCCESS) {
      return false;
    }
    entry.user_date = s.ColumnInt(3);
    entry.suppression = s.ColumnInt(4);
    entry.service_url = s.ColumnString(5);

    default_services->push_back(entry);
  }

  return s.Succeeded();

}

bool WebIntentsTable::SetDefaultService(
    const DefaultWebIntentService& default_service) {
  sql::Statement s(db_->GetUniqueStatement(
      "INSERT OR REPLACE INTO web_intents_defaults "
      "(action, type, url_pattern, user_date, suppression, service_url) "
      "VALUES (?, ?, ?, ?, ?, ?)"));
  s.BindString16(0, default_service.action);
  s.BindString16(1, default_service.type);
  s.BindString(2, default_service.url_pattern.GetAsString());
  s.BindInt(3, default_service.user_date);
  s.BindInt(4, default_service.suppression);
  s.BindString(5, default_service.service_url);

  return s.Run();
}

bool WebIntentsTable::RemoveDefaultService(
    const DefaultWebIntentService& default_service) {
  sql::Statement s(db_->GetUniqueStatement(
      "DELETE FROM web_intents_defaults "
      "WHERE action = ? AND type = ? AND url_pattern = ?"));
  s.BindString16(0, default_service.action);
  s.BindString16(1, default_service.type);
  s.BindString(2, default_service.url_pattern.GetAsString());

  return s.Run();
}