1 // Copyright 2019 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.
4 
5 #include "chrome/browser/media/history/media_history_table_base.h"
6 
7 #include "base/updateable_sequenced_task_runner.h"
8 #include "sql/statement.h"
9 #include "third_party/protobuf/src/google/protobuf/message_lite.h"
10 
11 namespace media_history {
12 
GetTaskRunner()13 base::UpdateableSequencedTaskRunner* MediaHistoryTableBase::GetTaskRunner() {
14   return db_task_runner_.get();
15 }
16 
MediaHistoryTableBase(scoped_refptr<base::UpdateableSequencedTaskRunner> db_task_runner)17 MediaHistoryTableBase::MediaHistoryTableBase(
18     scoped_refptr<base::UpdateableSequencedTaskRunner> db_task_runner)
19     : db_task_runner_(std::move(db_task_runner)), db_(nullptr) {}
20 
21 MediaHistoryTableBase::~MediaHistoryTableBase() = default;
22 
SetCancelled()23 void MediaHistoryTableBase::SetCancelled() {
24   cancelled_.Set();
25 }
26 
Initialize(sql::Database * db)27 sql::InitStatus MediaHistoryTableBase::Initialize(sql::Database* db) {
28   DCHECK(db_task_runner_->RunsTasksInCurrentSequence());
29   DCHECK(db);
30 
31   db_ = db;
32 
33   if (CanAccessDatabase())
34     return CreateTableIfNonExistent();
35   return sql::InitStatus::INIT_FAILURE;
36 }
37 
DB()38 sql::Database* MediaHistoryTableBase::DB() {
39   DCHECK(db_task_runner_->RunsTasksInCurrentSequence());
40   return db_;
41 }
42 
ResetDB()43 void MediaHistoryTableBase::ResetDB() {
44   DCHECK(db_task_runner_->RunsTasksInCurrentSequence());
45   db_ = nullptr;
46 }
47 
CanAccessDatabase()48 bool MediaHistoryTableBase::CanAccessDatabase() {
49   DCHECK(db_task_runner_->RunsTasksInCurrentSequence());
50   return !cancelled_.IsSet() && db_;
51 }
52 
BindProto(sql::Statement & s,int col,const google::protobuf::MessageLite & protobuf)53 void MediaHistoryTableBase::BindProto(
54     sql::Statement& s,
55     int col,
56     const google::protobuf::MessageLite& protobuf) {
57   std::string out;
58   CHECK(protobuf.SerializeToString(&out));
59   s.BindBlob(col, out.data(), out.size());
60 }
61 
GetProto(sql::Statement & s,int col,google::protobuf::MessageLite & protobuf)62 bool MediaHistoryTableBase::GetProto(sql::Statement& s,
63                                      int col,
64                                      google::protobuf::MessageLite& protobuf) {
65   std::string value;
66   s.ColumnBlobAsString(col, &value);
67   return protobuf.ParseFromString(value);
68 }
69 
DeleteURL(const GURL & url)70 bool MediaHistoryTableBase::DeleteURL(const GURL& url) {
71   NOTREACHED();
72   return false;
73 }
74 
75 }  // namespace media_history
76