OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // This script contains privileged chrome extension related javascript APIs. | 5 // This script contains privileged chrome extension related javascript APIs. |
6 // It is loaded by pages whose URL has the chrome-extension protocol. | 6 // It is loaded by pages whose URL has the chrome-extension protocol. |
7 | 7 |
8 var chrome = chrome || {}; | 8 var chrome = chrome || {}; |
9 (function() { | 9 (function() { |
10 native function GetExtensionAPIDefinition(); | 10 native function GetExtensionAPIDefinition(); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 "message": error | 85 "message": error |
86 }; | 86 }; |
87 } | 87 } |
88 | 88 |
89 if (request.customCallback) { | 89 if (request.customCallback) { |
90 request.customCallback(name, request, response); | 90 request.customCallback(name, request, response); |
91 } | 91 } |
92 | 92 |
93 if (request.callback) { | 93 if (request.callback) { |
94 // Callbacks currently only support one callback argument. | 94 // Callbacks currently only support one callback argument. |
95 var callbackArgs = response ? [JSON.parse(response)] : []; | 95 var callbackArgs = response ? [chromeHidden.JSON.parse(response)] : []; |
96 | 96 |
97 // Validate callback in debug only -- and only when the | 97 // Validate callback in debug only -- and only when the |
98 // caller has provided a callback. Implementations of api | 98 // caller has provided a callback. Implementations of api |
99 // calls my not return data if they observe the caller | 99 // calls my not return data if they observe the caller |
100 // has not provided a callback. | 100 // has not provided a callback. |
101 if (chromeHidden.validateCallbacks && !error) { | 101 if (chromeHidden.validateCallbacks && !error) { |
102 try { | 102 try { |
103 if (!request.callbackSchema.parameters) { | 103 if (!request.callbackSchema.parameters) { |
104 throw "No callback schemas defined"; | 104 throw "No callback schemas defined"; |
105 } | 105 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 // Send an API request and optionally register a callback. | 170 // Send an API request and optionally register a callback. |
171 function sendRequest(functionName, args, argSchemas, customCallback) { | 171 function sendRequest(functionName, args, argSchemas, customCallback) { |
172 var request = prepareRequest(args, argSchemas); | 172 var request = prepareRequest(args, argSchemas); |
173 if (customCallback) { | 173 if (customCallback) { |
174 request.customCallback = customCallback; | 174 request.customCallback = customCallback; |
175 } | 175 } |
176 // JSON.stringify doesn't support a root object which is undefined. | 176 // JSON.stringify doesn't support a root object which is undefined. |
177 if (request.args === undefined) | 177 if (request.args === undefined) |
178 request.args = null; | 178 request.args = null; |
179 | 179 |
180 // Some javascript libraries (e.g. prototype.js version <= 1.6) add a toJSON | 180 var sargs = chromeHidden.JSON.stringify(request.args); |
181 // serializer function on Array.prototype that is incompatible with our | 181 |
182 // native JSON library, causing incorrect deserialization in the C++ side of | |
183 // StartRequest. We work around that here by temporarily removing the toJSON | |
184 // function. | |
185 var arrayToJsonTmp; | |
186 if (Array.prototype.toJSON) { | |
187 arrayToJsonTmp = Array.prototype.toJSON; | |
188 Array.prototype.toJSON = null; | |
189 } | |
190 var sargs = JSON.stringify(request.args); | |
191 if (arrayToJsonTmp) { | |
192 Array.prototype.toJSON = arrayToJsonTmp; | |
193 } | |
194 var requestId = GetNextRequestId(); | 182 var requestId = GetNextRequestId(); |
195 requests[requestId] = request; | 183 requests[requestId] = request; |
196 var hasCallback = (request.callback || customCallback) ? true : false; | 184 var hasCallback = (request.callback || customCallback) ? true : false; |
197 return StartRequest(functionName, sargs, requestId, hasCallback); | 185 return StartRequest(functionName, sargs, requestId, hasCallback); |
198 } | 186 } |
199 | 187 |
200 // Send a special API request that is not JSON stringifiable, and optionally | 188 // Send a special API request that is not JSON stringifiable, and optionally |
201 // register a callback. | 189 // register a callback. |
202 function sendCustomRequest(nativeFunction, functionName, args, argSchemas) { | 190 function sendCustomRequest(nativeFunction, functionName, args, argSchemas) { |
203 var request = prepareRequest(args, argSchemas); | 191 var request = prepareRequest(args, argSchemas); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 // name & definition of the apiFunction. Custom handling of api functions | 315 // name & definition of the apiFunction. Custom handling of api functions |
328 // is implemented by adding a "handleRequest" function to the object. | 316 // is implemented by adding a "handleRequest" function to the object. |
329 var apiFunctions = {}; | 317 var apiFunctions = {}; |
330 | 318 |
331 // Read api definitions and setup api functions in the chrome namespace. | 319 // Read api definitions and setup api functions in the chrome namespace. |
332 // TODO(rafaelw): Consider defining a json schema for an api definition | 320 // TODO(rafaelw): Consider defining a json schema for an api definition |
333 // and validating either here, in a unit_test or both. | 321 // and validating either here, in a unit_test or both. |
334 // TODO(rafaelw): Handle synchronous functions. | 322 // TODO(rafaelw): Handle synchronous functions. |
335 // TOOD(rafaelw): Consider providing some convenient override points | 323 // TOOD(rafaelw): Consider providing some convenient override points |
336 // for api functions that wish to insert themselves into the call. | 324 // for api functions that wish to insert themselves into the call. |
337 var apiDefinitions = JSON.parse(GetExtensionAPIDefinition()); | 325 var apiDefinitions = chromeHidden.JSON.parse(GetExtensionAPIDefinition()); |
338 | 326 |
339 apiDefinitions.forEach(function(apiDef) { | 327 apiDefinitions.forEach(function(apiDef) { |
340 var module = chrome; | 328 var module = chrome; |
341 var namespaces = apiDef.namespace.split('.'); | 329 var namespaces = apiDef.namespace.split('.'); |
342 for (var index = 0, name; name = namespaces[index]; index++) { | 330 for (var index = 0, name; name = namespaces[index]; index++) { |
343 module[name] = module[name] || {}; | 331 module[name] = module[name] || {}; |
344 module = module[name]; | 332 module = module[name]; |
345 }; | 333 }; |
346 | 334 |
347 // Add types to global validationTypes | 335 // Add types to global validationTypes |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 }; | 590 }; |
603 | 591 |
604 apiFunctions["experimental.contextMenu.create"].customCallback = | 592 apiFunctions["experimental.contextMenu.create"].customCallback = |
605 function(name, request, response) { | 593 function(name, request, response) { |
606 if (chrome.extension.lastError || !response) { | 594 if (chrome.extension.lastError || !response) { |
607 return; | 595 return; |
608 } | 596 } |
609 | 597 |
610 // Set up the onclick handler if we were passed one in the request. | 598 // Set up the onclick handler if we were passed one in the request. |
611 if (request.args.onclick) { | 599 if (request.args.onclick) { |
612 var menuItemId = JSON.parse(response); | 600 var menuItemId = chromeHidden.JSON.parse(response); |
613 chromeHidden.contextMenuHandlers[menuItemId] = request.args.onclick; | 601 chromeHidden.contextMenuHandlers[menuItemId] = request.args.onclick; |
614 } | 602 } |
615 }; | 603 }; |
616 | 604 |
617 apiFunctions["experimental.contextMenu.remove"].customCallback = | 605 apiFunctions["experimental.contextMenu.remove"].customCallback = |
618 function(name, request, response) { | 606 function(name, request, response) { |
619 // Remove any onclick handler we had registered for this menu item. | 607 // Remove any onclick handler we had registered for this menu item. |
620 if (request.args.length > 0) { | 608 if (request.args.length > 0) { |
621 var menuItemId = request.args[0]; | 609 var menuItemId = request.args[0]; |
622 delete chromeHidden.contextMenuHandlers[menuItemId]; | 610 delete chromeHidden.contextMenuHandlers[menuItemId]; |
(...skipping 30 matching lines...) Expand all Loading... |
653 setupHiddenContextMenuEvent(extensionId); | 641 setupHiddenContextMenuEvent(extensionId); |
654 setupOmniboxEvents(extensionId); | 642 setupOmniboxEvents(extensionId); |
655 }); | 643 }); |
656 | 644 |
657 if (!chrome.experimental) | 645 if (!chrome.experimental) |
658 chrome.experimental = {}; | 646 chrome.experimental = {}; |
659 | 647 |
660 if (!chrome.experimental.accessibility) | 648 if (!chrome.experimental.accessibility) |
661 chrome.experimental.accessibility = {}; | 649 chrome.experimental.accessibility = {}; |
662 })(); | 650 })(); |
OLD | NEW |