File: power_bookmark_database_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (892 lines) | stat: -rw-r--r-- 29,344 bytes parent folder | download | duplicates (3)
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/power_bookmarks/storage/power_bookmark_database_impl.h"

#include "base/check.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/strings/pattern.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "components/power_bookmarks/common/power_bookmark_metrics.h"
#include "components/power_bookmarks/common/search_params.h"
#include "components/power_bookmarks/storage/power_bookmark_sync_metadata_database.h"
#include "components/sync/protocol/power_bookmark_specifics.pb.h"
#include "sql/error_delegate_util.h"
#include "sql/statement.h"
#include "sql/transaction.h"
#include "url/origin.h"

namespace power_bookmarks {

namespace {

// `kCurrentVersionNumber` and `kCompatibleVersionNumber` are used for DB
// migrations. Update both accordingly when changing the schema.
const int kCurrentVersionNumber = 1;
const int kCompatibleVersionNumber = 1;

static constexpr char kSaveTableName[] = "saves";
static constexpr char kBlobTableName[] = "blobs";

std::unique_ptr<Power> CreatePowerFromSpecifics(
    const sync_pb::PowerBookmarkSpecifics& specifics) {
  switch (specifics.power_type()) {
    case sync_pb::PowerBookmarkSpecifics::POWER_TYPE_UNSPECIFIED:
    case sync_pb::PowerBookmarkSpecifics::POWER_TYPE_MOCK:
    case sync_pb::PowerBookmarkSpecifics::POWER_TYPE_NOTE:
      return std::make_unique<Power>(specifics);
    default:
      NOTREACHED();
  }
}

bool CheckIfPowerWithIdExists(sql::Database* db, const base::Uuid& guid) {
  if (guid == base::Uuid()) {
    return false;
  }

  static constexpr char kCheckIfPowerWithIdExistsSql[] =
      // clang-format off
      "SELECT COUNT(*) FROM saves WHERE id=?";
  // clang-format on
  DCHECK(db->IsSQLValid(kCheckIfPowerWithIdExistsSql));

  sql::Statement count_statement(
      db->GetCachedStatement(SQL_FROM_HERE, kCheckIfPowerWithIdExistsSql));
  if (!count_statement.is_valid())
    return false;

  count_statement.BindString(0, guid.AsLowercaseString());
  if (!count_statement.Step())
    return false;

  size_t count = count_statement.ColumnInt(0);
  DCHECK(count == 0 || count == 1);
  return count > 0;
}

bool MatchPattern(std::string field, std::string pattern, bool case_sensitive) {
  if (!case_sensitive) {
    field = base::ToLowerASCII(field);
    pattern = base::ToLowerASCII(pattern);
  }
  return base::MatchPattern(field, pattern);
}

bool MatchesSearchQuery(const sync_pb::PowerBookmarkSpecifics& specifics,
                        const std::string& query,
                        bool case_sensitive) {
  if (query.empty()) {
    return true;
  }
  std::string pattern = base::StrCat({"*", query, "*"});
  std::vector<std::string> match_fields = {};

  // Different powers can define their own match fields.
  switch (specifics.power_type()) {
    case sync_pb::PowerBookmarkSpecifics::POWER_TYPE_NOTE:
      match_fields.push_back(
          specifics.power_entity().note_entity().plain_text());
      break;
    default:
      match_fields.push_back(specifics.url());
      break;
  }

  for (const std::string& match_field : match_fields) {
    if (MatchPattern(match_field, pattern, case_sensitive)) {
      return true;
    }
  }

  return false;
}

bool MatchesSearchParams(const sync_pb::PowerBookmarkSpecifics& specifics,
                         const SearchParams& search_params) {
  if (search_params.power_type !=
          sync_pb::PowerBookmarkSpecifics_PowerType_POWER_TYPE_UNSPECIFIED &&
      search_params.power_type != specifics.power_type()) {
    return false;
  }
  if (!MatchesSearchQuery(specifics, search_params.query,
                          search_params.case_sensitive)) {
    return false;
  }
  return true;
}

class SqliteDatabaseTransaction : public Transaction {
 public:
  explicit SqliteDatabaseTransaction(sql::Database& db);
  ~SqliteDatabaseTransaction() override;
  bool Begin();
  bool Commit() override;

 private:
  sql::Transaction transaction_;
  bool committed_ = false;
};

SqliteDatabaseTransaction::SqliteDatabaseTransaction(sql::Database& db)
    : transaction_(&db) {}

SqliteDatabaseTransaction::~SqliteDatabaseTransaction() {
  if (!committed_) {
    transaction_.Rollback();
  }
}

bool SqliteDatabaseTransaction::Begin() {
  return transaction_.Begin();
}

bool SqliteDatabaseTransaction::Commit() {
  DCHECK(!committed_);
  committed_ = true;
  return transaction_.Commit();
}

}  // namespace

PowerBookmarkDatabaseImpl::PowerBookmarkDatabaseImpl(
    const base::FilePath& database_dir)
    : db_(sql::DatabaseOptions().set_cache_size(128),
          /*tag=*/"PowerBookmarks"),
      database_path_(database_dir.Append(kDatabaseName)) {
  sync_db_ =
      std::make_unique<PowerBookmarkSyncMetadataDatabase>(&db_, &meta_table_);
}

PowerBookmarkDatabaseImpl::~PowerBookmarkDatabaseImpl() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

bool PowerBookmarkDatabaseImpl::Init() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (db_.is_open()) {
    return true;
  }

  // Use of Unretained is safe as sql::Database will only run the callback while
  // it's alive. As PowerBookmarkDatabaseImpl instance owns the sql::Database
  // it's guaranteed that the PowerBookmarkDatabaseImpl will be alive when the
  // callback is run.
  db_.set_error_callback(
      base::BindRepeating(&PowerBookmarkDatabaseImpl::DatabaseErrorCallback,
                          base::Unretained(this)));

  const base::FilePath dir = database_path_.DirName();
  bool dir_exists = base::DirectoryExists(dir);
  if (!dir_exists && !base::CreateDirectory(dir)) {
    DLOG(ERROR) << "Failed to create directory for power bookmarks database";
    return false;
  }

  if (!db_.Open(database_path_)) {
    DLOG(ERROR) << "Failed to open power bookmarks database: "
                << db_.GetErrorMessage();
    return false;
  }

  if (!InitSchema()) {
    DLOG(ERROR) << "Failed to create schema for power bookmarks database: "
                << db_.GetErrorMessage();
    db_.Close();
    return false;
  }

  if (!sync_db_->Init()) {
    DLOG(ERROR) << "Failed to initialize sync metadata db: "
                << db_.GetErrorMessage();
    db_.Close();
    return false;
  }

  // Directory will always exist here, but check to be safe.
  if (dir_exists) {
    int64_t file_size_bytes = base::ComputeDirectorySize(dir);
    metrics::RecordDatabaseSizeAtStartup(file_size_bytes);
  }

  return true;
}

bool PowerBookmarkDatabaseImpl::IsOpen() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  return db_.is_open();
}

void PowerBookmarkDatabaseImpl::DatabaseErrorCallback(int error,
                                                      sql::Statement* stmt) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  metrics::RecordDatabaseError(error);

  if (!sql::IsErrorCatastrophic(error))
    return;

  // Ignore repeated callbacks.
  db_.reset_error_callback();

  // After this call, the `db_` handle is poisoned so that future calls will
  // return errors until the handle is re-opened.
  db_.RazeAndPoison();
}

bool PowerBookmarkDatabaseImpl::InitSchema() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  bool has_metatable = meta_table_.DoesTableExist(&db_);
  bool has_schema =
      db_.DoesTableExist(kSaveTableName) && db_.DoesTableExist(kBlobTableName);

  if (!has_metatable && has_schema) {
    // Existing DB with no meta table. Cannot determine DB version.
    db_.Raze();
  }

  sql::Transaction transaction(&db_);
  if (!transaction.Begin()) {
    return false;
  }

  // Create the meta table if it doesn't exist.
  if (!meta_table_.Init(&db_, kCurrentVersionNumber,
                        kCompatibleVersionNumber)) {
    return false;
  }

  // If DB and meta table already existed and current version is not compatible
  // with DB then it should fail.
  if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) {
    return false;
  }
  if (!has_schema && !CreateSchema()) {
    return false;
  }

  return meta_table_.SetVersionNumber(kCurrentVersionNumber) &&
         meta_table_.SetCompatibleVersionNumber(kCompatibleVersionNumber) &&
         transaction.Commit();
}

bool PowerBookmarkDatabaseImpl::CreateSchema() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // Ensure that the schema is created atomically.
  DCHECK(db_.HasActiveTransactions());

  // `id` is the primary key of the table, corresponds to a base::Uuid.
  // `url` The URL of the target page.
  // `origin` The URL origin of the target page.
  // `power_type` The type of target this power.
  // `time_added` The date and time in seconds when the row was created.
  // `time_modified` The date and time in seconds when the row was last
  //  modified.
  static constexpr char kCreateSaveSchemaSql[] =
      // clang-format off
      "CREATE TABLE IF NOT EXISTS saves("
          "id TEXT PRIMARY KEY NOT NULL,"
          "url TEXT NOT NULL,"
          "origin TEXT NOT NULL,"
          "power_type INTEGER NOT NULL,"
          "time_added INTEGER NOT NULL,"
          "time_modified INTEGER NOT NULL)"
          "WITHOUT ROWID";
  // clang-format on
  DCHECK(db_.IsSQLValid(kCreateSaveSchemaSql));
  if (!db_.Execute(kCreateSaveSchemaSql))
    return false;

  // `id` is the primary key of the table, corresponds to a base::Uuid.
  // `specifics` The serialized specifics of the save. This is split into a
  // separate table because SQLite reads the whole row into memory when
  // querying. Having a separate table for blobs increases query performance
  // and also take take advantage of the "WITHOUT ROWID" optimization.
  static constexpr char kCreateBlobSchemaSql[] =
      // clang-format off
      "CREATE TABLE IF NOT EXISTS blobs("
          "id TEXT PRIMARY KEY NOT NULL,"
          "specifics TEXT NOT NULL)";
  // clang-format on
  DCHECK(db_.IsSQLValid(kCreateBlobSchemaSql));
  return db_.Execute(kCreateBlobSchemaSql);

  // TODO(crbug.com/40243263): Create indexes for searching capabilities.
}

std::vector<std::unique_ptr<Power>> PowerBookmarkDatabaseImpl::GetPowersForURL(
    const GURL& url,
    const sync_pb::PowerBookmarkSpecifics::PowerType& power_type) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  static constexpr char kGetPowersForURLSql[] =
      // clang-format off
      "SELECT blobs.id, blobs.specifics, saves.url "
          "FROM blobs JOIN saves ON blobs.id=saves.id "
          "WHERE (url=?) AND (power_type=? OR ?=?)";
  // clang-format on
  DCHECK(db_.IsSQLValid(kGetPowersForURLSql));

  sql::Statement statement(
      db_.GetCachedStatement(SQL_FROM_HERE, kGetPowersForURLSql));
  statement.BindString(0, url.spec());
  statement.BindInt(1, power_type);
  statement.BindInt(2, power_type);
  statement.BindInt(3, sync_pb::PowerBookmarkSpecifics::POWER_TYPE_UNSPECIFIED);

  std::vector<std::unique_ptr<Power>> powers;
  while (statement.Step()) {
    DCHECK_EQ(3, statement.ColumnCount());

    std::optional<sync_pb::PowerBookmarkSpecifics> specifics =
        DeserializeOrDelete(
            statement.ColumnStringView(1),
            base::Uuid::ParseLowercase(statement.ColumnStringView(0)));
    if (!specifics.has_value())
      continue;

    powers.emplace_back(CreatePowerFromSpecifics(specifics.value()));
  }

  return powers;
}

std::vector<std::unique_ptr<PowerOverview>>
PowerBookmarkDatabaseImpl::GetPowerOverviewsForType(
    const sync_pb::PowerBookmarkSpecifics::PowerType& power_type) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // TODO(crbug.com/40245847): Optimize this query to avoid SCAN TABLE.
  static constexpr char kGetPowerOverviewsForTypeSql[] =
      // clang-format off
      "SELECT blobs.id, blobs.specifics, COUNT(blobs.id) FROM blobs "
          "JOIN saves ON blobs.id=saves.id "
          "WHERE saves.power_type=? "
          "GROUP BY saves.url "
          "ORDER BY COUNT(saves.url) DESC, MAX(saves.time_modified)";
  // clang-format on
  DCHECK(db_.IsSQLValid(kGetPowerOverviewsForTypeSql));

  sql::Statement statement(
      db_.GetCachedStatement(SQL_FROM_HERE, kGetPowerOverviewsForTypeSql));
  statement.BindInt(0, power_type);

  std::vector<std::unique_ptr<PowerOverview>> power_overviews;
  while (statement.Step()) {
    DCHECK_EQ(3, statement.ColumnCount());

    std::optional<sync_pb::PowerBookmarkSpecifics> specifics =
        DeserializeOrDelete(
            statement.ColumnStringView(1),
            base::Uuid::ParseLowercase(statement.ColumnStringView(0)));
    if (!specifics.has_value())
      continue;

    power_overviews.emplace_back(std::make_unique<PowerOverview>(
        CreatePowerFromSpecifics(specifics.value()), statement.ColumnInt(2)));
  }

  return power_overviews;
}

std::vector<std::unique_ptr<Power>>
PowerBookmarkDatabaseImpl::GetPowersForSearchParams(
    const SearchParams& search_params) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // TODO(crbug.com/40245847): Optimize this query to avoid SCAN TABLE.
  static constexpr char kGetPowersForSearchParamsSql[] =
      // clang-format off
      "SELECT blobs.id, blobs.specifics "
          "FROM blobs JOIN saves ON blobs.id=saves.id "
          "ORDER BY url ASC";
  // clang-format on
  DCHECK(db_.IsSQLValid(kGetPowersForSearchParamsSql));

  sql::Statement statement(
      db_.GetCachedStatement(SQL_FROM_HERE, kGetPowersForSearchParamsSql));

  std::vector<std::unique_ptr<Power>> search_results;
  while (statement.Step()) {
    DCHECK_EQ(2, statement.ColumnCount());

    std::optional<sync_pb::PowerBookmarkSpecifics> specifics =
        DeserializeOrDelete(
            statement.ColumnStringView(1),
            base::Uuid::ParseLowercase(statement.ColumnStringView(0)));
    if (!specifics.has_value())
      continue;
    if (!MatchesSearchParams(specifics.value(), search_params))
      continue;

    search_results.emplace_back(CreatePowerFromSpecifics(specifics.value()));
  }

  return search_results;
}

std::vector<std::unique_ptr<PowerOverview>>
PowerBookmarkDatabaseImpl::GetPowerOverviewsForSearchParams(
    const SearchParams& search_params) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // TODO(crbug.com/40245847): Optimize this query to avoid SCAN TABLE.
  static constexpr char kGetPowerOverviewsForSearchParamsSql[] =
      // clang-format off
      "SELECT blobs.id, blobs.specifics, url, power_type "
          "FROM blobs JOIN saves ON blobs.id=saves.id "
          "ORDER BY url ASC, power_type ASC, time_modified DESC";
  // clang-format on
  DCHECK(db_.IsSQLValid(kGetPowerOverviewsForSearchParamsSql));

  sql::Statement statement(db_.GetCachedStatement(
      SQL_FROM_HERE, kGetPowerOverviewsForSearchParamsSql));

  std::vector<std::unique_ptr<PowerOverview>> search_results;
  // Between loop iterations these are the URL and PowerType of the last seen
  // power.
  std::string prev_url;
  sync_pb::PowerBookmarkSpecifics::PowerType prev_power_type =
      sync_pb::PowerBookmarkSpecifics::POWER_TYPE_UNSPECIFIED;
  // Between loop iterations this is the matched power that will be used to make
  // a PowerOverview. If set, it must match `prev_url` and `prev_power_type`.
  std::unique_ptr<Power> first_matching_power;
  // Between loop iterations this is the total count powers seen with
  // `prev_power_type` and `prev_url`.
  size_t overview_count = 0;
  while (statement.Step()) {
    DCHECK_EQ(4, statement.ColumnCount());

    std::string current_url = statement.ColumnString(2);
    sync_pb::PowerBookmarkSpecifics::PowerType current_power_type =
        (sync_pb::PowerBookmarkSpecifics::PowerType)statement.ColumnInt(3);

    if (current_url == prev_url && current_power_type == prev_power_type) {
      overview_count++;
      // Skip matching, if we already have a match for this URL and PowerType.
      if (first_matching_power) {
        continue;
      }
    } else {
      // We've scanned all powers for this URL and PowerType pair - saving the
      // matched power if we have one.
      if (first_matching_power) {
        search_results.emplace_back(std::make_unique<PowerOverview>(
            std::move(first_matching_power), overview_count));
      }
      DCHECK(!first_matching_power);
      prev_url = current_url;
      prev_power_type = current_power_type;
      overview_count = 1;
    }

    std::optional<sync_pb::PowerBookmarkSpecifics> specifics =
        DeserializeOrDelete(
            statement.ColumnStringView(1),
            base::Uuid::ParseLowercase(statement.ColumnStringView(0)));
    if (!specifics.has_value()) {
      continue;
    }
    if (!MatchesSearchParams(specifics.value(), search_params)) {
      continue;
    }

    first_matching_power = CreatePowerFromSpecifics(specifics.value());
  }

  if (first_matching_power) {
    search_results.emplace_back(std::make_unique<PowerOverview>(
        std::move(first_matching_power), overview_count));
  }
  return search_results;
}

bool PowerBookmarkDatabaseImpl::CreatePower(std::unique_ptr<Power> power) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (CheckIfPowerWithIdExists(&db_, power->guid())) {
    DLOG(ERROR)
        << "Failed to create power because the current power already exists.";
    return false;
  }

  return CreatePowerInternal(*power);
}

std::unique_ptr<Power> PowerBookmarkDatabaseImpl::UpdatePower(
    std::unique_ptr<Power> power) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  auto existing_power = GetPowerForGUID(power->guid_string());
  if (!existing_power) {
    DLOG(ERROR)
        << "Failed to update power because the current power does not exist.";
    return nullptr;
  }
  existing_power->Merge(*power);

  if (!UpdatePowerInternal(*existing_power)) {
    return nullptr;
  }

  return existing_power;
}

bool PowerBookmarkDatabaseImpl::DeletePower(const base::Uuid& guid) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!CheckIfPowerWithIdExists(&db_, guid)) {
    return true;
  }

  return DeletePowerInternal(guid);
}

bool PowerBookmarkDatabaseImpl::DeletePowersForURL(
    const GURL& url,
    const sync_pb::PowerBookmarkSpecifics::PowerType& power_type,
    std::vector<std::string>* deleted_guids) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  std::vector<std::string> guids = GetGUIDsForURL(url, power_type);
  if (guids.empty()) {
    return true;
  }

  static constexpr char kDeletePowersBlobsForURLSql[] =
      // clang-format off
      "DELETE FROM blobs WHERE id="
      "(SELECT id FROM saves WHERE url=? AND (power_type=? OR ?=?))";
  // clang-format on
  DCHECK(db_.IsSQLValid(kDeletePowersBlobsForURLSql));

  sql::Statement blob_statement(
      db_.GetCachedStatement(SQL_FROM_HERE, kDeletePowersBlobsForURLSql));
  blob_statement.BindString(0, url.spec());
  blob_statement.BindInt(1, power_type);
  blob_statement.BindInt(2, power_type);
  blob_statement.BindInt(
      3, sync_pb::PowerBookmarkSpecifics::POWER_TYPE_UNSPECIFIED);

  if (!blob_statement.Run())
    return false;

  static constexpr char kDeletePowersSavesForURLSql[] =
      // clang-format off
      "DELETE FROM saves WHERE url=? AND (power_type=? OR ?=?)";
  // clang-format on
  DCHECK(db_.IsSQLValid(kDeletePowersSavesForURLSql));

  sql::Statement save_statement(
      db_.GetCachedStatement(SQL_FROM_HERE, kDeletePowersSavesForURLSql));
  save_statement.BindString(0, url.spec());
  save_statement.BindInt(1, power_type);
  save_statement.BindInt(2, power_type);
  save_statement.BindInt(
      3, sync_pb::PowerBookmarkSpecifics::POWER_TYPE_UNSPECIFIED);
  if (!save_statement.Run())
    return false;

  if (deleted_guids) {
    for (const auto& guid : guids) {
      deleted_guids->push_back(guid);
    }
  }
  return true;
}

std::vector<std::unique_ptr<Power>>
PowerBookmarkDatabaseImpl::GetPowersForGUIDs(
    const std::vector<std::string>& guids) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  for (auto& guid : guids) {
    DCHECK(base::Uuid::ParseCaseInsensitive(guid).is_valid());
  }
  const std::string query =
      base::StrCat({"SELECT blobs.id, blobs.specifics, saves.url "
                    "FROM blobs JOIN saves ON blobs.id=saves.id "
                    "WHERE saves.id IN ('",
                    base::JoinString(guids, "','"), "')"});
  db_.IsSQLValid(query);
  sql::Statement statement(db_.GetUniqueStatement(query));
  std::vector<std::unique_ptr<Power>> powers;
  while (statement.Step()) {
    DCHECK_EQ(3, statement.ColumnCount());

    std::optional<sync_pb::PowerBookmarkSpecifics> specifics =
        DeserializeOrDelete(
            statement.ColumnStringView(1),
            base::Uuid::ParseLowercase(statement.ColumnStringView(0)));
    if (!specifics.has_value())
      continue;

    powers.emplace_back(CreatePowerFromSpecifics(specifics.value()));
  }

  return powers;
}

std::vector<std::unique_ptr<Power>> PowerBookmarkDatabaseImpl::GetAllPowers() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  static constexpr char kGetPowersSql[] =
      // clang-format off
      "SELECT blobs.id, blobs.specifics, saves.url "
          "FROM blobs JOIN saves ON blobs.id=saves.id";
  // clang-format on
  DCHECK(db_.IsSQLValid(kGetPowersSql));

  sql::Statement statement(
      db_.GetCachedStatement(SQL_FROM_HERE, kGetPowersSql));
  std::vector<std::unique_ptr<Power>> powers;
  while (statement.Step()) {
    DCHECK_EQ(3, statement.ColumnCount());

    std::optional<sync_pb::PowerBookmarkSpecifics> specifics =
        DeserializeOrDelete(
            statement.ColumnStringView(1),
            base::Uuid::ParseLowercase(statement.ColumnStringView(0)));
    if (!specifics.has_value())
      continue;

    powers.emplace_back(CreatePowerFromSpecifics(specifics.value()));
  }

  return powers;
}

std::unique_ptr<Power> PowerBookmarkDatabaseImpl::GetPowerForGUID(
    const std::string& guid) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  DCHECK(base::Uuid::ParseCaseInsensitive(guid).is_valid());
  static constexpr char kGetPowerForGUIDSql[] =
      // clang-format off
      "SELECT blobs.id, blobs.specifics, saves.url "
          "FROM blobs JOIN saves ON blobs.id=saves.id "
          "WHERE saves.id=?";
  // clang-format on
  DCHECK(db_.IsSQLValid(kGetPowerForGUIDSql));

  sql::Statement statement(
      db_.GetCachedStatement(SQL_FROM_HERE, kGetPowerForGUIDSql));
  statement.BindString(0, guid);
  std::vector<std::unique_ptr<Power>> powers;
  while (statement.Step()) {
    DCHECK_EQ(3, statement.ColumnCount());

    std::optional<sync_pb::PowerBookmarkSpecifics> specifics =
        DeserializeOrDelete(
            statement.ColumnStringView(1),
            base::Uuid::ParseLowercase(statement.ColumnStringView(0)));
    if (!specifics.has_value())
      continue;

    return CreatePowerFromSpecifics(specifics.value());
  }

  return nullptr;
}

bool PowerBookmarkDatabaseImpl::CreateOrMergePowerFromSync(const Power& power) {
  auto existing_power = GetPowerForGUID(power.guid_string());
  if (existing_power) {
    // The merged data is not sent back to sync in order to prevent sending data
    // back and forth between two clients. Usually sync data from the server
    // should override local data, which make it unnecessary to send data back.
    existing_power->Merge(power);
    return UpdatePowerInternal(*existing_power);
  } else {
    return CreatePowerInternal(power);
  }
}

bool PowerBookmarkDatabaseImpl::DeletePowerFromSync(const std::string& guid) {
  return DeletePower(base::Uuid::ParseLowercase(guid));
}

PowerBookmarkSyncMetadataDatabase*
PowerBookmarkDatabaseImpl::GetSyncMetadataDatabase() {
  return sync_db_.get();
}

std::optional<sync_pb::PowerBookmarkSpecifics>
PowerBookmarkDatabaseImpl::DeserializeOrDelete(std::string_view data,
                                               const base::Uuid& id) {
  sync_pb::PowerBookmarkSpecifics specifics;
  bool parse_success = specifics.ParseFromString(data);

  if (parse_success)
    return specifics;

  bool delete_success = DeletePower(id);
  DCHECK(delete_success);
  return std::nullopt;
}

std::vector<std::string> PowerBookmarkDatabaseImpl::GetGUIDsForURL(
    const GURL& url,
    const sync_pb::PowerBookmarkSpecifics::PowerType& power_type) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  static constexpr char kGetGUIDsForURLSql[] =
      // clang-format off
      "SELECT id FROM saves WHERE (url=?) AND (power_type=? OR ?=?)";
  // clang-format on
  DCHECK(db_.IsSQLValid(kGetGUIDsForURLSql));

  sql::Statement statement(
      db_.GetCachedStatement(SQL_FROM_HERE, kGetGUIDsForURLSql));
  statement.BindString(0, url.spec());
  statement.BindInt(1, power_type);
  statement.BindInt(2, power_type);
  statement.BindInt(3, sync_pb::PowerBookmarkSpecifics::POWER_TYPE_UNSPECIFIED);

  std::vector<std::string> guids;
  while (statement.Step()) {
    DCHECK_EQ(1, statement.ColumnCount());
    guids.push_back(statement.ColumnString(0));
  }

  return guids;
}

bool PowerBookmarkDatabaseImpl::CreatePowerInternal(const Power& power) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  static constexpr char kCreatePowerSaveSql[] =
      // clang-format off
      "INSERT INTO saves("
          "id, url, origin, power_type, "
          "time_added, time_modified)"
          "VALUES(?,?,?,?,?,?)";
  // clang-format on
  DCHECK(db_.IsSQLValid(kCreatePowerSaveSql));

  sql::Statement save_statement(
      db_.GetCachedStatement(SQL_FROM_HERE, kCreatePowerSaveSql));
  save_statement.BindString(0, power.guid_string());
  save_statement.BindString(1, power.url().spec());
  save_statement.BindString(2, url::Origin::Create(power.url()).Serialize());
  save_statement.BindInt(3, power.power_type());
  save_statement.BindTime(4, power.time_added());
  save_statement.BindTime(5, power.time_modified());
  if (!save_statement.Run()) {
    return false;
  }

  static constexpr char kCreatePowerBlobSql[] =
      // clang-format off
      "INSERT INTO blobs(id, specifics) VALUES(?, ?)";
  // clang-format on
  DCHECK(db_.IsSQLValid(kCreatePowerBlobSql));

  sql::Statement blob_statement(
      db_.GetCachedStatement(SQL_FROM_HERE, kCreatePowerBlobSql));
  blob_statement.BindString(0, power.guid_string());

  std::string data;
  sync_pb::PowerBookmarkSpecifics specifics;
  power.ToPowerBookmarkSpecifics(&specifics);
  specifics.SerializeToString(&data);
  blob_statement.BindString(1, data);
  if (!blob_statement.Run()) {
    return false;
  }

  return true;
}

bool PowerBookmarkDatabaseImpl::UpdatePowerInternal(const Power& power) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  static constexpr char kUpdatePowerSaveSql[] =
      // clang-format off
      "UPDATE saves SET "
          "url=?, origin=?, power_type=?, time_added=?, "
          "time_modified=?"
          "WHERE id=?";
  // clang-format on
  DCHECK(db_.IsSQLValid(kUpdatePowerSaveSql));

  sql::Statement save_statement(
      db_.GetCachedStatement(SQL_FROM_HERE, kUpdatePowerSaveSql));

  save_statement.BindString(0, power.url().spec());
  save_statement.BindString(1, url::Origin::Create(power.url()).Serialize());
  save_statement.BindInt(2, power.power_type());
  save_statement.BindTime(3, power.time_added());
  save_statement.BindTime(4, power.time_modified());
  if (!save_statement.Run()) {
    return false;
  }

  static constexpr char kUpdatePowerBlobSql[] =
      // clang-format off
      "UPDATE blobs SET specifics=? WHERE id=?";
  // clang-format on
  DCHECK(db_.IsSQLValid(kUpdatePowerBlobSql));

  sql::Statement blob_statement(
      db_.GetCachedStatement(SQL_FROM_HERE, kUpdatePowerBlobSql));

  std::string data;
  sync_pb::PowerBookmarkSpecifics specifics;
  power.ToPowerBookmarkSpecifics(&specifics);
  bool success = specifics.SerializeToString(&data);
  DCHECK(success);
  blob_statement.BindBlob(0, std::move(data));
  blob_statement.BindString(1, power.guid_string());
  if (!blob_statement.Run()) {
    return false;
  }

  return true;
}

bool PowerBookmarkDatabaseImpl::DeletePowerInternal(const base::Uuid& guid) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  static constexpr char kDeletePowerSaveSql[] =
      // clang-format off
      "DELETE FROM saves WHERE id=?";
  // clang-format on
  DCHECK(db_.IsSQLValid(kDeletePowerSaveSql));

  sql::Statement save_statement(
      db_.GetCachedStatement(SQL_FROM_HERE, kDeletePowerSaveSql));
  save_statement.BindString(0, guid.AsLowercaseString());
  if (!save_statement.Run()) {
    return false;
  }

  static constexpr char kDeletePowerBlobSql[] =
      // clang-format off
      "DELETE FROM blobs WHERE id=?";
  // clang-format on
  DCHECK(db_.IsSQLValid(kDeletePowerBlobSql));

  sql::Statement blob_statement(
      db_.GetCachedStatement(SQL_FROM_HERE, kDeletePowerBlobSql));
  blob_statement.BindString(0, guid.AsLowercaseString());
  if (!blob_statement.Run()) {
    return false;
  }

  return true;
}

std::unique_ptr<Transaction> PowerBookmarkDatabaseImpl::BeginTransaction() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  auto transaction = std::make_unique<SqliteDatabaseTransaction>(db_);
  if (transaction->Begin()) {
    return transaction;
  } else {
    return nullptr;
  }
}

}  // namespace power_bookmarks