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

Add rewards page initialization check #6323

Merged
merged 2 commits into from
Aug 4, 2020
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
17 changes: 17 additions & 0 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void RegisterMessages() override;

private:
void IsInitialized(const base::ListValue* args);
void HandleCreateWalletRequested(const base::ListValue* args);
void GetRewardsParameters(const base::ListValue* args);
void GetAutoContributeProperties(const base::ListValue* args);
Expand Down Expand Up @@ -339,6 +340,9 @@ void RewardsDOMHandler::RegisterMessages() {
profile, chrome::FaviconUrlFormat::kFaviconLegacy));
#endif

web_ui()->RegisterMessageCallback("brave_rewards.isInitialized",
base::BindRepeating(&RewardsDOMHandler::IsInitialized,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.createWalletRequested",
base::BindRepeating(&RewardsDOMHandler::HandleCreateWalletRequested,
base::Unretained(this)));
Expand Down Expand Up @@ -501,6 +505,19 @@ void RewardsDOMHandler::Init() {
rewards_service_->AddObserver(this);
}

void RewardsDOMHandler::IsInitialized(
const base::ListValue* args) {
if (!web_ui()->CanCallJavascript()) {
return;
}

if (rewards_service_ && rewards_service_->IsInitialized()) {
web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.initialized",
base::Value(0));
}
}

void RewardsDOMHandler::HandleCreateWalletRequested(
const base::ListValue* args) {
if (!rewards_service_)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MockRewardsService : public RewardsService {
MockRewardsService() {}
~MockRewardsService() {}

MOCK_METHOD0(IsInitialized, bool());
MOCK_METHOD1(CreateWallet, void(brave_rewards::CreateWalletCallback));
MOCK_METHOD1(
GetRewardsParameters,
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ class RewardsService : public KeyedService {
RewardsService();
~RewardsService() override;

virtual bool IsInitialized() = 0;

virtual void CreateWallet(CreateWalletCallback callback) = 0;
virtual void GetRewardsParameters(GetRewardsParametersCallback callback) = 0;
virtual void GetContentSiteList(
Expand Down
4 changes: 4 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ void RewardsServiceImpl::ConnectionClosed() {
base::TimeDelta::FromSeconds(1));
}

bool RewardsServiceImpl::IsInitialized() {
return Connected() && ready_->is_signaled();
}

void RewardsServiceImpl::Init(
std::unique_ptr<RewardsServiceObserver> extension_observer,
std::unique_ptr<RewardsServicePrivateObserver> private_observer,
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class RewardsServiceImpl : public RewardsService,
// KeyedService:
void Shutdown() override;

bool IsInitialized() override;

void Init(
std::unique_ptr<RewardsServiceObserver> extension_observer,
std::unique_ptr<RewardsServicePrivateObserver> private_observer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { action } from 'typesafe-actions'
// Constant
import { types } from '../constants/rewards_types'

export const isInitialized = () => action(types.IS_INITIALIZED)

export const createWallet = () => action(types.CREATE_WALLET)

export const onWalletCreated = () => action(types.WALLET_CREATED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export class App extends React.Component<Props, State> {
}

componentDidMount () {
this.actions.isInitialized()

if (!this.props.rewardsData.walletCreated) {
this.actions.checkWalletExistence()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

export const enum types {
IS_INITIALIZED = '@@rewards/IS_INITIALIZED',
CREATE_WALLET = '@@rewards/CREATE_WALLET',
WALLET_CREATED = '@@rewards/WALLET_CREATED',
WALLET_CREATE_FAILED = '@@rewards/WALLET_CREATE_FAILED',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { defaultState } from '../storage'

const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State, action) => {
switch (action.type) {
case types.IS_INITIALIZED: {
chrome.send('brave_rewards.isInitialized')
break
}
case types.TOGGLE_ENABLE_MAIN: {
if (state.initializing) {
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const defaultState: Rewards.State = {
autoContributeChoices: [],
rate: 0
},
initializing: false
initializing: true
}

const cleanData = (state: Rewards.State) => {
Expand All @@ -100,6 +100,7 @@ export const load = (): Rewards.State => {
if (data) {
try {
state = JSON.parse(data)
state.initializing = true
} catch (e) {
console.error('Could not parse local storage data: ', e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { action } from 'typesafe-actions'
// Constant
import { types } from '../constants/rewards_types'

export const isInitialized = () => action(types.IS_INITIALIZED)

export const createWallet = () => action(types.CREATE_WALLET)

export const onWalletCreated = () => action(types.WALLET_CREATED)
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/resources/page/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export class App extends React.Component<Props, State> {
}

componentDidMount () {
this.actions.isInitialized()

if (!this.props.rewardsData.walletCreated) {
this.actions.checkWalletExistence()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

export const enum types {
IS_INITIALIZED = '@@rewards/IS_INITIALIZED',
CREATE_WALLET = '@@rewards/CREATE_WALLET',
WALLET_CREATED = '@@rewards/WALLET_CREATED',
WALLET_CREATE_FAILED = '@@rewards/WALLET_CREATE_FAILED',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State
}

switch (action.type) {
case types.IS_INITIALIZED: {
chrome.send('brave_rewards.isInitialized')
break
}
case types.TOGGLE_ENABLE_MAIN: {
if (state.initializing) {
break
Expand Down
3 changes: 2 additions & 1 deletion components/brave_rewards/resources/page/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const defaultState: Rewards.State = {
autoContributeChoices: [],
rate: 0
},
initializing: false
initializing: true
}

const cleanData = (state: Rewards.State) => {
Expand All @@ -102,6 +102,7 @@ export const load = (): Rewards.State => {
if (data) {
try {
state = JSON.parse(data)
state.initializing = true
} catch (e) {
console.error('Could not parse local storage data: ', e)
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@
"@types/storybook__react": "^4.0.2",
"acorn": "^7.1.1",
"babel-loader": "^8.1.0",
"brave-ui": "github:brave/brave-ui#303a9d6424950a31836902f9bf2c03a0c174efda",
"brave-ui": "github:brave/brave-ui#36f3cd135399060641123136feb8253e53d68499",
"chalk": "^2.4.2",
"commander": "^2.9.0",
"css-loader": "^2.1.1",
Expand Down