diff options
author | sammc@chromium.org <sammc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-30 11:28:43 +0000 |
---|---|---|
committer | sammc@chromium.org <sammc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-30 11:28:43 +0000 |
commit | e8a5608bbec4c3ddfa491b01c122b3900a21980f (patch) | |
tree | 655237d35d31c9e6ed889479b6a98ecb40a85ff1 /extensions | |
parent | 0a5fdad7e4f2104722baecabbe16990da1911b8f (diff) | |
download | chromium_src-e8a5608bbec4c3ddfa491b01c122b3900a21980f.zip chromium_src-e8a5608bbec4c3ddfa491b01c122b3900a21980f.tar.gz chromium_src-e8a5608bbec4c3ddfa491b01c122b3900a21980f.tar.bz2 |
Implement chrome.serial.getDevices() on top of the Mojo serial service.
This does not change Chrome to use the new implementation.
BUG=389016
Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=286122
Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=286199
Review URL: https://codereview.chromium.org/391413005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@286480 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'extensions')
-rw-r--r-- | extensions/BUILD.gn | 4 | ||||
-rw-r--r-- | extensions/extensions.gyp | 1 | ||||
-rw-r--r-- | extensions/extensions_resources.gyp | 6 | ||||
-rw-r--r-- | extensions/renderer/api/serial/DEPS | 3 | ||||
-rw-r--r-- | extensions/renderer/api/serial/serial_api_unittest.cc | 65 | ||||
-rw-r--r-- | extensions/renderer/resources/binding.js | 19 | ||||
-rw-r--r-- | extensions/renderer/resources/extensions_renderer_resources.grd | 3 | ||||
-rw-r--r-- | extensions/renderer/resources/serial_custom_bindings.js | 30 | ||||
-rw-r--r-- | extensions/renderer/resources/serial_service.js | 42 | ||||
-rw-r--r-- | extensions/test/data/serial_unittest.js | 24 |
10 files changed, 197 insertions, 0 deletions
diff --git a/extensions/BUILD.gn b/extensions/BUILD.gn index 945d1c10..ca1295e 100644 --- a/extensions/BUILD.gn +++ b/extensions/BUILD.gn @@ -30,4 +30,8 @@ grit("extensions_renderer_resources") { "grit/extensions_renderer_resources.h", "extensions_renderer_resources.pak", ] + grit_flags = [ "-E", "mojom_root=" + rebase_path(root_gen_dir) ] + deps = [ + "//device/serial:serial_mojo", + ] } diff --git a/extensions/extensions.gyp b/extensions/extensions.gyp index 9117ce0..cec99e6 100644 --- a/extensions/extensions.gyp +++ b/extensions/extensions.gyp @@ -795,6 +795,7 @@ 'common/one_shot_event_unittest.cc', 'common/permissions/manifest_permission_set_unittest.cc', 'common/user_script_unittest.cc', + 'renderer/api/serial/serial_api_unittest.cc', 'renderer/api_test_base.cc', 'renderer/api_test_base.h', 'renderer/api_test_base_unittest.cc', diff --git a/extensions/extensions_resources.gyp b/extensions/extensions_resources.gyp index 8c56e20..0a000ac 100644 --- a/extensions/extensions_resources.gyp +++ b/extensions/extensions_resources.gyp @@ -7,6 +7,9 @@ { 'target_name': 'extensions_resources', 'type': 'none', + 'dependencies': [ + '../device/serial/serial.gyp:device_serial_mojo', + ], 'variables': { 'grit_out_dir': '<(SHARED_INTERMEDIATE_DIR)/extensions', }, @@ -22,6 +25,9 @@ 'action_name': 'generate_extensions_renderer_resources', 'variables': { 'grit_grd_file': 'renderer/resources/extensions_renderer_resources.grd', + 'grit_additional_defines': [ + '-E', 'mojom_root=<(SHARED_INTERMEDIATE_DIR)', + ], }, 'includes': [ '../build/grit_action.gypi' ], }, diff --git a/extensions/renderer/api/serial/DEPS b/extensions/renderer/api/serial/DEPS new file mode 100644 index 0000000..e273c39 --- /dev/null +++ b/extensions/renderer/api/serial/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "+device/serial", +] diff --git a/extensions/renderer/api/serial/serial_api_unittest.cc b/extensions/renderer/api/serial/serial_api_unittest.cc new file mode 100644 index 0000000..3f551ca --- /dev/null +++ b/extensions/renderer/api/serial/serial_api_unittest.cc @@ -0,0 +1,65 @@ +// Copyright 2014 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 "device/serial/serial_device_enumerator.h" +#include "device/serial/serial_service_impl.h" +#include "extensions/renderer/api_test_base.h" +#include "grit/extensions_renderer_resources.h" + +namespace extensions { + +namespace { + +class FakeSerialDeviceEnumerator : public device::SerialDeviceEnumerator { + virtual mojo::Array<device::serial::DeviceInfoPtr> GetDevices() OVERRIDE { + mojo::Array<device::serial::DeviceInfoPtr> result(3); + result[0] = device::serial::DeviceInfo::New(); + result[0]->path = "device"; + result[0]->vendor_id = 1234; + result[0]->has_vendor_id = true; + result[0]->product_id = 5678; + result[0]->has_product_id = true; + result[0]->display_name = "foo"; + result[1] = device::serial::DeviceInfo::New(); + result[1]->path = "another_device"; + // These IDs should be ignored. + result[1]->vendor_id = 1234; + result[1]->product_id = 5678; + result[2] = device::serial::DeviceInfo::New(); + result[2]->display_name = ""; + return result.Pass(); + } +}; + +} // namespace + +void CreateSerialService( + mojo::InterfaceRequest<device::serial::SerialService> request) { + mojo::BindToRequest( + new device::SerialServiceImpl( + new device::SerialConnectionFactory( + device::SerialConnectionFactory::IoHandlerFactory(), + base::MessageLoopProxy::current()), + scoped_ptr<device::SerialDeviceEnumerator>( + new FakeSerialDeviceEnumerator)), + &request); +} + +class SerialApiTest : public ApiTestBase { + public: + virtual void SetUp() OVERRIDE { + ApiTestBase::SetUp(); + env()->RegisterModule("serial", IDR_SERIAL_CUSTOM_BINDINGS_JS); + env()->RegisterModule("serial_service", IDR_SERIAL_SERVICE_JS); + env()->RegisterModule("device/serial/serial.mojom", IDR_SERIAL_MOJOM_JS); + service_provider()->AddService<device::serial::SerialService>( + base::Bind(CreateSerialService)); + } +}; + +TEST_F(SerialApiTest, GetDevices) { + RunTest("serial_unittest.js", "testGetDevices"); +} + +} // namespace extensions diff --git a/extensions/renderer/resources/binding.js b/extensions/renderer/resources/binding.js index eb858d7..be2056a 100644 --- a/extensions/renderer/resources/binding.js +++ b/extensions/renderer/resources/binding.js @@ -5,6 +5,7 @@ var Event = require('event_bindings').Event; var forEach = require('utils').forEach; var GetAvailability = requireNative('v8_context').GetAvailability; +var lastError = require('lastError'); var logActivity = requireNative('activityLogger'); var logging = requireNative('logging'); var process = requireNative('process'); @@ -62,6 +63,24 @@ APIFunctions.prototype.setHandleRequest = }); }; +APIFunctions.prototype.setHandleRequestWithPromise = + function(apiName, customizedFunction) { + var prefix = this.namespace; + return this.setHook_(apiName, 'handleRequest', function() { + var name = prefix + '.' + apiName; + logActivity.LogAPICall(extensionId, name, $Array.slice(arguments)); + var stack = sendRequestHandler.getExtensionStackTrace(); + var callback = arguments[arguments.length - 1]; + var args = $Array.slice(arguments, 0, arguments.length - 1); + $Function.apply(customizedFunction, this, args).then(function(result) { + sendRequestHandler.safeCallbackApply( + name, {'stack': stack}, callback, [result]); + }).catch(function(error) { + lastError.run(name, error.message, stack, callback); + }); + }); +}; + APIFunctions.prototype.setUpdateArgumentsPostValidate = function(apiName, customizedFunction) { return this.setHook_( diff --git a/extensions/renderer/resources/extensions_renderer_resources.grd b/extensions/renderer/resources/extensions_renderer_resources.grd index a2c0283..8ca06e2 100644 --- a/extensions/renderer/resources/extensions_renderer_resources.grd +++ b/extensions/renderer/resources/extensions_renderer_resources.grd @@ -18,6 +18,9 @@ <include name="IDR_MESSAGING_UTILS_JS" file="messaging_utils.js" type="BINDATA" /> <include name="IDR_SCHEMA_UTILS_JS" file="schema_utils.js" type="BINDATA" /> <include name="IDR_SEND_REQUEST_JS" file="send_request.js" type="BINDATA" /> + <include name="IDR_SERIAL_CUSTOM_BINDINGS_JS" file="serial_custom_bindings.js" type="BINDATA" /> + <include name="IDR_SERIAL_MOJOM_JS" file="${mojom_root}\device\serial\serial.mojom.js" use_base_dir="false" type="BINDATA" /> + <include name="IDR_SERIAL_SERVICE_JS" file="serial_service.js" type="BINDATA" /> <include name="IDR_SET_ICON_JS" file="set_icon.js" type="BINDATA" /> <include name="IDR_BROWSER_TEST_ENVIRONMENT_SPECIFIC_BINDINGS_JS" file="browser_test_environment_specific_bindings.js" type="BINDATA" /> <include name="IDR_TEST_CUSTOM_BINDINGS_JS" file="test_custom_bindings.js" type="BINDATA" /> diff --git a/extensions/renderer/resources/serial_custom_bindings.js b/extensions/renderer/resources/serial_custom_bindings.js new file mode 100644 index 0000000..3d15acf --- /dev/null +++ b/extensions/renderer/resources/serial_custom_bindings.js @@ -0,0 +1,30 @@ +// Copyright 2014 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. + +var binding = require('binding').Binding.create('serial'); + +function createAsyncProxy(targetPromise, functionNames) { + var functionProxies = {}; + $Array.forEach(functionNames, function(name) { + functionProxies[name] = function() { + var args = arguments; + return targetPromise.then(function(target) { + return $Function.apply(target[name], target, args); + }); + } + }); + return functionProxies; +} + +var serialService = createAsyncProxy(requireAsync('serial_service'), [ + 'getDevices', +]); + +binding.registerCustomHook(function(bindingsAPI) { + var apiFunctions = bindingsAPI.apiFunctions; + apiFunctions.setHandleRequestWithPromise('getDevices', + serialService.getDevices); +}); + +exports.binding = binding.generate(); diff --git a/extensions/renderer/resources/serial_service.js b/extensions/renderer/resources/serial_service.js new file mode 100644 index 0000000..92e17d0 --- /dev/null +++ b/extensions/renderer/resources/serial_service.js @@ -0,0 +1,42 @@ +// Copyright 2014 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. + +define('serial_service', [ + 'content/public/renderer/service_provider', + 'device/serial/serial.mojom', + 'mojo/public/js/bindings/router', +], function(serviceProvider, serialMojom, routerModule) { + + function defineService(proxy, handle) { + if (!handle) + handle = serviceProvider.connectToService(proxy.NAME_); + var router = new routerModule.Router(handle); + var service = new proxy(router); + return { + service: service, + router: router, + }; + } + + var service = defineService(serialMojom.SerialServiceProxy).service; + + function getDevices() { + return service.getDevices().then(function(response) { + return $Array.map(response.devices, function(device) { + var result = {path: device.path || ''}; + if (device.has_vendor_id) + result.vendorId = device.vendor_id; + if (device.has_product_id) + result.productId = device.product_id; + if (device.display_name) + result.displayName = device.display_name; + return result; + }); + }); + } + + return { + getDevices: getDevices, + }; +}); diff --git a/extensions/test/data/serial_unittest.js b/extensions/test/data/serial_unittest.js new file mode 100644 index 0000000..0755ed7 --- /dev/null +++ b/extensions/test/data/serial_unittest.js @@ -0,0 +1,24 @@ +// Copyright 2014 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. + +var test = require('test').binding; +var serial = require('serial').binding; +var unittestBindings = require('test_environment_specific_bindings'); + +unittestBindings.exportTests([ + function testGetDevices() { + serial.getDevices(test.callbackPass(function(devices) { + test.assertEq(3, devices.length); + test.assertEq(4, $Object.keys(devices[0]).length); + test.assertEq('device', devices[0].path); + test.assertEq(1234, devices[0].vendorId); + test.assertEq(5678, devices[0].productId); + test.assertEq('foo', devices[0].displayName); + test.assertEq(1, $Object.keys(devices[1]).length); + test.assertEq('another_device', devices[1].path); + test.assertEq(1, $Object.keys(devices[2]).length); + test.assertEq('', devices[2].path); + })); + }, +], test.runTests, exports); |