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

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

Issue 10273016: Refactor the socket API to remove onEvent callback in socket.create() function. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Update Created 8 years, 7 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 // Custom bindings for the experimental.socket API.
6
7 var experimentalSocketNatives = requireNative('experimental_socket');
8 var GetNextSocketEventId = experimentalSocketNatives.GetNextSocketEventId;
9
10 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
11 var sendRequest = require('sendRequest').sendRequest;
12 var lazyBG = requireNative('lazy_background_page');
13
14 chromeHidden.registerCustomHook('experimental.socket', function(api) {
15 var apiFunctions = api.apiFunctions;
16
17 apiFunctions.setHandleRequest('create', function() {
18 var args = arguments;
19 if (args.length > 1 && args[1] && args[1].onEvent) {
20 var id = GetNextSocketEventId();
21 args[1].srcId = id;
22 chromeHidden.socket.handlers[id] = args[1].onEvent;
23
24 // Keep the page alive until the event finishes.
25 // Balanced in eventHandler.
26 lazyBG.IncrementKeepaliveCount();
27 }
28 sendRequest(this.name, args, this.definition.parameters);
29 return id;
30 });
31
32 // Set up events.
33 chromeHidden.socket = {};
34 chromeHidden.socket.handlers = {};
35 chrome.experimental.socket.onEvent.addListener(function(event) {
36 var eventHandler = chromeHidden.socket.handlers[event.srcId];
37 if (eventHandler) {
38 switch (event.type) {
39 case 'writeComplete':
40 case 'connectComplete':
41 eventHandler({
42 type: event.type,
43 resultCode: event.resultCode,
44 });
45 break;
46 case 'dataRead':
47 eventHandler({
48 type: event.type,
49 resultCode: event.resultCode,
50 data: event.data,
51 });
52 break;
53 default:
54 console.error('Unexpected SocketEvent, type ' + event.type);
55 break;
56 }
57 if (event.isFinalEvent) {
58 delete chromeHidden.socket.handlers[event.srcId];
59 // Balanced in 'create' handler.
60 lazyBG.DecrementKeepaliveCount();
61 }
62 }
63 });
64 });
OLDNEW
« no previous file with comments | « chrome/renderer/renderer_resources.grd ('k') | chrome/test/data/extensions/api_test/socket/api/background.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698