Push API: use an enum to indicate status.

BUG=350377

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282348 0039d316-1c4b-4281-b951-d872f2087c98
parent f5ee30f6
...@@ -142,7 +142,11 @@ void PushMessagingServiceImpl::Register( ...@@ -142,7 +142,11 @@ void PushMessagingServiceImpl::Register(
if (profile_->GetPrefs()->GetInteger( if (profile_->GetPrefs()->GetInteger(
prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) {
DidRegister(app_id, callback, std::string(), GCMClient::UNKNOWN_ERROR); RegisterEnd(
app_id,
callback,
std::string(),
content::PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_LIMIT_REACHED);
return; return;
} }
...@@ -177,7 +181,11 @@ void PushMessagingServiceImpl::Register( ...@@ -177,7 +181,11 @@ void PushMessagingServiceImpl::Register(
gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_);
if (permission_context == NULL) { if (permission_context == NULL) {
DidRegister(app_id, callback, std::string(), GCMClient::UNKNOWN_ERROR); RegisterEnd(
app_id,
callback,
std::string(),
content::PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_PERMISSION_DENIED);
return; return;
} }
...@@ -193,15 +201,14 @@ void PushMessagingServiceImpl::Register( ...@@ -193,15 +201,14 @@ void PushMessagingServiceImpl::Register(
callback)); callback));
} }
void PushMessagingServiceImpl::DidRegister( void PushMessagingServiceImpl::RegisterEnd(
const std::string& app_id, const std::string& app_id,
const content::PushMessagingService::RegisterCallback& callback, const content::PushMessagingService::RegisterCallback& callback,
const std::string& registration_id, const std::string& registration_id,
GCMClient::Result result) { content::PushMessagingStatus status) {
GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); GURL endpoint = GURL("https://android.googleapis.com/gcm/send");
bool success = (result == GCMClient::SUCCESS); callback.Run(endpoint, registration_id, status);
callback.Run(endpoint, registration_id, success); if (status == content::PUSH_MESSAGING_STATUS_OK) {
if (success) {
// TODO(johnme): Make sure the pref doesn't get out of sync after crashes. // TODO(johnme): Make sure the pref doesn't get out of sync after crashes.
int registration_count = profile_->GetPrefs()->GetInteger( int registration_count = profile_->GetPrefs()->GetInteger(
prefs::kPushMessagingRegistrationCount); prefs::kPushMessagingRegistrationCount);
...@@ -210,15 +217,29 @@ void PushMessagingServiceImpl::DidRegister( ...@@ -210,15 +217,29 @@ void PushMessagingServiceImpl::DidRegister(
} }
} }
void PushMessagingServiceImpl::DidRegister(
const std::string& app_id,
const content::PushMessagingService::RegisterCallback& callback,
const std::string& registration_id,
GCMClient::Result result) {
content::PushMessagingStatus status =
result == GCMClient::SUCCESS
? content::PUSH_MESSAGING_STATUS_OK
: content::PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_SERVICE_ERROR;
RegisterEnd(app_id, callback, registration_id, status);
}
void PushMessagingServiceImpl::DidRequestPermission( void PushMessagingServiceImpl::DidRequestPermission(
const std::string& sender_id, const std::string& sender_id,
const std::string& app_id, const std::string& app_id,
const content::PushMessagingService::RegisterCallback& register_callback, const content::PushMessagingService::RegisterCallback& register_callback,
bool allow) { bool allow) {
if (!allow) { if (!allow) {
// TODO(miguelg) extend the error enum to allow for pemission failure. RegisterEnd(
DidRegister(app_id, register_callback, std::string(), app_id,
GCMClient::UNKNOWN_ERROR); register_callback,
std::string(),
content::PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_PERMISSION_DENIED);
return; return;
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "components/gcm_driver/gcm_app_handler.h" #include "components/gcm_driver/gcm_app_handler.h"
#include "components/gcm_driver/gcm_client.h" #include "components/gcm_driver/gcm_client.h"
#include "content/public/browser/push_messaging_service.h" #include "content/public/browser/push_messaging_service.h"
#include "content/public/common/push_messaging_status.h"
class Profile; class Profile;
...@@ -54,6 +55,12 @@ class PushMessagingServiceImpl : public content::PushMessagingService, ...@@ -54,6 +55,12 @@ class PushMessagingServiceImpl : public content::PushMessagingService,
const content::PushMessagingService::RegisterCallback& callback) OVERRIDE; const content::PushMessagingService::RegisterCallback& callback) OVERRIDE;
private: private:
void RegisterEnd(
const std::string& app_id,
const content::PushMessagingService::RegisterCallback& callback,
const std::string& registration_id,
content::PushMessagingStatus status);
void DidRegister( void DidRegister(
const std::string& app_id, const std::string& app_id,
const content::PushMessagingService::RegisterCallback& callback, const content::PushMessagingService::RegisterCallback& callback,
......
...@@ -50,7 +50,10 @@ void PushMessagingMessageFilter::OnRegister(int render_frame_id, ...@@ -50,7 +50,10 @@ void PushMessagingMessageFilter::OnRegister(int render_frame_id,
service_worker_context_->context()->GetProviderHost( service_worker_context_->context()->GetProviderHost(
render_process_id_, service_worker_provider_id); render_process_id_, service_worker_provider_id);
if (!service_worker_host || !service_worker_host->active_version()) { if (!service_worker_host || !service_worker_host->active_version()) {
Send(new PushMessagingMsg_RegisterError(render_frame_id, callbacks_id)); Send(new PushMessagingMsg_RegisterError(
render_frame_id,
callbacks_id,
PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_NO_SERVICE_WORKER));
return; return;
} }
BrowserThread::PostTask( BrowserThread::PostTask(
...@@ -75,7 +78,10 @@ void PushMessagingMessageFilter::DoRegister( ...@@ -75,7 +78,10 @@ void PushMessagingMessageFilter::DoRegister(
int64 service_worker_registration_id) { int64 service_worker_registration_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!service()) { if (!service()) {
Send(new PushMessagingMsg_RegisterError(render_frame_id, callbacks_id)); Send(new PushMessagingMsg_RegisterError(
render_frame_id,
callbacks_id,
PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_SERVICE_NOT_AVAILABLE));
return; return;
} }
// TODO(mvanouwerkerk): Is this the app_id format we want to use? // TODO(mvanouwerkerk): Is this the app_id format we want to use?
...@@ -97,13 +103,14 @@ void PushMessagingMessageFilter::DidRegister( ...@@ -97,13 +103,14 @@ void PushMessagingMessageFilter::DidRegister(
int callbacks_id, int callbacks_id,
const GURL& push_endpoint, const GURL& push_endpoint,
const std::string& push_registration_id, const std::string& push_registration_id,
bool success) { PushMessagingStatus status) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (success) { if (status == PUSH_MESSAGING_STATUS_OK) {
Send(new PushMessagingMsg_RegisterSuccess( Send(new PushMessagingMsg_RegisterSuccess(
render_frame_id, callbacks_id, push_endpoint, push_registration_id)); render_frame_id, callbacks_id, push_endpoint, push_registration_id));
} else { } else {
Send(new PushMessagingMsg_RegisterError(render_frame_id, callbacks_id)); Send(new PushMessagingMsg_RegisterError(
render_frame_id, callbacks_id, status));
} }
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "content/public/browser/browser_message_filter.h" #include "content/public/browser/browser_message_filter.h"
#include "content/public/common/push_messaging_status.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace content { namespace content {
...@@ -46,7 +47,7 @@ class PushMessagingMessageFilter : public BrowserMessageFilter { ...@@ -46,7 +47,7 @@ class PushMessagingMessageFilter : public BrowserMessageFilter {
int callbacks_id, int callbacks_id,
const GURL& push_endpoint, const GURL& push_endpoint,
const std::string& push_registration_id, const std::string& push_registration_id,
bool success); PushMessagingStatus status);
PushMessagingService* service(); PushMessagingService* service();
......
...@@ -5,11 +5,15 @@ ...@@ -5,11 +5,15 @@
// IPC messages for push messaging. // IPC messages for push messaging.
// Multiply-included message file, hence no include guard. // Multiply-included message file, hence no include guard.
#include "content/public/common/push_messaging_status.h"
#include "ipc/ipc_message_macros.h" #include "ipc/ipc_message_macros.h"
#include "url/gurl.h" #include "url/gurl.h"
#define IPC_MESSAGE_START PushMessagingMsgStart #define IPC_MESSAGE_START PushMessagingMsgStart
IPC_ENUM_TRAITS_MAX_VALUE(content::PushMessagingStatus,
content::PUSH_MESSAGING_STATUS_LAST)
// Messages sent from the browser to the renderer. // Messages sent from the browser to the renderer.
IPC_MESSAGE_ROUTED3(PushMessagingMsg_RegisterSuccess, IPC_MESSAGE_ROUTED3(PushMessagingMsg_RegisterSuccess,
...@@ -17,8 +21,9 @@ IPC_MESSAGE_ROUTED3(PushMessagingMsg_RegisterSuccess, ...@@ -17,8 +21,9 @@ IPC_MESSAGE_ROUTED3(PushMessagingMsg_RegisterSuccess,
GURL /* push_endpoint */, GURL /* push_endpoint */,
std::string /* push_registration_id */) std::string /* push_registration_id */)
IPC_MESSAGE_ROUTED1(PushMessagingMsg_RegisterError, IPC_MESSAGE_ROUTED2(PushMessagingMsg_RegisterError,
int32 /* callbacks_id */) int32 /* callbacks_id */,
content::PushMessagingStatus /* status */)
// Messages sent from the renderer to the browser. // Messages sent from the renderer to the browser.
......
...@@ -87,6 +87,8 @@ ...@@ -87,6 +87,8 @@
'public/common/pepper_plugin_info.cc', 'public/common/pepper_plugin_info.cc',
'public/common/pepper_plugin_info.h', 'public/common/pepper_plugin_info.h',
'public/common/process_type.h', 'public/common/process_type.h',
'public/common/push_messaging_status.cc',
'public/common/push_messaging_status.h',
'public/common/referrer.h', 'public/common/referrer.h',
'public/common/renderer_preferences.cc', 'public/common/renderer_preferences.cc',
'public/common/renderer_preferences.h', 'public/common/renderer_preferences.h',
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/callback.h" #include "base/callback.h"
#include "content/common/content_export.h" #include "content/common/content_export.h"
#include "content/public/common/push_messaging_status.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace content { namespace content {
...@@ -19,7 +20,7 @@ class CONTENT_EXPORT PushMessagingService { ...@@ -19,7 +20,7 @@ class CONTENT_EXPORT PushMessagingService {
public: public:
typedef base::Callback<void(const GURL& /* endpoint */, typedef base::Callback<void(const GURL& /* endpoint */,
const std::string& /* registration_id */, const std::string& /* registration_id */,
bool /* success */)> PushMessagingStatus /* status */)>
RegisterCallback; RegisterCallback;
virtual ~PushMessagingService() {} virtual ~PushMessagingService() {}
......
// 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.
#include "content/public/common/push_messaging_status.h"
#include "base/logging.h"
namespace content {
const char* PushMessagingStatusToString(PushMessagingStatus status) {
switch (status) {
case PUSH_MESSAGING_STATUS_OK:
return "Operation has succeeded";
case PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_NO_SERVICE_WORKER:
return "Registration failed - no Service Worker";
case PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_SERVICE_NOT_AVAILABLE:
return "Registration failed - push service not available";
case PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_LIMIT_REACHED:
return "Registration failed - registration limit has been reached";
case PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_PERMISSION_DENIED:
return "Registration failed - permission denied";
case PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_SERVICE_ERROR:
return "Registration failed - push service error";
case PUSH_MESSAGING_STATUS_ERROR:
return "Operation has failed (unspecified reason)";
}
NOTREACHED();
return "";
}
} // namespace content
// 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.
#ifndef CONTENT_PUBLIC_COMMON_PUSH_MESSAGING_STATUS_STATUS_H_
#define CONTENT_PUBLIC_COMMON_PUSH_MESSAGING_STATUS_STATUS_H_
namespace content {
enum PushMessagingStatus {
// Everything is ok.
PUSH_MESSAGING_STATUS_OK,
// Registration failed because there is no Service Worker.
PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_NO_SERVICE_WORKER,
// Registration failed because the push service is not available.
PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_SERVICE_NOT_AVAILABLE,
// Registration failed because the maximum number of registratons has been
// reached.
PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_LIMIT_REACHED,
// Registration failed because permission was denied.
PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_PERMISSION_DENIED,
// Registration failed in the push service implemented by the embedder.
PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_SERVICE_ERROR,
// Generic error (a more specific error should be used whenever possible).
PUSH_MESSAGING_STATUS_ERROR,
// Used for IPC message range checks.
PUSH_MESSAGING_STATUS_LAST = PUSH_MESSAGING_STATUS_ERROR
};
const char* PushMessagingStatusToString(PushMessagingStatus status);
} // namespace content
#endif // CONTENT_PUBLIC_COMMON_PUSH_MESSAGING_STATUS_STATUS_H_
...@@ -16,10 +16,6 @@ ...@@ -16,10 +16,6 @@
using blink::WebString; using blink::WebString;
namespace {
const char kAbortErrorReason[] = "Registration failed.";
}
namespace content { namespace content {
PushMessagingDispatcher::PushMessagingDispatcher(RenderFrame* render_frame) PushMessagingDispatcher::PushMessagingDispatcher(RenderFrame* render_frame)
...@@ -42,9 +38,10 @@ void PushMessagingDispatcher::registerPushMessaging( ...@@ -42,9 +38,10 @@ void PushMessagingDispatcher::registerPushMessaging(
const WebString& sender_id, const WebString& sender_id,
blink::WebPushRegistrationCallbacks* callbacks) { blink::WebPushRegistrationCallbacks* callbacks) {
DCHECK(callbacks); DCHECK(callbacks);
scoped_ptr<blink::WebPushError> error( scoped_ptr<blink::WebPushError> error(new blink::WebPushError(
new blink::WebPushError(blink::WebPushError::ErrorTypeAbort, blink::WebPushError::ErrorTypeAbort,
WebString::fromUTF8(kAbortErrorReason))); WebString::fromUTF8(PushMessagingStatusToString(
PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_NO_SERVICE_WORKER))));
callbacks->onError(error.release()); callbacks->onError(error.release());
delete callbacks; delete callbacks;
} }
...@@ -81,15 +78,15 @@ void PushMessagingDispatcher::OnRegisterSuccess( ...@@ -81,15 +78,15 @@ void PushMessagingDispatcher::OnRegisterSuccess(
registration_callbacks_.Remove(callbacks_id); registration_callbacks_.Remove(callbacks_id);
} }
void PushMessagingDispatcher::OnRegisterError(int32 callbacks_id) { void PushMessagingDispatcher::OnRegisterError(int32 callbacks_id,
PushMessagingStatus status) {
blink::WebPushRegistrationCallbacks* callbacks = blink::WebPushRegistrationCallbacks* callbacks =
registration_callbacks_.Lookup(callbacks_id); registration_callbacks_.Lookup(callbacks_id);
CHECK(callbacks); CHECK(callbacks);
scoped_ptr<blink::WebPushError> error( scoped_ptr<blink::WebPushError> error(new blink::WebPushError(
new blink::WebPushError( blink::WebPushError::ErrorTypeAbort,
blink::WebPushError::ErrorTypeAbort, WebString::fromUTF8(PushMessagingStatusToString(status))));
WebString::fromUTF8(kAbortErrorReason)));
callbacks->onError(error.release()); callbacks->onError(error.release());
registration_callbacks_.Remove(callbacks_id); registration_callbacks_.Remove(callbacks_id);
} }
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <string> #include <string>
#include "base/id_map.h" #include "base/id_map.h"
#include "content/public/common/push_messaging_status.h"
#include "content/public/renderer/render_frame_observer.h" #include "content/public/renderer/render_frame_observer.h"
#include "third_party/WebKit/public/platform/WebPushClient.h" #include "third_party/WebKit/public/platform/WebPushClient.h"
...@@ -48,7 +49,7 @@ class PushMessagingDispatcher : public RenderFrameObserver, ...@@ -48,7 +49,7 @@ class PushMessagingDispatcher : public RenderFrameObserver,
const GURL& endpoint, const GURL& endpoint,
const std::string& registration_id); const std::string& registration_id);
void OnRegisterError(int32 callbacks_id); void OnRegisterError(int32 callbacks_id, PushMessagingStatus status);
IDMap<blink::WebPushRegistrationCallbacks, IDMapOwnPointer> IDMap<blink::WebPushRegistrationCallbacks, IDMapOwnPointer>
registration_callbacks_; registration_callbacks_;
......
...@@ -15,9 +15,8 @@ using blink::WebString; ...@@ -15,9 +15,8 @@ using blink::WebString;
namespace content { namespace content {
MockWebPushClient::MockWebPushClient() MockWebPushClient::MockWebPushClient()
// The default state should be be an error with "Registration failed." message : error_message_(
// because LayoutTests are currently depending on that. "Registration failed (default mock client error message)") {
: error_message_("Registration failed.") {
} }
MockWebPushClient::~MockWebPushClient() {} MockWebPushClient::~MockWebPushClient() {}
......
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