| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 unprivileged javascript APIs related to chrome | 5 // This script contains unprivileged javascript APIs related to chrome |
| 6 // extensions. It is loaded by any extension-related context, such as content | 6 // extensions. It is loaded by any extension-related context, such as content |
| 7 // scripts or toolstrips. | 7 // scripts or toolstrips. |
| 8 // See user_script_slave.cc for script that is loaded by content scripts only. | 8 // See user_script_slave.cc for script that is loaded by content scripts only. |
| 9 // TODO(mpcomplete): we also load this in regular web pages, but don't need | 9 // TODO(mpcomplete): we also load this in regular web pages, but don't need |
| 10 // to. | 10 // to. |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 }; | 134 }; |
| 135 | 135 |
| 136 // Disconnects the port from the other end. | 136 // Disconnects the port from the other end. |
| 137 chrome.Port.prototype.disconnect = function() { | 137 chrome.Port.prototype.disconnect = function() { |
| 138 delete ports[this.portId_]; | 138 delete ports[this.portId_]; |
| 139 CloseChannel(this.portId_); | 139 CloseChannel(this.portId_); |
| 140 } | 140 } |
| 141 | 141 |
| 142 // This function is called on context initialization for both content scripts | 142 // This function is called on context initialization for both content scripts |
| 143 // and extension contexts. | 143 // and extension contexts. |
| 144 chrome.initExtension = function(extensionId) { | 144 chrome.initExtension = function(extensionId, warnOnPrivilegedApiAccess) { |
| 145 delete chrome.initExtension; | 145 delete chrome.initExtension; |
| 146 chromeHidden.extensionId = extensionId; | 146 chromeHidden.extensionId = extensionId; |
| 147 | 147 |
| 148 chrome.extension = chrome.extension || {}; | 148 chrome.extension = chrome.extension || {}; |
| 149 | 149 |
| 150 // TODO(EXTENSIONS_DEPRECATED): chrome.self is obsolete. | 150 // TODO(EXTENSIONS_DEPRECATED): chrome.self is obsolete. |
| 151 // http://code.google.com/p/chromium/issues/detail?id=16356 | 151 // http://code.google.com/p/chromium/issues/detail?id=16356 |
| 152 chrome.self = chrome.extension; | 152 chrome.self = chrome.extension; |
| 153 | 153 |
| 154 // Events for when a message channel is opened to our extension. | 154 // Events for when a message channel is opened to our extension. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 responseCallback(response); | 193 responseCallback(response); |
| 194 port.disconnect(); | 194 port.disconnect(); |
| 195 }); | 195 }); |
| 196 }; | 196 }; |
| 197 | 197 |
| 198 // Returns a resource URL that can be used to fetch a resource from this | 198 // Returns a resource URL that can be used to fetch a resource from this |
| 199 // extension. | 199 // extension. |
| 200 chrome.extension.getURL = function(path) { | 200 chrome.extension.getURL = function(path) { |
| 201 return "chrome-extension://" + extensionId + "/" + path; | 201 return "chrome-extension://" + extensionId + "/" + path; |
| 202 }; | 202 }; |
| 203 |
| 204 if (warnOnPrivilegedApiAccess) { |
| 205 setupApiStubs(); |
| 206 } |
| 203 } | 207 } |
| 208 |
| 209 var notSupportedSuffix = " is not supported in content scripts. " + |
| 210 "See the content scripts documentation for more details."; |
| 211 |
| 212 // Setup to throw an error message when trying to access |name| on the chrome |
| 213 // object. The |name| can be a dot-separated path. |
| 214 function createStub(name) { |
| 215 var module = chrome; |
| 216 var parts = name.split("."); |
| 217 for (var i = 0; i < parts.length - 1; i++) { |
| 218 var nextPart = parts[i]; |
| 219 // Make sure an object at the path so far is defined. |
| 220 module[nextPart] = module[nextPart] || {}; |
| 221 module = module[nextPart]; |
| 222 } |
| 223 var finalPart = parts[parts.length-1]; |
| 224 module.__defineGetter__(finalPart, function() { |
| 225 throw new Error("chrome." + name + notSupportedSuffix); |
| 226 }); |
| 227 } |
| 228 |
| 229 // Sets up stubs to throw a better error message for the common case of |
| 230 // developers trying to call extension API's that aren't allowed to be |
| 231 // called from content scripts. |
| 232 function setupApiStubs() { |
| 233 // TODO(asargent) It would be nice to eventually generate this |
| 234 // programmatically from extension_api.json (there is already a browser test |
| 235 // that should prevent it from getting stale). |
| 236 var privileged = [ |
| 237 // Entire namespaces. |
| 238 "bookmarks", "browserAction", "devtools", "experimental.extension", |
| 239 "experimental.history", "experimental.popup", "i18n", "pageAction", |
| 240 "pageActions", "tabs", "test", "toolstrip", "windows", |
| 241 |
| 242 // Functions/events/properties within the extension namespace. |
| 243 "extension.getBackgroundPage", "extension.getExtensionTabs", |
| 244 "extension.getToolstrips", "extension.getViews", "extension.lastError", |
| 245 "extension.onConnectExternal", "extension.onRequestExternal" |
| 246 ]; |
| 247 for (var i = 0; i < privileged.length; i++) { |
| 248 createStub(privileged[i]); |
| 249 } |
| 250 } |
| 251 |
| 204 })(); | 252 })(); |
| OLD | NEW |