#include <map>
#include <vector>
+#include "base/metrics/histogram.h"
#include "base/strings/string_number_conversions.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"
#include "chrome/common/extensions/api/gcm.h"
-#include "extensions/browser/extension_system.h"
+#include "components/gcm_driver/gcm_driver.h"
+#include "extensions/browser/event_router.h"
#include "extensions/common/extension.h"
namespace {
// Error messages.
const char kInvalidParameter[] =
"Function was called with invalid parameters.";
+const char kGCMDisabled[] = "GCM is currently disabled.";
const char kNotSignedIn[] = "Profile was not signed in.";
const char kAsyncOperationPending[] =
"Asynchronous operation is pending.";
return "";
case gcm::GCMClient::INVALID_PARAMETER:
return kInvalidParameter;
+ case gcm::GCMClient::GCM_DISABLED:
+ return kGCMDisabled;
case gcm::GCMClient::NOT_SIGNED_IN:
return kNotSignedIn;
case gcm::GCMClient::ASYNC_OPERATION_PENDING:
}
bool IsMessageKeyValid(const std::string& key) {
- std::string lower = StringToLowerASCII(key);
+ std::string lower = base::StringToLowerASCII(key);
return !key.empty() &&
key.compare(0, arraysize(kCollapseKey) - 1, kCollapseKey) != 0 &&
lower.compare(0,
namespace extensions {
-bool GcmApiFunction::RunImpl() {
+bool GcmApiFunction::RunAsync() {
if (!IsGcmApiEnabled())
return false;
if (profile->IsOffTheRecord())
return false;
- return gcm::GCMProfileService::GetGCMEnabledState(profile) !=
- gcm::GCMProfileService::ALWAYS_DISABLED;
+ return gcm::GCMProfileService::IsGCMEnabled(profile);
}
-gcm::GCMProfileService* GcmApiFunction::GCMProfileService() const {
+gcm::GCMDriver* GcmApiFunction::GetGCMDriver() const {
return gcm::GCMProfileServiceFactory::GetForProfile(
- Profile::FromBrowserContext(browser_context()));
+ Profile::FromBrowserContext(browser_context()))->driver();
}
GcmRegisterFunction::GcmRegisterFunction() {}
api::gcm::Register::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
- GCMProfileService()->Register(
- GetExtension()->id(),
+ GetGCMDriver()->Register(
+ extension()->id(),
params->sender_ids,
base::Bind(&GcmRegisterFunction::CompleteFunctionWithResult, this));
GcmUnregisterFunction::~GcmUnregisterFunction() {}
bool GcmUnregisterFunction::DoWork() {
- GCMProfileService()->Unregister(
- GetExtension()->id(),
+ UMA_HISTOGRAM_BOOLEAN("GCM.APICallUnregister", true);
+
+ GetGCMDriver()->Unregister(
+ extension()->id(),
base::Bind(&GcmUnregisterFunction::CompleteFunctionWithResult, this));
return true;
if (params->message.time_to_live.get())
outgoing_message.time_to_live = *params->message.time_to_live;
- GCMProfileService()->Send(
- GetExtension()->id(),
+ GetGCMDriver()->Send(
+ extension()->id(),
params->message.destination_id,
outgoing_message,
base::Bind(&GcmSendFunction::CompleteFunctionWithResult, this));
}
GcmJsEventRouter::GcmJsEventRouter(Profile* profile) : profile_(profile) {
- if (ExtensionSystem::Get(profile_)->event_router()) {
- ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
- this, api::gcm::OnMessage::kEventName);
- ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
- this, api::gcm::OnMessagesDeleted::kEventName);
- ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
- this, api::gcm::OnSendError::kEventName);
- }
}
GcmJsEventRouter::~GcmJsEventRouter() {
- if (ExtensionSystem::Get(profile_)->event_router())
- ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
}
void GcmJsEventRouter::OnMessage(
api::gcm::OnMessage::kEventName,
api::gcm::OnMessage::Create(message_arg).Pass(),
profile_));
- ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension(
- app_id, event.Pass());
+ EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass());
}
void GcmJsEventRouter::OnMessagesDeleted(const std::string& app_id) {
api::gcm::OnMessagesDeleted::kEventName,
api::gcm::OnMessagesDeleted::Create().Pass(),
profile_));
- ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension(
- app_id, event.Pass());
+ EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass());
}
void GcmJsEventRouter::OnSendError(
api::gcm::OnSendError::kEventName,
api::gcm::OnSendError::Create(error).Pass(),
profile_));
- ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension(
- app_id, event.Pass());
-}
-
-void GcmJsEventRouter::OnListenerAdded(const EventListenerInfo& details) {
- if (gcm::GCMProfileService::GetGCMEnabledState(profile_) ==
- gcm::GCMProfileService::ALWAYS_DISABLED) {
- return;
- }
- gcm::GCMProfileServiceFactory::GetForProfile(profile_)->Start();
+ EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass());
}
} // namespace extensions