Commit 4a9a9899 authored by Yue Zhang's avatar Yue Zhang Committed by Commit Bot

[ntp][modules] Request data for dummy modules via mojo

Bug: 1110059
Change-Id: Ib6e1e86dfa1a92a83e9aa7b02549cd18792ead9f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2399569Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarWill Harris <wfh@chromium.org>
Reviewed-by: default avatarTibor Goldschwendt <tiborg@chromium.org>
Commit-Queue: Yue Zhang <yuezhanggg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#814846}
parent 8049a527
......@@ -6132,6 +6132,10 @@ static_library("browser") {
if (tpm_fallback) {
defines += [ "TPM_FALLBACK" ]
}
if (!is_android && !is_official_build) {
deps += [ "//chrome/browser/ui/webui/new_tab_page/foo:mojo_bindings" ]
}
}
if (is_android) {
......
......@@ -114,6 +114,9 @@
#include "chrome/browser/speech/speech_recognition_service_factory.h"
#include "chrome/browser/ui/webui/downloads/downloads.mojom.h"
#include "chrome/browser/ui/webui/downloads/downloads_ui.h"
#if !defined(OFFICIAL_BUILD)
#include "chrome/browser/ui/webui/new_tab_page/foo/foo.mojom.h" // nogncheck crbug.com/1125897
#endif
#include "chrome/browser/ui/webui/media/media_feeds_ui.h"
#include "chrome/browser/ui/webui/new_tab_page/new_tab_page.mojom.h"
#include "chrome/browser/ui/webui/new_tab_page/new_tab_page_ui.h"
......@@ -615,6 +618,11 @@ void PopulateChromeWebUIFrameBinders(
#endif // !defined(OS_CHROMEOS)
>(map);
#if !defined(OFFICIAL_BUILD)
RegisterWebUIControllerInterfaceBinder<foo::mojom::FooHandler, NewTabPageUI>(
map);
#endif // !defined(OFFICIAL_BUILD)
RegisterWebUIControllerInterfaceBinder<media_feeds::mojom::MediaFeedsStore,
MediaFeedsUI>(map);
......
......@@ -225,6 +225,10 @@ if (!is_android) {
"//chrome/browser/ui/webui/new_tab_page:mojo_bindings_js",
"//skia/public/mojom:mojom_js",
]
if (!is_official_build) {
deps += [ "//chrome/browser/ui/webui/new_tab_page/foo:mojo_bindings_js" ]
}
if (optimize_webui) {
deps += [ "//chrome/browser/resources/new_tab_page:optimized_js" ]
} else {
......
......@@ -32,6 +32,7 @@ js_type_check("closure_compile") {
"modules:module_descriptor",
"modules:module_registry",
"modules:modules",
"modules/dummy:foo_proxy",
]
}
......@@ -336,6 +337,7 @@ if (optimize_webui) {
"omnibox.mojom-lite.js",
"promo_browser_command.mojom-lite.js",
"modules/shopping_tasks/shopping_tasks.mojom-lite.js",
"foo.mojom-lite.js",
]
}
}
......@@ -14,10 +14,12 @@ js_library("module_descriptor") {
js_library("modules") {
deps = [
":module_registry",
"dummy:module",
"kaleidoscope:module",
"shopping_tasks:module",
]
if (!is_official_build) {
deps += [ "dummy:module" ]
}
}
js_library("module_wrapper") {
......@@ -63,6 +65,9 @@ if (optimize_webui) {
"module_registry.js",
"shopping_tasks/shopping_tasks_handler_proxy.js",
]
if (!is_official_build) {
in_files += [ "dummy/foo_proxy.js" ]
}
}
preprocess_grit("preprocess_gen") {
......
......@@ -7,6 +7,7 @@ import("//tools/polymer/html_to_js.gni")
js_library("module") {
deps = [
":foo_proxy",
"..:module_descriptor",
"../..:img",
"//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled",
......@@ -14,6 +15,13 @@ js_library("module") {
]
}
js_library("foo_proxy") {
deps = [
"//chrome/browser/ui/webui/new_tab_page/foo:mojo_bindings_js_library_for_compile",
"//ui/webui/resources/js:cr.m",
]
}
html_to_js("web_components") {
js_files = [ "module.js" ]
}
// Copyright 2020 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.
import '../../foo.mojom-lite.js';
import {addSingletonGetter} from 'chrome://resources/js/cr.m.js';
/**
* @fileoverview This file provides a class that exposes the Mojo handler
* interface used for sending requests from NTP dummy module JS to the browser
* and receiving the browser response.
*/
export class FooProxy {
constructor() {
/** @type {!foo.mojom.FooHandlerRemote} */
this.handler = foo.mojom.FooHandler.getRemote();
}
}
addSingletonGetter(FooProxy);
\ No newline at end of file
......@@ -10,6 +10,7 @@ import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js';
import {html, PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {ModuleDescriptor} from '../module_descriptor.js';
import {FooProxy} from './foo_proxy.js';
/**
* @fileoverview A dummy module, which serves as an example and a helper to
......@@ -27,61 +28,21 @@ class DummyModuleElement extends PolymerElement {
static get properties() {
return {
tiles: {
type: Array,
value: () => ([
{
label: 'item1',
value: 'foo',
imageUrl: 'https://lh4.googleusercontent.com/proxy/' +
'kFIJNnm2DMbS3B5LXaIdm2JKI6twGWwmzQbcJCfqTfuaH_' +
'ULD50v1Z3BGPEF32xTPRvgGLx492zcy_kcatCde2wmz-9Z' +
'YFqifbJRMl2DzyE=w170-h85-p-k-no-nd-mv',
},
{
label: 'item2',
value: 'bar',
imageUrl: 'https://lh6.googleusercontent.com/proxy/' +
'KyyCsF6dIQ783r3Znmvdo76QY2RgzcR5t4rnA5kKjsmrlp' +
'sb_pWGndQkyuAI4mv68X_9ZX2Edd-0FP4iQZRFm8UAW3oD' +
'X8Coqk3C85UNAX3H4Eh_5wGyDB0SY6HOQjOXVQ=w170-h85-p-k-no-nd-mv',
},
{
label: 'item3',
value: 'baz',
imageUrl: 'https://lh6.googleusercontent.com/proxy/' +
'4IP40Q18w6aDF4oS4WRnUj0MlCCKPK-vLHqSd4r-RfS6Jx' +
'gblG5WJuRYpkJkoTzLMS0qv3Sxhf9wdaKkn3vHnyy6oe7Ah' +
'5y0=w170-h85-p-k-no-nd-mv',
},
{
label: 'item4',
value: 'foo',
imageUrl: 'https://lh3.googleusercontent.com/proxy/' +
'd_4gDNBtm9Ddv8zqqm0MVY93_j-_e5M-bGgH-bSAfIR65F' +
'YGacJTemvNp9fDT0eiIbi3bzrf7HMMsupe2QIIfm5H7BMH' +
'Y3AI5rkYUpx-lQ=w170-h85-p-k-no-nd-mv',
},
{
label: 'item5',
value: 'bar',
imageUrl: 'https://lh5.googleusercontent.com/proxy/' +
'xvtq6_782kBajCBr0GISHpujOb51XLKUeEOJ2lLPKh12-x' +
'NBTCtsoHT14NQcaH9l4JhatcXEMBkqgUeCWhb3XhdLnD1B' +
'iNzQ_LVydwg=w170-h85-p-k-no-nd-mv',
},
{
label: 'item6',
value: 'baz',
imageUrl: 'https://lh6.googleusercontent.com/proxy/' +
'fUx750lchxFJb3f37v_-4iJPzcTKtJbd5LDRO7S9Xy7nkP' +
'zh7HFU61tN36j4Diaa9Yk3K7kWshRwmqcrulnhbeJrRpIn' +
'79PjHN-N=w170-h85-p-k-no-nd-mv',
},
]),
}
/** @type {!Array<!foo.mojom.FooDataItem>} */
tiles: Array,
};
}
constructor() {
super();
this.initializeData_();
}
/** @private */
async initializeData_() {
const tileData = await FooProxy.getInstance().handler.getData();
this.tiles = tileData.data;
}
}
customElements.define(DummyModuleElement.is, DummyModuleElement);
......
......@@ -4,6 +4,8 @@
<include name="IDR_NEW_TAB_PAGE_MODULES_DUMMY_MODULE_JS"
file="${root_gen_dir}/chrome/browser/resources/new_tab_page/modules/dummy/module.js"
use_base_dir="false" type="BINDATA" compress="false" />
<include name="IDR_NEW_TAB_PAGE_MODULES_DUMMY_FOO_PROXY_JS"
file="modules/dummy/foo_proxy.js" type="BINDATA" compress="false" />
</if>
<include name="IDR_NEW_TAB_PAGE_MODULES_KALEIDOSCOPE_MODULE_JS"
file="modules/kaleidoscope/module.js"
......
......@@ -17,6 +17,7 @@ export {BrowserProxy} from './browser_proxy.js';
export {BackgroundSelectionType} from './customize_dialog.js';
export {ImgElement} from './img.js';
// <if expr="not is_official_build">
export {FooProxy} from './modules/dummy/foo_proxy.js';
export {dummyDescriptor} from './modules/dummy/module.js';
// </if>
export {kaleidoscopeDescriptor} from './modules/kaleidoscope/module.js';
......
......@@ -9,6 +9,11 @@
<include name="IDR_NEW_TAB_PAGE_PROMO_BROWSER_COMMAND_MOJO_LITE_JS"
file="${root_gen_dir}/chrome/browser/promo_browser_command/promo_browser_command.mojom-lite.js"
use_base_dir="false" type="BINDATA" />
<if expr="not is_official_build">
<include name="IDR_NEW_TAB_PAGE_FOO_MOJO_LITE_JS"
file="${root_gen_dir}/chrome/browser/ui/webui/new_tab_page/foo/foo.mojom-lite.js"
use_base_dir="false" type="BINDATA" />
</if>
<include name="IDR_NEW_TAB_PAGE_ACCOUNT_CIRCLE_SVG"
file="icons/account_circle.svg" type="BINDATA" />
<include name="IDR_NEW_TAB_PAGE_CHECK_CIRCLE_SVG"
......
......@@ -1579,6 +1579,14 @@ static_library("ui") {
"//google_apis/drive",
]
}
if (!is_official_build) {
sources += [
"webui/new_tab_page/foo/foo_handler.cc",
"webui/new_tab_page/foo/foo_handler.h",
]
deps += [ "//chrome/browser/ui/webui/new_tab_page/foo:mojo_bindings" ]
}
}
if (enable_paint_preview) {
......
# Copyright 2020 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.
import("//mojo/public/tools/bindings/mojom.gni")
mojom("mojo_bindings") {
sources = [ "foo.mojom" ]
}
file://chrome/browser/ui/webui/new_tab_page/OWNERS
per-file *.mojom=set noparent
per-file *.mojom=file://ipc/SECURITY_OWNERS
# COMPONENT: UI>Browser>NewTabPage
\ No newline at end of file
This directory contains the Mojo definitions for the dummy
module in NTP, which include an interface for a browser-side
handler to handle requests from JS. This directory also
contains FooHandler which is a concrete implementation of
the handler interface.
\ No newline at end of file
// Copyright 2020 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.
module foo.mojom;
// Information needed to show one item in dummy module.
struct FooDataItem {
string label;
string value;
string imageUrl;
};
// Browser-side handler for requests from JS.
interface FooHandler {
// Returns the data for items in dummy module.
GetData() => (array<FooDataItem> data);
};
\ No newline at end of file
// Copyright 2020 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.
#include "chrome/browser/ui/webui/new_tab_page/foo/foo_handler.h"
#include "base/strings/string_number_conversions.h"
FooHandler::FooHandler(mojo::PendingReceiver<foo::mojom::FooHandler> handler)
: handler_(this, std::move(handler)) {}
FooHandler::~FooHandler() = default;
void FooHandler::GetData(GetDataCallback callback) {
std::vector<std::tuple<std::string, std::string, std::string>> tileData(
{{"item1", "foo",
"https://lh4.googleusercontent.com/proxy/"
"kFIJNnm2DMbS3B5LXaIdm2JKI6twGWwmzQbcJCfqTfuaH_"
"ULD50v1Z3BGPEF32xTPRvgGLx492zcy_kcatCde2wmz-9ZYFqifbJRMl2DzyE=w170-"
"h85-p-k-no-nd-mv"},
{"item2", "bar",
"https://lh6.googleusercontent.com/proxy/"
"KyyCsF6dIQ783r3Znmvdo76QY2RgzcR5t4rnA5kKjsmrlpsb_pWGndQkyuAI4mv68X_"
"9ZX2Edd-0FP4iQZRFm8UAW3oDX8Coqk3C85UNAX3H4Eh_5wGyDB0SY6HOQjOXVQ=w170-"
"h85-p-k-no-nd-mv"},
{"item3", "baz",
"https://lh6.googleusercontent.com/proxy/"
"4IP40Q18w6aDF4oS4WRnUj0MlCCKPK-vLHqSd4r-"
"RfS6JxgblG5WJuRYpkJkoTzLMS0qv3Sxhf9wdaKkn3vHnyy6oe7Ah5y0=w170-h85-p-k-"
"no-nd-mv"},
{"item4", "foo",
"https://lh3.googleusercontent.com/proxy/"
"d_4gDNBtm9Ddv8zqqm0MVY93_j-_e5M-bGgH-"
"bSAfIR65FYGacJTemvNp9fDT0eiIbi3bzrf7HMMsupe2QIIfm5H7BMHY3AI5rkYUpx-lQ="
"w170-h85-p-k-no-nd-mv"},
{"item5", "bar",
"https://lh5.googleusercontent.com/proxy/"
"xvtq6_782kBajCBr0GISHpujOb51XLKUeEOJ2lLPKh12-"
"xNBTCtsoHT14NQcaH9l4JhatcXEMBkqgUeCWhb3XhdLnD1BiNzQ_LVydwg=w170-h85-p-"
"k-no-nd-mv"},
{"item6", "baz",
"https://lh6.googleusercontent.com/proxy/"
"fUx750lchxFJb3f37v_-"
"4iJPzcTKtJbd5LDRO7S9Xy7nkPzh7HFU61tN36j4Diaa9Yk3K7kWshRwmqcrulnhbeJrRp"
"In79PjHN-N=w170-h85-p-k-no-nd-mv"}});
std::vector<foo::mojom::FooDataItemPtr> data;
for (const auto& datum : tileData) {
auto data_item = foo::mojom::FooDataItem::New();
std::tie(data_item->label, data_item->value, data_item->imageUrl) = datum;
data.push_back(std::move(data_item));
}
std::move(callback).Run(std::move(data));
}
// Copyright 2020 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 CHROME_BROWSER_UI_WEBUI_NEW_TAB_PAGE_FOO_FOO_HANDLER_H_
#define CHROME_BROWSER_UI_WEBUI_NEW_TAB_PAGE_FOO_FOO_HANDLER_H_
#include "chrome/browser/ui/webui/new_tab_page/foo/foo.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
// Handles requests of dummy modules sent from JS.
class FooHandler : public foo::mojom::FooHandler {
public:
explicit FooHandler(mojo::PendingReceiver<foo::mojom::FooHandler> handler);
~FooHandler() override;
// foo::mojom::FooHandler:
void GetData(GetDataCallback callback) override;
private:
mojo::Receiver<foo::mojom::FooHandler> handler_;
};
#endif // CHROME_BROWSER_UI_WEBUI_NEW_TAB_PAGE_FOO_FOO_HANDLER_H_
......@@ -46,6 +46,10 @@
#include "ui/resources/grit/webui_resources.h"
#include "url/url_util.h"
#if !defined(OFFICIAL_BUILD)
#include "chrome/browser/ui/webui/new_tab_page/foo/foo_handler.h"
#endif
using content::BrowserContext;
using content::WebContents;
......@@ -245,6 +249,10 @@ content::WebUIDataSource* CreateNewTabPageUiHtmlSource(Profile* profile) {
source->AddResourcePath(
"modules/shopping_tasks/shopping_tasks.mojom-lite.js",
IDR_NEW_TAB_PAGE_MODULES_SHOPPING_TASKS_SHOPPING_TASKS_MOJO_LITE_JS);
#if !defined(OFFICIAL_BUILD)
source->AddResourcePath("foo.mojom-lite.js",
IDR_NEW_TAB_PAGE_FOO_MOJO_LITE_JS);
#endif
#if BUILDFLAG(OPTIMIZE_WEBUI)
source->AddResourcePath("new_tab_page.js", IDR_NEW_TAB_PAGE_NEW_TAB_PAGE_JS);
#endif // BUILDFLAG(OPTIMIZE_WEBUI)
......@@ -367,6 +375,12 @@ void NewTabPageUI::BindInterface(
shopping_tasks_handler_ = std::make_unique<ShoppingTasksHandler>(
std::move(pending_receiver), profile_);
}
#if !defined(OFFICIAL_BUILD)
void NewTabPageUI::BindInterface(
mojo::PendingReceiver<foo::mojom::FooHandler> pending_page_handler) {
foo_handler_ = std::make_unique<FooHandler>(std::move(pending_page_handler));
}
#endif
void NewTabPageUI::CreatePageHandler(
mojo::PendingRemote<new_tab_page::mojom::Page> pending_page,
......
......@@ -10,6 +10,9 @@
#include "chrome/browser/promo_browser_command/promo_browser_command.mojom-forward.h"
#include "chrome/browser/search/instant_service_observer.h"
#include "chrome/browser/search/shopping_tasks/shopping_tasks.mojom.h"
#if !defined(OFFICIAL_BUILD)
#include "chrome/browser/ui/webui/new_tab_page/foo/foo.mojom.h" // nogncheck crbug.com/1125897
#endif
#include "chrome/browser/ui/webui/new_tab_page/new_tab_page.mojom.h"
#include "content/public/browser/web_contents_observer.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
......@@ -25,6 +28,9 @@ class WebUI;
} // namespace content
class ChromeCustomizeThemesHandler;
#if !defined(OFFICIAL_BUILD)
class FooHandler;
#endif
class GURL;
class InstantService;
class KaleidoscopeDataProviderImpl;
......@@ -79,6 +85,13 @@ class NewTabPageUI
mojo::PendingReceiver<shopping_tasks::mojom::ShoppingTasksHandler>
pending_receiver);
#if !defined(OFFICIAL_BUILD)
// Instantiates the implementor of the foo::mojom::FooHandler mojo interface
// passing the pending receiver that will be internally bound.
void BindInterface(
mojo::PendingReceiver<foo::mojom::FooHandler> pending_receiver);
#endif
private:
// new_tab_page::mojom::PageHandlerFactory:
void CreatePageHandler(
......@@ -113,6 +126,9 @@ class NewTabPageUI
mojo::Receiver<customize_themes::mojom::CustomizeThemesHandlerFactory>
customize_themes_factory_receiver_;
std::unique_ptr<PromoBrowserCommandHandler> promo_browser_command_handler_;
#if !defined(OFFICIAL_BUILD)
std::unique_ptr<FooHandler> foo_handler_;
#endif
Profile* profile_;
InstantService* instant_service_;
content::WebContents* web_contents_;
......
......@@ -2,16 +2,41 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {$$, dummyDescriptor} from 'chrome://new-tab-page/new_tab_page.js';
import {dummyDescriptor, FooProxy} from 'chrome://new-tab-page/new_tab_page.js';
import {TestBrowserProxy} from 'chrome://test/test_browser_proxy.m.js';
import {isVisible} from 'chrome://test/test_util.m.js';
suite('NewTabPageModulesDummyModuleTest', () => {
let testProxy;
setup(() => {
PolymerTest.clearBody();
testProxy = FooProxy.getInstance();
testProxy.handler = TestBrowserProxy.fromClass(foo.mojom.FooHandlerRemote);
testProxy.handler.setResultFor('getData', Promise.resolve({data: []}));
});
test('creates module', async () => {
test('creates module with data', async () => {
// Act.
const data = [
{
label: 'item1',
value: 'foo',
imageUrl: 'foo.com',
},
{
label: 'item2',
value: 'bar',
imageUrl: 'bar.com',
},
{
label: 'item3',
value: 'baz',
imageUrl: 'baz.com',
},
];
testProxy.handler.setResultFor('getData', Promise.resolve({data}));
await dummyDescriptor.initialize();
const module = dummyDescriptor.element;
document.body.append(module);
......@@ -20,14 +45,22 @@ suite('NewTabPageModulesDummyModuleTest', () => {
// Assert.
assertTrue(isVisible(module.$.tiles));
const tiles = module.shadowRoot.querySelectorAll('#tiles .tile-item');
assertEquals(6, tiles.length);
assertEquals(3, tiles.length);
assertEquals('item3', tiles[2].getAttribute('title'));
assertEquals('baz', tiles[2].querySelector('span').textContent);
assertEquals(
'https://lh6.googleusercontent.com/proxy/' +
'4IP40Q18w6aDF4oS4WRnUj0MlCCKPK-vLHqSd4r-RfS6Jx' +
'gblG5WJuRYpkJkoTzLMS0qv3Sxhf9wdaKkn3vHnyy6oe7Ah' +
'5y0=w170-h85-p-k-no-nd-mv',
tiles[2].querySelector('img').autoSrc);
assertEquals('baz.com', tiles[2].querySelector('img').autoSrc);
});
test('creates module without data', async () => {
// Act.
await dummyDescriptor.initialize();
const module = dummyDescriptor.element;
document.body.append(module);
module.$.tileList.render();
// Assert.
assertFalse(isVisible(module.$.tiles));
const tiles = module.shadowRoot.querySelectorAll('#tiles .tile-item');
assertEquals(0, tiles.length);
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment