Commit 5b4e8c54 authored by Rob Wu's avatar Rob Wu Committed by Commit Bot

Add callbacks to all pageAction/browserAction/action methods

browserAction and pageAction methods take a tabId as parameter.
An extension cannot ensure that the tabId is valid, since the tab might
be removed between the invocation and the handling of the API method.
To suppress the error, extensions need to be able to access the
`chrome.runtime.lastError` object in the callback, which means that
all extension action API methods need to accept an optional callback.

BUG=451320

Change-Id: I4bd1fe8b17bdb98da7b33a03ec814294d5860da1
Reviewed-on: https://chromium-review.googlesource.com/979451Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Commit-Queue: Rob Wu <rob@robwu.nl>
Cr-Commit-Position: refs/heads/master@{#545797}
parent a147393b
......@@ -25,6 +25,11 @@
"description": "Limits the change to when a particular tab is selected. Automatically resets when the tab is closed."
}
}
}, {
"type": "function",
"name": "callback",
"parameters": [],
"optional": true
}]
}, {
"name": "getTitle",
......@@ -110,6 +115,11 @@
"description": "The html file to show in a popup. If set to the empty string (''), no popup is shown."
}
}
}, {
"type": "function",
"name": "callback",
"parameters": [],
"optional": true
}
]
}, {
......@@ -159,6 +169,11 @@
"description": "Limits the change to when a particular tab is selected. Automatically resets when the tab is closed."
}
}
}, {
"type": "function",
"name": "callback",
"parameters": [],
"optional": true
}]
}, {
"name": "getBadgeText",
......@@ -205,6 +220,11 @@
"description": "Limits the change to when a particular tab is selected. Automatically resets when the tab is closed."
}
}
}, {
"type": "function",
"name": "callback",
"parameters": [],
"optional": true
}]
}, {
"name": "getBadgeBackgroundColor",
......@@ -239,6 +259,11 @@
"name": "tabId",
"minimum": 0,
"description": "The id of the tab for which you want to modify the action."
}, {
"type": "function",
"name": "callback",
"parameters": [],
"optional": true
}]
}, {
"name": "disable",
......@@ -250,6 +275,11 @@
"name": "tabId",
"minimum": 0,
"description": "The id of the tab for which you want to modify the action."
}, {
"type": "function",
"name": "callback",
"parameters": [],
"optional": true
}]
}],
"events": [{
......
......@@ -46,7 +46,8 @@
"description": "Limits the change to when a particular tab is selected. Automatically resets when the tab is closed."
}
}
}
},
{"type": "function", "name": "callback", "parameters": [], "optional": true}
]
},
{
......@@ -143,7 +144,8 @@
"description": "The html file to show in a popup. If set to the empty string (''), no popup is shown."
}
}
}
},
{"type": "function", "name": "callback", "parameters": [], "optional": true}
]
},
{
......@@ -193,7 +195,8 @@
"description": "Limits the change to when a particular tab is selected. Automatically resets when the tab is closed."
}
}
}
},
{"type": "function", "name": "callback", "parameters": [], "optional": true}
]
},
{
......@@ -246,7 +249,8 @@
"description": "Limits the change to when a particular tab is selected. Automatically resets when the tab is closed."
}
}
}
},
{"type": "function", "name": "callback", "parameters": [], "optional": true}
]
},
{
......@@ -288,7 +292,8 @@
"name": "tabId",
"minimum": 0,
"description": "The id of the tab for which you want to modify the browser action."
}
},
{"type": "function", "name": "callback", "parameters": [], "optional": true}
]
},
{
......@@ -302,7 +307,8 @@
"name": "tabId",
"minimum": 0,
"description": "The id of the tab for which you want to modify the browser action."
}
},
{"type": "function", "name": "callback", "parameters": [], "optional": true}
]
},
{
......
......@@ -21,7 +21,8 @@
"type": "function",
"description": "Shows the page action. The page action is shown whenever the tab is selected.",
"parameters": [
{"type": "integer", "name": "tabId", "minimum": 0, "description": "The id of the tab for which you want to modify the page action."}
{"type": "integer", "name": "tabId", "minimum": 0, "description": "The id of the tab for which you want to modify the page action."},
{"type": "function", "name": "callback", "parameters": [], "optional": true}
]
},
{
......@@ -29,7 +30,8 @@
"type": "function",
"description": "Hides the page action. Hidden page actions still appear in the Chrome toolbar, but are grayed out.",
"parameters": [
{"type": "integer", "name": "tabId", "minimum": 0, "description": "The id of the tab for which you want to modify the page action."}
{"type": "integer", "name": "tabId", "minimum": 0, "description": "The id of the tab for which you want to modify the page action."},
{"type": "function", "name": "callback", "parameters": [], "optional": true}
]
},
{
......@@ -44,7 +46,8 @@
"tabId": {"type": "integer", "minimum": 0, "description": "The id of the tab for which you want to modify the page action."},
"title": {"type": "string", "description": "The tooltip string."}
}
}
},
{"type": "function", "name": "callback", "parameters": [], "optional": true}
]
},
{
......@@ -137,7 +140,8 @@
"description": "The html file to show in a popup. If set to the empty string (''), no popup is shown."
}
}
}
},
{"type": "function", "name": "callback", "parameters": [], "optional": true}
]
},
{
......
......@@ -29,7 +29,44 @@ chrome.browserAction.onClicked.addListener(function(tab) {
tabId: tab.id
});
chrome.test.notifyPass();
// Test that callbacks work as expected.
chrome.browserAction.setIcon({
imageData: new ImageData(1, 1),
tabId: 133713371,
}, function() {
chrome.test.assertLastError("No tab with id: 133713371.");
chrome.browserAction.setTitle({
title: "Ignore because of invalid tabId",
tabId: 133713372,
}, function() {
chrome.test.assertLastError("No tab with id: 133713372.");
chrome.browserAction.setBadgeText({
text: "Ignore because of invalid tabId",
tabId: 133713373,
}, function() {
chrome.test.assertLastError("No tab with id: 133713373.");
chrome.browserAction.setBadgeBackgroundColor({
color: [12, 34, 56, 78],
tabId: 133713374,
}, function() {
chrome.test.assertLastError("No tab with id: 133713374.");
chrome.browserAction.enable(133713375, function() {
chrome.test.assertLastError("No tab with id: 133713375.");
chrome.browserAction.disable(133713376, function() {
chrome.test.assertLastError("No tab with id: 133713376.");
chrome.test.notifyPass();
});
});
});
});
});
});
});
chrome.test.notifyPass();
......@@ -11,6 +11,22 @@ chrome.pageAction.onClicked.addListener(function(tab) {
chrome.tabs.getSelected(null, function(tab) {
tabId = tab.id;
// Callbacks should be not be required:
chrome.pageAction.hide(tabId);
chrome.pageAction.show(tabId);
chrome.test.sendMessage('ready');
// Callbacks should be permitted:
chrome.pageAction.show(tabId, function() {
chrome.test.assertNoLastError();
chrome.pageAction.show(123456789, function() {
chrome.test.assertLastError('No tab with id: 123456789.');
chrome.pageAction.hide(987654321, function() {
chrome.test.assertLastError('No tab with id: 987654321.');
chrome.test.sendMessage('ready');
});
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment