Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: chrome/renderer/resources/extensions/experimental.bluetooth_custom_bindings.js

Issue 10915148: Change getDevices to use a DeviceCallback. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix test breakage Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Custom bindings for the Bluetooth API. 5 // Custom bindings for the Bluetooth API.
6 6
7 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); 7 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
8 var sendRequest = require('sendRequest').sendRequest; 8 var sendRequest = require('sendRequest').sendRequest;
9 var lastError = require('last_error'); 9 var lastError = require('lastError');
10 10
11 // Use custom bindings to create an undocumented event listener that will 11 // Use custom bindings to create an undocumented event listener that will
12 // receive events about device discovery and call the event listener that was 12 // receive events about device discovery and call the event listener that was
13 // provided with the request to begin discovery. 13 // provided with the request to begin discovery.
14 chromeHidden.registerCustomHook('experimental.bluetooth', function(api) { 14 chromeHidden.registerCustomHook('experimental.bluetooth', function(api) {
15 var apiFunctions = api.apiFunctions; 15 var apiFunctions = api.apiFunctions;
16 16
17 chromeHidden.bluetooth = {}; 17 chromeHidden.bluetooth = {};
18
19 function callCallbackIfPresent(args) {
20 if (typeof(args[args.length-1]) == "function") {
21 args[args.length-1]();
22 }
23 }
24
18 chromeHidden.bluetooth.deviceDiscoveredHandler = null; 25 chromeHidden.bluetooth.deviceDiscoveredHandler = null;
19 chromeHidden.bluetooth.onDeviceDiscovered = 26 chromeHidden.bluetooth.onDeviceDiscovered =
20 new chrome.Event("experimental.bluetooth.onDeviceDiscovered"); 27 new chrome.Event("experimental.bluetooth.onDeviceDiscovered");
21
22 function clearDeviceDiscoveredHandler() { 28 function clearDeviceDiscoveredHandler() {
23 chromeHidden.bluetooth.onDeviceDiscovered.removeListener( 29 chromeHidden.bluetooth.onDeviceDiscovered.removeListener(
24 chromeHidden.bluetooth.deviceDiscoveredHandler); 30 chromeHidden.bluetooth.deviceDiscoveredHandler);
25 chromeHidden.bluetooth.deviceDiscoveredHandler = null; 31 chromeHidden.bluetooth.deviceDiscoveredHandler = null;
26 } 32 }
27
28 apiFunctions.setHandleRequest('startDiscovery', 33 apiFunctions.setHandleRequest('startDiscovery',
29 function() { 34 function() {
30 var args = arguments; 35 var args = arguments;
31 if (args.length > 0 && args[0] && args[0].deviceCallback) { 36 if (args.length > 0 && args[0] && args[0].deviceCallback) {
37 if (chromeHidden.bluetooth.deviceDiscoveredHandler != null) {
38 lastError.set("Concurrent discovery is not allowed.");
39 callCallbackIfPresent(args);
40 return;
41 }
42
32 chromeHidden.bluetooth.deviceDiscoveredHandler = 43 chromeHidden.bluetooth.deviceDiscoveredHandler =
33 args[0].deviceCallback; 44 args[0].deviceCallback;
34 chromeHidden.bluetooth.onDeviceDiscovered.addListener( 45 chromeHidden.bluetooth.onDeviceDiscovered.addListener(
35 chromeHidden.bluetooth.deviceDiscoveredHandler); 46 chromeHidden.bluetooth.deviceDiscoveredHandler);
36 sendRequest(this.name, 47 sendRequest(this.name,
37 args, 48 args,
38 this.definition.parameters, 49 this.definition.parameters,
39 {customCallback:this.customCallback}); 50 {customCallback:this.customCallback});
40 } else { 51 } else {
41 if (typeof(args[args.length-1]) == "function") { 52 lastError.set("deviceCallback is required in the options object");
42 var callback = args[args.length-1]; 53 callCallbackIfPresent(args);
43 lastError.set("deviceCallback is required in the options object");
44 callback();
45 return;
46 }
47 } 54 }
48 }); 55 });
49 apiFunctions.setCustomCallback('startDiscovery', 56 apiFunctions.setCustomCallback('startDiscovery',
50 function(name, request, response) { 57 function(name, request, response) {
51 if (chrome.runtime.lastError) { 58 if (chrome.runtime.lastError) {
52 clearDeviceDiscoveredHandler(); 59 clearDeviceDiscoveredHandler();
53 return; 60 return;
54 } 61 }
55 }); 62 });
56 apiFunctions.setHandleRequest('stopDiscovery', 63 apiFunctions.setHandleRequest('stopDiscovery',
57 function() { 64 function() {
58 clearDeviceDiscoveredHandler(); 65 clearDeviceDiscoveredHandler();
59 sendRequest(this.name, arguments, this.definition.parameters); 66 sendRequest(this.name, arguments, this.definition.parameters);
60 }); 67 });
68
69 chromeHidden.bluetooth.deviceSearchResultHandler = null;
70 chromeHidden.bluetooth.onDeviceSearchResult =
71 new chrome.Event("experimental.bluetooth.onDeviceSearchResult");
72 apiFunctions.setHandleRequest('getDevices',
73 function() {
74 var args = arguments;
75 if (args.length > 0 && args[0] && args[0].deviceCallback) {
76 if (chromeHidden.bluetooth.deviceSearchResultHandler != null) {
77 lastError.set("Concurrent calls to getDevices are not allowed.");
78 callCallbackIfPresent(args);
79 return;
80 }
81
82 chromeHidden.bluetooth.deviceSearchResultHandler =
83 args[0].deviceCallback;
84 chromeHidden.bluetooth.onDeviceSearchResult.addListener(
85 chromeHidden.bluetooth.deviceSearchResultHandler);
86 sendRequest(this.name,
87 args,
88 this.definition.parameters,
89 {customCallback:this.customCallback});
90 } else {
91 lastError.set("deviceCallback is required in the options object");
92 callCallbackIfPresent(args);
93 }
94 });
95 apiFunctions.setCustomCallback('getDevices',
96 function(name, request, response) {
97 chromeHidden.bluetooth.onDeviceSearchResult.removeListener(
98 chromeHidden.bluetooth.deviceSearchResultHandler);
99 chromeHidden.bluetooth.deviceSearchResultHandler = null;
100 });
61 }); 101 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698