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

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

Issue 9423049: Make registering custom hooks with schema_generated_bindings.js safer and (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 9 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 experimental.socket API. 5 // Custom bindings for the experimental.socket API.
6 6
7 (function() { 7 (function() {
8 native function GetChromeHidden(); 8 native function GetChromeHidden();
9 native function GetNextSocketEventId(); 9 native function GetNextSocketEventId();
10 10
11 var chromeHidden = GetChromeHidden(); 11 var chromeHidden = GetChromeHidden();
12 12
13 chromeHidden.registerCustomHook('experimental.socket', function(api) { 13 chromeHidden.registerCustomHook('experimental.socket', function(api) {
14 var apiFunctions = api.apiFunctions; 14 var apiFunctions = api.apiFunctions;
15 var sendRequest = api.sendRequest; 15 var sendRequest = api.sendRequest;
16 16
17 apiFunctions.setHandleRequest("experimental.socket.create", function() { 17 apiFunctions.setHandleRequest('create', function() {
18 var args = arguments; 18 var args = arguments;
19 if (args.length > 3 && args[3] && args[3].onEvent) { 19 if (args.length > 3 && args[3] && args[3].onEvent) {
20 var id = GetNextSocketEventId(); 20 var id = GetNextSocketEventId();
21 args[3].srcId = id; 21 args[3].srcId = id;
22 chromeHidden.socket.handlers[id] = args[3].onEvent; 22 chromeHidden.socket.handlers[id] = args[3].onEvent;
23 } 23 }
24 sendRequest(this.name, args, this.definition.parameters); 24 sendRequest(this.name, args, this.definition.parameters);
25 return id; 25 return id;
26 }); 26 });
27 27
28 // Set up events. 28 // Set up events.
29 chromeHidden.socket = {}; 29 chromeHidden.socket = {};
30 chromeHidden.socket.handlers = {}; 30 chromeHidden.socket.handlers = {};
31 chrome.experimental.socket.onEvent.addListener(function(event) { 31 chrome.experimental.socket.onEvent.addListener(function(event) {
32 var eventHandler = chromeHidden.socket.handlers[event.srcId]; 32 var eventHandler = chromeHidden.socket.handlers[event.srcId];
33 if (eventHandler) { 33 if (eventHandler) {
34 switch (event.type) { 34 switch (event.type) {
35 case "writeComplete": 35 case 'writeComplete':
36 case "connectComplete": 36 case 'connectComplete':
37 eventHandler({ 37 eventHandler({
38 type: event.type, 38 type: event.type,
39 resultCode: event.resultCode, 39 resultCode: event.resultCode,
40 }); 40 });
41 break; 41 break;
42 case "dataRead": 42 case 'dataRead':
43 eventHandler({ 43 eventHandler({
44 type: event.type, 44 type: event.type,
45 resultCode: event.resultCode, 45 resultCode: event.resultCode,
46 data: event.data, 46 data: event.data,
47 }); 47 });
48 break; 48 break;
49 default: 49 default:
50 console.error("Unexpected SocketEvent, type " + event.type); 50 console.error('Unexpected SocketEvent, type ' + event.type);
51 break; 51 break;
52 } 52 }
53 if (event.isFinalEvent) { 53 if (event.isFinalEvent) {
54 delete chromeHidden.socket.handlers[event.srcId]; 54 delete chromeHidden.socket.handlers[event.srcId];
55 } 55 }
56 } 56 }
57 }); 57 });
58 }); 58 });
59 59
60 })(); 60 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698