From 10f271568497f15cd3f3215257788feb005b62f2 Mon Sep 17 00:00:00 2001 From: Brian Clifton Date: Wed, 23 May 2018 22:19:21 -0700 Subject: [PATCH] Only call createTabRequested if window is ready Referrer page loads quickly and code was previously calling createTabRequested well before window was ready. This would cause the partner page to load first (before about:welcome). Fixes https://github.com/brave/browser-laptop/issues/14220 Auditors: @NejcZdovc --- app/browser/api/ledger.js | 7 +++++-- app/browser/reducers/tabsReducer.js | 1 + app/sessionStore.js | 8 +++++++- docs/state.md | 1 + test/unit/app/browser/api/ledgerTest.js | 6 ++++-- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/browser/api/ledger.js b/app/browser/api/ledger.js index fcc7917987e..d4c60f9c5c9 100644 --- a/app/browser/api/ledger.js +++ b/app/browser/api/ledger.js @@ -2161,12 +2161,15 @@ const onReferralRead = (state, body, activeWindowId) => { if (body.has('offer_page_url')) { const url = body.get('offer_page_url') if (urlutil.isURL(url)) { - if (activeWindowId === windowState.WINDOW_ID_NONE) { + if (activeWindowId === windowState.WINDOW_ID_NONE || !state.get('windowReady')) { + // write referralPage to state if initial window is not created/visible yet state = updateState.setUpdateProp(state, 'referralPage', url) } else { + // initial window exists and should be ready; create tab directly appActions.createTabRequested({ url, - windowId: activeWindowId + windowId: activeWindowId, + active: true }) state = updateState.setUpdateProp(state, 'referralPage', null) } diff --git a/app/browser/reducers/tabsReducer.js b/app/browser/reducers/tabsReducer.js index d4413d2b008..596995be691 100644 --- a/app/browser/reducers/tabsReducer.js +++ b/app/browser/reducers/tabsReducer.js @@ -525,6 +525,7 @@ const tabsReducer = (state, action, immutableAction) => { state = updateState.setUpdateProp(state, 'referralPage', null) } } + state = state.set('windowReady', true) break } case appConstants.APP_ENABLE_PEPPER_MENU: { diff --git a/app/sessionStore.js b/app/sessionStore.js index d04af74c088..0b8bcdf3c2a 100644 --- a/app/sessionStore.js +++ b/app/sessionStore.js @@ -451,6 +451,11 @@ module.exports.cleanAppData = (immutableData, isShutdown) => { console.error('cleanAppData: error cleaning up data: urls', e) } + // delete the window ready state (gets set again on program start) + if (immutableData.has('windowReady')) { + immutableData = immutableData.delete('windowReady') + } + return immutableData } @@ -1162,7 +1167,8 @@ module.exports.defaultAppState = () => { publishers: {} }, promotion: {} - } + }, + windowReady: false } } diff --git a/docs/state.md b/docs/state.md index 8da8ae96417..a45d0607e10 100644 --- a/docs/state.md +++ b/docs/state.md @@ -649,6 +649,7 @@ AppStore autocompleteURL: string, // ditto re: {searchTerms} searchURL: string // with replacement var in string: {searchTerms} }, + windowReady: boolean // set to false on start; set to true when first window is ready } ``` diff --git a/test/unit/app/browser/api/ledgerTest.js b/test/unit/app/browser/api/ledgerTest.js index 7247f4b8727..a0de689b61e 100644 --- a/test/unit/app/browser/api/ledgerTest.js +++ b/test/unit/app/browser/api/ledgerTest.js @@ -3231,7 +3231,8 @@ describe('ledger api unit tests', function () { }) it('window is ready', function () { - ledgerApi.onReferralRead(defaultAppState, Immutable.fromJS({ + const windowReadyState = defaultAppState.set('windowReady', true) + ledgerApi.onReferralRead(windowReadyState, Immutable.fromJS({ download_id: 1, referral_code: 'code', offer_page_url: url @@ -3239,7 +3240,8 @@ describe('ledger api unit tests', function () { assert(setUpdatePropSpy.withArgs(sinon.match.any, 'referralPage', null).calledOnce) assert(createTabRequestedSpy.withArgs({ url, - windowId: 1 + windowId: 1, + active: true }).calledOnce) }) })