Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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(); |
| 11 native function StartRequest(); | 11 native function StartRequest(); |
| 12 native function StartRequestForIOThread(); | |
| 12 native function GetCurrentPageActions(extensionId); | 13 native function GetCurrentPageActions(extensionId); |
| 13 native function GetExtensionViews(); | 14 native function GetExtensionViews(); |
| 14 native function GetChromeHidden(); | 15 native function GetChromeHidden(); |
| 15 native function GetNextRequestId(); | 16 native function GetNextRequestId(); |
| 16 native function GetNextContextMenuId(); | 17 native function GetNextContextMenuId(); |
| 17 native function OpenChannelToTab(); | 18 native function OpenChannelToTab(); |
| 18 native function GetRenderViewId(); | 19 native function GetRenderViewId(); |
| 19 native function SetIconCommon(); | 20 native function SetIconCommon(); |
| 20 native function IsExtensionProcess(); | 21 native function IsExtensionProcess(); |
| 21 native function IsIncognitoProcess(); | 22 native function IsIncognitoProcess(); |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 | 177 |
| 177 request.args = []; | 178 request.args = []; |
| 178 for (var k = 0; k < argCount; k++) { | 179 for (var k = 0; k < argCount; k++) { |
| 179 request.args[k] = args[k]; | 180 request.args[k] = args[k]; |
| 180 } | 181 } |
| 181 | 182 |
| 182 return request; | 183 return request; |
| 183 } | 184 } |
| 184 | 185 |
| 185 // Send an API request and optionally register a callback. | 186 // Send an API request and optionally register a callback. |
| 186 function sendRequest(functionName, args, argSchemas, customCallback) { | 187 // If |opt_rawargs| is true, don't JSONify the arguments. If |
| 188 // |opt_nativeFunction| is provided, use that instead of StartRequest. | |
| 189 function sendRequest(functionName, args, argSchemas, opt_customCallback, | |
|
Mihai Parparita -not on Chrome
2011/06/08 01:11:59
The callsites of this are confusing to follow beca
Matt Perry
2011/06/08 20:47:00
I've changed the optional arguments to a single op
| |
| 190 opt_rawargs, opt_nativeFunction) { | |
| 187 var request = prepareRequest(args, argSchemas); | 191 var request = prepareRequest(args, argSchemas); |
| 188 if (customCallback) { | 192 if (opt_customCallback) { |
| 189 request.customCallback = customCallback; | 193 request.customCallback = opt_customCallback; |
| 190 } | 194 } |
| 191 // JSON.stringify doesn't support a root object which is undefined. | 195 // JSON.stringify doesn't support a root object which is undefined. |
| 192 if (request.args === undefined) | 196 if (request.args === undefined) |
| 193 request.args = null; | 197 request.args = null; |
| 194 | 198 |
| 195 var sargs = chromeHidden.JSON.stringify(request.args); | 199 var sargs = opt_rawargs ? |
| 200 request.args : chromeHidden.JSON.stringify(request.args); | |
| 201 var nativeFunction = opt_nativeFunction || StartRequest; | |
| 196 | 202 |
| 197 var requestId = GetNextRequestId(); | 203 var requestId = GetNextRequestId(); |
| 198 requests[requestId] = request; | 204 requests[requestId] = request; |
| 199 var hasCallback = (request.callback || customCallback) ? true : false; | 205 var hasCallback = (request.callback || opt_customCallback) ? true : false; |
| 200 return StartRequest(functionName, sargs, requestId, hasCallback); | 206 return nativeFunction(functionName, sargs, requestId, hasCallback); |
| 201 } | |
| 202 | |
| 203 // Send a special API request that is not JSON stringifiable, and optionally | |
| 204 // register a callback. | |
| 205 function sendCustomRequest(nativeFunction, functionName, args, argSchemas) { | |
| 206 var request = prepareRequest(args, argSchemas); | |
| 207 var requestId = GetNextRequestId(); | |
| 208 requests[requestId] = request; | |
| 209 return nativeFunction(functionName, request.args, requestId, | |
| 210 request.callback ? true : false); | |
| 211 } | 207 } |
| 212 | 208 |
| 213 // Helper function for positioning pop-up windows relative to DOM objects. | 209 // Helper function for positioning pop-up windows relative to DOM objects. |
| 214 // Returns the absolute position of the given element relative to the hosting | 210 // Returns the absolute position of the given element relative to the hosting |
| 215 // browser frame. | 211 // browser frame. |
| 216 function findAbsolutePosition(domElement) { | 212 function findAbsolutePosition(domElement) { |
| 217 var left = domElement.offsetLeft; | 213 var left = domElement.offsetLeft; |
| 218 var top = domElement.offsetTop; | 214 var top = domElement.offsetTop; |
| 219 | 215 |
| 220 // Ascend through the parent hierarchy, taking into account object nesting | 216 // Ascend through the parent hierarchy, taking into account object nesting |
| (...skipping 522 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 743 "The imageData property must contain an ImageData object."); | 739 "The imageData property must contain an ImageData object."); |
| 744 } | 740 } |
| 745 | 741 |
| 746 if (details.imageData.width > iconSize || | 742 if (details.imageData.width > iconSize || |
| 747 details.imageData.height > iconSize) { | 743 details.imageData.height > iconSize) { |
| 748 throw new Error( | 744 throw new Error( |
| 749 "The imageData property must contain an ImageData object that " + | 745 "The imageData property must contain an ImageData object that " + |
| 750 "is no larger than " + iconSize + " pixels square."); | 746 "is no larger than " + iconSize + " pixels square."); |
| 751 } | 747 } |
| 752 | 748 |
| 753 sendCustomRequest(nativeFunction, name, [details], parameters); | 749 sendRequest(name, [details], parameters, null, true, nativeFunction); |
| 754 } else if ("path" in details) { | 750 } else if ("path" in details) { |
| 755 var img = new Image(); | 751 var img = new Image(); |
| 756 img.onerror = function() { | 752 img.onerror = function() { |
| 757 console.error("Could not load " + actionType + " icon '" + | 753 console.error("Could not load " + actionType + " icon '" + |
| 758 details.path + "'."); | 754 details.path + "'."); |
| 759 }; | 755 }; |
| 760 img.onload = function() { | 756 img.onload = function() { |
| 761 var canvas = document.createElement("canvas"); | 757 var canvas = document.createElement("canvas"); |
| 762 canvas.width = img.width > iconSize ? iconSize : img.width; | 758 canvas.width = img.width > iconSize ? iconSize : img.width; |
| 763 canvas.height = img.height > iconSize ? iconSize : img.height; | 759 canvas.height = img.height > iconSize ? iconSize : img.height; |
| 764 | 760 |
| 765 var canvas_context = canvas.getContext('2d'); | 761 var canvas_context = canvas.getContext('2d'); |
| 766 canvas_context.clearRect(0, 0, canvas.width, canvas.height); | 762 canvas_context.clearRect(0, 0, canvas.width, canvas.height); |
| 767 canvas_context.drawImage(img, 0, 0, canvas.width, canvas.height); | 763 canvas_context.drawImage(img, 0, 0, canvas.width, canvas.height); |
| 768 delete details.path; | 764 delete details.path; |
| 769 details.imageData = canvas_context.getImageData(0, 0, canvas.width, | 765 details.imageData = canvas_context.getImageData(0, 0, canvas.width, |
| 770 canvas.height); | 766 canvas.height); |
| 771 sendCustomRequest(nativeFunction, name, [details], parameters); | 767 sendRequest(name, [details], parameters, null, true, nativeFunction); |
| 772 }; | 768 }; |
| 773 img.src = details.path; | 769 img.src = details.path; |
| 774 } else { | 770 } else { |
| 775 throw new Error( | 771 throw new Error( |
| 776 "Either the path or imageData property must be specified."); | 772 "Either the path or imageData property must be specified."); |
| 777 } | 773 } |
| 778 } | 774 } |
| 779 | 775 |
| 780 function setExtensionActionIconCommon(details, name, parameters, | 776 function setExtensionActionIconCommon(details, name, parameters, |
| 781 actionType) { | 777 actionType) { |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 811 this.customCallback); | 807 this.customCallback); |
| 812 return id; | 808 return id; |
| 813 }; | 809 }; |
| 814 | 810 |
| 815 apiFunctions["omnibox.setDefaultSuggestion"].handleRequest = | 811 apiFunctions["omnibox.setDefaultSuggestion"].handleRequest = |
| 816 function(details) { | 812 function(details) { |
| 817 var parseResult = parseOmniboxDescription(details.description); | 813 var parseResult = parseOmniboxDescription(details.description); |
| 818 sendRequest(this.name, [parseResult], this.definition.parameters); | 814 sendRequest(this.name, [parseResult], this.definition.parameters); |
| 819 }; | 815 }; |
| 820 | 816 |
| 817 apiFunctions["experimental.webRequest.addEventListener"].handleRequest = | |
| 818 function() { | |
| 819 var args = Array.prototype.slice.call(arguments); | |
| 820 sendRequest(this.name, args, this.definition.parameters, | |
| 821 null, false, StartRequestForIOThread); | |
| 822 }; | |
| 823 | |
| 824 apiFunctions["experimental.webRequest.eventHandled"].handleRequest = | |
| 825 function() { | |
| 826 var args = Array.prototype.slice.call(arguments); | |
| 827 sendRequest(this.name, args, this.definition.parameters, | |
| 828 null, false, StartRequestForIOThread); | |
| 829 }; | |
| 830 | |
| 821 apiFunctions["contextMenus.create"].customCallback = | 831 apiFunctions["contextMenus.create"].customCallback = |
| 822 function(name, request, response) { | 832 function(name, request, response) { |
| 823 if (chrome.extension.lastError) { | 833 if (chrome.extension.lastError) { |
| 824 return; | 834 return; |
| 825 } | 835 } |
| 826 | 836 |
| 827 var id = request.args[0].generatedId; | 837 var id = request.args[0].generatedId; |
| 828 | 838 |
| 829 // Set up the onclick handler if we were passed one in the request. | 839 // Set up the onclick handler if we were passed one in the request. |
| 830 var onclick = request.args.length ? request.args[0].onclick : null; | 840 var onclick = request.args.length ? request.args[0].onclick : null; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 907 | 917 |
| 908 if (!chrome.experimental) | 918 if (!chrome.experimental) |
| 909 chrome.experimental = {}; | 919 chrome.experimental = {}; |
| 910 | 920 |
| 911 if (!chrome.experimental.accessibility) | 921 if (!chrome.experimental.accessibility) |
| 912 chrome.experimental.accessibility = {}; | 922 chrome.experimental.accessibility = {}; |
| 913 | 923 |
| 914 if (!chrome.experimental.tts) | 924 if (!chrome.experimental.tts) |
| 915 chrome.experimental.tts = {}; | 925 chrome.experimental.tts = {}; |
| 916 })(); | 926 })(); |
| OLD | NEW |