diff options
author | johnme@chromium.org <johnme@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-13 14:43:37 +0000 |
---|---|---|
committer | johnme@chromium.org <johnme@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-13 14:43:37 +0000 |
commit | c5c89d0418fa2386b3b5da8232252ba4770dc06c (patch) | |
tree | 3573784623cd44075061dbf0d51e41e8890f1091 /chrome/browser/services | |
parent | 8bb3e51628226abcc38a66b2a50adbdbefe5152f (diff) | |
download | chromium_src-c5c89d0418fa2386b3b5da8232252ba4770dc06c.zip chromium_src-c5c89d0418fa2386b3b5da8232252ba4770dc06c.tar.gz chromium_src-c5c89d0418fa2386b3b5da8232252ba4770dc06c.tar.bz2 |
Hook PushMessagingMessageFilter up to GCMDriver
Add plumbing for registration from push service-agnostic content layer
to GCMDriver in chrome layer.
To achieve this, GCMProfileService owns an implementation of a new
PushMessagingService class, which can be used from the content layer.
Based on mvanouwerkerk's prototype in https://codereview.chromium.org/186023002, but significantly refactored.
BUG=350384
TBR=benm@chromium.org
Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=276792
Review URL: https://codereview.chromium.org/317823007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@277016 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/services')
4 files changed, 104 insertions, 3 deletions
diff --git a/chrome/browser/services/gcm/gcm_profile_service.cc b/chrome/browser/services/gcm/gcm_profile_service.cc index 424f5909..d4ab702 100644 --- a/chrome/browser/services/gcm/gcm_profile_service.cc +++ b/chrome/browser/services/gcm/gcm_profile_service.cc @@ -44,7 +44,8 @@ void GCMProfileService::RegisterProfilePrefs( #if defined(OS_ANDROID) GCMProfileService::GCMProfileService(Profile* profile) - : profile_(profile) { + : profile_(profile), + push_messaging_service_(this) { DCHECK(!profile->IsOffTheRecord()); driver_.reset(new GCMDriverAndroid); @@ -53,7 +54,8 @@ GCMProfileService::GCMProfileService(Profile* profile) GCMProfileService::GCMProfileService( Profile* profile, scoped_ptr<GCMClientFactory> gcm_client_factory) - : profile_(profile) { + : profile_(profile), + push_messaging_service_(this) { DCHECK(!profile->IsOffTheRecord()); driver_ = CreateGCMDriverDesktop( @@ -67,7 +69,9 @@ GCMProfileService::GCMProfileService( } #endif // defined(OS_ANDROID) -GCMProfileService::GCMProfileService() : profile_(NULL) { +GCMProfileService::GCMProfileService() + : profile_(NULL), + push_messaging_service_(this) { } GCMProfileService::~GCMProfileService() { diff --git a/chrome/browser/services/gcm/gcm_profile_service.h b/chrome/browser/services/gcm/gcm_profile_service.h index 70b6112..df05dbb 100644 --- a/chrome/browser/services/gcm/gcm_profile_service.h +++ b/chrome/browser/services/gcm/gcm_profile_service.h @@ -10,6 +10,7 @@ #include "base/compiler_specific.h" #include "base/macros.h" #include "base/memory/scoped_ptr.h" +#include "chrome/browser/services/gcm/push_messaging_service_impl.h" // TODO(jianli): include needed for obsolete methods that are going to be // removed soon. #include "components/gcm_driver/gcm_driver.h" @@ -58,6 +59,10 @@ class GCMProfileService : public KeyedService { GCMDriver* driver() const { return driver_.get(); } + content::PushMessagingService* push_messaging_service() { + return &push_messaging_service_; + } + protected: // Used for constructing fake GCMProfileService for testing purpose. GCMProfileService(); @@ -68,6 +73,9 @@ class GCMProfileService : public KeyedService { scoped_ptr<GCMDriver> driver_; + // Implementation of content::PushMessagingService using GCMProfileService. + PushMessagingServiceImpl push_messaging_service_; + DISALLOW_COPY_AND_ASSIGN(GCMProfileService); }; diff --git a/chrome/browser/services/gcm/push_messaging_service_impl.cc b/chrome/browser/services/gcm/push_messaging_service_impl.cc new file mode 100644 index 0000000..f6043b4 --- /dev/null +++ b/chrome/browser/services/gcm/push_messaging_service_impl.cc @@ -0,0 +1,46 @@ +// 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 "chrome/browser/services/gcm/push_messaging_service_impl.h" + +#include <vector> + +#include "base/bind.h" +#include "chrome/browser/services/gcm/gcm_profile_service.h" +#include "components/gcm_driver/gcm_driver.h" + +namespace gcm { + +PushMessagingServiceImpl::PushMessagingServiceImpl( + GCMProfileService* gcm_profile_service) + : gcm_profile_service_(gcm_profile_service), + weak_factory_(this) {} + +PushMessagingServiceImpl::~PushMessagingServiceImpl() {} + +void PushMessagingServiceImpl::Register( + const std::string& app_id, + const std::string& sender_id, + const content::PushMessagingService::RegisterCallback& callback) { + // The GCMDriver could be NULL if GCMProfileService has been shut down. + if (!gcm_profile_service_->driver()) + return; + std::vector<std::string> sender_ids(1, sender_id); + gcm_profile_service_->driver()->Register( + app_id, + sender_ids, + base::Bind(&PushMessagingServiceImpl::DidRegister, + weak_factory_.GetWeakPtr(), + callback)); +} + +void PushMessagingServiceImpl::DidRegister( + const content::PushMessagingService::RegisterCallback& callback, + const std::string& registration_id, + GCMClient::Result result) { + GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); + callback.Run(endpoint, registration_id, result == GCMClient::SUCCESS); +} + +} // namespace gcm diff --git a/chrome/browser/services/gcm/push_messaging_service_impl.h b/chrome/browser/services/gcm/push_messaging_service_impl.h new file mode 100644 index 0000000..64aa91f --- /dev/null +++ b/chrome/browser/services/gcm/push_messaging_service_impl.h @@ -0,0 +1,43 @@ +// 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 CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_SERVICE_IMPL_H_ +#define CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_SERVICE_IMPL_H_ + +#include "base/compiler_specific.h" +#include "base/memory/weak_ptr.h" +#include "components/gcm_driver/gcm_client.h" +#include "content/public/browser/push_messaging_service.h" + +namespace gcm { + +class GCMProfileService; + +class PushMessagingServiceImpl : public content::PushMessagingService { + public: + explicit PushMessagingServiceImpl(GCMProfileService* gcm_profile_service); + virtual ~PushMessagingServiceImpl(); + + // content::PushMessagingService implementation: + virtual void Register( + const std::string& app_id, + const std::string& sender_id, + const content::PushMessagingService::RegisterCallback& callback) OVERRIDE; + + private: + void DidRegister( + const content::PushMessagingService::RegisterCallback& callback, + const std::string& registration_id, + GCMClient::Result result); + + GCMProfileService* gcm_profile_service_; // It owns us. + + base::WeakPtrFactory<PushMessagingServiceImpl> weak_factory_; + + DISALLOW_COPY_AND_ASSIGN(PushMessagingServiceImpl); +}; + +} // namespace gcm + +#endif // CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_SERVICE_IMPL_H_ |