summaryrefslogtreecommitdiffstats
path: root/chrome/browser/webdata/keyword_table.cc
blob: 92439020e53fb2441c930a914a00371991c884f2 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
// Copyright (c) 2011 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/keyword_table.h"

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram.h"
#include "base/metrics/stats_counters.h"
#include "base/string_number_conversions.h"
#include "base/string_split.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/history/history_database.h"
#include "chrome/browser/protector/histograms.h"
#include "chrome/browser/protector/protector.h"
#include "chrome/browser/search_engines/template_url.h"
#include "googleurl/src/gurl.h"
#include "sql/statement.h"
#include "sql/transaction.h"

using base::Time;

namespace {

// ID of the url column in keywords.
const int kUrlIdPosition = 18;

// Keys used in the meta table.
const char kDefaultSearchProviderKey[] = "Default Search Provider ID";
const char kBuiltinKeywordVersion[] = "Builtin Keyword Version";

// Meta table key to store backup value for the default search provider.
const char kDefaultSearchBackupKey[] = "Default Search Provider Backup";

// Meta table key to store backup value for the default search provider id.
const char kDefaultSearchIDBackupKey[] =
    "Default Search Provider ID Backup";

// Meta table key to store backup value signature for the default search
// provider. Default search provider id, its row in |keywords| table and
// the whole |keywords| table are signed.
const char kBackupSignatureKey[] =
    "Default Search Provider ID Backup Signature";

const char kKeywordColumnsConcatenated[] =
    "id || short_name || keyword || favicon_url || url || "
    "safe_for_autoreplace || originating_url || date_created || "
    "usage_count || input_encodings || show_in_default_list || "
    "suggest_url || prepopulate_id || autogenerate_keyword || logo_id || "
    "created_by_policy || instant_url || last_modified || sync_guid";

void BindURLToStatement(const TemplateURL& url, sql::Statement* s) {
  s->BindString(0, UTF16ToUTF8(url.short_name()));
  s->BindString(1, UTF16ToUTF8(url.keyword()));
  GURL favicon_url = url.GetFaviconURL();
  if (!favicon_url.is_valid()) {
    s->BindString(2, std::string());
  } else {
    s->BindString(2, history::HistoryDatabase::GURLToDatabaseURL(
        url.GetFaviconURL()));
  }
  s->BindString(3, url.url() ? url.url()->url() : std::string());
  s->BindInt(4, url.safe_for_autoreplace() ? 1 : 0);
  if (!url.originating_url().is_valid()) {
    s->BindString(5, std::string());
  } else {
    s->BindString(5, history::HistoryDatabase::GURLToDatabaseURL(
        url.originating_url()));
  }
  s->BindInt64(6, url.date_created().ToTimeT());
  s->BindInt(7, url.usage_count());
  s->BindString(8, JoinString(url.input_encodings(), ';'));
  s->BindInt(9, url.show_in_default_list() ? 1 : 0);
  s->BindString(10, url.suggestions_url() ? url.suggestions_url()->url() :
                std::string());
  s->BindInt(11, url.prepopulate_id());
  s->BindInt(12, url.autogenerate_keyword() ? 1 : 0);
  s->BindInt(13, url.logo_id());
  s->BindBool(14, url.created_by_policy());
  s->BindString(15, url.instant_url() ? url.instant_url()->url() :
                std::string());
  s->BindInt64(16, url.last_modified().ToTimeT());
  s->BindString(17, url.sync_guid());
}

// Signs search provider id and returns its signature.
std::string GetSearchProviderIDSignature(int64 id) {
  return protector::SignSetting(base::Int64ToString(id));
}

// Checks if signature for search provider id is correct and returns the
// result.
bool IsSearchProviderIDValid(int64 id, const std::string& signature) {
  return protector::IsSettingValid(base::Int64ToString(id), signature);
}

}  // anonymous namespace

KeywordTable::~KeywordTable() {}

bool KeywordTable::Init() {
  if (!db_->DoesTableExist("keywords")) {
    if (!db_->Execute(
        "CREATE TABLE keywords ("
        "id INTEGER PRIMARY KEY,"
        "short_name VARCHAR NOT NULL,"
        "keyword VARCHAR NOT NULL,"
        "favicon_url VARCHAR NOT NULL,"
        "url VARCHAR NOT NULL,"
        "show_in_default_list INTEGER,"
        "safe_for_autoreplace INTEGER,"
        "originating_url VARCHAR,"
        "date_created INTEGER DEFAULT 0,"
        "usage_count INTEGER DEFAULT 0,"
        "input_encodings VARCHAR,"
        "suggest_url VARCHAR,"
        "prepopulate_id INTEGER DEFAULT 0,"
        "autogenerate_keyword INTEGER DEFAULT 0,"
        "logo_id INTEGER DEFAULT 0,"
        "created_by_policy INTEGER DEFAULT 0,"
        "instant_url VARCHAR,"
        "last_modified INTEGER DEFAULT 0,"
        "sync_guid VARCHAR)")) {
      NOTREACHED();
      return false;
    }
    if (!UpdateBackupSignature())
      return false;
  }
  return true;
}

bool KeywordTable::IsSyncable() {
  return true;
}

bool KeywordTable::AddKeyword(const TemplateURL& url) {
  DCHECK(url.id());
  // Be sure to change kUrlIdPosition if you add columns
  sql::Statement s(db_->GetUniqueStatement(
      "INSERT INTO keywords "
      "(short_name, keyword, favicon_url, url, safe_for_autoreplace, "
      "originating_url, date_created, usage_count, input_encodings, "
      "show_in_default_list, suggest_url, prepopulate_id, "
      "autogenerate_keyword, logo_id, created_by_policy, instant_url, "
      "last_modified, sync_guid, id) VALUES "
      "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"));
  if (!s) {
    NOTREACHED() << "Statement prepare failed";
    return false;
  }
  BindURLToStatement(url, &s);
  s.BindInt64(kUrlIdPosition, url.id());
  if (!s.Run()) {
    NOTREACHED();
    return false;
  }
  return UpdateBackupSignature();
}

bool KeywordTable::RemoveKeyword(TemplateURLID id) {
  DCHECK(id);
  sql::Statement s(db_->GetUniqueStatement("DELETE FROM keywords WHERE id=?"));
  if (!s) {
    NOTREACHED() << "Statement prepare failed";
    return false;
  }
  s.BindInt64(0, id);
  return s.Run() && UpdateBackupSignature();
}

bool KeywordTable::GetKeywords(std::vector<TemplateURL*>* urls) {
  sql::Statement s(db_->GetUniqueStatement(
      "SELECT id, short_name, keyword, favicon_url, url, "
      "safe_for_autoreplace, originating_url, date_created, "
      "usage_count, input_encodings, show_in_default_list, "
      "suggest_url, prepopulate_id, autogenerate_keyword, logo_id, "
      "created_by_policy, instant_url, last_modified, sync_guid "
      "FROM keywords ORDER BY id ASC"));
  if (!s) {
    NOTREACHED() << "Statement prepare failed";
    return false;
  }
  while (s.Step()) {
    TemplateURL* template_url = new TemplateURL();
    GetURLFromStatement(s, template_url);
    urls->push_back(template_url);
  }
  return s.Succeeded();
}

bool KeywordTable::UpdateKeyword(const TemplateURL& url) {
  DCHECK(url.id());
  // Be sure to change kUrlIdPosition if you add columns
  sql::Statement s(db_->GetUniqueStatement(
      "UPDATE keywords "
      "SET short_name=?, keyword=?, favicon_url=?, url=?, "
      "safe_for_autoreplace=?, originating_url=?, date_created=?, "
      "usage_count=?, input_encodings=?, show_in_default_list=?, "
      "suggest_url=?, prepopulate_id=?, autogenerate_keyword=?, "
      "logo_id=?, created_by_policy=?, instant_url=?, last_modified=?, "
      "sync_guid=? WHERE id=?"));
  if (!s) {
    NOTREACHED() << "Statement prepare failed";
    return false;
  }
  BindURLToStatement(url, &s);
  s.BindInt64(kUrlIdPosition, url.id());
  return s.Run() && UpdateBackupSignature();
}

bool KeywordTable::SetDefaultSearchProviderID(int64 id) {
  return meta_table_->SetValue(kDefaultSearchProviderKey, id) &&
         UpdateBackupSignature();
}

int64 KeywordTable::GetDefaultSearchProviderID() {
  int64 value = 0;
  meta_table_->GetValue(kDefaultSearchProviderKey, &value);
  return value;
}

TemplateURL* KeywordTable::GetDefaultSearchProviderBackup() {
  if (!IsBackupSignatureValid())
    return NULL;

  int64 backup_id = 0;
  if (!meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_id)) {
    LOG(ERROR) << "No default search id backup found.";
    return NULL;
  }
  sql::Statement s(db_->GetUniqueStatement(
      "SELECT id, short_name, keyword, favicon_url, url, "
      "safe_for_autoreplace, originating_url, date_created, "
      "usage_count, input_encodings, show_in_default_list, "
      "suggest_url, prepopulate_id, autogenerate_keyword, logo_id, "
      "created_by_policy, instant_url, last_modified, sync_guid "
      "FROM keywords_backup WHERE id=?"));
  if (!s) {
    NOTREACHED() << "Statement prepare failed";
    return NULL;
  }
  s.BindInt64(0, backup_id);
  if (!s.Step()) {
    LOG(ERROR) << "No default search provider with backup id.";
    return NULL;
  }

  scoped_ptr<TemplateURL> template_url(new TemplateURL());
  GetURLFromStatement(s, template_url.get());

  if (!s.Succeeded()) {
    LOG(ERROR) << "Statement has not succeeded.";
    return NULL;
  }
  return template_url.release();
}

bool KeywordTable::DidDefaultSearchProviderChange() {
  if (!IsBackupSignatureValid()) {
    UMA_HISTOGRAM_ENUMERATION(
        protector::kProtectorHistogramDefaultSearchProvider,
        protector::kProtectorErrorBackupInvalid,
        protector::kProtectorErrorCount);
    LOG(ERROR) << "Backup signature is invalid.";
    return true;
  }

  int64 backup_id = 0;
  meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_id);
  int64 current_id = GetDefaultSearchProviderID();
  if (backup_id == current_id) {
    std::string backup_url;
    std::string current_url;
    // Either this is a new profile and both IDs are zero or the search
    // engines with the ids are equal.
    if (backup_id == 0) {
      UMA_HISTOGRAM_ENUMERATION(
          protector::kProtectorHistogramDefaultSearchProvider,
          protector::kProtectorErrorValueValidZero,
          protector::kProtectorErrorCount);
      return false;
    } else if (GetKeywordAsString(backup_id, "keywords_backup", &backup_url) &&
               GetKeywordAsString(current_id, "keywords", &current_url) &&
               current_url == backup_url) {
      UMA_HISTOGRAM_ENUMERATION(
          protector::kProtectorHistogramDefaultSearchProvider,
          protector::kProtectorErrorValueValid,
          protector::kProtectorErrorCount);
      return false;
    }
  }

  UMA_HISTOGRAM_ENUMERATION(
      protector::kProtectorHistogramDefaultSearchProvider,
      protector::kProtectorErrorValueChanged,
      protector::kProtectorErrorCount);
  LOG(WARNING) << "Default Search Provider is changed.";
  return true;
}

bool KeywordTable::SetBuiltinKeywordVersion(int version) {
  return meta_table_->SetValue(kBuiltinKeywordVersion, version);
}

int KeywordTable::GetBuiltinKeywordVersion() {
  int version = 0;
  if (!meta_table_->GetValue(kBuiltinKeywordVersion, &version))
    return 0;
  return version;
}

bool KeywordTable::MigrateToVersion21AutoGenerateKeywordColumn() {
  return db_->Execute("ALTER TABLE keywords ADD COLUMN autogenerate_keyword "
                      "INTEGER DEFAULT 0");
}

bool KeywordTable::MigrateToVersion25AddLogoIDColumn() {
  return db_->Execute(
      "ALTER TABLE keywords ADD COLUMN logo_id INTEGER DEFAULT 0");
}

bool KeywordTable::MigrateToVersion26AddCreatedByPolicyColumn() {
  return db_->Execute("ALTER TABLE keywords ADD COLUMN created_by_policy "
                      "INTEGER DEFAULT 0");
}

bool KeywordTable::MigrateToVersion28SupportsInstantColumn() {
  return db_->Execute("ALTER TABLE keywords ADD COLUMN supports_instant "
                      "INTEGER DEFAULT 0");
}

bool KeywordTable::MigrateToVersion29InstantUrlToSupportsInstant() {
  if (!db_->Execute("ALTER TABLE keywords ADD COLUMN instant_url VARCHAR"))
    return false;

  if (!db_->Execute("CREATE TABLE keywords_temp ("
                    "id INTEGER PRIMARY KEY,"
                    "short_name VARCHAR NOT NULL,"
                    "keyword VARCHAR NOT NULL,"
                    "favicon_url VARCHAR NOT NULL,"
                    "url VARCHAR NOT NULL,"
                    "show_in_default_list INTEGER,"
                    "safe_for_autoreplace INTEGER,"
                    "originating_url VARCHAR,"
                    "date_created INTEGER DEFAULT 0,"
                    "usage_count INTEGER DEFAULT 0,"
                    "input_encodings VARCHAR,"
                    "suggest_url VARCHAR,"
                    "prepopulate_id INTEGER DEFAULT 0,"
                    "autogenerate_keyword INTEGER DEFAULT 0,"
                    "logo_id INTEGER DEFAULT 0,"
                    "created_by_policy INTEGER DEFAULT 0,"
                    "instant_url VARCHAR)")) {
    return false;
  }

  if (!db_->Execute(
      "INSERT INTO keywords_temp "
      "SELECT id, short_name, keyword, favicon_url, url, "
      "show_in_default_list, safe_for_autoreplace, originating_url, "
      "date_created, usage_count, input_encodings, suggest_url, "
      "prepopulate_id, autogenerate_keyword, logo_id, created_by_policy, "
      "instant_url FROM keywords")) {
    return false;
  }

  if (!db_->Execute("DROP TABLE keywords"))
    return false;

  if (!db_->Execute("ALTER TABLE keywords_temp RENAME TO keywords"))
    return false;

  return true;
}

bool KeywordTable::MigrateToVersion38AddLastModifiedColumn() {
  return db_->Execute(
      "ALTER TABLE keywords ADD COLUMN last_modified INTEGER DEFAULT 0");
}

bool KeywordTable::MigrateToVersion39AddSyncGUIDColumn() {
  return db_->Execute(
      "ALTER TABLE keywords ADD COLUMN sync_guid VARCHAR");
}

bool KeywordTable::MigrateToVersion40AddDefaultSearchProviderBackup() {
  int64 value = 0;
  if (!meta_table_->GetValue(kDefaultSearchProviderKey, &value)) {
    // Write default search provider id if it's absent. TemplateURLService
    // will replace 0 with some real value.
    if (!meta_table_->SetValue(kDefaultSearchProviderKey, 0))
      return false;
  }
  return meta_table_->SetValue(kDefaultSearchIDBackupKey, value) &&
         meta_table_->SetValue(
             kBackupSignatureKey,
             GetSearchProviderIDSignature(value));
}

bool KeywordTable::MigrateToVersion41RewriteDefaultSearchProviderBackup() {
  // Due to crbug.com/101815 version 40 may contain corrupt or empty
  // signature. So ignore the signature and simply rewrite it.
  return MigrateToVersion40AddDefaultSearchProviderBackup();
}

bool KeywordTable::MigrateToVersion42AddFullDefaultSearchProviderBackup() {
  sql::Transaction transaction(db_);
  if (!transaction.Begin()) {
    NOTREACHED() << "Failed to start transaction";
    return false;
  }

  int64 id = 0;
  if (!UpdateDefaultSearchProviderIDBackup(&id))
    return false;

  std::string keyword_backup;
  if (!UpdateDefaultSearchProviderBackup(id, &keyword_backup))
    return false;

  std::string keywords;
  if (!GetTableContents("keywords", &keywords)) {
    NOTREACHED() << "Can't get keywords table contents to sign";
    return false;
  }

  std::string data_to_sign = base::Int64ToString(id) +
                             keyword_backup +
                             keywords;
  std::string signature = protector::SignSetting(data_to_sign);
  if (signature.empty()) {
    NOTREACHED() << "Signature is empty";
    return false;
  }
  if (!meta_table_->SetValue(kBackupSignatureKey, signature)) {
    NOTREACHED() << "Failed to write signature.";
    return false;
  }

  return transaction.Commit();
}

bool KeywordTable::MigrateToVersion43AddKeywordsBackupTable() {
  return meta_table_->SetValue(kDefaultSearchBackupKey, std::string()) &&
         UpdateBackupSignature();
}

bool KeywordTable::GetSignatureData(std::string* backup) {
  DCHECK(backup);

  int64 backup_value = 0;
  if (!meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_value)) {
    LOG(ERROR) << "No backup id for signing.";
    return false;
  }

  std::string keywords_backup_data;
  if (!GetTableContents("keywords_backup", &keywords_backup_data)) {
    LOG(ERROR) << "Can't get keywords backup data";
    return false;
  }
  *backup = base::Int64ToString(backup_value) + keywords_backup_data;
  return true;
}

bool KeywordTable::GetTableContents(const char* table_name,
                                    std::string* contents) {
  DCHECK(contents);
  std::string table_data;

  std::string query =
      "SELECT " + std::string(kKeywordColumnsConcatenated) +
      " FROM " + std::string(table_name) + " ORDER BY id ASC";
  sql::Statement s(db_->GetCachedStatement(sql::StatementID(table_name),
                                           query.c_str()));
  if (!s) {
    NOTREACHED() << "Statement prepare failed";
    return false;
  }
  while (s.Step())
    table_data += s.ColumnString(0);
  if (!s.Succeeded()) {
    NOTREACHED() << "Statement execution failed";
    return false;
  }
  *contents = table_data;
  return true;
}

bool KeywordTable::UpdateBackupSignature() {
  sql::Transaction transaction(db_);
  if (!transaction.Begin()) {
    NOTREACHED() << "Failed to start transaction";
    return false;
  }

  int64 id = 0;
  if (!UpdateDefaultSearchProviderIDBackup(&id)) {
    LOG(ERROR) << "Failed to update default search id backup.";
    return false;
  }

  // Backup of all keywords.
  if (db_->DoesTableExist("keywords_backup") &&
      !db_->Execute("DROP TABLE keywords_backup"))
    return false;

  if (!db_->Execute(
      "CREATE TABLE keywords_backup AS "
      "SELECT id, short_name, keyword, favicon_url, url, "
      "safe_for_autoreplace, originating_url, date_created, "
      "usage_count, input_encodings, show_in_default_list, "
      "suggest_url, prepopulate_id, autogenerate_keyword, logo_id, "
      "created_by_policy, instant_url, last_modified, sync_guid "
      "FROM keywords ORDER BY id ASC")) {
    LOG(ERROR) << "Failed to create keywords_backup table.";
    return false;
  }

  std::string data_to_sign;
  if (!GetSignatureData(&data_to_sign)) {
    LOG(ERROR) << "No data to sign.";
    return false;
  }

  std::string signature = protector::SignSetting(data_to_sign);
  if (signature.empty()) {
    LOG(ERROR) << "Signature is empty";
    return false;
  }

  if (!meta_table_->SetValue(kBackupSignatureKey, signature)) {
    NOTREACHED() << "Failed to write signature.";
    return false;
  }

  return transaction.Commit();
}

bool KeywordTable::IsBackupSignatureValid() {
  std::string signature;
  std::string signature_data;
  return meta_table_->GetValue(kBackupSignatureKey, &signature) &&
         GetSignatureData(&signature_data) &&
         protector::IsSettingValid(signature_data, signature);
}

void KeywordTable::GetURLFromStatement(
    const sql::Statement& s,
    TemplateURL* url) {
  url->set_id(s.ColumnInt64(0));

  std::string tmp;
  tmp = s.ColumnString(1);
  DCHECK(!tmp.empty());
  url->set_short_name(UTF8ToUTF16(tmp));

  url->set_keyword(UTF8ToUTF16(s.ColumnString(2)));

  tmp = s.ColumnString(3);
  if (!tmp.empty())
    url->SetFaviconURL(GURL(tmp));

  url->SetURL(s.ColumnString(4), 0, 0);

  url->set_safe_for_autoreplace(s.ColumnInt(5) == 1);

  tmp = s.ColumnString(6);
  if (!tmp.empty())
    url->set_originating_url(GURL(tmp));

  url->set_date_created(Time::FromTimeT(s.ColumnInt64(7)));

  url->set_usage_count(s.ColumnInt(8));

  std::vector<std::string> encodings;
  base::SplitString(s.ColumnString(9), ';', &encodings);
  url->set_input_encodings(encodings);

  url->set_show_in_default_list(s.ColumnInt(10) == 1);

  url->SetSuggestionsURL(s.ColumnString(11), 0, 0);

  url->SetPrepopulateId(s.ColumnInt(12));

  url->set_autogenerate_keyword(s.ColumnInt(13) == 1);

  url->set_logo_id(s.ColumnInt(14));

  url->set_created_by_policy(s.ColumnBool(15));

  url->SetInstantURL(s.ColumnString(16), 0, 0);

  url->set_last_modified(Time::FromTimeT(s.ColumnInt64(17)));

  url->set_sync_guid(s.ColumnString(18));
}

bool KeywordTable::GetKeywordAsString(TemplateURLID id,
                                      const std::string& table_name,
                                      std::string* result) {
  std::string query =
      "SELECT " + std::string(kKeywordColumnsConcatenated) +
      " FROM " + table_name + " WHERE id=?";
  sql::Statement s(db_->GetUniqueStatement(query.c_str()));
  if (!s) {
    NOTREACHED() << "Statement prepare failed";
    return false;
  }
  s.BindInt64(0, id);
  if (!s.Step()) {
    LOG(WARNING) << "No keyword with id: " << id << ", ignoring.";
    return true;
  }

  if (!s.Succeeded()) {
    NOTREACHED() << "Statement failed.";
    return false;
  }

  *result = s.ColumnString(0);
  return true;
}

bool KeywordTable::UpdateDefaultSearchProviderIDBackup(TemplateURLID* id) {
  DCHECK(id);
  int64 default_search_id = GetDefaultSearchProviderID();
  if (!meta_table_->SetValue(kDefaultSearchIDBackupKey,
                             default_search_id)) {
    LOG(ERROR) << "Can't write default search id backup.";
    return false;
  }

  *id = default_search_id;
  return true;
}

bool KeywordTable::UpdateDefaultSearchProviderBackup(TemplateURLID id,
                                                     std::string* backup) {
  DCHECK(backup);
  std::string backup_url;
  if (id != 0 && !GetKeywordAsString(id, "keywords", &backup_url)) {
    LOG(WARNING) << "Failed to get the keyword with id " << id;
    return false;
  }
  if (!meta_table_->SetValue(kDefaultSearchBackupKey, backup_url)) {
    LOG(WARNING) << "Failed to update the keyword backup";
    return false;
  }

  *backup = backup_url;
  return true;
}