#ifndef CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_
#define CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_
#include <map>
#include <string>
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/services/gcm/default_gcm_app_handler.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/signin/core/browser/signin_manager_base.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "google_apis/gcm/gcm_client.h"
class Profile;
namespace base {
class Value;
}
namespace extensions {
class ExtensionGCMAppHandlerTest;
}
namespace user_prefs {
class PrefRegistrySyncable;
}
namespace gcm {
class GCMAppHandler;
class GCMClientFactory;
class GCMProfileServiceTestConsumer;
class GCMProfileService : public KeyedService,
public content::NotificationObserver,
public SigninManagerBase::Observer {
public:
typedef base::Callback<void(const std::string& registration_id,
GCMClient::Result result)> RegisterCallback;
typedef base::Callback<void(const std::string& message_id,
GCMClient::Result result)> SendCallback;
typedef base::Callback<void(GCMClient::Result result)> UnregisterCallback;
typedef base::Callback<void(const GCMClient::GCMStatistics& stats)>
RequestGCMStatisticsCallback;
enum GCMEnabledState {
ALWAYS_ENABLED,
ENABLED_FOR_APPS,
ALWAYS_DISABLED
};
static GCMEnabledState GetGCMEnabledState(Profile* profile);
static std::string GetGCMEnabledStateString(GCMEnabledState state);
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
explicit GCMProfileService(Profile* profile);
virtual ~GCMProfileService();
void Initialize(scoped_ptr<GCMClientFactory> gcm_client_factory);
void Start();
void Stop();
virtual void Shutdown() OVERRIDE;
virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler);
virtual void RemoveAppHandler(const std::string& app_id);
virtual void Register(const std::string& app_id,
const std::vector<std::string>& sender_ids,
RegisterCallback callback);
virtual void Unregister(const std::string& app_id,
UnregisterCallback callback);
virtual void Send(const std::string& app_id,
const std::string& receiver_id,
const GCMClient::OutgoingMessage& message,
SendCallback callback);
GCMClient* GetGCMClientForTesting() const;
std::string SignedInUserName() const;
bool IsGCMClientReady() const;
void RequestGCMStatistics(RequestGCMStatisticsCallback callback);
private:
friend class GCMProfileServiceTestConsumer;
friend class extensions::ExtensionGCMAppHandlerTest;
class DelayedTaskController;
class IOWorker;
typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap;
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
virtual void GoogleSigninSucceeded(const std::string& username,
const std::string& password) OVERRIDE;
virtual void GoogleSignedOut(const std::string& username) OVERRIDE;
void EnsureLoaded();
void RemoveCachedData();
void CheckOut();
void ResetGCMClient();
GCMClient::Result EnsureAppReady(const std::string& app_id);
bool IsAsyncOperationPending(const std::string& app_id) const;
void DoRegister(const std::string& app_id,
const std::vector<std::string>& sender_ids);
void DoUnregister(const std::string& app_id);
void DoSend(const std::string& app_id,
const std::string& receiver_id,
const GCMClient::OutgoingMessage& message);
void RegisterFinished(const std::string& app_id,
const std::string& registration_id,
GCMClient::Result result);
void UnregisterFinished(const std::string& app_id, GCMClient::Result result);
void SendFinished(const std::string& app_id,
const std::string& message_id,
GCMClient::Result result);
void MessageReceived(const std::string& app_id,
GCMClient::IncomingMessage message);
void MessagesDeleted(const std::string& app_id);
void MessageSendError(const std::string& app_id,
const GCMClient::SendErrorDetails& send_error_details);
void GCMClientReady();
GCMAppHandler* GetAppHandler(const std::string& app_id);
void RequestGCMStatisticsFinished(GCMClient::GCMStatistics stats);
Profile* profile_;
bool gcm_client_ready_;
std::string username_;
content::NotificationRegistrar registrar_;
scoped_ptr<DelayedTaskController> delayed_task_controller_;
scoped_refptr<IOWorker> io_worker_;
GCMAppHandlerMap app_handlers_;
DefaultGCMAppHandler default_app_handler_;
std::map<std::string, RegisterCallback> register_callbacks_;
std::map<std::string, UnregisterCallback> unregister_callbacks_;
std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_;
RequestGCMStatisticsCallback request_gcm_statistics_callback_;
base::WeakPtrFactory<GCMProfileService> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(GCMProfileService);
};
}
#endif