Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8932)

Unified Diff: chrome/renderer/resources/renderer_extension_bindings.js

Issue 366024: Add stub functions for chrome.* APIs in content scripts. ... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/renderer/resources/extension_process_bindings.js ('k') | chrome/renderer/user_script_slave.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/renderer/resources/renderer_extension_bindings.js
===================================================================
--- chrome/renderer/resources/renderer_extension_bindings.js (revision 31822)
+++ chrome/renderer/resources/renderer_extension_bindings.js (working copy)
@@ -141,7 +141,7 @@
// This function is called on context initialization for both content scripts
// and extension contexts.
- chrome.initExtension = function(extensionId) {
+ chrome.initExtension = function(extensionId, warnOnPrivilegedApiAccess) {
delete chrome.initExtension;
chromeHidden.extensionId = extensionId;
@@ -200,5 +200,53 @@
chrome.extension.getURL = function(path) {
return "chrome-extension://" + extensionId + "/" + path;
};
+
+ if (warnOnPrivilegedApiAccess) {
+ setupApiStubs();
+ }
}
+
+ var notSupportedSuffix = " is not supported in content scripts. " +
+ "See the content scripts documentation for more details.";
+
+ // Setup to throw an error message when trying to access |name| on the chrome
+ // object. The |name| can be a dot-separated path.
+ function createStub(name) {
+ var module = chrome;
+ var parts = name.split(".");
+ for (var i = 0; i < parts.length - 1; i++) {
+ var nextPart = parts[i];
+ // Make sure an object at the path so far is defined.
+ module[nextPart] = module[nextPart] || {};
+ module = module[nextPart];
+ }
+ var finalPart = parts[parts.length-1];
+ module.__defineGetter__(finalPart, function() {
+ throw new Error("chrome." + name + notSupportedSuffix);
+ });
+ }
+
+ // Sets up stubs to throw a better error message for the common case of
+ // developers trying to call extension API's that aren't allowed to be
+ // called from content scripts.
+ function setupApiStubs() {
+ // TODO(asargent) It would be nice to eventually generate this
+ // programmatically from extension_api.json (there is already a browser test
+ // that should prevent it from getting stale).
+ var privileged = [
+ // Entire namespaces.
+ "bookmarks", "browserAction", "devtools", "experimental.extension",
+ "experimental.history", "experimental.popup", "i18n", "pageAction",
+ "pageActions", "tabs", "test", "toolstrip", "windows",
+
+ // Functions/events/properties within the extension namespace.
+ "extension.getBackgroundPage", "extension.getExtensionTabs",
+ "extension.getToolstrips", "extension.getViews", "extension.lastError",
+ "extension.onConnectExternal", "extension.onRequestExternal"
+ ];
+ for (var i = 0; i < privileged.length; i++) {
+ createStub(privileged[i]);
+ }
+ }
+
})();
« no previous file with comments | « chrome/renderer/resources/extension_process_bindings.js ('k') | chrome/renderer/user_script_slave.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698