From 664db4318189b5c9a93a0892998442822a7edc13 Mon Sep 17 00:00:00 2001 From: jianli Date: Fri, 21 Nov 2014 14:00:08 -0800 Subject: [PATCH] Expose "from" property in GCM incoming data message BUG=433536 TEST=test updated Review URL: https://codereview.chromium.org/740073002 Cr-Commit-Position: refs/heads/master@{#305294} --- chrome/browser/extensions/api/gcm/gcm_api.cc | 2 ++ chrome/browser/extensions/api/gcm/gcm_apitest.cc | 13 ++++++++-- chrome/common/extensions/api/gcm.json | 7 +++++- .../extensions/api_test/gcm/events/on_message.js | 28 ++++++++++++++++++---- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/chrome/browser/extensions/api/gcm/gcm_api.cc b/chrome/browser/extensions/api/gcm/gcm_api.cc index c8bac687ed2c..0c29f093cac5 100644 --- a/chrome/browser/extensions/api/gcm/gcm_api.cc +++ b/chrome/browser/extensions/api/gcm/gcm_api.cc @@ -213,6 +213,8 @@ void GcmJsEventRouter::OnMessage( const gcm::GCMClient::IncomingMessage& message) { api::gcm::OnMessage::Message message_arg; message_arg.data.additional_properties = message.data; + if (!message.sender_id.empty()) + message_arg.from.reset(new std::string(message.sender_id)); if (!message.collapse_key.empty()) message_arg.collapse_key.reset(new std::string(message.collapse_key)); diff --git a/chrome/browser/extensions/api/gcm/gcm_apitest.cc b/chrome/browser/extensions/api/gcm/gcm_apitest.cc index 4ab4e24621ac..fdd057b0d4de 100644 --- a/chrome/browser/extensions/api/gcm/gcm_apitest.cc +++ b/chrome/browser/extensions/api/gcm/gcm_apitest.cc @@ -200,13 +200,22 @@ IN_PROC_BROWSER_TEST_F(GcmApiTest, OnMessage) { gcm::GCMClient::IncomingMessage message; message.data["property1"] = "value1"; message.data["property2"] = "value2"; - // First message is sent without a collapse key. + // First message is sent without from and collapse key. app_handler.OnMessage(extension->id(), message); - // Second message carries the same data and a collapse key. + // Second message is send with from. + message.sender_id = "12345678"; + app_handler.OnMessage(extension->id(), message); + + // Third message is send with a collapse key. + message.sender_id.clear(); message.collapse_key = "collapseKeyValue"; app_handler.OnMessage(extension->id(), message); + // Fourth message carries the same data, from and collapse key. + message.sender_id = "12345678"; + app_handler.OnMessage(extension->id(), message); + EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); } diff --git a/chrome/common/extensions/api/gcm.json b/chrome/common/extensions/api/gcm.json index 3586292519a2..aefee4b3efe5 100644 --- a/chrome/common/extensions/api/gcm.json +++ b/chrome/common/extensions/api/gcm.json @@ -128,10 +128,15 @@ }, "description": "The message data." }, + "from": { + "type": "string", + "optional": true, + "description": "The sender who issued the message." + }, "collapseKey": { "type": "string", "optional": true, - "desctription": "The collapse key of a message. See Collapsible Messages section of Cloud Messaging documentation for details." + "description": "The collapse key of a message. See Collapsible Messages section of Cloud Messaging documentation for details." } } } diff --git a/chrome/test/data/extensions/api_test/gcm/events/on_message.js b/chrome/test/data/extensions/api_test/gcm/events/on_message.js index c6d4d2b9e415..cfc1e38b080f 100644 --- a/chrome/test/data/extensions/api_test/gcm/events/on_message.js +++ b/chrome/test/data/extensions/api_test/gcm/events/on_message.js @@ -5,20 +5,37 @@ onload = function() { chrome.test.runTests([ function onMessage() { - var expectedCalls = 2; + var expectedCalls = 4; + var fromAndCollapseKeyTested = false; + var fromTested = false; var collapseKeyTested = false; var regularMessageTested = false; var eventHandler = function(message) { - // Test with and without a collapse key. + var hasFrom = false; + var hasCollapseKey = false; + if (message.hasOwnProperty('from')) { + // Test with from. + chrome.test.assertEq('12345678', message.from); + hasFrom = true; + } if (message.hasOwnProperty('collapseKey')) { + // Test with a collapse key. chrome.test.assertEq('collapseKeyValue', message.collapseKey); + hasCollapseKey = true; + } + + if (hasFrom && hasCollapseKey) { + fromAndCollapseKeyTested = true; + } else if (hasFrom) { + fromTested = true; + } else if (hasCollapseKey) { collapseKeyTested = true; } else { - chrome.test.assertFalse(message.hasOwnProperty('collapseKey')); regularMessageTested = true; } - // The message is expected to carry data regardless of collapse key. + // The message is expected to carry data regardless of other optional + // fields. chrome.test.assertEq(2, Object.keys(message.data).length); chrome.test.assertTrue(message.data.hasOwnProperty('property1')); chrome.test.assertTrue(message.data.hasOwnProperty('property2')); @@ -28,7 +45,8 @@ onload = function() { --expectedCalls; if (expectedCalls == 0) { chrome.gcm.onMessage.removeListener(eventHandler); - if (collapseKeyTested && regularMessageTested) { + if (fromAndCollapseKeyTested && fromTested && collapseKeyTested && + regularMessageTested) { chrome.test.succeed(); } else { chrome.test.fail(); -- 2.11.4.GIT