| 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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 "message": error | 74 "message": error |
| 75 }; | 75 }; |
| 76 } | 76 } |
| 77 | 77 |
| 78 if (request.customCallback) { | 78 if (request.customCallback) { |
| 79 request.customCallback(name, request, response); | 79 request.customCallback(name, request, response); |
| 80 } | 80 } |
| 81 | 81 |
| 82 if (request.callback) { | 82 if (request.callback) { |
| 83 // Callbacks currently only support one callback argument. | 83 // Callbacks currently only support one callback argument. |
| 84 var callbackArgs = response ? [JSON.parse(response)] : []; | 84 var callbackArgs = response ? [chromeHidden.JSON.parse(response)] : []; |
| 85 | 85 |
| 86 // Validate callback in debug only -- and only when the | 86 // Validate callback in debug only -- and only when the |
| 87 // caller has provided a callback. Implementations of api | 87 // caller has provided a callback. Implementations of api |
| 88 // calls my not return data if they observe the caller | 88 // calls my not return data if they observe the caller |
| 89 // has not provided a callback. | 89 // has not provided a callback. |
| 90 if (chromeHidden.validateCallbacks && !error) { | 90 if (chromeHidden.validateCallbacks && !error) { |
| 91 try { | 91 try { |
| 92 if (!request.callbackSchema.parameters) { | 92 if (!request.callbackSchema.parameters) { |
| 93 throw "No callback schemas defined"; | 93 throw "No callback schemas defined"; |
| 94 } | 94 } |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // Send an API request and optionally register a callback. | 166 // Send an API request and optionally register a callback. |
| 167 function sendRequest(functionName, args, argSchemas, customCallback) { | 167 function sendRequest(functionName, args, argSchemas, customCallback) { |
| 168 var request = prepareRequest(args, argSchemas); | 168 var request = prepareRequest(args, argSchemas); |
| 169 if (customCallback) { | 169 if (customCallback) { |
| 170 request.customCallback = customCallback; | 170 request.customCallback = customCallback; |
| 171 } | 171 } |
| 172 // JSON.stringify doesn't support a root object which is undefined. | 172 // JSON.stringify doesn't support a root object which is undefined. |
| 173 if (request.args === undefined) | 173 if (request.args === undefined) |
| 174 request.args = null; | 174 request.args = null; |
| 175 | 175 |
| 176 // Some javascript libraries (e.g. prototype.js version <= 1.6) add a toJSON | 176 var sargs = chromeHidden.JSON.stringify(request.args); |
| 177 // serializer function on Array.prototype that is incompatible with our | 177 |
| 178 // native JSON library, causing incorrect deserialization in the C++ side of | |
| 179 // StartRequest. We work around that here by temporarily removing the toJSON | |
| 180 // function. | |
| 181 var arrayToJsonTmp; | |
| 182 if (Array.prototype.toJSON) { | |
| 183 arrayToJsonTmp = Array.prototype.toJSON; | |
| 184 Array.prototype.toJSON = null; | |
| 185 } | |
| 186 var sargs = JSON.stringify(request.args); | |
| 187 if (arrayToJsonTmp) { | |
| 188 Array.prototype.toJSON = arrayToJsonTmp; | |
| 189 } | |
| 190 var requestId = GetNextRequestId(); | 178 var requestId = GetNextRequestId(); |
| 191 requests[requestId] = request; | 179 requests[requestId] = request; |
| 192 var hasCallback = (request.callback || customCallback) ? true : false; | 180 var hasCallback = (request.callback || customCallback) ? true : false; |
| 193 return StartRequest(functionName, sargs, requestId, hasCallback); | 181 return StartRequest(functionName, sargs, requestId, hasCallback); |
| 194 } | 182 } |
| 195 | 183 |
| 196 // Send a special API request that is not JSON stringifiable, and optionally | 184 // Send a special API request that is not JSON stringifiable, and optionally |
| 197 // register a callback. | 185 // register a callback. |
| 198 function sendCustomRequest(nativeFunction, functionName, args, argSchemas) { | 186 function sendCustomRequest(nativeFunction, functionName, args, argSchemas) { |
| 199 var request = prepareRequest(args, argSchemas); | 187 var request = prepareRequest(args, argSchemas); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 // name & definition of the apiFunction. Custom handling of api functions | 305 // name & definition of the apiFunction. Custom handling of api functions |
| 318 // is implemented by adding a "handleRequest" function to the object. | 306 // is implemented by adding a "handleRequest" function to the object. |
| 319 var apiFunctions = {}; | 307 var apiFunctions = {}; |
| 320 | 308 |
| 321 // Read api definitions and setup api functions in the chrome namespace. | 309 // Read api definitions and setup api functions in the chrome namespace. |
| 322 // TODO(rafaelw): Consider defining a json schema for an api definition | 310 // TODO(rafaelw): Consider defining a json schema for an api definition |
| 323 // and validating either here, in a unit_test or both. | 311 // and validating either here, in a unit_test or both. |
| 324 // TODO(rafaelw): Handle synchronous functions. | 312 // TODO(rafaelw): Handle synchronous functions. |
| 325 // TOOD(rafaelw): Consider providing some convenient override points | 313 // TOOD(rafaelw): Consider providing some convenient override points |
| 326 // for api functions that wish to insert themselves into the call. | 314 // for api functions that wish to insert themselves into the call. |
| 327 var apiDefinitions = JSON.parse(GetExtensionAPIDefinition()); | 315 var apiDefinitions = chromeHidden.JSON.parse(GetExtensionAPIDefinition()); |
| 328 | 316 |
| 329 apiDefinitions.forEach(function(apiDef) { | 317 apiDefinitions.forEach(function(apiDef) { |
| 330 var module = chrome; | 318 var module = chrome; |
| 331 var namespaces = apiDef.namespace.split('.'); | 319 var namespaces = apiDef.namespace.split('.'); |
| 332 for (var index = 0, name; name = namespaces[index]; index++) { | 320 for (var index = 0, name; name = namespaces[index]; index++) { |
| 333 module[name] = module[name] || {}; | 321 module[name] = module[name] || {}; |
| 334 module = module[name]; | 322 module = module[name]; |
| 335 }; | 323 }; |
| 336 | 324 |
| 337 // Add types to global validationTypes | 325 // Add types to global validationTypes |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 }; | 580 }; |
| 593 | 581 |
| 594 apiFunctions["experimental.contextMenu.create"].customCallback = | 582 apiFunctions["experimental.contextMenu.create"].customCallback = |
| 595 function(name, request, response) { | 583 function(name, request, response) { |
| 596 if (chrome.extension.lastError || !response) { | 584 if (chrome.extension.lastError || !response) { |
| 597 return; | 585 return; |
| 598 } | 586 } |
| 599 | 587 |
| 600 // Set up the onclick handler if we were passed one in the request. | 588 // Set up the onclick handler if we were passed one in the request. |
| 601 if (request.args.onclick) { | 589 if (request.args.onclick) { |
| 602 var menuItemId = JSON.parse(response); | 590 var menuItemId = chromeHidden.JSON.parse(response); |
| 603 chromeHidden.contextMenuHandlers[menuItemId] = request.args.onclick; | 591 chromeHidden.contextMenuHandlers[menuItemId] = request.args.onclick; |
| 604 } | 592 } |
| 605 }; | 593 }; |
| 606 | 594 |
| 607 apiFunctions["experimental.contextMenu.remove"].customCallback = | 595 apiFunctions["experimental.contextMenu.remove"].customCallback = |
| 608 function(name, request, response) { | 596 function(name, request, response) { |
| 609 // Remove any onclick handler we had registered for this menu item. | 597 // Remove any onclick handler we had registered for this menu item. |
| 610 if (request.args.length > 0) { | 598 if (request.args.length > 0) { |
| 611 var menuItemId = request.args[0]; | 599 var menuItemId = request.args[0]; |
| 612 delete chromeHidden.contextMenuHandlers[menuItemId]; | 600 delete chromeHidden.contextMenuHandlers[menuItemId]; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 642 setupPopupEvents(GetRenderViewId()); | 630 setupPopupEvents(GetRenderViewId()); |
| 643 setupHiddenContextMenuEvent(extensionId); | 631 setupHiddenContextMenuEvent(extensionId); |
| 644 }); | 632 }); |
| 645 | 633 |
| 646 if (!chrome.experimental) | 634 if (!chrome.experimental) |
| 647 chrome.experimental = {}; | 635 chrome.experimental = {}; |
| 648 | 636 |
| 649 if (!chrome.experimental.accessibility) | 637 if (!chrome.experimental.accessibility) |
| 650 chrome.experimental.accessibility = {}; | 638 chrome.experimental.accessibility = {}; |
| 651 })(); | 639 })(); |
| OLD | NEW |