Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moves media publisher info into separate file #4100

Merged
merged 1 commit into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ source_set("browser") {
"database/database_contribution_queue.h",
"database/database_contribution_queue_publishers.cc",
"database/database_contribution_queue_publishers.h",
"database/database_media_publisher_info.cc",
"database/database_media_publisher_info.h",
"database/database_pending_contribution.cc",
"database/database_pending_contribution.h",
"database/database_promotion.cc",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <string>
#include <utility>

#include "base/bind.h"
#include "base/strings/stringprintf.h"
#include "brave/components/brave_rewards/browser/database/database_media_publisher_info.h"
#include "sql/statement.h"
#include "sql/transaction.h"

namespace brave_rewards {

namespace {
const char* table_name_ = "media_publisher_info";
NejcZdovc marked this conversation as resolved.
Show resolved Hide resolved
const int minimum_version_ = 1;
} // namespace

DatabaseMediaPublisherInfo::DatabaseMediaPublisherInfo(
int current_db_version) :
DatabaseTable(current_db_version) {
}

DatabaseMediaPublisherInfo::~DatabaseMediaPublisherInfo() = default;

bool DatabaseMediaPublisherInfo::Init(sql::Database* db) {
if (GetCurrentDBVersion() < minimum_version_) {
return true;
}

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

bool success = CreateTable(db);
if (!success) {
return false;
}

success = CreateIndex(db);
if (!success) {
return false;
}

return transaction.Commit();
}

bool DatabaseMediaPublisherInfo::CreateTable(sql::Database* db) {
if (db->DoesTableExist(table_name_)) {
return true;
}

return CreateTableV1(db);
}

bool DatabaseMediaPublisherInfo::CreateTableV1(sql::Database* db) {
const std::string query = base::StringPrintf(
"CREATE TABLE %s ("
"media_key TEXT NOT NULL PRIMARY KEY UNIQUE,"
"publisher_id LONGVARCHAR NOT NULL,"
"CONSTRAINT fk_%s_publisher_id"
" FOREIGN KEY (publisher_id)"
" REFERENCES publisher_info (publisher_id)"
" ON DELETE CASCADE"
")",
table_name_,
table_name_);

return db->Execute(query.c_str());
}

bool DatabaseMediaPublisherInfo::CreateIndex(sql::Database* db) {
return true;
}

bool DatabaseMediaPublisherInfo::InsertOrUpdate(
sql::Database* db,
const std::string& media_key,
const std::string& publisher_key) {
if (media_key.empty() || publisher_key.empty()) {
return false;
}

const std::string query = base::StringPrintf(
"INSERT OR REPLACE INTO %s (media_key, publisher_id) VALUES (?, ?)",
table_name_);

sql::Statement statement(
db->GetCachedStatement(SQL_FROM_HERE, query.c_str()));

statement.BindString(0, media_key);
statement.BindString(1, publisher_key);

return statement.Run();
}

ledger::PublisherInfoPtr DatabaseMediaPublisherInfo::GetRecord(
sql::Database* db,
const std::string& media_key) {
if (media_key.empty()) {
return nullptr;
}

const std::string query = base::StringPrintf(
"SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, "
"pi.provider, spi.status, pi.excluded "
"FROM %s as mpi "
"INNER JOIN publisher_info AS pi ON mpi.publisher_id = pi.publisher_id "
"LEFT JOIN server_publisher_info AS spi "
"ON spi.publisher_key = pi.publisher_id "
"WHERE mpi.media_key=?",
table_name_);

sql::Statement statement(db->GetUniqueStatement(query.c_str()));

statement.BindString(0, media_key);

if (!statement.Step()) {
return nullptr;
}

auto info = ledger::PublisherInfo::New();
info->id = statement.ColumnString(0);
info->name = statement.ColumnString(1);
info->url = statement.ColumnString(2);
info->favicon_url = statement.ColumnString(3);
info->provider = statement.ColumnString(4);
info->status =
static_cast<ledger::mojom::PublisherStatus>(statement.ColumnInt64(5));
info->excluded = static_cast<ledger::PublisherExclude>(
statement.ColumnInt(6));

return info;
}

} // namespace brave_rewards
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_DATABASE_DATABASE_MEDIA_PUBLISHER_INFO_H_
#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_DATABASE_DATABASE_MEDIA_PUBLISHER_INFO_H_

#include <string>

#include "bat/ledger/mojom_structs.h"
#include "brave/components/brave_rewards/browser/database/database_table.h"

namespace brave_rewards {

class DatabaseMediaPublisherInfo: public DatabaseTable {
public:
explicit DatabaseMediaPublisherInfo(int current_db_version);

~DatabaseMediaPublisherInfo() override;

bool Init(sql::Database* db) override;

bool CreateTable(sql::Database* db) override;

bool CreateIndex(sql::Database* db) override;

bool InsertOrUpdate(
sql::Database* db,
const std::string& media_key,
const std::string& publisher_key);

ledger::PublisherInfoPtr GetRecord(
sql::Database* db,
const std::string& media_key);

private:
bool CreateTableV1(sql::Database* db);
};

} // namespace brave_rewards

#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_DATABASE_DATABASE_MEDIA_PUBLISHER_INFO_H_
Loading