Skip to content

Commit 0d9e1f5

Browse files
committed
Added visible mdns discovery for testing.
1 parent 5c37c8a commit 0d9e1f5

File tree

7 files changed

+140
-24
lines changed

7 files changed

+140
-24
lines changed

app/chrome_app/background.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,21 @@ chrome.app.runtime.onLaunched.addListener(
4545
hidden: false,
4646
};
4747

48-
chrome.app.window.create('index.html', editorConfig, function(
49-
editorWindow) {
50-
if (editorWindow) {
51-
editorWindow.outerBounds.setPosition(
48+
chrome.app.window.create('index.html', editorConfig, function(editor) {
49+
if (editor) {
50+
editor.outerBounds.setPosition(
5251
Math.round((screenWidth - editorWidth) / 2),
5352
Math.round((screenHeight - editorHeight) / 2)
5453
);
55-
editorWindow.drawAttention();
54+
editor.drawAttention();
55+
editor.contentWindow.addEventListener('load', function() {
56+
console.log('Looking for p2p devices ...');
57+
chrome.mdns.onServiceList.addListener((data) => {
58+
console.log('Found p2p device', data);
59+
}, {
60+
serviceType: '_cros_p2p._tcp.local',
61+
});
62+
});
5663
} else {
5764
console.warn('Loaded inside sand-boxed window!');
5865
}

app/chrome_app/manifest.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,13 @@
7777
"storage", {
7878
"fileSystem": ["write"]
7979
},
80-
"system.network",
81-
"serial",
8280
"identity",
81+
"mdns",
82+
"serial",
83+
"system.network",
8384
"tts",
84-
"usb",
8585
"unlimitedStorage",
86+
"usb",
8687
"webview"
8788
],
8889
"sockets": {

src/protocol/low-level/mdns/api.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @fileoverview Handles the mDNS discovery.
3+
*
4+
* @license Copyright 2018 The Coding with Chrome Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @author [email protected] (Markus Bordihn)
19+
*/
20+
goog.provide('cwc.protocol.mDNS.Api');
21+
22+
goog.require('cwc.utils.Logger');
23+
24+
25+
/**
26+
* @constructor
27+
* @struct
28+
* @final
29+
*/
30+
cwc.protocol.mDNS.Api = function() {
31+
/** @type {string} */
32+
this.name = 'mDNS';
33+
34+
/** @type {!Object} */
35+
this.services = {};
36+
37+
/** @type {boolean} */
38+
this.prepared = false;
39+
40+
/** @private {!cwc.utils.Logger} */
41+
this.log_ = new cwc.utils.Logger(this.name);
42+
};
43+
44+
45+
cwc.protocol.mDNS.Api.prototype.prepare = function() {
46+
if (!chrome.mdns) {
47+
this.log_.warn('mDNS support is not available!');
48+
this.log_.warn(chrome.mdns);
49+
return;
50+
}
51+
if (this.prepared) {
52+
return;
53+
}
54+
this.log_.info('Preparing mDNS support...');
55+
56+
// Monitor P2P devices
57+
chrome.mdns.onServiceList.addListener((data) => {
58+
this.log('Found p2p device', data);
59+
this.services['_cros_p2p._tcp.local'] = data;
60+
}, {
61+
serviceType: '_cros_p2p._tcp.local',
62+
});
63+
this.prepared = true;
64+
};

src/ui/builder.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ goog.require('cwc.lib.protocol.bluetoothChrome.Api');
3535
goog.require('cwc.lib.protocol.bluetoothWeb.Api');
3636
goog.require('cwc.mode.Modder');
3737
goog.require('cwc.protocol.arduino.Api');
38+
goog.require('cwc.protocol.mDNS.Api');
3839
goog.require('cwc.protocol.raspberryPi.Api');
3940
goog.require('cwc.protocol.serial.Api');
4041
goog.require('cwc.protocol.tcp.HTTPServer');
@@ -124,6 +125,7 @@ cwc.ui.BuilderHelpers = {
124125
cwc.ui.supportedProtocols = {
125126
// Low-level
126127
'serial': cwc.protocol.serial.Api,
128+
'mdns': cwc.protocol.mDNS.Api,
127129

128130
// Boards
129131
'arduino': cwc.protocol.arduino.Api,
@@ -273,9 +275,6 @@ cwc.ui.Builder.prototype.decorateUI = function() {
273275
this.prepareOauth2Helper);
274276
}
275277
this.setProgressFunc('Render editor GUI ...', this.renderGui);
276-
if (this.helper.checkChromeFeature('serial')) {
277-
this.setProgressFunc('Prepare Serial support ...', this.prepareSerial);
278-
}
279278
if (this.helper.checkChromeFeature('manifest.oauth2')) {
280279
this.setProgressFunc('Prepare account support ...', this.prepareAccount);
281280
}
@@ -418,17 +417,6 @@ cwc.ui.Builder.prototype.prepareBluetooth = function() {
418417
};
419418

420419

421-
/**
422-
* Prepare Serial interface if needed.
423-
*/
424-
cwc.ui.Builder.prototype.prepareSerial = function() {
425-
let serialInstance = this.helper.getInstance('serial');
426-
if (serialInstance) {
427-
serialInstance.prepare();
428-
}
429-
};
430-
431-
432420
/**
433421
* Prepare internal Servers if needed.
434422
*/

src/utils/feature.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @fileoverview Simplified feature detection.
3+
*
4+
* This helper class provides shortcuts to get the different of UI elements.
5+
*
6+
* @license Copyright 2018 The Coding with Chrome Authors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*
20+
* @author [email protected] (Markus Bordihn)
21+
*/
22+
goog.provide('cwc.lib.utils.Feature');
23+
24+
25+
/**
26+
* @param {string} name
27+
* @return {boolean|Object|Function}
28+
* @export
29+
*/
30+
cwc.lib.utils.Feature.getChromeFeature = function(name) {
31+
if (typeof chrome === 'undefined') {
32+
return false;
33+
}
34+
return cwc.lib.utils.Feature.getFeature(
35+
name.startsWith('chrome.') ? name : 'chrome.' + name);
36+
};
37+
38+
39+
/**
40+
* @param {string} name
41+
* @return {boolean|Object|Function}
42+
* @export
43+
*/
44+
cwc.lib.utils.Feature.getFeature = function(name) {
45+
try {
46+
let feature = Function('return ' + name)();
47+
if (typeof feature === 'object' || typeof feature === 'function') {
48+
return feature;
49+
}
50+
} catch (error) {
51+
return false;
52+
}
53+
return false;
54+
};

src/utils/features.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ cwc.utils.Features.prototype.detectFeatures = function() {
7474
this.detectOnlineStatus();
7575
this.detectJavaScripts();
7676
this.monitorOnlineStatus();
77-
this.log();
7877
};
7978

8079

@@ -139,6 +138,7 @@ cwc.utils.Features.prototype.detectChromeFeatures = function() {
139138
this.setChromeFeature('bluetoothLowEnergy', typeof chrome.bluetoothLowEnergy);
140139
this.setChromeFeature('browser', typeof chrome.browser);
141140
this.setChromeFeature('serial', typeof chrome.serial);
141+
this.setChromeFeature('mdns', typeof chrome.mdns);
142142
this.setChromeFeature('webview', 'src' in document.createElement('webview'));
143143

144144
// System features.

test/deps.js

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)