Skip to content

Commit

Permalink
Adds custom loader for the panel
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Dec 5, 2018
1 parent 4b9edf6 commit 2b4a126
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 36 deletions.
49 changes: 47 additions & 2 deletions components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,50 @@ PublisherInfoDatabase::GetPublisherInfo(const std::string& publisher_key) {
return nullptr;
}

std::unique_ptr<ledger::PublisherInfo>
PublisherInfoDatabase::GetPanelPublisher(
const ledger::ActivityInfoFilter& filter) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

bool initialized = Init();
DCHECK(initialized);

if (!initialized) {
return nullptr;
}

sql::Statement info_sql(db_.GetUniqueStatement(
"SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, pi.provider, "
"pi.verified, pi.excluded, ai.percent FROM publisher_info AS pi "
"LEFT JOIN activity_info AS ai ON pi.publisher_id = ai.publisher_id "
"WHERE pi.publisher_id=? AND ((ai.month = ? "
"AND ai.year = ? AND ai.reconcile_stamp = ?) OR "
"ai.percent IS NULL) LIMIT 1"));

info_sql.BindString(0, filter.id);
info_sql.BindInt(1, filter.month);
info_sql.BindInt(2, filter.year);
info_sql.BindInt64(3, filter.reconcile_stamp);

if (info_sql.Step()) {
std::unique_ptr<ledger::PublisherInfo> info;
info.reset(new ledger::PublisherInfo());
info->id = info_sql.ColumnString(0);
info->name = info_sql.ColumnString(1);
info->url = info_sql.ColumnString(2);
info->favicon_url = info_sql.ColumnString(3);
info->provider = info_sql.ColumnString(4);
info->verified = info_sql.ColumnBool(5);
info->excluded = static_cast<ledger::PUBLISHER_EXCLUDE>(
info_sql.ColumnInt(6));
info->percent = info_sql.ColumnInt(7);

return info;
}

return nullptr;
}

bool PublisherInfoDatabase::RestorePublishers() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

Expand Down Expand Up @@ -608,8 +652,7 @@ PublisherInfoDatabase::GetMediaPublisherInfo(const std::string& media_key) {
info_sql.BindString(0, media_key);

if (info_sql.Step()) {
std::unique_ptr<ledger::PublisherInfo> info;
info.reset(new ledger::PublisherInfo());
std::unique_ptr<ledger::PublisherInfo> info(new ledger::PublisherInfo());
info->id = info_sql.ColumnString(0);
info->name = info_sql.ColumnString(1);
info->url = info_sql.ColumnString(2);
Expand All @@ -618,6 +661,8 @@ PublisherInfoDatabase::GetMediaPublisherInfo(const std::string& media_key) {
info->verified = info_sql.ColumnBool(5);
info->excluded = static_cast<ledger::PUBLISHER_EXCLUDE>(
info_sql.ColumnInt(6));

return info;
}

return nullptr;
Expand Down
9 changes: 6 additions & 3 deletions components/brave_rewards/browser/publisher_info_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ class PublisherInfoDatabase {
std::unique_ptr<ledger::PublisherInfo> GetPublisherInfo(
const std::string& media_key);

std::unique_ptr<ledger::PublisherInfo> GetPanelPublisher(
const ledger::ActivityInfoFilter& filter);

bool RestorePublishers();

bool InsertOrUpdateActivityInfo(const ledger::PublisherInfo& info);

bool GetActivityList(int start,
int limit,
const ledger::ActivityInfoFilter& filter,
ledger::PublisherInfoList* list);
int limit,
const ledger::ActivityInfoFilter& filter,
ledger::PublisherInfoList* list);

bool InsertOrUpdateMediaPublisherInfo(const std::string& media_key,
const std::string& publisher_id);
Expand Down
37 changes: 36 additions & 1 deletion components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,17 @@ ledger::PublisherInfoList GetActivityListOnFileTaskRunner(
return list;
}

std::unique_ptr<ledger::PublisherInfo> GetPanelPublisherInfoOnFileTaskRunner(
ledger::ActivityInfoFilter filter,
PublisherInfoDatabase* backend) {
ledger::PublisherInfoList list;
if (!backend) {
return nullptr;
}

return backend->GetPanelPublisher(filter);
}

// `callback` has a WeakPtr so this won't crash if the file finishes
// writing after RewardsServiceImpl has been destroyed
void PostWriteCallback(
Expand Down Expand Up @@ -860,6 +871,30 @@ void RewardsServiceImpl::OnActivityInfoLoaded(
std::make_unique<ledger::PublisherInfo>(list[0]));
}

void RewardsServiceImpl::LoadPanelPublisherInfo(
ledger::ActivityInfoFilter filter,
ledger::PublisherInfoCallback callback) {
base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE,
base::Bind(&GetPanelPublisherInfoOnFileTaskRunner,
filter,
publisher_info_backend_.get()),
base::Bind(&RewardsServiceImpl::OnPanelPublisherInfoLoaded,
AsWeakPtr(),
callback));
}

void RewardsServiceImpl::OnPanelPublisherInfoLoaded(
ledger::PublisherInfoCallback callback,
std::unique_ptr<ledger::PublisherInfo> publisher_info) {
if (!publisher_info) {
callback(ledger::Result::NOT_FOUND,
std::unique_ptr<ledger::PublisherInfo>());
return;
}

callback(ledger::Result::LEDGER_OK, std::move(publisher_info));
}

void RewardsServiceImpl::LoadActivityInfoList(
uint32_t start,
uint32_t limit,
Expand Down Expand Up @@ -1804,7 +1839,7 @@ void RewardsServiceImpl::OnDonate(

ledger::PublisherInfo info;
info.id = publisher_key;
info.month = ledger::PUBLISHER_MONTH::ANY;
info.month = ledger::ACTIVITY_MONTH::ANY;
info.year = -1;
info.verified = site->verified;
info.excluded = ledger::PUBLISHER_EXCLUDE::DEFAULT;
Expand Down
5 changes: 5 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ class RewardsServiceImpl : public RewardsService,
ledger::PublisherInfoCallback callback) override;
void LoadActivityInfo(ledger::ActivityInfoFilter filter,
ledger::PublisherInfoCallback callback) override;
void LoadPanelPublisherInfo(ledger::ActivityInfoFilter filter,
ledger::PublisherInfoCallback callback) override;
void LoadActivityInfoList(
uint32_t start,
uint32_t limit,
Expand Down Expand Up @@ -293,6 +295,9 @@ class RewardsServiceImpl : public RewardsService,
int line,
const ledger::LogLevel log_level) const override;
void OnRestorePublishers(ledger::OnRestoreCallback callback) override;
void OnPanelPublisherInfoLoaded(
ledger::PublisherInfoCallback callback,
std::unique_ptr<ledger::PublisherInfo> publisher_info);

void OnIOTaskComplete(std::function<void(void)> callback);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,37 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a
}
break
case types.ON_TAB_RETRIEVED:
const tab: chrome.tabs.Tab = payload.tab
if (
!tab ||
!tab.url ||
tab.incognito ||
!tab.active ||
!state.walletCreated
) {
break
}
{
const tab: chrome.tabs.Tab = payload.tab
if (
!tab ||
!tab.url ||
tab.incognito ||
!tab.active ||
!state.walletCreated
) {
break
}

chrome.braveRewards.getPublisherData(tab.windowId, tab.url, tab.favIconUrl || '')
const id = getWindowId(tab.windowId)
let publishers: Record<string, RewardsExtension.Publisher> = state.publishers
if (publishers[id]) {
delete publishers[id]
}
state = {
...state,
publishers
const id = getWindowId(tab.windowId)
const publishers: Record<string, RewardsExtension.Publisher> = state.publishers
const publisher = publishers[id]
chrome.braveRewards.getPublisherData(tab.windowId, tab.url, tab.favIconUrl || '')
if (!publisher || (publisher && publisher.tabUrl !== tab.url)) {
if (publisher) {
delete publishers[id]
}

publishers[id] = {
tabUrl: tab.url
}
}
state = {
...state,
publishers
}
break
}
break
case types.ON_PUBLISHER_DATA:
{
const publisher = payload.publisher
Expand All @@ -91,7 +100,7 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a
if (publisher && !publisher.publisher_key) {
delete publishers[id]
} else {
publishers[id] = payload.publisher
publishers[id] = { ...publishers[id], ...payload.publisher }
}

state = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ export class Panel extends React.Component<Props, State> {
return
}
const tabId = tabs[0].id
if (tabId === undefined || !publisher) {
if (tabId === undefined || !publisher || !publisher.publisher_key) {
return
}

chrome.braveRewards.donateToSite(tabId, publisher.publisher_key)
window.close()
})
Expand Down
17 changes: 9 additions & 8 deletions components/definitions/rewardsExtensions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ declare namespace RewardsExtension {
}

interface Publisher {
excluded: boolean
favicon_url: string
publisher_key: string
name: string
percentage: number
provider: string
url: string
verified: boolean
excluded?: boolean
favicon_url?: string
publisher_key?: string
name?: string
percentage?: number
provider?: string
tabUrl?: string
url?: string
verified?: boolean
}

export interface Grant {
Expand Down

0 comments on commit 2b4a126

Please sign in to comment.