Skip to content

Commit aeff192

Browse files
committed
[Merge m67][Extensions Bindings] Tweak messaging argument parsing
There was a bug where if the ID were passed as null or undefined (rather than completely omitted), the message would fail to parse. Fix this, and add a unittest. Bug: 828664 TBR=rdevlin.cronin@chromium.org (cherry picked from commit 7c723eb) Change-Id: I92b0f071a1627322e12e8eaf0f5787ccd0793138 Reviewed-on: https://chromium-review.googlesource.com/1012601 Reviewed-by: Istiaque Ahmed <lazyboy@chromium.org> Commit-Queue: Devlin <rdevlin.cronin@chromium.org> Cr-Original-Commit-Position: refs/heads/master@{#550718} Reviewed-on: https://chromium-review.googlesource.com/1015412 Reviewed-by: Devlin <rdevlin.cronin@chromium.org> Cr-Commit-Position: refs/branch-heads/3396@{#50} Cr-Branched-From: 9ef2aa8-refs/heads/master@{#550428}
1 parent 76b3dbf commit aeff192

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

extensions/renderer/messaging_util.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,21 @@ void MassageSendMessageArguments(
270270
// Argument must be the message.
271271
message = arguments[0];
272272
break;
273-
case 2:
274-
// Assume the meaning is (id, message) if id would be a string, or if
275-
// the options argument isn't expected.
276-
// Otherwise the meaning is (message, options).
277-
if (!allow_options_argument || arguments[0]->IsString()) {
273+
case 2: {
274+
// Assume the first argument is the ID if we don't expect options, or if
275+
// the argument could match the ID parameter.
276+
// ID could be either a string, or null/undefined (since it's optional).
277+
bool could_match_id =
278+
arguments[0]->IsString() || arguments[0]->IsNullOrUndefined();
279+
if (!allow_options_argument || could_match_id) {
278280
target_id = arguments[0];
279281
message = arguments[1];
280-
} else {
282+
} else { // Otherwise, the meaning is (message, options).
281283
message = arguments[0];
282284
options = arguments[1];
283285
}
284286
break;
287+
}
285288
case 3:
286289
DCHECK(allow_options_argument);
287290
// The meaning in this case is unambiguous.

extensions/renderer/runtime_hooks_delegate_unittest.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,21 @@ TEST_F(RuntimeHooksDelegateTest, SendMessage) {
254254
R"("string message")", other_target, false,
255255
SendMessageTester::CLOSED);
256256

257+
// The sender could omit the ID by passing null or undefined explicitly.
258+
// Regression tests for https://crbug.com/828664.
259+
tester.TestSendMessage("null, {data: 'hello'}, function() {}",
260+
kStandardMessage, self_target, false,
261+
SendMessageTester::OPEN);
262+
tester.TestSendMessage("null, 'test', function() {}",
263+
R"("test")", self_target, false,
264+
SendMessageTester::OPEN);
265+
tester.TestSendMessage("null, 'test'",
266+
R"("test")", self_target, false,
267+
SendMessageTester::CLOSED);
268+
tester.TestSendMessage("undefined, 'test', function() {}",
269+
R"("test")", self_target, false,
270+
SendMessageTester::OPEN);
271+
257272
// Funny case. The only required argument is `message`, which can be any type.
258273
// This means that if an extension provides a <string, object> pair for the
259274
// first three arguments, it could apply to either the target id and the

0 commit comments

Comments
 (0)