Chromium Code Reviews| Index: chrome/browser/services/gcm/gcm_service.cc |
| diff --git a/chrome/browser/services/gcm/gcm_profile_service.cc b/chrome/browser/services/gcm/gcm_service.cc |
| similarity index 58% |
| copy from chrome/browser/services/gcm/gcm_profile_service.cc |
| copy to chrome/browser/services/gcm/gcm_service.cc |
| index cf7e1b54f6c96e38983435c57b4d7b54aa7278d6..cd8f764318b57ef862a216fb883295cb359e0f8a 100644 |
| --- a/chrome/browser/services/gcm/gcm_profile_service.cc |
| +++ b/chrome/browser/services/gcm/gcm_service.cc |
| @@ -1,36 +1,22 @@ |
| -// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// 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/gcm_profile_service.h" |
| +#include "chrome/browser/services/gcm/gcm_service.h" |
| #include <algorithm> |
| -#include <string> |
| -#include <vector> |
| +#include <utility> |
| -#include "base/base64.h" |
| -#include "base/files/file_path.h" |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/location.h" |
| #include "base/logging.h" |
| -#include "base/path_service.h" |
| -#include "base/prefs/pref_service.h" |
| -#include "base/strings/string_number_conversions.h" |
| +#include "base/sequenced_task_runner.h" |
| #include "base/threading/sequenced_worker_pool.h" |
| -#include "chrome/browser/chrome_notification_types.h" |
| -#include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/services/gcm/gcm_app_handler.h" |
| #include "chrome/browser/services/gcm/gcm_client_factory.h" |
| -#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| -#include "chrome/browser/signin/signin_manager_factory.h" |
| -#include "chrome/common/chrome_constants.h" |
| -#include "chrome/common/chrome_paths.h" |
| #include "chrome/common/chrome_version_info.h" |
| -#include "chrome/common/pref_names.h" |
| -#include "components/signin/core/browser/profile_oauth2_token_service.h" |
| -#include "components/signin/core/browser/signin_manager.h" |
| -#include "components/user_prefs/pref_registry_syncable.h" |
| #include "content/public/browser/browser_thread.h" |
| -#include "content/public/browser/notification_details.h" |
| -#include "content/public/browser/notification_source.h" |
| #include "google_apis/gcm/protocol/android_checkin.pb.h" |
| #include "net/url_request/url_request_context_getter.h" |
| @@ -82,13 +68,13 @@ checkin_proto::ChromeBuildProto_Channel GetChannel() { |
| } // namespace |
| // Helper class to save tasks to run until we're ready to execute them. |
| -class GCMProfileService::DelayedTaskController { |
| +class GCMService::DelayedTaskController { |
| public: |
| DelayedTaskController(); |
| ~DelayedTaskController(); |
| // Adds a task that will be invoked once we're ready. |
| - void AddTask(base::Closure task); |
| + void AddTask(const base::Closure& task); |
| // Sets ready status. It is ready only when check-in is completed and |
| // the GCMClient is fully initialized. |
| @@ -104,29 +90,30 @@ class GCMProfileService::DelayedTaskController { |
| bool ready_; |
| std::vector<base::Closure> delayed_tasks_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DelayedTaskController); |
| }; |
| -GCMProfileService::DelayedTaskController::DelayedTaskController() |
| - : ready_(false) { |
| +GCMService::DelayedTaskController::DelayedTaskController() : ready_(false) { |
| } |
| -GCMProfileService::DelayedTaskController::~DelayedTaskController() { |
| +GCMService::DelayedTaskController::~DelayedTaskController() { |
| } |
| -void GCMProfileService::DelayedTaskController::AddTask(base::Closure task) { |
| +void GCMService::DelayedTaskController::AddTask(const base::Closure& task) { |
| delayed_tasks_.push_back(task); |
| } |
| -void GCMProfileService::DelayedTaskController::SetReady() { |
| +void GCMService::DelayedTaskController::SetReady() { |
| ready_ = true; |
| RunTasks(); |
| } |
| -bool GCMProfileService::DelayedTaskController::CanRunTaskWithoutDelay() const { |
| +bool GCMService::DelayedTaskController::CanRunTaskWithoutDelay() const { |
| return ready_; |
| } |
| -void GCMProfileService::DelayedTaskController::RunTasks() { |
| +void GCMService::DelayedTaskController::RunTasks() { |
| DCHECK(ready_); |
| for (size_t i = 0; i < delayed_tasks_.size(); ++i) |
| @@ -134,12 +121,11 @@ void GCMProfileService::DelayedTaskController::RunTasks() { |
| delayed_tasks_.clear(); |
| } |
| -class GCMProfileService::IOWorker |
| - : public GCMClient::Delegate, |
| - public base::RefCountedThreadSafe<GCMProfileService::IOWorker>{ |
| +class GCMService::IOWorker : public GCMClient::Delegate { |
| public: |
| // Called on UI thread. |
| IOWorker(); |
| + virtual ~IOWorker(); |
| // Overridden from GCMClient::Delegate: |
| // Called on IO thread. |
| @@ -167,7 +153,7 @@ class GCMProfileService::IOWorker |
| const scoped_refptr<net::URLRequestContextGetter>& |
| url_request_context_getter); |
| void Reset(); |
| - void Load(const base::WeakPtr<GCMProfileService>& service); |
| + void Load(const base::WeakPtr<GCMService>& service); |
| void Stop(); |
| void CheckOut(); |
| void Register(const std::string& app_id, |
| @@ -182,22 +168,22 @@ class GCMProfileService::IOWorker |
| GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); } |
| private: |
| - friend class base::RefCountedThreadSafe<IOWorker>; |
| - virtual ~IOWorker(); |
| - |
| - base::WeakPtr<GCMProfileService> service_; |
| + base::WeakPtr<GCMService> service_; |
| scoped_ptr<GCMClient> gcm_client_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(IOWorker); |
| }; |
| -GCMProfileService::IOWorker::IOWorker() { |
| +GCMService::IOWorker::IOWorker() { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| } |
| -GCMProfileService::IOWorker::~IOWorker() { |
| +GCMService::IOWorker::~IOWorker() { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| } |
| -void GCMProfileService::IOWorker::Initialize( |
| +void GCMService::IOWorker::Initialize( |
| scoped_ptr<GCMClientFactory> gcm_client_factory, |
| const base::FilePath& store_path, |
| const std::vector<std::string>& account_ids, |
| @@ -227,7 +213,7 @@ void GCMProfileService::IOWorker::Initialize( |
| this); |
| } |
| -void GCMProfileService::IOWorker::Reset() { |
| +void GCMService::IOWorker::Reset() { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| // GCMClient instance must be destroyed from the same thread where it was |
| @@ -235,121 +221,111 @@ void GCMProfileService::IOWorker::Reset() { |
| gcm_client_.reset(); |
|
jianli
2014/04/18 01:49:44
I think we can now rely on the destructor to dispo
bartfab (slow)
2014/04/22 17:51:07
Done.
|
| } |
| -void GCMProfileService::IOWorker::OnRegisterFinished( |
| +void GCMService::IOWorker::OnRegisterFinished( |
| const std::string& app_id, |
| const std::string& registration_id, |
| GCMClient::Result result) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| - content::BrowserThread::PostTask( |
| - content::BrowserThread::UI, |
| - FROM_HERE, |
| - base::Bind(&GCMProfileService::RegisterFinished, |
| - service_, |
| - app_id, |
| - registration_id, |
| - result)); |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&GCMService::RegisterFinished, |
| + service_, |
| + app_id, |
| + registration_id, |
| + result)); |
| } |
| -void GCMProfileService::IOWorker::OnUnregisterFinished( |
| - const std::string& app_id, |
| - GCMClient::Result result) { |
| +void GCMService::IOWorker::OnUnregisterFinished(const std::string& app_id, |
| + GCMClient::Result result) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| content::BrowserThread::PostTask( |
| content::BrowserThread::UI, |
| FROM_HERE, |
| - base::Bind( |
| - &GCMProfileService::UnregisterFinished, service_, app_id, result)); |
| + base::Bind(&GCMService::UnregisterFinished, service_, app_id, result)); |
| } |
| -void GCMProfileService::IOWorker::OnSendFinished( |
| - const std::string& app_id, |
| - const std::string& message_id, |
| - GCMClient::Result result) { |
| +void GCMService::IOWorker::OnSendFinished(const std::string& app_id, |
| + const std::string& message_id, |
| + GCMClient::Result result) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| - content::BrowserThread::PostTask( |
| - content::BrowserThread::UI, |
| - FROM_HERE, |
| - base::Bind(&GCMProfileService::SendFinished, |
| - service_, |
| - app_id, |
| - message_id, |
| - result)); |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&GCMService::SendFinished, |
| + service_, |
| + app_id, |
| + message_id, |
| + result)); |
| } |
| -void GCMProfileService::IOWorker::OnMessageReceived( |
| +void GCMService::IOWorker::OnMessageReceived( |
| const std::string& app_id, |
| const GCMClient::IncomingMessage& message) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| - content::BrowserThread::PostTask( |
| - content::BrowserThread::UI, |
| - FROM_HERE, |
| - base::Bind(&GCMProfileService::MessageReceived, |
| - service_, |
| - app_id, |
| - message)); |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&GCMService::MessageReceived, |
| + service_, |
| + app_id, |
| + message)); |
| } |
| -void GCMProfileService::IOWorker::OnMessagesDeleted(const std::string& app_id) { |
| +void GCMService::IOWorker::OnMessagesDeleted(const std::string& app_id) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| - content::BrowserThread::PostTask( |
| - content::BrowserThread::UI, |
| - FROM_HERE, |
| - base::Bind(&GCMProfileService::MessagesDeleted, |
| - service_, |
| - app_id)); |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&GCMService::MessagesDeleted, |
| + service_, |
| + app_id)); |
| } |
| -void GCMProfileService::IOWorker::OnMessageSendError( |
| +void GCMService::IOWorker::OnMessageSendError( |
| const std::string& app_id, |
| const GCMClient::SendErrorDetails& send_error_details) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| - content::BrowserThread::PostTask( |
| - content::BrowserThread::UI, |
| - FROM_HERE, |
| - base::Bind(&GCMProfileService::MessageSendError, |
| - service_, |
| - app_id, |
| - send_error_details)); |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&GCMService::MessageSendError, |
| + service_, |
| + app_id, |
| + send_error_details)); |
| } |
| -void GCMProfileService::IOWorker::OnGCMReady() { |
| - content::BrowserThread::PostTask( |
| - content::BrowserThread::UI, |
| - FROM_HERE, |
| - base::Bind(&GCMProfileService::GCMClientReady, |
| - service_)); |
| +void GCMService::IOWorker::OnGCMReady() { |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&GCMService::GCMClientReady, |
| + service_)); |
| } |
| -void GCMProfileService::IOWorker::Load( |
| - const base::WeakPtr<GCMProfileService>& service) { |
| +void GCMService::IOWorker::Load(const base::WeakPtr<GCMService>& service) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| service_ = service; |
| gcm_client_->Load(); |
| } |
| -void GCMProfileService::IOWorker::Stop() { |
| +void GCMService::IOWorker::Stop() { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| gcm_client_->Stop(); |
| } |
| -void GCMProfileService::IOWorker::CheckOut() { |
| +void GCMService::IOWorker::CheckOut() { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| gcm_client_->CheckOut(); |
| - // Note that we still need to keep GCMClient instance alive since the profile |
| - // might be signed in again. |
| + // Note that we still need to keep GCMClient instance alive since the |
| + // GCMService may check in again. |
| } |
| -void GCMProfileService::IOWorker::Register( |
| +void GCMService::IOWorker::Register( |
| const std::string& app_id, |
| const std::vector<std::string>& sender_ids) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| @@ -357,22 +333,21 @@ void GCMProfileService::IOWorker::Register( |
| gcm_client_->Register(app_id, sender_ids); |
| } |
| -void GCMProfileService::IOWorker::Unregister(const std::string& app_id) { |
| +void GCMService::IOWorker::Unregister(const std::string& app_id) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| gcm_client_->Unregister(app_id); |
| } |
| -void GCMProfileService::IOWorker::Send( |
| - const std::string& app_id, |
| - const std::string& receiver_id, |
| - const GCMClient::OutgoingMessage& message) { |
| +void GCMService::IOWorker::Send(const std::string& app_id, |
| + const std::string& receiver_id, |
| + const GCMClient::OutgoingMessage& message) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| gcm_client_->Send(app_id, receiver_id, message); |
| } |
| -void GCMProfileService::IOWorker::RequestGCMStatistics() { |
| +void GCMService::IOWorker::RequestGCMStatistics() { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| gcm::GCMClient::GCMStatistics stats; |
| @@ -384,114 +359,58 @@ void GCMProfileService::IOWorker::RequestGCMStatistics() { |
| content::BrowserThread::PostTask( |
| content::BrowserThread::UI, |
| FROM_HERE, |
| - base::Bind(&GCMProfileService::RequestGCMStatisticsFinished, |
| - service_, |
| - stats)); |
| -} |
| - |
| -std::string GCMProfileService::GetGCMEnabledStateString(GCMEnabledState state) { |
| - switch (state) { |
| - case GCMProfileService::ALWAYS_ENABLED: |
| - return "ALWAYS_ENABLED"; |
| - case GCMProfileService::ENABLED_FOR_APPS: |
| - return "ENABLED_FOR_APPS"; |
| - case GCMProfileService::ALWAYS_DISABLED: |
| - return "ALWAYS_DISABLED"; |
| - default: |
| - NOTREACHED(); |
| - return std::string(); |
| - } |
| -} |
| - |
| -// static |
| -GCMProfileService::GCMEnabledState GCMProfileService::GetGCMEnabledState( |
| - Profile* profile) { |
| - const base::Value* gcm_enabled_value = |
| - profile->GetPrefs()->GetUserPrefValue(prefs::kGCMChannelEnabled); |
| - if (!gcm_enabled_value) |
| - return ENABLED_FOR_APPS; |
| - |
| - bool gcm_enabled = false; |
| - if (!gcm_enabled_value->GetAsBoolean(&gcm_enabled)) |
| - return ENABLED_FOR_APPS; |
| - |
| - return gcm_enabled ? ALWAYS_ENABLED : ALWAYS_DISABLED; |
| -} |
| - |
| -// static |
| -void GCMProfileService::RegisterProfilePrefs( |
| - user_prefs::PrefRegistrySyncable* registry) { |
| - // GCM support is only enabled by default for Canary/Dev/Custom builds. |
| - chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel(); |
| - bool on_by_default = false; |
| - if (channel == chrome::VersionInfo::CHANNEL_UNKNOWN || |
| - channel == chrome::VersionInfo::CHANNEL_CANARY || |
| - channel == chrome::VersionInfo::CHANNEL_DEV) { |
| - on_by_default = true; |
| - } |
| - registry->RegisterBooleanPref( |
| - prefs::kGCMChannelEnabled, |
| - on_by_default, |
| - user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| + base::Bind(&GCMService::RequestGCMStatisticsFinished, service_, stats)); |
| } |
| -GCMProfileService::GCMProfileService(Profile* profile) |
| - : profile_(profile), |
| +GCMService::GCMService(scoped_ptr<IdentityProvider> identity_provider) |
| + : identity_provider_(identity_provider.Pass()), |
| gcm_client_ready_(false), |
| weak_ptr_factory_(this) { |
| - DCHECK(!profile->IsOffTheRecord()); |
| } |
| -GCMProfileService::~GCMProfileService() { |
| +GCMService::~GCMService() { |
| } |
| -void GCMProfileService::Initialize( |
| - scoped_ptr<GCMClientFactory> gcm_client_factory) { |
| - registrar_.Add(this, |
| - chrome::NOTIFICATION_PROFILE_DESTROYED, |
| - content::Source<Profile>(profile_)); |
| - |
| - SigninManagerFactory::GetForProfile(profile_)->AddObserver(this); |
| - |
| +void GCMService::Initialize(scoped_ptr<GCMClientFactory> gcm_client_factory) { |
| // Get the list of available accounts. |
| std::vector<std::string> account_ids; |
| #if !defined(OS_ANDROID) |
| - account_ids = |
| - ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->GetAccounts(); |
| + account_ids = identity_provider_->GetAllAccountIds(); |
| #endif |
| // Create and initialize the GCMClient. Note that this does not initiate the |
| // GCM check-in. |
| - io_worker_ = new IOWorker(); |
| - scoped_refptr<net::URLRequestContextGetter> url_request_context_getter = |
| - profile_->GetRequestContext(); |
| + DCHECK(!io_worker_); |
| + io_worker_.reset(new IOWorker()); |
| content::BrowserThread::PostTask( |
| content::BrowserThread::IO, |
| FROM_HERE, |
| - base::Bind(&GCMProfileService::IOWorker::Initialize, |
| - io_worker_, |
| + base::Bind(&GCMService::IOWorker::Initialize, |
| + base::Unretained(io_worker_.get()), |
| base::Passed(&gcm_client_factory), |
| - profile_->GetPath().Append(chrome::kGCMStoreDirname), |
| + GetStorePath(), |
| account_ids, |
| - url_request_context_getter)); |
| + GetURLRequestContextGetter())); |
| // Load from the GCM store and initiate the GCM check-in if the rollout signal |
| // indicates yes. |
| - if (GetGCMEnabledState(profile_) == ALWAYS_ENABLED) |
| + if (StartAutomatically()) |
| EnsureLoaded(); |
| + |
| + identity_provider_->AddObserver(this); |
| } |
| -void GCMProfileService::Start() { |
| +void GCMService::Start() { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| EnsureLoaded(); |
| } |
| -void GCMProfileService::Stop() { |
| +void GCMService::Stop() { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| // No need to stop GCM service if not started yet. |
| - if (username_.empty()) |
| + if (account_id_.empty()) |
| return; |
| RemoveCachedData(); |
| @@ -499,21 +418,26 @@ void GCMProfileService::Stop() { |
| content::BrowserThread::PostTask( |
| content::BrowserThread::IO, |
| FROM_HERE, |
| - base::Bind(&GCMProfileService::IOWorker::Stop, io_worker_)); |
| + base::Bind(&GCMService::IOWorker::Stop, |
| + base::Unretained(io_worker_.get()))); |
| } |
| -void GCMProfileService::Shutdown() { |
| +void GCMService::Shutdown() { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + identity_provider_->RemoveObserver(this); |
| for (GCMAppHandlerMap::const_iterator iter = app_handlers_.begin(); |
| iter != app_handlers_.end(); ++iter) { |
| iter->second->ShutdownHandler(); |
| } |
| app_handlers_.clear(); |
| - |
| - SigninManagerFactory::GetForProfile(profile_)->RemoveObserver(this); |
| + content::BrowserThread::DeleteSoon(content::BrowserThread::IO, |
| + FROM_HERE, |
| + io_worker_.release()); |
| } |
| -void GCMProfileService::AddAppHandler(const std::string& app_id, |
| - GCMAppHandler* handler) { |
| +void GCMService::AddAppHandler(const std::string& app_id, |
| + GCMAppHandler* handler) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| DCHECK(!app_id.empty()); |
| DCHECK(handler); |
| DCHECK(app_handlers_.find(app_id) == app_handlers_.end()); |
| @@ -521,17 +445,20 @@ void GCMProfileService::AddAppHandler(const std::string& app_id, |
| app_handlers_[app_id] = handler; |
| } |
| -void GCMProfileService::RemoveAppHandler(const std::string& app_id) { |
| +void GCMService::RemoveAppHandler(const std::string& app_id) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| DCHECK(!app_id.empty()); |
| app_handlers_.erase(app_id); |
| } |
| -void GCMProfileService::Register(const std::string& app_id, |
| - const std::vector<std::string>& sender_ids, |
| - RegisterCallback callback) { |
| +void GCMService::Register(const std::string& app_id, |
| + const std::vector<std::string>& sender_ids, |
| + RegisterCallback callback) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| - DCHECK(!app_id.empty() && !sender_ids.empty() && !callback.is_null()); |
| + DCHECK(!app_id.empty()); |
| + DCHECK(!sender_ids.empty()); |
| + DCHECK(!callback.is_null()); |
| GCMClient::Result result = EnsureAppReady(app_id); |
| if (result != GCMClient::SUCCESS) { |
| @@ -549,19 +476,19 @@ void GCMProfileService::Register(const std::string& app_id, |
| // Delay the register operation until GCMClient is ready. |
| if (!delayed_task_controller_->CanRunTaskWithoutDelay()) { |
| - delayed_task_controller_->AddTask( |
| - base::Bind(&GCMProfileService::DoRegister, |
| - weak_ptr_factory_.GetWeakPtr(), |
| - app_id, |
| - sender_ids)); |
| + delayed_task_controller_->AddTask(base::Bind(&GCMService::DoRegister, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + app_id, |
| + sender_ids)); |
| return; |
| } |
| DoRegister(app_id, sender_ids); |
| } |
| -void GCMProfileService::DoRegister(const std::string& app_id, |
| - const std::vector<std::string>& sender_ids) { |
| +void GCMService::DoRegister(const std::string& app_id, |
| + const std::vector<std::string>& sender_ids) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| std::map<std::string, RegisterCallback>::iterator callback_iter = |
| register_callbacks_.find(app_id); |
| if (callback_iter == register_callbacks_.end()) { |
| @@ -576,16 +503,17 @@ void GCMProfileService::DoRegister(const std::string& app_id, |
| content::BrowserThread::PostTask( |
| content::BrowserThread::IO, |
| FROM_HERE, |
| - base::Bind(&GCMProfileService::IOWorker::Register, |
| - io_worker_, |
| + base::Bind(&GCMService::IOWorker::Register, |
| + base::Unretained(io_worker_.get()), |
| app_id, |
| normalized_sender_ids)); |
| } |
| -void GCMProfileService::Unregister(const std::string& app_id, |
| - UnregisterCallback callback) { |
| +void GCMService::Unregister(const std::string& app_id, |
| + UnregisterCallback callback) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| - DCHECK(!app_id.empty() && !callback.is_null()); |
| + DCHECK(!app_id.empty()); |
| + DCHECK(!callback.is_null()); |
| GCMClient::Result result = EnsureAppReady(app_id); |
| if (result != GCMClient::SUCCESS) { |
| @@ -603,17 +531,16 @@ void GCMProfileService::Unregister(const std::string& app_id, |
| // Delay the unregister operation until GCMClient is ready. |
| if (!delayed_task_controller_->CanRunTaskWithoutDelay()) { |
| - delayed_task_controller_->AddTask( |
| - base::Bind(&GCMProfileService::DoUnregister, |
| - weak_ptr_factory_.GetWeakPtr(), |
| - app_id)); |
| + delayed_task_controller_->AddTask(base::Bind(&GCMService::DoUnregister, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + app_id)); |
| return; |
| } |
| DoUnregister(app_id); |
| } |
| -void GCMProfileService::DoUnregister(const std::string& app_id) { |
| +void GCMService::DoUnregister(const std::string& app_id) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| // Ask the server to unregister it. There could be a small chance that the |
| @@ -622,17 +549,19 @@ void GCMProfileService::DoUnregister(const std::string& app_id) { |
| content::BrowserThread::PostTask( |
| content::BrowserThread::IO, |
| FROM_HERE, |
| - base::Bind(&GCMProfileService::IOWorker::Unregister, |
| - io_worker_, |
| + base::Bind(&GCMService::IOWorker::Unregister, |
| + base::Unretained(io_worker_.get()), |
| app_id)); |
| } |
| -void GCMProfileService::Send(const std::string& app_id, |
| - const std::string& receiver_id, |
| - const GCMClient::OutgoingMessage& message, |
| - SendCallback callback) { |
| +void GCMService::Send(const std::string& app_id, |
| + const std::string& receiver_id, |
| + const GCMClient::OutgoingMessage& message, |
| + SendCallback callback) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| - DCHECK(!app_id.empty() && !receiver_id.empty() && !callback.is_null()); |
| + DCHECK(!app_id.empty()); |
| + DCHECK(!receiver_id.empty()); |
| + DCHECK(!callback.is_null()); |
| GCMClient::Result result = EnsureAppReady(app_id); |
| if (result != GCMClient::SUCCESS) { |
| @@ -651,45 +580,47 @@ void GCMProfileService::Send(const std::string& app_id, |
| // Delay the send operation until all GCMClient is ready. |
| if (!delayed_task_controller_->CanRunTaskWithoutDelay()) { |
| - delayed_task_controller_->AddTask( |
| - base::Bind(&GCMProfileService::DoSend, |
| - weak_ptr_factory_.GetWeakPtr(), |
| - app_id, |
| - receiver_id, |
| - message)); |
| + delayed_task_controller_->AddTask(base::Bind(&GCMService::DoSend, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + app_id, |
| + receiver_id, |
| + message)); |
| return; |
| } |
| DoSend(app_id, receiver_id, message); |
| } |
| -void GCMProfileService::DoSend(const std::string& app_id, |
| - const std::string& receiver_id, |
| - const GCMClient::OutgoingMessage& message) { |
| +void GCMService::DoSend(const std::string& app_id, |
| + const std::string& receiver_id, |
| + const GCMClient::OutgoingMessage& message) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| content::BrowserThread::PostTask( |
| content::BrowserThread::IO, |
| FROM_HERE, |
| - base::Bind(&GCMProfileService::IOWorker::Send, |
| - io_worker_, |
| + base::Bind(&GCMService::IOWorker::Send, |
| + base::Unretained(io_worker_.get()), |
| app_id, |
| receiver_id, |
| message)); |
| } |
| -GCMClient* GCMProfileService::GetGCMClientForTesting() const { |
| +GCMClient* GCMService::GetGCMClientForTesting() const { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| return io_worker_ ? io_worker_->gcm_client_for_testing() : NULL; |
| } |
| -std::string GCMProfileService::SignedInUserName() const { |
| - return username_; |
| +bool GCMService::IsStarted() const { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + return !account_id_.empty(); |
| } |
| -bool GCMProfileService::IsGCMClientReady() const { |
| +bool GCMService::IsGCMClientReady() const { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| return gcm_client_ready_; |
| } |
| -void GCMProfileService::RequestGCMStatistics( |
| - RequestGCMStatisticsCallback callback) { |
| +void GCMService::RequestGCMStatistics(RequestGCMStatisticsCallback callback) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| DCHECK(!callback.is_null()); |
| @@ -697,48 +628,40 @@ void GCMProfileService::RequestGCMStatistics( |
| content::BrowserThread::PostTask( |
| content::BrowserThread::IO, |
| FROM_HERE, |
| - base::Bind(&GCMProfileService::IOWorker::RequestGCMStatistics, |
| - io_worker_)); |
| + base::Bind(&GCMService::IOWorker::RequestGCMStatistics, |
| + base::Unretained(io_worker_.get()))); |
| } |
| -void GCMProfileService::Observe(int type, |
| - const content::NotificationSource& source, |
| - const content::NotificationDetails& details) { |
| - DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| - |
| - switch (type) { |
| - case chrome::NOTIFICATION_PROFILE_DESTROYED: |
| - ResetGCMClient(); |
| - break; |
| - default: |
| - NOTREACHED(); |
| - } |
| -} |
| - |
| -void GCMProfileService::GoogleSigninSucceeded(const std::string& username, |
| - const std::string& password) { |
| - if (GetGCMEnabledState(profile_) == ALWAYS_ENABLED) |
| +void GCMService::OnLogin() { |
| + if (StartAutomatically()) |
| EnsureLoaded(); |
| } |
| -void GCMProfileService::GoogleSignedOut(const std::string& username) { |
| +void GCMService::OnLogout() { |
| CheckOut(); |
| } |
| -void GCMProfileService::EnsureLoaded() { |
| - SigninManagerBase* manager = SigninManagerFactory::GetForProfile(profile_); |
| - if (!manager) |
| - return; |
| - std::string username = manager->GetAuthenticatedUsername(); |
| - if (username.empty()) |
| +void GCMService::ResetGCMClient() { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(&GCMService::IOWorker::Reset, |
| + base::Unretained(io_worker_.get()))); |
| +} |
| + |
| +void GCMService::EnsureLoaded() { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + const std::string account_id = identity_provider_->GetAccountId(); |
| + if (account_id.empty()) |
| return; |
| // CheckIn could be called more than once when: |
| // 1) The password changes. |
| // 2) Register/send function calls it to ensure CheckIn is done. |
| - if (username_ == username) |
| + if (account_id_ == account_id) |
| return; |
| - username_ = username; |
| + account_id_ = account_id; |
| DCHECK(!delayed_task_controller_); |
| delayed_task_controller_.reset(new DelayedTaskController); |
| @@ -750,24 +673,25 @@ void GCMProfileService::EnsureLoaded() { |
| content::BrowserThread::PostTask( |
| content::BrowserThread::IO, |
| FROM_HERE, |
| - base::Bind(&GCMProfileService::IOWorker::Load, |
| - io_worker_, |
| + base::Bind(&GCMService::IOWorker::Load, |
| + base::Unretained(io_worker_.get()), |
| weak_ptr_factory_.GetWeakPtr())); |
| } |
| -void GCMProfileService::RemoveCachedData() { |
| +void GCMService::RemoveCachedData() { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| // Remove all the queued tasks since they no longer make sense after |
| // GCM service is stopped. |
| weak_ptr_factory_.InvalidateWeakPtrs(); |
| - username_.clear(); |
| + account_id_.clear(); |
| gcm_client_ready_ = false; |
| delayed_task_controller_.reset(); |
| register_callbacks_.clear(); |
| send_callbacks_.clear(); |
| } |
| -void GCMProfileService::CheckOut() { |
| +void GCMService::CheckOut() { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| // We still proceed with the check-out logic even if the check-in is not |
| @@ -779,36 +703,31 @@ void GCMProfileService::CheckOut() { |
| content::BrowserThread::PostTask( |
| content::BrowserThread::IO, |
| FROM_HERE, |
| - base::Bind(&GCMProfileService::IOWorker::CheckOut, io_worker_)); |
| + base::Bind(&GCMService::IOWorker::CheckOut, |
| + base::Unretained(io_worker_.get()))); |
| } |
| -void GCMProfileService::ResetGCMClient() { |
| - content::BrowserThread::PostTask( |
| - content::BrowserThread::IO, |
| - FROM_HERE, |
| - base::Bind(&GCMProfileService::IOWorker::Reset, io_worker_)); |
| -} |
| - |
| -GCMClient::Result GCMProfileService::EnsureAppReady(const std::string& app_id) { |
| +GCMClient::Result GCMService::EnsureAppReady(const std::string& app_id) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| // Ensure that check-in has been done. |
| EnsureLoaded(); |
| - // If the profile was not signed in, bail out. |
| - if (username_.empty()) |
| + // If the service was not started, bail out. |
| + if (account_id_.empty()) |
| return GCMClient::NOT_SIGNED_IN; |
| return GCMClient::SUCCESS; |
| } |
| -bool GCMProfileService::IsAsyncOperationPending( |
| - const std::string& app_id) const { |
| +bool GCMService::IsAsyncOperationPending(const std::string& app_id) const { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| return register_callbacks_.find(app_id) != register_callbacks_.end() || |
| unregister_callbacks_.find(app_id) != unregister_callbacks_.end(); |
| } |
| -void GCMProfileService::RegisterFinished(const std::string& app_id, |
| - const std::string& registration_id, |
| - GCMClient::Result result) { |
| +void GCMService::RegisterFinished(const std::string& app_id, |
| + const std::string& registration_id, |
| + GCMClient::Result result) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| std::map<std::string, RegisterCallback>::iterator callback_iter = |
| @@ -823,8 +742,8 @@ void GCMProfileService::RegisterFinished(const std::string& app_id, |
| callback.Run(registration_id, result); |
| } |
| -void GCMProfileService::UnregisterFinished(const std::string& app_id, |
| - GCMClient::Result result) { |
| +void GCMService::UnregisterFinished(const std::string& app_id, |
| + GCMClient::Result result) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| std::map<std::string, UnregisterCallback>::iterator callback_iter = |
| @@ -837,9 +756,9 @@ void GCMProfileService::UnregisterFinished(const std::string& app_id, |
| callback.Run(result); |
| } |
| -void GCMProfileService::SendFinished(const std::string& app_id, |
| - const std::string& message_id, |
| - GCMClient::Result result) { |
| +void GCMService::SendFinished(const std::string& app_id, |
| + const std::string& message_id, |
| + GCMClient::Result result) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| std::map<std::pair<std::string, std::string>, SendCallback>::iterator |
| @@ -855,40 +774,40 @@ void GCMProfileService::SendFinished(const std::string& app_id, |
| callback.Run(message_id, result); |
| } |
| -void GCMProfileService::MessageReceived(const std::string& app_id, |
| - GCMClient::IncomingMessage message) { |
| +void GCMService::MessageReceived(const std::string& app_id, |
| + GCMClient::IncomingMessage message) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| // Drop the event if signed out. |
| - if (username_.empty()) |
| + if (account_id_.empty()) |
| return; |
| GetAppHandler(app_id)->OnMessage(app_id, message); |
| } |
| -void GCMProfileService::MessagesDeleted(const std::string& app_id) { |
| +void GCMService::MessagesDeleted(const std::string& app_id) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| // Drop the event if signed out. |
| - if (username_.empty()) |
| + if (account_id_.empty()) |
| return; |
| GetAppHandler(app_id)->OnMessagesDeleted(app_id); |
| } |
| -void GCMProfileService::MessageSendError( |
| +void GCMService::MessageSendError( |
| const std::string& app_id, |
| const GCMClient::SendErrorDetails& send_error_details) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| // Drop the event if signed out. |
| - if (username_.empty()) |
| + if (account_id_.empty()) |
| return; |
| GetAppHandler(app_id)->OnSendError(app_id, send_error_details); |
| } |
| -void GCMProfileService::GCMClientReady() { |
| +void GCMService::GCMClientReady() { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| if (gcm_client_ready_) |
| @@ -898,13 +817,15 @@ void GCMProfileService::GCMClientReady() { |
| delayed_task_controller_->SetReady(); |
| } |
| -GCMAppHandler* GCMProfileService::GetAppHandler(const std::string& app_id) { |
| +GCMAppHandler* GCMService::GetAppHandler(const std::string& app_id) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + |
| std::map<std::string, GCMAppHandler*>::const_iterator iter = |
| app_handlers_.find(app_id); |
| return iter == app_handlers_.end() ? &default_app_handler_ : iter->second; |
| } |
| -void GCMProfileService::RequestGCMStatisticsFinished( |
| +void GCMService::RequestGCMStatisticsFinished( |
| GCMClient::GCMStatistics stats) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |