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

Uplift for 1.22.x: Brave Today Japanese content #8094

Merged
merged 1 commit into from
Mar 9, 2021
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
7 changes: 5 additions & 2 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,11 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
brave_l10n::LocaleHelper::GetInstance()->GetLocale();
const std::string language_code = brave_l10n::GetLanguageCode(locale);
const bool is_english_language = language_code == "en";
registry->RegisterBooleanPref(kNewTabPageShowToday, is_english_language);

const bool is_japanese_language = language_code == "ja";
const bool brave_today_enabled_default = is_english_language ||
is_japanese_language;
registry->RegisterBooleanPref(kNewTabPageShowToday,
brave_today_enabled_default);
registry->RegisterBooleanPref(kNewTabPageShowRewards, true);
registry->RegisterBooleanPref(kNewTabPageShowBinance, true);
registry->RegisterBooleanPref(kNewTabPageShowTogether, false);
Expand Down
2 changes: 1 addition & 1 deletion browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ source_set("extensions") {
"//brave/components/brave_rewards/browser/buildflags",
"//brave/components/brave_shields/browser",
"//brave/components/brave_shields/common",
"//brave/components/brave_today/common",
"//brave/components/brave_today/browser",
"//brave/components/brave_wayback_machine:buildflags",
"//brave/components/ipfs/buildflags",
"//brave/components/sidebar/buildflags",
Expand Down
6 changes: 5 additions & 1 deletion browser/extensions/api/brave_today_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "base/json/json_writer.h"
#include "base/values.h"
#include "brave/common/extensions/api/brave_theme.h"
#include "brave/components/brave_today/common/urls.h"
#include "brave/components/brave_today/browser/urls.h"

namespace extensions {
namespace api {
Expand All @@ -20,5 +20,9 @@ ExtensionFunction::ResponseAction BraveTodayGetHostnameFunction::Run() {
return RespondNow(OneArgument(base::Value(brave_today::GetHostname())));
}

ExtensionFunction::ResponseAction BraveTodayGetRegionUrlPartFunction::Run() {
return RespondNow(OneArgument(base::Value(brave_today::GetRegionUrlPart())));
}

} // namespace api
} // namespace extensions
10 changes: 10 additions & 0 deletions browser/extensions/api/brave_today_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class BraveTodayGetHostnameFunction : public ExtensionFunction {
ResponseAction Run() override;
};

class BraveTodayGetRegionUrlPartFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveToday.getRegionUrlPart", UNKNOWN)

protected:
~BraveTodayGetRegionUrlPartFunction() override {}

ResponseAction Run() override;
};

} // namespace api
} // namespace extensions

Expand Down
18 changes: 18 additions & 0 deletions common/extensions/api/brave_today.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
]
}
]
},
{
"name": "getRegionUrlPart",
"type": "function",
"description": "Gets optional url part for currently configured Brave Today region variation.",
"parameters": [
{
"name": "callback",
"type": "function",
"description": "Callback providing the URL part string",
"parameters": [
{
"name": "regionUrlPart",
"type": "string"
}
]
}
]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type RemoteData = BraveToday.FeedItem[]

type FeedInStorage = {
storageSchemaVersion: number,
sourceUrl: string,
feed: BraveToday.Feed
}

Expand All @@ -36,10 +37,11 @@ function getFromStorage<T> (key: string) {
})
}

function setStorageData (feed: BraveToday.Feed) {
async function setStorageData (feed: BraveToday.Feed) {
chrome.storage.local.set({
[STORAGE_KEY]: {
storageSchemaVersion: STORAGE_SCHEMA_VERSION,
sourceUrl: await getFeedUrl(),
feed
}
})
Expand Down Expand Up @@ -77,6 +79,9 @@ async function getStorageData () {
if (data.storageSchemaVersion !== STORAGE_SCHEMA_VERSION) {
return
}
if (data.sourceUrl !== await getFeedUrl()) {
return
}
memoryTodayData = data.feed
}

Expand Down Expand Up @@ -126,7 +131,7 @@ function performUpdateFeed () {
isKnownRemoteUpdateAvailable = false
resolve()
if (memoryTodayData) {
setStorageData(memoryTodayData)
await setStorageData(memoryTodayData)
}
} else {
throw new Error(`Not ok when fetching feed. Status ${feedResponse.status} (${feedResponse.statusText})`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@ const eventNameChanged = 'publishers-changed'
const storageKey = 'todayPublishers'
const STORAGE_SCHEMA_VERSION = 1

function isValidStorageData (data: {[key: string]: any}) {
async function isValidStorageData (data: {[key: string]: any}) {
return (
data && data.todayPublishers &&
data.todayPublishers.storageSchemaVersion === STORAGE_SCHEMA_VERSION &&
data.todayPublishers.sourceUrl === (await getSourcesUrl()) &&
data.todayPublishers.publishers
)
}

function setPublishersCache (publishers: BraveToday.Publishers) {
async function setPublishersCache (publishers: BraveToday.Publishers) {
chrome.storage.local.set({
[storageKey]: {
storageSchemaVersion: STORAGE_SCHEMA_VERSION,
sourceUrl: (await getSourcesUrl()),
publishers
}
})
}

function getPublishersFromCache () {
return new Promise<void>(resolve => {
chrome.storage.local.get(storageKey, (data) => {
if (isValidStorageData(data)) {
chrome.storage.local.get(storageKey, async (data) => {
if (await isValidStorageData(data)) {
memoryData = data[storageKey].publishers
}
resolve()
Expand Down Expand Up @@ -76,8 +78,8 @@ function performUpdate (notify: boolean = true) {
console.debug('fetched today publishers', feedContents)
memoryData = await convertToObject(feedContents)
resolve()
await setPublishersCache(memoryData)
// Notify
setPublishersCache(memoryData)
if (notify) {
publishersEvents.dispatchEvent<BraveToday.Publishers>(eventNameChanged, memoryData)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// This won't change unless browser restarted (provided via flag), so it's ok
// to store as a global.
let hostnameCache: string
// This won't change unless OS region changes.
let regionURLPartCache: string

function getHostname (): Promise<string> {
if (hostnameCache) {
Expand All @@ -19,14 +21,35 @@ function getHostname (): Promise<string> {
})
}

function getRegionUrlPart (): Promise<string> {
if (regionURLPartCache) {
return Promise.resolve(regionURLPartCache)
}
return new Promise(resolve => {
chrome.braveToday.getRegionUrlPart((regionURLPart) => {
regionURLPartCache = regionURLPart
if (regionURLPart) {
regionURLPartCache += '.'
}
resolve(regionURLPartCache)
})
})
}

export async function getFeedUrl () {
const hostname = await getHostname()
return `https://${hostname}/brave-today/feed.json`
let [hostname, regionUrlPart] = await Promise.all([
getHostname(),
getRegionUrlPart()
])
return `https://${hostname}/brave-today/feed.${regionUrlPart}json`
}

export async function getSourcesUrl () {
const hostname = await getHostname()
return `https://${hostname}/sources.json`
let [hostname, regionUrlPart] = await Promise.all([
getHostname(),
getRegionUrlPart()
])
return `https://${hostname}/sources.${regionUrlPart}json`
}

// Always get the hostname at startup, it's cheap
Expand Down
15 changes: 15 additions & 0 deletions components/brave_today/browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import("//brave/build/config.gni")

source_set("browser") {
sources = [
"urls.cc",
"urls.h",
]

deps = [
"//base",
"//brave/components/brave_today/common",
"//brave/components/l10n/browser",
"//brave/components/l10n/common",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
// 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/components/brave_today/common/urls.h"
#include "brave/components/brave_today/browser/urls.h"

#include <string>

#include "base/command_line.h"
#include "brave/components/brave_today/common/switches.h"
#include "brave/components/l10n/browser/locale_helper.h"
#include "brave/components/l10n/common/locale_util.h"

namespace brave_today {

Expand All @@ -23,4 +25,16 @@ std::string GetHostname() {
}
}

std::string GetRegionUrlPart() {
const std::string locale =
brave_l10n::LocaleHelper::GetInstance()->GetLocale();
const std::string language_code = brave_l10n::GetLanguageCode(locale);
// TODO(petemill): Have a remotely-updatable list of supported language
// variations.
if (language_code == "ja") {
return "ja";
}
return "";
}

} // namespace brave_today
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
// 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_COMPONENTS_BRAVE_TODAY_COMMON_URLS_H_
#define BRAVE_COMPONENTS_BRAVE_TODAY_COMMON_URLS_H_
#ifndef BRAVE_COMPONENTS_BRAVE_TODAY_BROWSER_URLS_H_
#define BRAVE_COMPONENTS_BRAVE_TODAY_BROWSER_URLS_H_

#include <string>

namespace brave_today {

std::string GetHostname();
std::string GetRegionUrlPart();

} // namespace brave_today

#endif // BRAVE_COMPONENTS_BRAVE_TODAY_COMMON_URLS_H_
#endif // BRAVE_COMPONENTS_BRAVE_TODAY_BROWSER_URLS_H_
2 changes: 0 additions & 2 deletions components/brave_today/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ source_set("common") {
sources = [
"switches.cc",
"switches.h",
"urls.cc",
"urls.h",
]

deps = [ "//base" ]
Expand Down
1 change: 1 addition & 0 deletions components/definitions/chromel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ declare namespace chrome.braveToday {
addListener: (callback: () => any) => void
}
const getHostname: (callback: (hostname: string) => any) => void
const getRegionUrlPart: (callback: (regionURLPart: string) => any) => void
}

type BlockTypes = 'shieldsAds' | 'trackers' | 'httpUpgradableResources' | 'javascript' | 'fingerprinting'
Expand Down