1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 jsonRpc.responseObject = null;
8 jsonRpc.setLastEvent = function(action, value, modifiers) {
9 var request = jsonRpc.generateJsonRpcRequest(
10 'SetLastEvent', [action, value, modifiers]);
11 return jsonRpc.sendRpcRequest(request);
14 jsonRpc.getLastEvent = function() {
15 var request = jsonRpc.generateJsonRpcRequest('GetLastEvent', []);
16 return jsonRpc.sendRpcRequest(request);
19 jsonRpc.clearLastEvent = function() {
20 var request = jsonRpc.generateJsonRpcRequest('ClearLastEvent', []);
21 return jsonRpc.sendRpcRequest(request);
25 * Generate the JSON request.
26 * @param {string} methodname The name of the remote method.
27 * @param {list} params The method parameters to pass.
28 * @param {number=} opt_ident The request id.
29 * @return The JSON-RPC request object
31 jsonRpc.generateJsonRpcRequest = function(methodname, params, opt_ident) {
32 ident = opt_ident == undefined ? 0 : opt_ident;
43 * Method to POST the request to the RPC server.
44 * @param {object} json_request The JSON request object.
46 jsonRpc.sendRpcRequest = function(json_request) {
47 jsonRpc.responseObject = null;
48 var xhr = new XMLHttpRequest();
49 xhr.open('POST', '/RPC2', true);
50 xhr.onreadystatechange = function () {
51 if (xhr.readyState == 4 && xhr.status == 200) {
53 var response = xhr.responseText;
54 jsonRpc.responseObject = JSON.parse(response).response;
56 console.error('Could not parse server response.');
61 string_request = JSON.stringify(json_request);
62 xhr.send(string_request);