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

Create about:welcome and about:newtab WebUI placeholders #20

Merged
merged 5 commits into from Jan 18, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -5,6 +5,39 @@
#include "brave/browser/brave_content_browser_client.h"

#include "brave/browser/brave_browser_main_extra_parts.h"
#include "brave/common/webui_url_constants.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/browser_url_handler.h"


namespace {

bool HandleNewTabURLRewrite(GURL* url,
content::BrowserContext* browser_context) {
if (!url->SchemeIs(content::kChromeUIScheme) ||
url->host() != chrome::kChromeUINewTabHost)
return false;

*url = GURL(kBraveNewTabUrl);
return true;
}

bool HandleNewTabURLReverseRewrite(GURL* url,
content::BrowserContext* browser_context) {
// Do nothing in incognito.
Profile* profile = Profile::FromBrowserContext(browser_context);
if (profile && profile->IsOffTheRecord())
return false;

if (url->spec() == kBraveNewTabUrl) {
*url = GURL(kBraveNewTabUrl);
return true;
}

return false;
}

}

BraveContentBrowserClient::BraveContentBrowserClient() {}

@@ -17,3 +50,10 @@ content::BrowserMainParts* BraveContentBrowserClient::CreateBrowserMainParts(
main_parts->AddParts(new BraveBrowserMainExtraParts());
return main_parts;
}

void BraveContentBrowserClient::BrowserURLHandlerCreated(content::BrowserURLHandler* handler) {
// Handler to rewrite chrome://newtab
handler->AddHandlerPair(&HandleNewTabURLRewrite,
&HandleNewTabURLReverseRewrite);
ChromeContentBrowserClient::BrowserURLHandlerCreated(handler);
}
@@ -15,6 +15,7 @@ class BraveContentBrowserClient : public ChromeContentBrowserClient {
// Overridden from ChromeContentBrowserClient:
content::BrowserMainParts* CreateBrowserMainParts(
const content::MainFunctionParams& parameters) override;
void BrowserURLHandlerCreated(content::BrowserURLHandler* handler) override;

private:
DISALLOW_COPY_AND_ASSIGN(BraveContentBrowserClient);
@@ -6,6 +6,7 @@

#include "brave/common/webui_url_constants.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/url_constants.h"
#include "components/grit/brave_components_resources.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
@@ -16,14 +17,14 @@ using content::WebUIMessageHandler;

namespace {

content::WebUIDataSource* CreateBasicUIHTMLSource(const std::string& name) {
content::WebUIDataSource* CreateBasicUIHTMLSource(const std::string& name,
const std::string& js_file,
int js_resource_id,
int html_resource_id) {
content::WebUIDataSource* source =
content::WebUIDataSource::Create(kPaymentsHost);

if (name == kPaymentsHost) {
source->AddResourcePath(kPaymentsJS, IDR_BRAVE_PAYMENTS_JS);
source->SetDefaultResource(IDR_BRAVE_PAYMENTS_HTML);
}
content::WebUIDataSource::Create(name);
source->AddResourcePath(js_file, js_resource_id);
source->SetDefaultResource(html_resource_id);

return source;
}
@@ -52,13 +53,18 @@ void BasicDOMHandler::Init() {

} // namespace

BasicUI::BasicUI(content::WebUI* web_ui, const std::string& name)
BasicUI::BasicUI(content::WebUI* web_ui,
const std::string& name,
const std::string& js_file,
int js_resource_id,
int html_resource_id)
: WebUIController(web_ui) {
Profile* profile = Profile::FromWebUI(web_ui);

auto handler_owner = base::MakeUnique<BasicDOMHandler>();
BasicDOMHandler* handler = handler_owner.get();
web_ui->AddMessageHandler(std::move(handler_owner));
handler->Init();
content::WebUIDataSource::Add(profile, CreateBasicUIHTMLSource(name));
content::WebUIDataSource::Add(profile,
CreateBasicUIHTMLSource(name, js_file, js_resource_id, html_resource_id));
}
@@ -13,7 +13,8 @@

class BasicUI : public content::WebUIController {
public:
explicit BasicUI(content::WebUI* web_ui, const std::string& host);
explicit BasicUI(content::WebUI* web_ui, const std::string& host,
const std::string& js_file, int js_resource_id, int html_resource_id);
~BasicUI() override {}

private:
@@ -6,6 +6,8 @@

#include "brave/common/webui_url_constants.h"
#include "brave/browser/ui/webui/basic_ui.h"
#include "chrome/common/url_constants.h"
#include "components/grit/brave_components_resources.h"
#include "url/gurl.h"

using content::WebUI;
@@ -26,15 +28,28 @@ WebUIController* NewWebUI(WebUI* web_ui, const GURL& url) {

template<>
WebUIController* NewWebUI<BasicUI>(WebUI* web_ui, const GURL& url) {
return new BasicUI(web_ui, url.host());
auto host = url.host_piece();
if (host == kPaymentsHost) {
return new BasicUI(web_ui, url.host(), kPaymentsJS,
IDR_BRAVE_PAYMENTS_JS, IDR_BRAVE_PAYMENTS_HTML);
} else if (host == kBraveNewTabHost) {
return new BasicUI(web_ui, url.host(), kBraveNewTabJS,
IDR_BRAVE_NEW_TAB_JS, IDR_BRAVE_NEW_TAB_HTML);
} else if (host == chrome::kChromeUIWelcomeHost) {
return new BasicUI(web_ui, url.host(), kWelcomeJS,
IDR_BRAVE_WELCOME_JS, IDR_BRAVE_WELCOME_HTML);
}
return nullptr;
}

// Returns a function that can be used to create the right type of WebUI for a
// tab, based on its URL. Returns NULL if the URL doesn't have WebUI associated
// with it.
WebUIFactoryFunction GetWebUIFactoryFunction(WebUI* web_ui,
const GURL& url) {
if (url.host_piece() == kPaymentsHost) {
if (url.host_piece() == kPaymentsHost ||
url.host_piece() == kBraveNewTabHost||
url.host_piece() == chrome::kChromeUIWelcomeHost) {
return &NewWebUI<BasicUI>;
}

@@ -2,3 +2,7 @@

const char kPaymentsHost[] = "payments";
const char kPaymentsJS[] = "brave_payments.js";
const char kBraveNewTabJS[] = "brave_new_tab.js";
const char kWelcomeJS[] = "welcome.js";
const char kBraveNewTabHost[] = "bravenewtab";
const char kBraveNewTabUrl[] = "chrome://bravenewtab";;
@@ -3,6 +3,10 @@

extern const char kPaymentsHost[];
extern const char kPaymentsJS[];
extern const char kBraveNewTabJS[];
extern const char kWelcomeJS[];
extern const char kBraveNewTabHost[];
extern const char kBraveNewTabUrl[];

#endif // BRAVE_COMMON_URL_CONSTANTS_H_

@@ -0,0 +1,12 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>New Tab</title>
<link rel="stylesheet" href="chrome://resources/css/text_defaults.css">
<body>
<div id="root"></div>
<script src="chrome://bravenewtab/brave_new_tab.js"></script>
</body>
</html>
@@ -0,0 +1,17 @@
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import reducers from './reducers'
import App from './components/app'

let store = createStore(reducers)

render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)

console.log('brave_new_tab.js loaded')
@@ -0,0 +1,7 @@
import React from 'react'

const App = () => (
<div>New Tab!</div>
)

export default App
@@ -0,0 +1,8 @@
import { combineReducers } from 'redux'
import newTabReducer from './newTabReducer'

const combinedReducer = combineReducers({
newTabReducer
})

export default combinedReducer
@@ -0,0 +1,8 @@
const newTabReducer = (state = {}, action) => {
switch (action.type) {
default:
return state
}
}

export default newTabReducer
@@ -1,6 +1,34 @@
import("//printing/features/features.gni")
import("//tools/grit/grit_rule.gni")


action("transpile_web_uis") {
script = "//brave/script/transpile-web-ui.py"
inputs = [
"../brave_new_tab_ui/brave_new_tab.html",
"../brave_new_tab_ui/brave_new_tab.js",
"../brave_payments_ui/brave_payments.html",
"../welcome_ui/welcome.html",
"../welcome_ui/welcome.js"
]
outputs = [
"$target_out_dir/brave_new_tab.bundle.js",
"$target_out_dir/brave_payments.bundle.js",
"$target_out_dir/welcome.bundle.js"
]
gen_dir = rebase_path(root_gen_dir, "//")
args = [
"--target_gen_dir=$gen_dir"
]
if (is_official_build) {
args += [
"--production",
]
}
}



grit("brave_components_resources_grit") {
source = "brave_components_resources.grd"

@@ -9,6 +37,11 @@ grit("brave_components_resources_grit") {
"grit/brave_components_resources.h",
"brave_components_resources.pak",
]
deps = [ ":transpile_web_uis" ]
grit_flags = [
"-E",
"root_gen_dir=" + rebase_path(root_gen_dir, root_build_dir),
]
output_dir = "$root_gen_dir/components"
resource_ids = "//brave/browser/resources/resource_ids"
}
@@ -13,7 +13,13 @@

<!-- WebUI resrouces -->
<include name="IDR_BRAVE_PAYMENTS_HTML" file="../brave_payments_ui/brave_payments.html" type="BINDATA" />
<include name="IDR_BRAVE_PAYMENTS_JS" file="../brave_payments_ui/brave_payments.js" type="BINDATA" />
<include name="IDR_BRAVE_PAYMENTS_JS" file="${root_gen_dir}/brave/brave_payments.bundle.js" type="BINDATA" use_base_dir="false" />

<include name="IDR_BRAVE_NEW_TAB_HTML" file="../brave_new_tab_ui/brave_new_tab.html" type="BINDATA" />
<include name="IDR_BRAVE_NEW_TAB_JS" file="${root_gen_dir}/brave/brave_new_tab.bundle.js" type="BINDATA" use_base_dir="false" />

<include name="IDR_BRAVE_WELCOME_HTML" file="../welcome_ui/welcome.html" type="BINDATA" />
<include name="IDR_BRAVE_WELCOME_JS" file="${root_gen_dir}/brave/welcome.bundle.js" type="BINDATA" use_base_dir="false" />
</includes>
</release>
</grit>
@@ -0,0 +1,51 @@
const path = require('path')
const webpack = require('webpack')
const postCSSConfig = require('./postcss.config')

module.exports = {
entry: {
brave_new_tab: path.join(__dirname, '../brave_new_tab_ui/brave_new_tab'),
welcome: path.join(__dirname, '../welcome_ui/welcome'),
brave_payments: path.join(__dirname, '../brave_payments_ui/brave_payments')
},
output: {
path: path.join(__dirname, '..', '..', '..', process.env.TARGET_GEN_DIR, 'brave'),

This comment has been minimized.

Copy link
@bridiver

bridiver Jan 18, 2018

Collaborator

with just one env var this isn't a big deal, but might want to think about generating from a template so we can inject directly from gn

This comment has been minimized.

Copy link
@bridiver

bridiver Jan 18, 2018

Collaborator

that would also make it easier to combine dev and prod

This comment has been minimized.

Copy link
@bbondy

bbondy Jan 18, 2018

Author Member

agreed, will do this at a later date.
Posted here to track it:
https://github.com/brave/brave/issues/59

filename: '[name].bundle.js',
chunkFilename: '[id].chunk.js'
},
plugins: [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.IgnorePlugin(/[^/]+\/[\S]+.dev$/),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('development')
}
})
],
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader'
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['react-optimize']
}
}, {
test: /\.css$/,
loaders: [
'style-loader',
'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]',
{loader: 'postcss-loader', options: postCSSConfig}
]
}]
}
}

@@ -0,0 +1,4 @@
module.exports = {
plugins: [
]
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.