Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / copresence / copresence_api.cc
index 61749b8..f4ae23e 100644 (file)
@@ -7,12 +7,14 @@
 #include "base/lazy_instance.h"
 #include "base/memory/linked_ptr.h"
 #include "chrome/browser/copresence/chrome_whispernet_client.h"
+#include "chrome/browser/services/gcm/gcm_profile_service.h"
+#include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
 #include "chrome/common/chrome_version_info.h"
 #include "chrome/common/extensions/api/copresence.h"
+#include "components/copresence/copresence_manager_impl.h"
 #include "components/copresence/proto/data.pb.h"
 #include "components/copresence/proto/enums.pb.h"
 #include "components/copresence/proto/rpcs.pb.h"
-#include "components/copresence/public/copresence_manager.h"
 #include "components/copresence/public/whispernet_client.h"
 #include "content/public/browser/browser_context.h"
 #include "extensions/browser/event_router.h"
@@ -30,16 +32,23 @@ const char kShuttingDownMessage[] = "Shutting down.";
 
 }  // namespace
 
-// CopresenceService implementation:
+
+// CopresenceService implementation.
 
 CopresenceService::CopresenceService(content::BrowserContext* context)
     : is_shutting_down_(false), browser_context_(context) {}
 
 CopresenceService::~CopresenceService() {}
 
+void CopresenceService::Shutdown() {
+  is_shutting_down_ = true;
+  manager_.reset();
+  whispernet_client_.reset();
+}
+
 copresence::CopresenceManager* CopresenceService::manager() {
   if (!manager_ && !is_shutting_down_)
-    manager_ = copresence::CopresenceManager::Create(this);
+    manager_.reset(new copresence::CopresenceManagerImpl(this));
   return manager_.get();
 }
 
@@ -49,10 +58,14 @@ copresence::WhispernetClient* CopresenceService::whispernet_client() {
   return whispernet_client_.get();
 }
 
-void CopresenceService::Shutdown() {
-  is_shutting_down_ = true;
-  manager_.reset();
-  whispernet_client_.reset();
+void CopresenceService::set_api_key(const std::string& app_id,
+                                    const std::string& api_key) {
+  DCHECK(!app_id.empty());
+  api_keys_by_app_[app_id] = api_key;
+}
+
+void CopresenceService::set_auth_token(const std::string& token) {
+  auth_token_ = token;
 }
 
 void CopresenceService::set_manager_for_testing(
@@ -104,6 +117,17 @@ void CopresenceService::HandleMessages(
            << app_id << "\" for subscription \"" << subscription_id << "\"";
 }
 
+void CopresenceService::HandleStatusUpdate(
+    copresence::CopresenceStatus status) {
+  scoped_ptr<Event> event(
+      new Event(api::copresence::OnStatusUpdated::kEventName,
+                api::copresence::OnStatusUpdated::Create(
+                    api::copresence::STATUS_AUDIOFAILED),
+                browser_context_));
+  EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass());
+  DVLOG(2) << "Sent Audio Failed status update.";
+}
+
 net::URLRequestContextGetter* CopresenceService::GetRequestContext() const {
   return browser_context_->GetRequestContext();
 }
@@ -112,21 +136,29 @@ const std::string CopresenceService::GetPlatformVersionString() const {
   return chrome::VersionInfo().CreateVersionString();
 }
 
-const std::string CopresenceService::GetAPIKey() const {
-  return api_key_;
+const std::string CopresenceService::GetAPIKey(const std::string& app_id)
+    const {
+  // This won't be const if we use map[]
+  const auto& key = api_keys_by_app_.find(app_id);
+  return key == api_keys_by_app_.end() ? std::string() : key->second;
 }
 
 copresence::WhispernetClient* CopresenceService::GetWhispernetClient() {
   return whispernet_client();
 }
 
+gcm::GCMDriver* CopresenceService::GetGCMDriver() {
+  return gcm::GCMProfileServiceFactory::GetForProfile(browser_context_)
+      ->driver();
+}
+
 template <>
 void
 BrowserContextKeyedAPIFactory<CopresenceService>::DeclareFactoryDependencies() {
   DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
 }
 
-// CopresenceExecuteFunction implementation:
+// CopresenceExecuteFunction implementation.
 ExtensionFunction::ResponseAction CopresenceExecuteFunction::Run() {
   scoped_ptr<api::copresence::Execute::Params> params(
       api::copresence::Execute::Params::Create(*args_));
@@ -152,6 +184,7 @@ ExtensionFunction::ResponseAction CopresenceExecuteFunction::Run() {
   service->manager()->ExecuteReportRequest(
       request,
       extension_id(),
+      service->auth_token(),
       base::Bind(&CopresenceExecuteFunction::SendResult, this));
   return RespondLater();
 }
@@ -164,7 +197,7 @@ void CopresenceExecuteFunction::SendResult(
   Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status)));
 }
 
-// CopresenceSetApiKeyFunction implementation:
+// CopresenceSetApiKeyFunction implementation.
 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() {
   scoped_ptr<api::copresence::SetApiKey::Params> params(
       api::copresence::SetApiKey::Params::Create(*args_));
@@ -172,7 +205,20 @@ ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() {
 
   // The api key may be set to empty, to clear it.
   CopresenceService::GetFactoryInstance()->Get(browser_context())
-      ->set_api_key(params->api_key);
+      ->set_api_key(extension_id(), params->api_key);
+  return RespondNow(NoArguments());
+}
+
+// CopresenceSetAuthTokenFunction implementation
+ExtensionFunction::ResponseAction CopresenceSetAuthTokenFunction::Run() {
+  scoped_ptr<api::copresence::SetAuthToken::Params> params(
+      api::copresence::SetAuthToken::Params::Create(*args_));
+  EXTENSION_FUNCTION_VALIDATE(params.get());
+
+  // The token may be set to empty, to clear it.
+  // TODO(ckehoe): Scope the auth token appropriately (crbug/423517).
+  CopresenceService::GetFactoryInstance()->Get(browser_context())
+      ->set_auth_token(params->token);
   return RespondNow(NoArguments());
 }