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

Side by Side Diff: chrome/renderer/resources/extensions/extension_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, 10 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 extension API. 5 // Custom bindings for the extension API.
6 6
7 (function() { 7 (function() {
8 8
9 native function GetChromeHidden(); 9 native function GetChromeHidden();
10 native function GetExtensionViews(); 10 native function GetExtensionViews();
11 native function OpenChannelToExtension(sourceId, targetId, name); 11 native function OpenChannelToExtension(sourceId, targetId, name);
12 12
13 var chromeHidden = GetChromeHidden(); 13 var chromeHidden = GetChromeHidden();
14 14
15 // This should match chrome.windows.WINDOW_ID_NONE. 15 // This should match chrome.windows.WINDOW_ID_NONE.
16 // 16 //
17 // We can't use chrome.windows.WINDOW_ID_NONE directly because the 17 // We can't use chrome.windows.WINDOW_ID_NONE directly because the
18 // chrome.windows API won't exist unless this extension has permission for it; 18 // chrome.windows API won't exist unless this extension has permission for it;
19 // which may not be the case. 19 // which may not be the case.
20 var WINDOW_ID_NONE = -1; 20 var WINDOW_ID_NONE = -1;
21 21
22 chromeHidden.registerCustomHook('extension', 22 chromeHidden.registerCustomHook('extension',
23 function(bindingsAPI, extensionId) { 23 function(bindingsAPI, extensionId) {
24 var apiFunctions = bindingsAPI.apiFunctions; 24 var apiFunctions = bindingsAPI.apiFunctions;
25 25
26 apiFunctions.setHandleRequest("extension.getViews", function(properties) { 26 apiFunctions.setHandleRequest('getViews', function(properties) {
27 var windowId = WINDOW_ID_NONE; 27 var windowId = WINDOW_ID_NONE;
28 var type = "ALL"; 28 var type = 'ALL';
29 if (typeof(properties) != "undefined") { 29 if (typeof(properties) != 'undefined') {
30 if (typeof(properties.type) != "undefined") { 30 if (typeof(properties.type) != 'undefined') {
31 type = properties.type; 31 type = properties.type;
32 } 32 }
33 if (typeof(properties.windowId) != "undefined") { 33 if (typeof(properties.windowId) != 'undefined') {
34 windowId = properties.windowId; 34 windowId = properties.windowId;
35 } 35 }
36 } 36 }
37 return GetExtensionViews(windowId, type) || null; 37 return GetExtensionViews(windowId, type) || null;
38 }); 38 });
39 39
40 apiFunctions.setHandleRequest("extension.getBackgroundPage", function() { 40 apiFunctions.setHandleRequest('getBackgroundPage', function() {
41 return GetExtensionViews(-1, "BACKGROUND")[0] || null; 41 return GetExtensionViews(-1, 'BACKGROUND')[0] || null;
42 }); 42 });
43 43
44 apiFunctions.setHandleRequest("extension.getExtensionTabs", 44 apiFunctions.setHandleRequest('getExtensionTabs', function(windowId) {
45 function(windowId) { 45 if (typeof(windowId) == 'undefined')
46 if (typeof(windowId) == "undefined")
47 windowId = WINDOW_ID_NONE; 46 windowId = WINDOW_ID_NONE;
48 return GetExtensionViews(windowId, "TAB"); 47 return GetExtensionViews(windowId, 'TAB');
49 }); 48 });
50 49
51 apiFunctions.setHandleRequest("extension.getURL", function(path) { 50 apiFunctions.setHandleRequest('getURL', function(path) {
52 path = String(path); 51 path = String(path);
53 if (!path.length || path[0] != "/") 52 if (!path.length || path[0] != '/')
54 path = "/" + path; 53 path = '/' + path;
55 return "chrome-extension://" + extensionId + path; 54 return 'chrome-extension://' + extensionId + path;
56 }); 55 });
57 56
58 apiFunctions.setUpdateArgumentsPreValidate("extension.sendRequest", 57 apiFunctions.setUpdateArgumentsPreValidate('sendRequest', function() {
59 function() {
60 // Align missing (optional) function arguments with the arguments that 58 // Align missing (optional) function arguments with the arguments that
61 // schema validation is expecting, e.g. 59 // schema validation is expecting, e.g.
62 // extension.sendRequest(req) -> extension.sendRequest(null, req) 60 // extension.sendRequest(req) -> extension.sendRequest(null, req)
63 // extension.sendRequest(req, cb) -> extension.sendRequest(null, req, cb) 61 // extension.sendRequest(req, cb) -> extension.sendRequest(null, req, cb)
64 var lastArg = arguments.length - 1; 62 var lastArg = arguments.length - 1;
65 63
66 // responseCallback (last argument) is optional. 64 // responseCallback (last argument) is optional.
67 var responseCallback = null; 65 var responseCallback = null;
68 if (typeof(arguments[lastArg]) == "function") 66 if (typeof(arguments[lastArg]) == 'function')
69 responseCallback = arguments[lastArg--]; 67 responseCallback = arguments[lastArg--];
70 68
71 // request (second argument) is required. 69 // request (second argument) is required.
72 var request = arguments[lastArg--]; 70 var request = arguments[lastArg--];
73 71
74 // targetId (first argument, extensionId in the manfiest) is optional. 72 // targetId (first argument, extensionId in the manfiest) is optional.
75 var targetId = null; 73 var targetId = null;
76 if (lastArg >= 0) 74 if (lastArg >= 0)
77 targetId = arguments[lastArg--]; 75 targetId = arguments[lastArg--];
78 76
79 if (lastArg != -1) 77 if (lastArg != -1)
80 throw new Error("Invalid arguments to sendRequest."); 78 throw new Error('Invalid arguments to sendRequest.');
81 return [targetId, request, responseCallback]; 79 return [targetId, request, responseCallback];
82 }); 80 });
83 81
84 apiFunctions.setHandleRequest( 82 apiFunctions.setHandleRequest('sendRequest',
85 "extension.sendRequest", 83 function(targetId, request, responseCallback) {
86 function(targetId, request, responseCallback) {
87 if (!targetId) 84 if (!targetId)
88 targetId = extensionId; 85 targetId = extensionId;
89 if (!responseCallback) 86 if (!responseCallback)
90 responseCallback = function() {}; 87 responseCallback = function() {};
91 88
92 var connectInfo = { name: chromeHidden.kRequestChannel }; 89 var connectInfo = { name: chromeHidden.kRequestChannel };
93 var port = chrome.extension.connect(targetId, connectInfo); 90 var port = chrome.extension.connect(targetId, connectInfo);
94 91
95 port.postMessage(request); 92 port.postMessage(request);
96 port.onDisconnect.addListener(function() { 93 port.onDisconnect.addListener(function() {
97 // For onDisconnects, we only notify the callback if there was an error 94 // For onDisconnects, we only notify the callback if there was an error
98 try { 95 try {
99 if (chrome.extension.lastError) 96 if (chrome.extension.lastError)
100 responseCallback(); 97 responseCallback();
101 } finally { 98 } finally {
102 port = null; 99 port = null;
103 } 100 }
104 }); 101 });
105 port.onMessage.addListener(function(response) { 102 port.onMessage.addListener(function(response) {
106 try { 103 try {
107 responseCallback(response); 104 responseCallback(response);
108 } finally { 105 } finally {
109 port.disconnect(); 106 port.disconnect();
110 port = null; 107 port = null;
111 } 108 }
112 }); 109 });
113 }); 110 });
114 111
115 apiFunctions.setUpdateArgumentsPreValidate("extension.connect", function() { 112 apiFunctions.setUpdateArgumentsPreValidate('connect', function() {
116 // Align missing (optional) function arguments with the arguments that 113 // Align missing (optional) function arguments with the arguments that
117 // schema validation is expecting, e.g. 114 // schema validation is expecting, e.g.
118 // extension.connect() -> extension.connect(null, null) 115 // extension.connect() -> extension.connect(null, null)
119 // extension.connect({}) -> extension.connect(null, {}) 116 // extension.connect({}) -> extension.connect(null, {})
120 var nextArg = 0; 117 var nextArg = 0;
121 118
122 // targetId (first argument) is optional. 119 // targetId (first argument) is optional.
123 var targetId = null; 120 var targetId = null;
124 if (typeof(arguments[nextArg]) == "string") 121 if (typeof(arguments[nextArg]) == 'string')
125 targetId = arguments[nextArg++]; 122 targetId = arguments[nextArg++];
126 123
127 // connectInfo (second argument) is optional. 124 // connectInfo (second argument) is optional.
128 var connectInfo = null; 125 var connectInfo = null;
129 if (typeof(arguments[nextArg]) == "object") 126 if (typeof(arguments[nextArg]) == 'object')
130 connectInfo = arguments[nextArg++]; 127 connectInfo = arguments[nextArg++];
131 128
132 if (nextArg != arguments.length) 129 if (nextArg != arguments.length)
133 throw new Error("Invalid arguments to connect"); 130 throw new Error('Invalid arguments to connect');
134 return [targetId, connectInfo]; 131 return [targetId, connectInfo];
135 }); 132 });
136 133
137 apiFunctions.setHandleRequest("extension.connect", 134 apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) {
138 function(targetId, connectInfo) {
139 if (!targetId) 135 if (!targetId)
140 targetId = extensionId; 136 targetId = extensionId;
141 var name = ""; 137 var name = '';
142 if (connectInfo && connectInfo.name) 138 if (connectInfo && connectInfo.name)
143 name = connectInfo.name; 139 name = connectInfo.name;
144 140
145 var portId = OpenChannelToExtension(extensionId, targetId, name); 141 var portId = OpenChannelToExtension(extensionId, targetId, name);
146 if (portId >= 0) 142 if (portId >= 0)
147 return chromeHidden.Port.createPort(portId, name); 143 return chromeHidden.Port.createPort(portId, name);
148 throw new Error("Error connecting to extension '" + targetId + "'"); 144 throw new Error('Error connecting to extension ' + targetId);
149 }); 145 });
150 }); 146 });
151 147
152 })(); 148 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698