Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / gcm / gcm_api.cc
index 65b1d49..c8bac68 100644 (file)
@@ -8,6 +8,7 @@
 #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"
@@ -15,7 +16,8 @@
 #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 {
@@ -28,6 +30,7 @@ const char kGoogleRestrictedPrefix[] = "google";
 // 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.";
@@ -42,6 +45,8 @@ const char* GcmResultToError(gcm::GCMClient::Result result) {
       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:
@@ -64,7 +69,7 @@ const char* GcmResultToError(gcm::GCMClient::Result result) {
 }
 
 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,
@@ -79,7 +84,7 @@ bool IsMessageKeyValid(const std::string& key) {
 
 namespace extensions {
 
-bool GcmApiFunction::RunImpl() {
+bool GcmApiFunction::RunAsync() {
   if (!IsGcmApiEnabled())
     return false;
 
@@ -93,13 +98,12 @@ bool GcmApiFunction::IsGcmApiEnabled() const {
   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() {}
@@ -111,8 +115,8 @@ bool GcmRegisterFunction::DoWork() {
       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));
 
@@ -132,8 +136,10 @@ GcmUnregisterFunction::GcmUnregisterFunction() {}
 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;
@@ -162,8 +168,8 @@ bool GcmSendFunction::DoWork() {
   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));
@@ -197,19 +203,9 @@ bool GcmSendFunction::ValidateMessageData(
 }
 
 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(
@@ -224,8 +220,7 @@ 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) {
@@ -233,8 +228,7 @@ 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(
@@ -249,16 +243,7 @@ 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