diff --git a/DEPS b/DEPS index 871e8e26fb1d..7d656efbfee6 100644 --- a/DEPS +++ b/DEPS @@ -10,6 +10,11 @@ deps = { "vendor/boto": "https://github.com/boto/boto@f7574aa6cc2c819430c1f05e9a1a1a666ef8169b", "vendor/python-patch": "https://github.com/svn2github/python-patch@a336a458016ced89aba90dfc3f4c8222ae3b1403", "vendor/sparkle": "https://github.com/brave/Sparkle.git@c0759cce415d7c0feae45005c8a013b1898711f0", + "vendor/bat-native-ledger": "https://github.com/brave-intl/bat-native-ledger@2f1f6480cfa618c6dc2209dbbbfdece1ae8dd377", + "vendor/bat-native-rapidjson": "https://github.com/brave-intl/bat-native-rapidjson.git@744b43313525a047eda4f2e2e689aa88b6c596fa", + "vendor/bip39wally-core-native": "https://github.com/brave-intl/bip39wally-core-native.git@e5aba371a56d3e41f7e80e868312446ce7bd434c", + "vendor/bat-native-anonize": "https://github.com/brave-intl/bat-native-anonize.git@8f60c03329f475959b44cd22838284e19508ec80", + "vendor/bat-native-tweetnacl": "https://github.com/brave-intl/bat-native-tweetnacl.git@d61f0cdc88dd2c4320176d2c514b8dd8dd1f22c2", } hooks = [ diff --git a/browser/BUILD.gn b/browser/BUILD.gn index 3d7a3e98df5a..7bda0719490a 100644 --- a/browser/BUILD.gn +++ b/browser/BUILD.gn @@ -20,6 +20,8 @@ source_set("browser_process") { "brave_stats_updater_params.h", "brave_tab_helpers.cc", "brave_tab_helpers.h", + "browser_context_keyed_service_factories.cc", + "browser_context_keyed_service_factories.h", "component_updater/brave_component_installer.cc", "component_updater/brave_component_installer.h", "component_updater/brave_component_updater_configurator.cc", @@ -42,6 +44,7 @@ source_set("browser_process") { ] deps = [ + "//brave/browser/payments", "//brave/components/brave_shields/browser:brave_shields", "//brave/components/content_settings/core/browser", "//brave/common", diff --git a/browser/browser_context_keyed_service_factories.cc b/browser/browser_context_keyed_service_factories.cc new file mode 100644 index 000000000000..12eeb64bc958 --- /dev/null +++ b/browser/browser_context_keyed_service_factories.cc @@ -0,0 +1,15 @@ +/* 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 "brave/browser/browser_context_keyed_service_factories.h" + +#include "brave/browser/payments/payments_service_factory.h" + +namespace brave { + +void EnsureBrowserContextKeyedServiceFactoriesBuilt() { + PaymentsServiceFactory::GetInstance(); +} + +} // namespace brave diff --git a/browser/browser_context_keyed_service_factories.h b/browser/browser_context_keyed_service_factories.h new file mode 100644 index 000000000000..68a283540b0d --- /dev/null +++ b/browser/browser_context_keyed_service_factories.h @@ -0,0 +1,14 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BRAVE_BROWSER_CONTEXT_KEYED_SERVICE_FACTORIES_H_ +#define BRAVE_BROWSER_CONTEXT_KEYED_SERVICE_FACTORIES_H_ + +namespace brave { + +void EnsureBrowserContextKeyedServiceFactoriesBuilt(); + +} // namespace apps + +#endif // BRAVE_BROWSER_CONTEXT_KEYED_SERVICE_FACTORIES_H_ diff --git a/browser/payments/BUILD.gn b/browser/payments/BUILD.gn new file mode 100644 index 000000000000..24dfdde9eb76 --- /dev/null +++ b/browser/payments/BUILD.gn @@ -0,0 +1,47 @@ +declare_args() { + # anonize doesn't currently build on windows + brave_payments_enabled = is_mac || is_linux +} + +config("external_config") { + visibility = [ ":payments" ] + + defines = [] + if (brave_payments_enabled) { + defines += [ "BRAVE_PAYMENTS_ENABLED" ] + } +} + +source_set("payments") { + public_configs = [ + ":external_config" + ] + + sources = [ + "payments_service.cc", + "payments_service.h", + "payments_service_factory.cc", + "payments_service_factory.h", + ] + + deps = [ + "//base", + "//components/keyed_service/content", + "//components/keyed_service/core", + # for profile.h + "//components/domain_reliability", + "//content/public/browser", + "//services/network/public/mojom", + ] + + if (brave_payments_enabled) { + sources += [ + "payments_service_impl.cc", + "payments_service_impl.h", + ] + + deps += [ + "//brave/vendor/bat-native-ledger", + ] + } +} diff --git a/browser/payments/payments_service.cc b/browser/payments/payments_service.cc new file mode 100644 index 000000000000..2f707d1cdafc --- /dev/null +++ b/browser/payments/payments_service.cc @@ -0,0 +1,19 @@ +/* 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 "brave/browser/payments/payments_service.h" + +#include "base/logging.h" + +namespace payments { + + PaymentsService::PaymentsService() { + } + + PaymentsService::~PaymentsService() { + } + + void PaymentsService::CreateWallet() { + NOTREACHED(); + } +} // namespace history diff --git a/browser/payments/payments_service.h b/browser/payments/payments_service.h new file mode 100644 index 000000000000..ab26540b5acb --- /dev/null +++ b/browser/payments/payments_service.h @@ -0,0 +1,28 @@ +/* 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_BROWSER_PAYMENTS_PAYMENTS_SERVICE_ +#define BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_ + +#include + +#include "base/macros.h" +#include "components/keyed_service/core/keyed_service.h" + +namespace payments { + +class PaymentsService : public KeyedService { + public: + PaymentsService(); + ~PaymentsService() override; + + virtual void CreateWallet(); + + private: + DISALLOW_COPY_AND_ASSIGN(PaymentsService); +}; + +} // namespace history + +#endif // BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_ diff --git a/browser/payments/payments_service_factory.cc b/browser/payments/payments_service_factory.cc new file mode 100644 index 000000000000..28c66613b964 --- /dev/null +++ b/browser/payments/payments_service_factory.cc @@ -0,0 +1,62 @@ +/* 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 "brave/browser/payments/payments_service_factory.h" + +#include "brave/browser/payments/payments_service.h" +#include "chrome/browser/profiles/incognito_helpers.h" +#include "chrome/browser/profiles/profile.h" +#include "components/keyed_service/content/browser_context_dependency_manager.h" + +#if defined(BRAVE_PAYMENTS_ENABLED) +#include "brave/browser/payments/payments_service_impl.h" +#endif + +// static +payments::PaymentsService* PaymentsServiceFactory::GetForProfile( + Profile* profile) { + if (profile->IsOffTheRecord()) + return NULL; + + return static_cast( + GetInstance()->GetServiceForBrowserContext(profile, true)); +} + +// static +PaymentsServiceFactory* PaymentsServiceFactory::GetInstance() { + return base::Singleton::get(); +} + +PaymentsServiceFactory::PaymentsServiceFactory() + : BrowserContextKeyedServiceFactory( + "PaymentsService", + BrowserContextDependencyManager::GetInstance()) { +} + +PaymentsServiceFactory::~PaymentsServiceFactory() { +} + +KeyedService* PaymentsServiceFactory::BuildServiceInstanceFor( + content::BrowserContext* context) const { +#if defined(BRAVE_PAYMENTS_ENABLED) + std::unique_ptr payments_service( + new payments::PaymentsServiceImpl()); + return payments_service.release(); +#else + return NULL; +#endif +} + +content::BrowserContext* PaymentsServiceFactory::GetBrowserContextToUse( + content::BrowserContext* context) const { + if (context->IsOffTheRecord()) + return chrome::GetBrowserContextOwnInstanceInIncognito(context); + + // use original profile for session profiles + return chrome::GetBrowserContextRedirectedInIncognito(context); +} + +bool PaymentsServiceFactory::ServiceIsNULLWhileTesting() const { + return true; +} diff --git a/browser/payments/payments_service_factory.h b/browser/payments/payments_service_factory.h new file mode 100644 index 000000000000..d6149df65b61 --- /dev/null +++ b/browser/payments/payments_service_factory.h @@ -0,0 +1,39 @@ +/* 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_BROWSER_PAYMENTS_PAYMENTS_SERVICE_FACTORY_H_ +#define BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_FACTORY_H_ + +#include "base/memory/singleton.h" +#include "components/keyed_service/content/browser_context_keyed_service_factory.h" + +class Profile; + +namespace payments { +class PaymentsService; +} + +// Singleton that owns all PaymentsService and associates them with +// Profiles. +class PaymentsServiceFactory : public BrowserContextKeyedServiceFactory { + public: + static payments::PaymentsService* GetForProfile(Profile* profile); + + static PaymentsServiceFactory* GetInstance(); + + private: + friend struct base::DefaultSingletonTraits; + + PaymentsServiceFactory(); + ~PaymentsServiceFactory() override; + + // BrowserContextKeyedServiceFactory: + KeyedService* BuildServiceInstanceFor( + content::BrowserContext* context) const override; + content::BrowserContext* GetBrowserContextToUse( + content::BrowserContext* context) const override; + bool ServiceIsNULLWhileTesting() const override; +}; + +#endif // BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_FACTORY_H_ diff --git a/browser/payments/payments_service_impl.cc b/browser/payments/payments_service_impl.cc new file mode 100644 index 000000000000..ce085651a135 --- /dev/null +++ b/browser/payments/payments_service_impl.cc @@ -0,0 +1,25 @@ +/* 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 "brave/browser/payments/payments_service_impl.h" + +#include "bat/ledger/ledger.h" + +namespace payments { + + PaymentsServiceImpl::PaymentsServiceImpl() : + ledger_(new braveledger_ledger::Ledger()) { + } + + PaymentsServiceImpl::~PaymentsServiceImpl() { + } + + void PaymentsServiceImpl::CreateWallet() { + ledger_->createWallet(); + } + + void PaymentsServiceImpl::Shutdown() { + ledger_.reset(); + PaymentsService::Shutdown(); + } +} // namespace history diff --git a/browser/payments/payments_service_impl.h b/browser/payments/payments_service_impl.h new file mode 100644 index 000000000000..d356227b12c0 --- /dev/null +++ b/browser/payments/payments_service_impl.h @@ -0,0 +1,36 @@ +/* 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_BROWSER_PAYMENTS_PAYMENTS_SERVICE_IMPL_ +#define BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_IMPL_ + +#include + +#include "brave/browser/payments/payments_service.h" + +namespace braveledger_ledger { +class Ledger; +} + +namespace payments { + +class PaymentsServiceImpl : public PaymentsService { + public: + PaymentsServiceImpl(); + ~PaymentsServiceImpl() override; + + // KeyedService: + void Shutdown() override; + + void CreateWallet() override; + + private: + std::unique_ptr ledger_; + + DISALLOW_COPY_AND_ASSIGN(PaymentsServiceImpl); +}; + +} // namespace history + +#endif // BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_IMPL_ diff --git a/browser/ui/BUILD.gn b/browser/ui/BUILD.gn index ed89cddf9307..4e2ca09bd489 100644 --- a/browser/ui/BUILD.gn +++ b/browser/ui/BUILD.gn @@ -50,6 +50,7 @@ source_set("ui") { "//brave/app:command_ids", "//brave/app/theme:brave_unscaled_resources", "//brave/app/theme:brave_theme_resources", + "//brave/browser/payments", "//brave/components/resources:brave_components_resources_grit", "//chrome/app:command_ids", ] diff --git a/browser/ui/brave_pages.cc b/browser/ui/brave_pages.cc index 99e11ecb1686..15c97fa282dc 100644 --- a/browser/ui/brave_pages.cc +++ b/browser/ui/brave_pages.cc @@ -5,6 +5,7 @@ #include "brave/browser/ui/brave_pages.h" #include "brave/common/webui_url_constants.h" +#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/singleton_tabs.h" #include "url/gurl.h" diff --git a/browser/ui/webui/brave_rewards_ui.cc b/browser/ui/webui/brave_rewards_ui.cc index e80cde9559c2..128a895536ab 100644 --- a/browser/ui/webui/brave_rewards_ui.cc +++ b/browser/ui/webui/brave_rewards_ui.cc @@ -4,12 +4,87 @@ #include "brave/browser/ui/webui/brave_rewards_ui.h" +#include "brave/browser/payments/payments_service.h" +#include "brave/browser/payments/payments_service_factory.h" #include "brave/common/webui_url_constants.h" +#include "chrome/browser/profiles/profile.h" #include "components/grit/brave_components_resources.h" +#include "content/public/browser/web_ui_message_handler.h" +#include "content/public/common/bindings_policy.h" + + +using content::WebUIMessageHandler; + +namespace { + +// The handler for Javascript messages for Brave about: pages +class RewardsDOMHandler : public WebUIMessageHandler { + public: + RewardsDOMHandler() { + } + ~RewardsDOMHandler() override {} + + void Init(); + + // WebUIMessageHandler implementation. + void RegisterMessages() override; + + private: + void HandleCreateWalletRequested(const base::ListValue* args); + void OnWalletCreated(); + void OnWalletCreateFailed(); + DISALLOW_COPY_AND_ASSIGN(RewardsDOMHandler); +}; + +void RewardsDOMHandler::RegisterMessages() { + web_ui()->RegisterMessageCallback("createWalletRequested", + base::BindRepeating(&RewardsDOMHandler::HandleCreateWalletRequested, + base::Unretained(this))); +} + +void RewardsDOMHandler::Init() { +} + +void RewardsDOMHandler::HandleCreateWalletRequested(const base::ListValue* args) { +#if defined(BRAVE_PAYMENTS_ENABLED) + Profile* profile = Profile::FromWebUI(web_ui()); + payments::PaymentsService* payments_service = + PaymentsServiceFactory::GetForProfile(profile); + + if (payments_service) { + payments_service->CreateWallet(); + } + + // TODO(bbondy): Use an observer or client override for when the wallet is actually + // created once the native-ledger library supports it. + OnWalletCreated(); +#else + OnWalletCreateFailed(); +#endif +} + +void RewardsDOMHandler::OnWalletCreated() { + if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)) { + web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.walletCreated"); + } +} + +void RewardsDOMHandler::OnWalletCreateFailed() { + if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)) { + web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.walletCreateFailed"); + } +} + +} // namespace BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name) : BasicUI(web_ui, name, kRewardsJS, IDR_BRAVE_REWARDS_JS, IDR_BRAVE_REWARDS_HTML) { + + auto handler_owner = std::make_unique(); + RewardsDOMHandler * handler = handler_owner.get(); + web_ui->AddMessageHandler(std::move(handler_owner)); + handler->Init(); } BraveRewardsUI::~BraveRewardsUI() { diff --git a/chromium_src/chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.cc b/chromium_src/chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.cc new file mode 100644 index 000000000000..d4bc03db740d --- /dev/null +++ b/chromium_src/chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.cc @@ -0,0 +1,30 @@ +#define AddProfilesExtraParts AddProfilesExtraParts_ChromiumImpl +#include "../../../../chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.cc" +#undef AddProfilesExtraParts + +#include "brave/browser/browser_context_keyed_service_factories.h" + +namespace { + +class BraveBrowserMainExtraPartsProfiles : public ChromeBrowserMainExtraPartsProfiles { + public: + BraveBrowserMainExtraPartsProfiles() : ChromeBrowserMainExtraPartsProfiles() {} + + void PreProfileInit() override { + ChromeBrowserMainExtraPartsProfiles::PreProfileInit(); + brave::EnsureBrowserContextKeyedServiceFactoriesBuilt(); + } + + private: + DISALLOW_COPY_AND_ASSIGN(BraveBrowserMainExtraPartsProfiles); +}; + +} // namespace + +namespace chrome { + +void AddProfilesExtraParts(ChromeBrowserMainParts* main_parts) { + main_parts->AddParts(new BraveBrowserMainExtraPartsProfiles()); +} + +} // namespace chrome diff --git a/components/brave_rewards_ui/actions/rewards_actions.js b/components/brave_rewards_ui/actions/rewards_actions.js new file mode 100644 index 000000000000..e20d5314a9c2 --- /dev/null +++ b/components/brave_rewards_ui/actions/rewards_actions.js @@ -0,0 +1,17 @@ +/* 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/. */ + +const types = require('../constants/rewards_types') + +export const createWalletRequested = () => ({ + type: types.CREATE_WALLET_REQUESTED +}) + +export const walletCreated = () => ({ + type: types.WALLET_CREATED +}) + +export const walletCreateFailed = () => ({ + type: types.WALLET_CREATE_FAILED +}) diff --git a/components/brave_rewards_ui/brave_rewards.html b/components/brave_rewards_ui/brave_rewards.html index f74d8577af79..bfbf20290de0 100644 --- a/components/brave_rewards_ui/brave_rewards.html +++ b/components/brave_rewards_ui/brave_rewards.html @@ -5,8 +5,18 @@ Rewards + + + + + + + + - Brave Rewards +
diff --git a/components/brave_rewards_ui/brave_rewards.js b/components/brave_rewards_ui/brave_rewards.js index 3daed83bb0e3..331ebd645992 100644 --- a/components/brave_rewards_ui/brave_rewards.js +++ b/components/brave_rewards_ui/brave_rewards.js @@ -1 +1,46 @@ -console.log('brave_rewards.js loaded') +/* 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/. */ + +const React = require('react') +const { render } = require('react-dom') +const { Provider } = require('react-redux') +const App = require('./components/app') +const { bindActionCreators } = require('redux') + +window.cr.define('brave_rewards', function () { + 'use strict' + + function initialize () { + const store = require('./store') + render( + + + , + document.getElementById('root')) + window.i18nTemplate.process(window.document, window.loadTimeData) + } + + function getActions () { + const store = require('./store') + const rewardsActions = require('./actions/rewards_actions') + return bindActionCreators(rewardsActions, store.dispatch.bind(store)) + } + + function walletCreated () { + getActions().walletCreated() + } + + function walletCreateFailed () { + getActions().walletCreateFailed() + } + + return { + initialize, + walletCreated, + walletCreateFailed + } +}) + +document.addEventListener('DOMContentLoaded', window.brave_rewards.initialize) + diff --git a/components/brave_rewards_ui/components/app.js b/components/brave_rewards_ui/components/app.js new file mode 100644 index 000000000000..72a0663743f5 --- /dev/null +++ b/components/brave_rewards_ui/components/app.js @@ -0,0 +1,59 @@ +/* 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/. */ + +const React = require('react') +const { bindActionCreators } = require('redux') +const { connect } = require('react-redux') +const rewardsActions = require('../actions/rewards_actions') + +const CreateWalletLink = (props) => + + +class RewardsPage extends React.Component { + constructor (props) { + super(props) + this.onCreateWalletClicked = this.onCreateWalletClicked.bind(this) + } + + onCreateWalletClicked() { + this.actions.createWalletRequested() + } + + get actions () { + return this.props.actions + } + + render () { + const { rewardsData } = this.props + return ( +
+ + { + rewardsData.walletCreated + ?
Wallet Created!
+ : null + } + { + rewardsData.walletCreateFailed + ?
Wallet Create Failed!
+ : null + } +
) + } +} + +const mapStateToProps = (state) => ({ + rewardsData: state.rewardsData +}) + +const mapDispatchToProps = (dispatch) => ({ + actions: bindActionCreators(rewardsActions, dispatch) +}) + +export default connect( + mapStateToProps, + mapDispatchToProps +)(RewardsPage) diff --git a/components/brave_rewards_ui/constants/rewards_types.js b/components/brave_rewards_ui/constants/rewards_types.js new file mode 100644 index 000000000000..2c90b830451e --- /dev/null +++ b/components/brave_rewards_ui/constants/rewards_types.js @@ -0,0 +1,7 @@ +/* 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/. */ + +export const CREATE_WALLET_REQUESTED = 'CREATE_WALLET_REQUESTED' +export const WALLET_CREATED = 'WALLET_CREATED' +export const WALLET_CREATE_FAILED = 'WALLET_CREATE_FAILED' diff --git a/components/brave_rewards_ui/reducers/index.js b/components/brave_rewards_ui/reducers/index.js new file mode 100644 index 000000000000..dd69ce98bcfb --- /dev/null +++ b/components/brave_rewards_ui/reducers/index.js @@ -0,0 +1,12 @@ +/* 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/. */ + +import { combineReducers } from 'redux' +import rewardsReducer from './rewards_reducer' + +const combinedReducer = combineReducers({ + rewardsData: rewardsReducer +}) + +export default combinedReducer diff --git a/components/brave_rewards_ui/reducers/rewards_reducer.js b/components/brave_rewards_ui/reducers/rewards_reducer.js new file mode 100644 index 000000000000..53a3c01aad77 --- /dev/null +++ b/components/brave_rewards_ui/reducers/rewards_reducer.js @@ -0,0 +1,37 @@ +/* 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/. */ + +/* global chrome */ + +const types = require('../constants/rewards_types') +const storage = require('../storage') + +const rewardsReducer = (state, action) => { + if (state === undefined) { + state = storage.load() || {} + state = Object.assign(storage.getInitialState(), state) + } + const startingState = state + switch (action.type) { + case types.CREATE_WALLET_REQUESTED: + chrome.send('createWalletRequested', []); + break + case types.WALLET_CREATED: + state = {... state} + state.walletCreated = true + break + case types.WALLET_CREATE_FAILED: + state = {... state} + state.walletCreateFailed = true + break + } + + if (state !== startingState) { + storage.debouncedSave(state) + } + + return state +} + +export default rewardsReducer diff --git a/components/brave_rewards_ui/storage.js b/components/brave_rewards_ui/storage.js new file mode 100644 index 000000000000..ffed7631bbb8 --- /dev/null +++ b/components/brave_rewards_ui/storage.js @@ -0,0 +1,30 @@ +/* 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/. */ + +const debounce = require('../common/debounce') + +const keyName = 'rewards-data' + +const cleanData = (state) => ({}) + +module.exports.getInitialState = () => cleanData({}) + +module.exports.load = () => { + const data = window.localStorage.getItem(keyName) + let state + if (data) { + try { + state = JSON.parse(data) + } catch (e) { + console.error('Could not parse local storage data: ', e) + } + } + return cleanData(state) +} + +module.exports.debouncedSave = debounce((data) => { + if (data) { + window.localStorage.setItem(keyName, JSON.stringify(cleanData(data))) + } +}, 50) diff --git a/components/brave_rewards_ui/store.js b/components/brave_rewards_ui/store.js new file mode 100644 index 000000000000..426939fda2d6 --- /dev/null +++ b/components/brave_rewards_ui/store.js @@ -0,0 +1,9 @@ +/* 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/. */ + +import { createStore } from 'redux' +import reducers from './reducers' + +const store = createStore(reducers) +module.exports = store diff --git a/components/resources/BUILD.gn b/components/resources/BUILD.gn index 370de9e5e581..b32ce174caad 100644 --- a/components/resources/BUILD.gn +++ b/components/resources/BUILD.gn @@ -21,6 +21,13 @@ transpile_includes = [ "../brave_new_tab_ui/reducers/newTabReducer.js", # Brave Rewards "../brave_rewards_ui/brave_rewards.html", + "../brave_rewards_ui/components/app.js", + "../brave_rewards_ui/constants/rewards_types.js", + "../brave_rewards_ui/actions/rewards_actions.js", + "../brave_rewards_ui/storage.js", + "../brave_rewards_ui/store.js", + "../brave_rewards_ui/reducers/index.js", + "../brave_rewards_ui/reducers/rewards_reducer.js", # Brave welcome page "../brave_welcome_ui/brave_welcome.html", # Brave adblock page