Commit 8c91586b authored by jianli@chromium.org's avatar jianli@chromium.org

[GCM] Move the manifest key checking logic to gcm.register function

We should only require the manifest key for gcm.register. A meaningful
error should also be returned.

BUG=344027
TEST=tests updated and new test added

Review URL: https://codereview.chromium.org/167063004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@251535 0039d316-1c4b-4281-b951-d872f2087c98
parent 694c1224
...@@ -31,6 +31,7 @@ const char kGoogleRestrictedPrefix[] = "google"; ...@@ -31,6 +31,7 @@ const char kGoogleRestrictedPrefix[] = "google";
const char kInvalidParameter[] = const char kInvalidParameter[] =
"Function was called with invalid parameters."; "Function was called with invalid parameters.";
const char kNotSignedIn[] = "Profile was not signed in."; const char kNotSignedIn[] = "Profile was not signed in.";
const char kCertificateMissing[] = "Manifest key was missing.";
const char kAsyncOperationPending[] = const char kAsyncOperationPending[] =
"Asynchronous operation is pending."; "Asynchronous operation is pending.";
const char kNetworkError[] = "Network error occurred."; const char kNetworkError[] = "Network error occurred.";
...@@ -51,6 +52,8 @@ const char* GcmResultToError(gcm::GCMClient::Result result) { ...@@ -51,6 +52,8 @@ const char* GcmResultToError(gcm::GCMClient::Result result) {
return kInvalidParameter; return kInvalidParameter;
case gcm::GCMClient::NOT_SIGNED_IN: case gcm::GCMClient::NOT_SIGNED_IN:
return kNotSignedIn; return kNotSignedIn;
case gcm::GCMClient::CERTIFICATE_MISSING:
return kCertificateMissing;
case gcm::GCMClient::ASYNC_OPERATION_PENDING: case gcm::GCMClient::ASYNC_OPERATION_PENDING:
return kAsyncOperationPending; return kAsyncOperationPending;
case gcm::GCMClient::NETWORK_ERROR: case gcm::GCMClient::NETWORK_ERROR:
...@@ -95,8 +98,7 @@ bool GcmApiFunction::RunImpl() { ...@@ -95,8 +98,7 @@ bool GcmApiFunction::RunImpl() {
bool GcmApiFunction::IsGcmApiEnabled() const { bool GcmApiFunction::IsGcmApiEnabled() const {
return gcm::GCMProfileService::IsGCMEnabled( return gcm::GCMProfileService::IsGCMEnabled(
Profile::FromBrowserContext(context())) && Profile::FromBrowserContext(context()));
!GetExtension()->public_key().empty();
} }
gcm::GCMProfileService* GcmApiFunction::GCMProfileService() const { gcm::GCMProfileService* GcmApiFunction::GCMProfileService() const {
...@@ -113,6 +115,12 @@ bool GcmRegisterFunction::DoWork() { ...@@ -113,6 +115,12 @@ bool GcmRegisterFunction::DoWork() {
api::gcm::Register::Params::Create(*args_)); api::gcm::Register::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get()); EXTENSION_FUNCTION_VALIDATE(params.get());
if (GetExtension()->public_key().empty()) {
CompleteFunctionWithResult(std::string(),
gcm::GCMClient::CERTIFICATE_MISSING);
return false;
}
GCMProfileService()->Register( GCMProfileService()->Register(
GetExtension()->id(), GetExtension()->id(),
params->sender_ids, params->sender_ids,
......
...@@ -101,6 +101,13 @@ IN_PROC_BROWSER_TEST_F(GcmApiTest, Register) { ...@@ -101,6 +101,13 @@ IN_PROC_BROWSER_TEST_F(GcmApiTest, Register) {
sender_ids.end()); sender_ids.end());
} }
IN_PROC_BROWSER_TEST_F(GcmApiTest, RegisterWithoutKey) {
if (ShouldSkipTest())
return;
ASSERT_TRUE(RunExtensionTest("gcm/functions/register_without_key"));
}
IN_PROC_BROWSER_TEST_F(GcmApiTest, SendValidation) { IN_PROC_BROWSER_TEST_F(GcmApiTest, SendValidation) {
if (ShouldSkipTest()) if (ShouldSkipTest())
return; return;
......
{
"manifest_version": 2,
"name": "Test GCM App",
"version": "1.0",
"description": "Tests GCM API",
"background": {
"scripts": ["register.js"]
},
"permissions": ["gcm"]
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
chrome.test.runTests([
function testRegister() {
var senderIds = ["Sender1", "Sender2"];
chrome.gcm.register(senderIds, function(registrationId) {
if (chrome.runtime.lastError.message == "Manifest key was missing.")
chrome.test.succeed();
else
chrome.test.fail("gcm.register should fail.");
});
}
]);
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Test GCM App", "name": "Test GCM App",
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCzXCAvAUOKVNuBGWfaRY6QW1YSs+2EnmERkltllVyk9T6H/a7wkdBAKpvKsEbE9PtjmHrP/Jh/XFX1AdpTFAJ7KsQ55GdyTwUW7iB5HsR4NI6dXewK2ba+vDbtBLytR9msXFpvuVnAsEmLlHf9RGuxczvOEbGXcHkUw0AuzGu72wIDAQAB",
"version": "1.0", "version": "1.0",
"description": "Tests GCM API", "description": "Tests GCM API",
"background": { "background": {
......
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Test GCM App", "name": "Test GCM App",
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCzXCAvAUOKVNuBGWfaRY6QW1YSs+2EnmERkltllVyk9T6H/a7wkdBAKpvKsEbE9PtjmHrP/Jh/XFX1AdpTFAJ7KsQ55GdyTwUW7iB5HsR4NI6dXewK2ba+vDbtBLytR9msXFpvuVnAsEmLlHf9RGuxczvOEbGXcHkUw0AuzGu72wIDAQAB",
"version": "1.0", "version": "1.0",
"description": "Tests GCM API", "description": "Tests GCM API",
"background": { "background": {
......
...@@ -40,6 +40,8 @@ class GCM_EXPORT GCMClient { ...@@ -40,6 +40,8 @@ class GCM_EXPORT GCMClient {
INVALID_PARAMETER, INVALID_PARAMETER,
// Profile not signed in. // Profile not signed in.
NOT_SIGNED_IN, NOT_SIGNED_IN,
// Certificate was missing. Certain operation, like register, requires it.
CERTIFICATE_MISSING,
// Previous asynchronous operation is still pending to finish. Certain // Previous asynchronous operation is still pending to finish. Certain
// operation, like register, is only allowed one at a time. // operation, like register, is only allowed one at a time.
ASYNC_OPERATION_PENDING, ASYNC_OPERATION_PENDING,
......
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