Commit d34074f0 authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS MultiDevice] Convert MojoApiBehavior to class-based approach.

The previous approach used a behavior, meaning that any Polymer element
which needs to access the JS Mojo API needs to create its own
InterfacePtr to the Mojo service. In a follow-up CL, I will be adding a
new client of this interface, so it would have been wasteful to create
two separate connections to the service.

This CL converts this behavior to a class-based approach which utilizes
a singleton class which stores a single instance of the pointer.

Bug: 884058
Change-Id: I0b2c13677309b9949a0f26d2f11dd40ea8f15b1c
Reviewed-on: https://chromium-review.googlesource.com/1247035Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#594518}
parent 54341715
...@@ -30,7 +30,7 @@ cr.define('multidevice_setup', () => { ...@@ -30,7 +30,7 @@ cr.define('multidevice_setup', () => {
setup(() => { setup(() => {
multiDeviceSetupElement = document.createElement('multidevice-setup'); multiDeviceSetupElement = document.createElement('multidevice-setup');
multiDeviceSetupElement.multideviceSetup = new FakeMojoService(); multiDeviceSetupElement.multideviceSetup_ = new FakeMojoService();
multiDeviceSetupElement.uiMode = multidevice_setup.UiMode.POST_OOBE; multiDeviceSetupElement.uiMode = multidevice_setup.UiMode.POST_OOBE;
document.body.appendChild(multiDeviceSetupElement); document.body.appendChild(multiDeviceSetupElement);
...@@ -66,7 +66,7 @@ cr.define('multidevice_setup', () => { ...@@ -66,7 +66,7 @@ cr.define('multidevice_setup', () => {
}); });
setVisiblePage(START); setVisiblePage(START);
multiDeviceSetupElement.multideviceSetup.shouldSetHostSucceed = true; multiDeviceSetupElement.multideviceSetup_.shouldSetHostSucceed = true;
multiDeviceSetupElement.uiMode = multidevice_setup.UiMode.OOBE; multiDeviceSetupElement.uiMode = multidevice_setup.UiMode.OOBE;
backwardButton.click(); backwardButton.click();
...@@ -83,7 +83,7 @@ cr.define('multidevice_setup', () => { ...@@ -83,7 +83,7 @@ cr.define('multidevice_setup', () => {
}); });
setVisiblePage(START); setVisiblePage(START);
multiDeviceSetupElement.multideviceSetup.shouldSetHostSucceed = multiDeviceSetupElement.multideviceSetup_.shouldSetHostSucceed =
true; true;
multiDeviceSetupElement.uiMode = multidevice_setup.UiMode.OOBE; multiDeviceSetupElement.uiMode = multidevice_setup.UiMode.OOBE;
...@@ -98,7 +98,7 @@ cr.define('multidevice_setup', () => { ...@@ -98,7 +98,7 @@ cr.define('multidevice_setup', () => {
multiDeviceSetupElement.addEventListener('setup-exited', () => done()); multiDeviceSetupElement.addEventListener('setup-exited', () => done());
setVisiblePage(START); setVisiblePage(START);
multiDeviceSetupElement.multideviceSetup.shouldSetHostSucceed = true; multiDeviceSetupElement.multideviceSetup_.shouldSetHostSucceed = true;
multiDeviceSetupElement.uiMode = multidevice_setup.UiMode.POST_OOBE; multiDeviceSetupElement.uiMode = multidevice_setup.UiMode.POST_OOBE;
backwardButton.click(); backwardButton.click();
...@@ -108,7 +108,7 @@ cr.define('multidevice_setup', () => { ...@@ -108,7 +108,7 @@ cr.define('multidevice_setup', () => {
multiDeviceSetupElement.addEventListener('setup-exited', () => done()); multiDeviceSetupElement.addEventListener('setup-exited', () => done());
setVisiblePage(PASSWORD); setVisiblePage(PASSWORD);
multiDeviceSetupElement.multideviceSetup.shouldSetHostSucceed = true; multiDeviceSetupElement.multideviceSetup_.shouldSetHostSucceed = true;
multiDeviceSetupElement.uiMode = multidevice_setup.UiMode.POST_OOBE; multiDeviceSetupElement.uiMode = multidevice_setup.UiMode.POST_OOBE;
backwardButton.click(); backwardButton.click();
...@@ -125,7 +125,7 @@ cr.define('multidevice_setup', () => { ...@@ -125,7 +125,7 @@ cr.define('multidevice_setup', () => {
}); });
setVisiblePage(PASSWORD); setVisiblePage(PASSWORD);
multiDeviceSetupElement.multideviceSetup.shouldSetHostSucceed = multiDeviceSetupElement.multideviceSetup_.shouldSetHostSucceed =
true; true;
multiDeviceSetupElement.uiMode = multidevice_setup.UiMode.POST_OOBE; multiDeviceSetupElement.uiMode = multidevice_setup.UiMode.POST_OOBE;
......
...@@ -10,7 +10,7 @@ js_type_check("closure_compile") { ...@@ -10,7 +10,7 @@ js_type_check("closure_compile") {
deps = [ deps = [
":button_bar", ":button_bar",
":fake_mojo_service", ":fake_mojo_service",
":mojo_api_behavior", ":mojo_api",
":multidevice_setup", ":multidevice_setup",
":multidevice_setup_browser_proxy", ":multidevice_setup_browser_proxy",
":setup_failed_page", ":setup_failed_page",
...@@ -49,14 +49,14 @@ js_library("fake_mojo_service") { ...@@ -49,14 +49,14 @@ js_library("fake_mojo_service") {
] ]
} }
js_library("mojo_api_behavior") { js_library("mojo_api") {
} }
js_library("multidevice_setup") { js_library("multidevice_setup") {
deps = [ deps = [
":button_bar", ":button_bar",
":fake_mojo_service", ":fake_mojo_service",
":mojo_api_behavior", ":mojo_api",
":password_page", ":password_page",
":setup_failed_page", ":setup_failed_page",
":setup_succeeded_page", ":setup_succeeded_page",
......
...@@ -8,5 +8,5 @@ ...@@ -8,5 +8,5 @@
</script> </script>
<script src="chrome://resources/js/chromeos/multidevice_setup_constants.mojom.js"> <script src="chrome://resources/js/chromeos/multidevice_setup_constants.mojom.js">
</script> </script>
<script src="chrome://resources/cr_components/chromeos/multidevice_setup/mojo_api_behavior.js"> <script src="chrome://resources/cr_components/chromeos/multidevice_setup/mojo_api.js">
</script> </script>
// Copyright 2018 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.
cr.define('multidevice_setup', function() {
/** @interface */
class MojoInterfaceProvider {
/**
* @return {!chromeos.multideviceSetup.mojom.MultiDeviceSetupImpl}
*/
getInterfacePtr() {}
}
/** @implements {multidevice_setup.MojoInterfaceProvider} */
class MojoInterfaceProviderImpl {
constructor() {
/** @private {!chromeos.multideviceSetup.mojom.MultiDeviceSetupPtr} */
this.ptr_ = new chromeos.multideviceSetup.mojom.MultiDeviceSetupPtr();
Mojo.bindInterface(
chromeos.multideviceSetup.mojom.MultiDeviceSetup.name,
mojo.makeRequest(this.ptr_).handle);
}
/** @override */
getInterfacePtr() {
return this.ptr_;
}
}
cr.addSingletonGetter(MojoInterfaceProviderImpl);
return {
MojoInterfaceProvider: MojoInterfaceProvider,
MojoInterfaceProviderImpl: MojoInterfaceProviderImpl,
};
});
// Copyright 2018 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.
/** @polymerBehavior */
const MojoApiBehavior = {
properties: {
/**
* Provides access to the MultiDeviceSetup Mojo service.
* @type {!chromeos.multideviceSetup.mojom.MultiDeviceSetupImpl}
*/
multideviceSetup: {
type: Object,
value: function() {
let ptr = new chromeos.multideviceSetup.mojom.MultiDeviceSetupPtr();
Mojo.bindInterface(
chromeos.multideviceSetup.mojom.MultiDeviceSetup.name,
mojo.makeRequest(ptr).handle);
return ptr;
},
}
},
};
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<link rel="import" href="chrome://resources/cr_components/chromeos/multidevice_setup/button_bar.html"> <link rel="import" href="chrome://resources/cr_components/chromeos/multidevice_setup/button_bar.html">
<link rel="import" href="chrome://resources/cr_components/chromeos/multidevice_setup/fake_mojo_service.html"> <link rel="import" href="chrome://resources/cr_components/chromeos/multidevice_setup/fake_mojo_service.html">
<link rel="import" href="chrome://resources/cr_components/chromeos/multidevice_setup/mojo_api_behavior.html"> <link rel="import" href="chrome://resources/cr_components/chromeos/multidevice_setup/mojo_api.html">
<link rel="import" href="chrome://resources/cr_components/chromeos/multidevice_setup/multidevice_setup_shared_css.html"> <link rel="import" href="chrome://resources/cr_components/chromeos/multidevice_setup/multidevice_setup_shared_css.html">
<link rel="import" href="chrome://resources/cr_components/chromeos/multidevice_setup/password_page.html"> <link rel="import" href="chrome://resources/cr_components/chromeos/multidevice_setup/password_page.html">
<link rel="import" href="chrome://resources/cr_components/chromeos/multidevice_setup/setup_failed_page.html"> <link rel="import" href="chrome://resources/cr_components/chromeos/multidevice_setup/setup_failed_page.html">
......
...@@ -88,20 +88,29 @@ cr.define('multidevice_setup', function() { ...@@ -88,20 +88,29 @@ cr.define('multidevice_setup', function() {
computed: 'shouldForwardButtonBeDisabled_(' + computed: 'shouldForwardButtonBeDisabled_(' +
'passwordPageForwardButtonDisabled_, visiblePageName_)', 'passwordPageForwardButtonDisabled_, visiblePageName_)',
}, },
},
behaviors: [ /**
MojoApiBehavior, * Interface to the MultiDeviceSetup Mojo service.
], * @private {!chromeos.multideviceSetup.mojom.MultiDeviceSetupImpl}
*/
multideviceSetup_: Object
},
listeners: { listeners: {
'backward-navigation-requested': 'onBackwardNavigationRequested_', 'backward-navigation-requested': 'onBackwardNavigationRequested_',
'forward-navigation-requested': 'onForwardNavigationRequested_', 'forward-navigation-requested': 'onForwardNavigationRequested_',
}, },
/** @override */
created: function() {
this.multideviceSetup_ =
multidevice_setup.MojoInterfaceProviderImpl.getInstance()
.getInterfacePtr();
},
/** @override */ /** @override */
attached: function() { attached: function() {
this.multideviceSetup.getEligibleHostDevices() this.multideviceSetup_.getEligibleHostDevices()
.then((responseParams) => { .then((responseParams) => {
if (responseParams.eligibleHostDevices.length == 0) { if (responseParams.eligibleHostDevices.length == 0) {
console.warn('Potential host list is empty.'); console.warn('Potential host list is empty.');
...@@ -138,7 +147,7 @@ cr.define('multidevice_setup', function() { ...@@ -138,7 +147,7 @@ cr.define('multidevice_setup', function() {
case PageName.PASSWORD: case PageName.PASSWORD:
this.$.passwordPage.clearPasswordTextInput(); this.$.passwordPage.clearPasswordTextInput();
let deviceId = /** @type {string} */ (this.selectedDeviceId_); let deviceId = /** @type {string} */ (this.selectedDeviceId_);
this.multideviceSetup.setHostDevice(deviceId, this.authToken_) this.multideviceSetup_.setHostDevice(deviceId, this.authToken_)
.then((responseParams) => { .then((responseParams) => {
if (!responseParams.success) { if (!responseParams.success) {
console.warn( console.warn(
......
...@@ -264,12 +264,12 @@ ...@@ -264,12 +264,12 @@
file="cr_components/chromeos/multidevice_setup/icons.html" file="cr_components/chromeos/multidevice_setup/icons.html"
type="chrome_html" type="chrome_html"
compress="gzip" /> compress="gzip" />
<structure name="IDR_WEBUI_CHROMEOS_MULTIDEVICE_SETUP_MOJO_API_BEHAVIOR_HTML" <structure name="IDR_WEBUI_CHROMEOS_MULTIDEVICE_SETUP_MOJO_API_HTML"
file="cr_components/chromeos/multidevice_setup/mojo_api_behavior.html" file="cr_components/chromeos/multidevice_setup/mojo_api.html"
type="chrome_html" type="chrome_html"
compress="gzip" /> compress="gzip" />
<structure name="IDR_WEBUI_CHROMEOS_MULTIDEVICE_SETUP_MOJO_API_BEHAVIOR_JS" <structure name="IDR_WEBUI_CHROMEOS_MULTIDEVICE_SETUP_MOJO_API_JS"
file="cr_components/chromeos/multidevice_setup/mojo_api_behavior.js" file="cr_components/chromeos/multidevice_setup/mojo_api.js"
type="chrome_html" type="chrome_html"
compress="gzip" /> compress="gzip" />
<structure name="IDR_WEBUI_CHROMEOS_MULTIDEVICE_SETUP_MULTIDEVICE_SETUP_HTML" <structure name="IDR_WEBUI_CHROMEOS_MULTIDEVICE_SETUP_MULTIDEVICE_SETUP_HTML"
......
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