Skip to content

Commit

Permalink
[Cast2Class] Add page handler for enterprise casting WebUI.
Browse files Browse the repository at this point in the history
Stub implementation for the WebUI and page handler, to ensure we have
the mojom, WebUI, and page handler communicating correctly.
Additionally, add ChromeOS EDU team as OWNERS of the enterprise casting
resources folder.

Bug: b/193421383
Change-Id: I346203207f657b0ab1755cf46677749d6a916684
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3064083
Reviewed-by: calamity <calamity@chromium.org>
Reviewed-by: Brian Malcolm <bmalcolm@chromium.org>
Reviewed-by: Sam McNally <sammc@chromium.org>
Commit-Queue: Benjamin Zielinski <bzielinski@google.com>
Cr-Commit-Position: refs/heads/master@{#909038}
  • Loading branch information
Benjamin Zielinski authored and Chromium LUCI CQ committed Aug 5, 2021
1 parent 2d63471 commit 2424a7a
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 15 deletions.
44 changes: 33 additions & 11 deletions chrome/browser/resources/chromeos/enterprise_casting/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,53 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//third_party/closure_compiler/compile_js.gni")
import("//tools/grit/grit_rule.gni")
import("//tools/polymer/html_to_js.gni")
import("//tools/typescript/ts_library.gni")
import("//ui/webui/resources/tools/generate_grd.gni")

assert(is_chromeos, "enterprise_casting is Chrome OS only.")

mojo_grdp_file = "$target_gen_dir/mojo_resources.grdp"
resources_grd_file = "$target_gen_dir/resources.grd"

generate_grd("build_mojo_grdp") {
input_files = [ "enterprise_casting.mojom-webui.js" ]
input_files_base_dir = rebase_path(
"${root_gen_dir}/mojom-webui/chrome/browser/ui/webui/chromeos/enterprise_casting",
"$root_build_dir")
html_to_js("web_components") {
js_files = [ "enterprise_casting.ts" ]
}

copy("copy_browser_proxy") {
sources = [ "browser_proxy.ts" ]
outputs = [ "$target_gen_dir/{{source_file_part}}" ]
}

copy("copy_mojo") {
deps = [ "//chrome/browser/ui/webui/chromeos/enterprise_casting:mojo_bindings_webui_js" ]
mojom_folder = "$root_gen_dir/mojom-webui/chrome/browser/ui/webui/chromeos/enterprise_casting/"
sources = [ "$mojom_folder/enterprise_casting.mojom-webui.js" ]
outputs = [ "$target_gen_dir/{{source_file_part}}" ]
}

grd_prefix = "enterprise_casting"
out_grd = mojo_grdp_file
ts_library("build_ts") {
deps = [ "//third_party/polymer/v3_0:library" ]
extra_deps = [
":copy_browser_proxy",
":copy_mojo",
":web_components",
]
root_dir = "$target_gen_dir"
out_dir = "$target_gen_dir/tsc"
tsconfig_base = "tsconfig_base.json"
in_files = [
"browser_proxy.ts",
"enterprise_casting.mojom-webui.js",
"enterprise_casting.ts",
]
}

generate_grd("build_grd") {
input_files = [ "index.html" ]
input_files_base_dir = rebase_path(".", "//")
deps = [ ":build_mojo_grdp" ]
grdp_files = [ mojo_grdp_file ]
manifest_files = [ "$target_gen_dir/tsconfig.manifest" ]
deps = [ ":build_ts" ]

grd_prefix = "enterprise_casting"
out_grd = resources_grd_file
Expand Down
4 changes: 4 additions & 0 deletions chrome/browser/resources/chromeos/enterprise_casting/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gbj@google.com
bmalcolm@chromium.org
bzielinski@google.com
jacqueli@google.com
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2021 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 {PageCallbackRouter, PageHandlerFactory, PageHandlerRemote} from './enterprise_casting.mojom-webui.js';

export class BrowserProxy {
callbackRouter: PageCallbackRouter;
handler: PageHandlerRemote;

constructor() {
this.callbackRouter = new PageCallbackRouter();

this.handler = new PageHandlerRemote();

const factory = PageHandlerFactory.getRemote();
factory.createPageHandler(
this.callbackRouter.$.bindNewPipeAndPassRemote(),
this.handler.$.bindNewPipeAndPassReceiver());
}

static getInstance(): BrowserProxy {
return instance || (instance = new BrowserProxy());
}

static setInstance(obj: BrowserProxy) {
instance = obj;
}
}

let instance: BrowserProxy|null = null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Cast Receiver</h1>
<div id="castingPin">Casting PIN: [[pin]]</div>
<button id="newPin" on-click="requestPin">Generate New PIN</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2021 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 {html, PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';

import {BrowserProxy} from './browser_proxy.js';
import {PageCallbackRouter} from './enterprise_casting.mojom-webui.js';

class EnterpriseCastingElement extends PolymerElement {
static get is() {
return 'enterprise-casting';
}

static get template() {
return html`{__html_template__}`;
}

static get properties() {
return {
pin: String,
};
}

private pin: string;
private listenerIds: Array<number>;
private router: PageCallbackRouter;

constructor() {
super();
this.pin = '';
this.listenerIds = [];
this.router = BrowserProxy.getInstance().callbackRouter;
}

connectedCallback() {
super.connectedCallback();
this.listenerIds.push(
this.router.setPin.addListener(this.setPin.bind(this)));
}

disconnectedCallback() {
super.disconnectedCallback();
this.listenerIds.forEach(id => this.router.removeListener(id));
}

requestPin() {
BrowserProxy.getInstance().handler.updatePin();
}

private setPin(pin: string) {
if (this.pin != pin) {
this.pin = pin;
}
}
}

customElements.define(EnterpriseCastingElement.is, EnterpriseCastingElement);
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
<meta charset="utf-8">
<title>Cast Receiver</title>
<link rel="stylesheet" href="chrome://resources/css/text_defaults.css">
<script type="module" src="enterprise_casting.js"></script>
</head>

<body>
enterprise_casting Skeleton
<enterprise-casting></enterprise-casting>
</body>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../../../../tools/typescript/tsconfig_base.json",
"compilerOptions": {
"allowJs": true
}
}
2 changes: 2 additions & 0 deletions chrome/browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,8 @@ static_library("ui") {
"webui/chromeos/emoji/emoji_page_handler.h",
"webui/chromeos/emoji/emoji_ui.cc",
"webui/chromeos/emoji/emoji_ui.h",
"webui/chromeos/enterprise_casting/enterprise_casting_handler.cc",
"webui/chromeos/enterprise_casting/enterprise_casting_handler.h",
"webui/chromeos/enterprise_casting/enterprise_casting_ui.cc",
"webui/chromeos/enterprise_casting/enterprise_casting_ui.h",
"webui/chromeos/image_source.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@

module enterprise_casting.mojom;

// Interface for creating enterprise_casting handlers. Implemented by the WebUI
// controller and used by chrome://enterprise-casting.
// Used by the WebUI page to bootstrap bidirectional communication.
interface PageHandlerFactory {
// TODO(b/193421383): Add a CreatePageHandler method.
// The WebUI page's |BrowserProxy| singleton calls this method when the page
// is first initialized.
CreatePageHandler(pending_remote<Page> page,
pending_receiver<PageHandler> handler);
};

// Browser-side handler for requests from WebUI page.
interface PageHandler {
// Triggers a call to |SetPin|.
UpdatePin();
};

// WebUI-side handler for requests from the browser.
interface Page {
// Sets the page with a new casting pin.
SetPin(string pin);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2021 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/chromeos/enterprise_casting/enterprise_casting_handler.h"

#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"

namespace chromeos {

EnterpriseCastingHandler::EnterpriseCastingHandler(
mojo::PendingReceiver<enterprise_casting::mojom::PageHandler> page_handler,
mojo::PendingRemote<enterprise_casting::mojom::Page> page)
: page_(std::move(page)), receiver_(this, std::move(page_handler)) {}

EnterpriseCastingHandler::~EnterpriseCastingHandler() = default;

void EnterpriseCastingHandler::UpdatePin() {
page_->SetPin(base::NumberToString(base::RandInt(0, 9999)));
}

} // namespace chromeos
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2021 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_CHROMEOS_ENTERPRISE_CASTING_ENTERPRISE_CASTING_HANDLER_H_
#define CHROME_BROWSER_UI_WEBUI_CHROMEOS_ENTERPRISE_CASTING_ENTERPRISE_CASTING_HANDLER_H_

#include "base/scoped_observation.h"
#include "chrome/browser/ui/app_list/search/search_controller.h"
#include "chrome/browser/ui/webui/chromeos/enterprise_casting/enterprise_casting.mojom.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace chromeos {

class EnterpriseCastingHandler : public enterprise_casting::mojom::PageHandler {
public:
EnterpriseCastingHandler(
mojo::PendingReceiver<enterprise_casting::mojom::PageHandler>
page_handler,
mojo::PendingRemote<enterprise_casting::mojom::Page> page);
~EnterpriseCastingHandler() override;

// enterprise_casting::mojom::PageHandler :
void UpdatePin() override;

private:
mojo::Remote<enterprise_casting::mojom::Page> page_;
mojo::Receiver<enterprise_casting::mojom::PageHandler> receiver_;
};

} // namespace chromeos

#endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_ENTERPRISE_CASTING_ENTERPRISE_CASTING_HANDLER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ void EnterpriseCastingUI::BindInterface(
factory_receiver_.Bind(std::move(receiver));
}

void EnterpriseCastingUI::CreatePageHandler(
mojo::PendingRemote<enterprise_casting::mojom::Page> page,
mojo::PendingReceiver<enterprise_casting::mojom::PageHandler> receiver) {
DCHECK(page);

page_handler_ = std::make_unique<EnterpriseCastingHandler>(
std::move(receiver), std::move(page));
}

WEB_UI_CONTROLLER_TYPE_IMPL(EnterpriseCastingUI)

} // namespace chromeos
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define CHROME_BROWSER_UI_WEBUI_CHROMEOS_ENTERPRISE_CASTING_ENTERPRISE_CASTING_UI_H_

#include "chrome/browser/ui/webui/chromeos/enterprise_casting/enterprise_casting.mojom.h"
#include "chrome/browser/ui/webui/chromeos/enterprise_casting/enterprise_casting_handler.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
Expand All @@ -29,6 +30,13 @@ class EnterpriseCastingUI
receiver);

private:
// launcher_internals::mojom::PageHandlerFactory:
void CreatePageHandler(
mojo::PendingRemote<enterprise_casting::mojom::Page> page,
mojo::PendingReceiver<enterprise_casting::mojom::PageHandler>
page_handler) override;

std::unique_ptr<EnterpriseCastingHandler> page_handler_;
mojo::Receiver<enterprise_casting::mojom::PageHandlerFactory>
factory_receiver_{this};

Expand Down

0 comments on commit 2424a7a

Please sign in to comment.