Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
use cdn proxy for ad replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
bridiver committed Dec 18, 2015
1 parent 217b164 commit d3b8070
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
21 changes: 16 additions & 5 deletions app/content/webviewPreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ function getAdSize (node, iframeData) {
*
* @param node The node of the ad to process
* @param iframeData The iframe data of the node to process from the slimerJS bot
* @param placeholderUrl The vault URL with encoded user ID and session ID to use
* @param replacementUrl The vault replacement url
*/
function processAdNode (node, iframeData, placeholderUrl) {
function processAdNode (node, iframeData, replacementUrl) {
if (!node) {
return
}
Expand All @@ -93,9 +93,20 @@ function processAdNode (node, iframeData, placeholderUrl) {
node.style.display = 'none'
return
}
var srcUrl = placeholderUrl + '&width=' + encodeURIComponent(adSize[0]) + '&height=' + encodeURIComponent(adSize[1])

// generate a random segment
// @todo - replace with renko targeting
var segments = ['IAB2', 'IAB17', 'IAB14', 'IAB21', 'IAB20']
var segment = segments[Math.floor(Math.random() * 4)]
var time_in_segment = new Date().getSeconds()
var segment_expiration_time = 0 // no expiration

// ref param for referrer when possible
var srcUrl = replacementUrl + '?width=' + adSize[0] + '&height=' + adSize[1] + '&seg=' + segment + ':' + time_in_segment + ':' + segment_expiration_time
var src = '<html><body style="width: ' + adSize[0] + 'px; height: ' + adSize[1] + '; padding: 0; margin: 0;"><script src="' + srcUrl + '"></script></body></html>'

if (node.tagName === 'IFRAME') {
node.src = srcUrl
node.srcdoc = src
} else {
while (node.firstChild) {
node.removeChild(node.firstChild)
Expand All @@ -106,7 +117,7 @@ function processAdNode (node, iframeData, placeholderUrl) {
iframe.style.margin = 0
iframe.style.width = adSize[0] + 'px'
iframe.style.height = adSize[1] + 'px'
iframe.src = srcUrl
iframe.srcdoc = src
node.appendChild(iframe)
ensureNodeVisible(node)
if (node.parentNode) {
Expand Down
11 changes: 1 addition & 10 deletions js/components/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const ReactDOM = require('react-dom')
const AppActions = require('../actions/appActions')
const ImmutableComponent = require('./immutableComponent')
const cx = require('../lib/classSet.js')
const uuid = require('node-uuid')

import adInfo from '../data/adInfo.js'
import Config from '../constants/config.js'
Expand Down Expand Up @@ -112,15 +111,7 @@ class Frame extends ImmutableComponent {
let host = new window.URL(currentLocation).hostname.replace('www.', '')
let adDivCandidates = adInfo[host]
if (adDivCandidates) {
// TODO: Use a real user ID and sessionID
const userId = uuid.v4()
const sessionId = uuid.v4()

const placeholderUrl = Config.vault.replacementUrl(userId) + '?' + [
`sessionId=${sessionId}`,
`tagName=IFRAME`
].join('&')
this.webview.send('set-ad-div-candidates', adDivCandidates, placeholderUrl)
this.webview.send('set-ad-div-candidates', adDivCandidates, Config.vault.replacementUrl)
}
}

Expand Down
5 changes: 3 additions & 2 deletions js/constants/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// https://vault-staging.brave.com for a dev build
// http://localhost:3000 for production
var vaultHost = process.env.VAULT_HOST || 'https://vault-staging.brave.com'
var adHost = process.env.AD_HOST || '//cdn.brave.com'

export default {
zoom: {
Expand Down Expand Up @@ -36,9 +37,9 @@ export default {
},
defaultOpenSearchPath: './content/search/duckduckgo.xml',
vault: {
replacementUrl: (userId) => `${vaultHost}/v1/users/${userId}/replacement`,
syncUrl: (userId) => `${vaultHost}/v1/users/${userId}/appState`,
authUrl: (userId) => `${vaultHost}/v1/users/${userId}`,
intentUrl: (userId) => `${vaultHost}/v1/users/${userId}/intents`
intentUrl: (userId) => `${vaultHost}/v1/users/${userId}/intents`,
replacementUrl: adHost
}
}

0 comments on commit d3b8070

Please sign in to comment.