| Index: chrome/browser/services/gcm/push_messaging_service_impl.cc
|
| diff --git a/chrome/browser/services/gcm/push_messaging_service_impl.cc b/chrome/browser/services/gcm/push_messaging_service_impl.cc
|
| index 0306d111fe66e95f69e299af9d68056c0989f568..c2563b8c17315a0f31efdaa9389bdec73bf4510a 100644
|
| --- a/chrome/browser/services/gcm/push_messaging_service_impl.cc
|
| +++ b/chrome/browser/services/gcm/push_messaging_service_impl.cc
|
| @@ -9,7 +9,6 @@
|
| #include "base/bind.h"
|
| #include "base/command_line.h"
|
| #include "base/prefs/pref_service.h"
|
| -#include "base/strings/string_util.h"
|
| #include "chrome/browser/profiles/profile.h"
|
| #include "chrome/browser/services/gcm/gcm_profile_service.h"
|
| #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
|
| @@ -17,11 +16,22 @@
|
| #include "chrome/common/pref_names.h"
|
| #include "components/gcm_driver/gcm_driver.h"
|
| #include "components/pref_registry/pref_registry_syncable.h"
|
| +#include "content/browser/service_worker/service_worker_context_wrapper.h"
|
| +#include "content/browser/service_worker/service_worker_registration.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "content/public/browser/push_messaging_application_id.h"
|
| +#include "content/public/browser/storage_partition.h"
|
| +
|
| +using content::BrowserContext;
|
| +using content::BrowserThread;
|
| +using content::PushMessagingApplicationId;
|
| +using content::ServiceWorkerContextWrapper;
|
|
|
| namespace gcm {
|
|
|
| namespace {
|
| -const char kAppIdPrefix[] = "push:";
|
| +// TODO(mvanouwerkerk): Move this to a public shared place.
|
| +const char kAppIdPrefix[] = "push";
|
| const int kMaxRegistrations = 1000000;
|
| } // namespace
|
|
|
| @@ -75,8 +85,7 @@ PushMessagingServiceImpl::~PushMessagingServiceImpl() {
|
| }
|
|
|
| bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const {
|
| - // TODO(mvanouwerkerk): Finalize and centralize format of Push API app_id.
|
| - return StartsWithASCII(app_id, kAppIdPrefix, true);
|
| + return PushMessagingApplicationId::IsValid(app_id);
|
| }
|
|
|
| void PushMessagingServiceImpl::ShutdownHandler() {
|
| @@ -86,6 +95,8 @@ void PushMessagingServiceImpl::ShutdownHandler() {
|
| void PushMessagingServiceImpl::OnMessage(
|
| const std::string& app_id,
|
| const GCMClient::IncomingMessage& message) {
|
| + LOG(WARNING) << "app_id: " << app_id;
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| // The Push API only exposes a single string of data in the push event fired
|
| // on the Service Worker. When developers send messages using GCM to the Push
|
| // API, they must pass a single key-value pair, where the key is "data" and
|
| @@ -99,12 +110,25 @@ void PushMessagingServiceImpl::OnMessage(
|
| // "delay_while_idle": true,
|
| // }
|
| // TODO(johnme): Make sure this is clearly documented for developers.
|
| + PushMessagingApplicationId id = PushMessagingApplicationId::Parse(app_id);
|
| + DCHECK(id.is_valid());
|
| GCMClient::MessageData::const_iterator it = message.data.find("data");
|
| - if (it != message.data.end()) {
|
| - const std::string& data ALLOW_UNUSED = it->second;
|
| - // TODO(mvanouwerkerk): Fire push event with data on the Service Worker
|
| - // corresponding to app_id (and remove ALLOW_UNUSED above).
|
| + if (id.is_valid() && it != message.data.end()) {
|
| + const std::string& data = it->second;
|
| + content::StoragePartition* partition =
|
| + BrowserContext::GetStoragePartitionForSite(profile_, id.origin);
|
| + scoped_refptr<ServiceWorkerContextWrapper> worker_context =
|
| + static_cast<ServiceWorkerContextWrapper*>(
|
| + partition->GetServiceWorkerContext());
|
| + BrowserThread::PostTask(BrowserThread::IO,
|
| + FROM_HERE,
|
| + base::Bind(&PushMessagingServiceImpl::SendMessage,
|
| + weak_factory_.GetWeakPtr(),
|
| + id,
|
| + data,
|
| + worker_context));
|
| } else {
|
| + LOG(WARNING) << "Dropping the message.";
|
| // Drop the message, as it is invalid.
|
| // TODO(mvanouwerkerk): Show a warning in the developer console of the
|
| // Service Worker corresponding to app_id.
|
| @@ -113,6 +137,32 @@ void PushMessagingServiceImpl::OnMessage(
|
| }
|
| }
|
|
|
| +void PushMessagingServiceImpl::SendMessage(
|
| + const PushMessagingApplicationId& id,
|
| + const std::string& data,
|
| + scoped_refptr<ServiceWorkerContextWrapper> worker_context) {
|
| + LOG(WARNING) << "data: " << data;
|
| + // TODO(mvanouwerkerk): Can this method live in content::PushMessagingService?
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| + content::ServiceWorkerRegistration* worker_registration =
|
| + worker_context->context()->GetLiveRegistration(id.worker_registration_id);
|
| + if (worker_registration) {
|
| + base::Callback<void(ServiceWorkerStatusCode)> callback =
|
| + base::Bind(&PushMessagingServiceImpl::SendMessageCallback,
|
| + weak_factory_.GetWeakPtr());
|
| + worker_registration->active_version()->DispatchPushEvent(callback, data);
|
| + } else {
|
| + // TODO(mvanouwerkerk): UMA logging. Can we recover from failure?
|
| + LOG(WARNING) << "Could not find ServiceWorkerRegistration.";
|
| + }
|
| +}
|
| +
|
| +void PushMessagingServiceImpl::SendMessageCallback(
|
| + ServiceWorkerStatusCode status) {
|
| + // TODO(mvanouwerkerk): UMA logging. Can we recover from failures?
|
| + LOG(WARNING) << "ServiceWorkerStatusCode: " << status;
|
| +}
|
| +
|
| void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) {
|
| // TODO(mvanouwerkerk): Fire push error event on the Service Worker
|
| // corresponding to app_id.
|
| @@ -128,12 +178,14 @@ void PushMessagingServiceImpl::Register(
|
| const std::string& app_id,
|
| const std::string& sender_id,
|
| const content::PushMessagingService::RegisterCallback& callback) {
|
| + LOG(WARNING) << "app_id: " << app_id;
|
| if (!gcm_profile_service_->driver()) {
|
| NOTREACHED() << "There is no GCMDriver. Has GCMProfileService shut down?";
|
| }
|
|
|
| if (profile_->GetPrefs()->GetInteger(
|
| prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) {
|
| + LOG(WARNING) << "Registration limit has been reached.";
|
| DidRegister(app_id, callback, "", GCMClient::UNKNOWN_ERROR);
|
| return;
|
| }
|
| @@ -158,6 +210,7 @@ void PushMessagingServiceImpl::DidRegister(
|
| const content::PushMessagingService::RegisterCallback& callback,
|
| const std::string& registration_id,
|
| GCMClient::Result result) {
|
| + LOG(WARNING) << "result: " << result;
|
| GURL endpoint = GURL("https://android.googleapis.com/gcm/send");
|
| bool success = (result == GCMClient::SUCCESS);
|
| callback.Run(endpoint, registration_id, success);
|
|
|