| 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 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 background pages. | 7 // scripts or background pages. |
| 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 to. | 9 // TODO(mpcomplete): we also load this in regular web pages, but don't need to. |
| 10 | 10 |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 chromeHidden.extensionId = extensionId; | 200 chromeHidden.extensionId = extensionId; |
| 201 | 201 |
| 202 chrome.extension = chrome.extension || {}; | 202 chrome.extension = chrome.extension || {}; |
| 203 chrome.self = chrome.extension; | 203 chrome.self = chrome.extension; |
| 204 | 204 |
| 205 chrome.extension.inIncognitoTab = inIncognitoContext; // deprecated | 205 chrome.extension.inIncognitoTab = inIncognitoContext; // deprecated |
| 206 chrome.extension.inIncognitoContext = inIncognitoContext; | 206 chrome.extension.inIncognitoContext = inIncognitoContext; |
| 207 | 207 |
| 208 // Events for when a message channel is opened to our extension. | 208 // Events for when a message channel is opened to our extension. |
| 209 chrome.extension.onConnect = new chrome.Event(); | 209 chrome.extension.onConnect = new chrome.Event(); |
| 210 chrome.extension.onConnectExternal = new chrome.Event(); | |
| 211 chrome.extension.onRequest = new chrome.Event(); | 210 chrome.extension.onRequest = new chrome.Event(); |
| 212 chrome.extension.onRequestExternal = new chrome.Event(); | 211 |
| 212 if (isExtensionProcess) { |
| 213 chrome.extension.onConnectExternal = new chrome.Event(); |
| 214 chrome.extension.onRequestExternal = new chrome.Event(); |
| 215 } |
| 213 | 216 |
| 214 // Opens a message channel to the given target extension, or the current one | 217 // Opens a message channel to the given target extension, or the current one |
| 215 // if unspecified. Returns a Port for message passing. | 218 // if unspecified. Returns a Port for message passing. |
| 216 chrome.extension.connect = function(targetId_opt, connectInfo_opt) { | 219 chrome.extension.connect = function(targetId_opt, connectInfo_opt) { |
| 217 var name = ""; | 220 var name = ""; |
| 218 var targetId = extensionId; | 221 var targetId = extensionId; |
| 219 var nextArg = 0; | 222 var nextArg = 0; |
| 220 if (typeof(arguments[nextArg]) == "string") | 223 if (typeof(arguments[nextArg]) == "string") |
| 221 targetId = arguments[nextArg++]; | 224 targetId = arguments[nextArg++]; |
| 222 if (typeof(arguments[nextArg]) == "object") | 225 if (typeof(arguments[nextArg]) == "object") |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 path = String(path); | 275 path = String(path); |
| 273 if (!path.length || path[0] != "/") | 276 if (!path.length || path[0] != "/") |
| 274 path = "/" + path; | 277 path = "/" + path; |
| 275 return "chrome-extension://" + extensionId + path; | 278 return "chrome-extension://" + extensionId + path; |
| 276 }; | 279 }; |
| 277 | 280 |
| 278 chrome.i18n = chrome.i18n || {}; | 281 chrome.i18n = chrome.i18n || {}; |
| 279 chrome.i18n.getMessage = function(message_name, placeholders) { | 282 chrome.i18n.getMessage = function(message_name, placeholders) { |
| 280 return GetL10nMessage(message_name, placeholders, extensionId); | 283 return GetL10nMessage(message_name, placeholders, extensionId); |
| 281 }; | 284 }; |
| 282 | |
| 283 if (!isExtensionProcess) | |
| 284 setupApiStubs(); | |
| 285 }); | 285 }); |
| 286 | 286 |
| 287 var notSupportedSuffix = " can only be used in extension processes. " + | |
| 288 "See the content scripts documentation for more details."; | |
| 289 | |
| 290 // Setup to throw an error message when trying to access |name| on the chrome | |
| 291 // object. The |name| can be a dot-separated path. | |
| 292 function createStub(name) { | |
| 293 var module = chrome; | |
| 294 var parts = name.split("."); | |
| 295 for (var i = 0; i < parts.length - 1; i++) { | |
| 296 var nextPart = parts[i]; | |
| 297 // Make sure an object at the path so far is defined. | |
| 298 module[nextPart] = module[nextPart] || {}; | |
| 299 module = module[nextPart]; | |
| 300 } | |
| 301 var finalPart = parts[parts.length-1]; | |
| 302 module.__defineGetter__(finalPart, function() { | |
| 303 throw new Error("chrome." + name + notSupportedSuffix); | |
| 304 }); | |
| 305 } | |
| 306 | |
| 307 // Sets up stubs to throw a better error message for the common case of | |
| 308 // developers trying to call extension API's that aren't allowed to be | |
| 309 // called from content scripts. | |
| 310 function setupApiStubs() { | |
| 311 // TODO(asargent) It would be nice to eventually generate this | |
| 312 // programmatically from extension_api.json (there is already a browser test | |
| 313 // that should prevent it from getting stale). | |
| 314 var privileged = [ | |
| 315 // Entire namespaces. | |
| 316 "bookmarks", | |
| 317 "browserAction", | |
| 318 "chromeAuthPrivate", | |
| 319 "chromePrivate", | |
| 320 "chromeosInfoPrivate", | |
| 321 "contentSettings", | |
| 322 "contextMenus", | |
| 323 "cookies", | |
| 324 "devtools", | |
| 325 "experimental.accessibility", | |
| 326 "experimental.app", | |
| 327 "experimental.bookmarkManager", | |
| 328 "experimental.clear", | |
| 329 "experimental.clipboard", | |
| 330 "experimental.debugger", | |
| 331 "experimental.downloads", | |
| 332 "experimental.extension", | |
| 333 "experimental.infobars", | |
| 334 "experimental.input", | |
| 335 "experimental.inputUI", | |
| 336 "experimental.metrics", | |
| 337 "experimental.settings", | |
| 338 "experimental.popup", | |
| 339 "experimental.processes", | |
| 340 "experimental.privacy", | |
| 341 "experimental.rlz", | |
| 342 "experimental.savePage", | |
| 343 "experimental.sidebar", | |
| 344 "experimental.speechInput", | |
| 345 "experimental.topSites", | |
| 346 "experimental.webRequest", | |
| 347 "fileBrowserHandler", | |
| 348 "fileBrowserPrivate", | |
| 349 "fileSystem", | |
| 350 "history", | |
| 351 "idle", | |
| 352 "inputMethodPrivate", | |
| 353 "management", | |
| 354 "mediaPlayerPrivate", | |
| 355 "omnibox", | |
| 356 "pageAction", | |
| 357 "pageActions", | |
| 358 "permissions", | |
| 359 "proxy", | |
| 360 "tabs", | |
| 361 "test", | |
| 362 "tts", | |
| 363 "ttsEngine", | |
| 364 "types", | |
| 365 "webNavigation", | |
| 366 "webSocketProxyPrivate", | |
| 367 "webstorePrivate", | |
| 368 "windows", | |
| 369 | |
| 370 // Functions/events/properties within the extension namespace. | |
| 371 "extension.getBackgroundPage", | |
| 372 "extension.getExtensionTabs", | |
| 373 "extension.getViews", | |
| 374 "extension.isAllowedIncognitoAccess", | |
| 375 "extension.isAllowedFileSchemeAccess", | |
| 376 "extension.onConnectExternal", | |
| 377 "extension.onRequestExternal", | |
| 378 "extension.setUpdateUrlData", | |
| 379 "i18n.getAcceptLanguages" | |
| 380 ]; | |
| 381 for (var i = 0; i < privileged.length; i++) { | |
| 382 createStub(privileged[i]); | |
| 383 } | |
| 384 } | |
| 385 | |
| 386 })(); | 287 })(); |
| OLD | NEW |