Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5427)

Unified Diff: chrome/browser/extensions/api/copresence/copresence_api.cc

Issue 444513005: Add the Copresence API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/copresence/copresence_api.cc
diff --git a/chrome/browser/extensions/api/copresence/copresence_api.cc b/chrome/browser/extensions/api/copresence/copresence_api.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9fc3585a88def71d6ea1d36441b7612a94dbe928
--- /dev/null
+++ b/chrome/browser/extensions/api/copresence/copresence_api.cc
@@ -0,0 +1,181 @@
+// 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/extensions/api/copresence/copresence_api.h"
+
+#include "base/lazy_instance.h"
+#include "base/prefs/pref_registry_simple.h"
+#include "base/prefs/pref_service.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/copresence/chrome_whispernet_client.h"
+#include "chrome/browser/extensions/api/copresence/copresence_util.h"
+#include "chrome/browser/profiles/profile.h"
xiyuan 2014/08/05 21:02:35 nit: seems not used
rkc 2014/08/05 22:25:21 Done.
+#include "chrome/common/chrome_version_info.h"
+#include "chrome/common/extensions/api/copresence.h"
+#include "chrome/common/pref_names.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_client.h"
+#include "components/copresence/public/whispernet_client.h"
+#include "content/public/browser/browser_context.h"
+#include "extensions/browser/event_router.h"
+
+namespace extensions {
+
+namespace {
+
+base::LazyInstance<BrowserContextKeyedAPIFactory<CopresenceService> >
+ g_factory = LAZY_INSTANCE_INITIALIZER;
+
+} // namespace
+
+// CopresenceService implementation:
+
+CopresenceService::CopresenceService(content::BrowserContext* context)
+ : browser_context_(context) {
+}
+
+CopresenceService::~CopresenceService() {
+}
+
+copresence::CopresenceClient* CopresenceService::client() {
+ if (!client_)
+ client_.reset(new copresence::CopresenceClient(this));
+ return client_.get();
+}
+
+copresence::WhispernetClient* CopresenceService::whispernet_client() {
+ if (!whispernet_client_)
+ whispernet_client_.reset(new ChromeWhispernetClient(browser_context_));
+ return whispernet_client_.get();
+}
+
+void CopresenceService::Shutdown() {
+ if (client_.get())
+ client_->Shutdown();
+}
+
+// static
+void CopresenceService::RegisterPrefs(PrefRegistrySimple* registry) {
+ registry->RegisterStringPref(prefs::kCopresenceDeviceId, std::string());
+}
+
+// static
+BrowserContextKeyedAPIFactory<CopresenceService>*
+CopresenceService::GetFactoryInstance() {
+ return g_factory.Pointer();
+}
+
+void CopresenceService::HandleMessages(
+ const std::string& /* app_id */,
+ const std::string& subscription_id,
+ const std::vector<copresence::Message>& messages) {
+ std::string app_id = GetCopresenceService(browser_context_)
xiyuan 2014/08/05 21:02:35 What is difference between this |app_id| from subs
rkc 2014/08/05 22:25:22 Eventually the server API will return the app_id t
+ ->SubscriptionToAppsRegistry()[subscription_id];
+
+ if (app_id.empty()) {
+ LOG(ERROR) << "Skipping message from unrecognized subscription "
+ << subscription_id;
+ return;
+ }
+
+ int message_count = messages.size();
+ std::vector<linked_ptr<api::copresence::Message> > api_messages(
xiyuan 2014/08/05 21:02:35 nit: #include "base/memory/linked_ptr.h"
rkc 2014/08/05 22:25:22 Done.
+ message_count);
+
+ for (int m = 0; m < message_count; ++m) {
+ api_messages[m].reset(new api::copresence::Message);
+ api_messages[m]->type = messages[m].type().type();
+ api_messages[m]->payload = messages[m].payload();
+ DVLOG(2) << "Dispatching message of type " << api_messages[m]->type << ":\n"
+ << api_messages[m]->payload;
+ }
+
+ // Send the messages to the client app.
+ scoped_ptr<Event> event(
+ new Event(api::copresence::OnMessagesReceived::kEventName,
+ api::copresence::OnMessagesReceived::Create(subscription_id,
+ api_messages)));
+ event->restrict_to_browser_context = browser_context_;
+ EventRouter::Get(browser_context_)
+ ->DispatchEventToExtension(app_id, event.Pass());
+ DVLOG(2) << "Passed " << api_messages.size() << " messages to app \""
+ << app_id << "\" for subscription \"" << subscription_id << "\"";
+}
+
+net::URLRequestContextGetter* CopresenceService::GetRequestContext() const {
+ return browser_context_->GetRequestContext();
+}
+
+const std::string CopresenceService::GetPlatformVersionString() const {
+ return chrome::VersionInfo().CreateVersionString();
+}
+
+const std::string CopresenceService::GetDeviceId() const {
not at google - send to devlin 2014/08/05 17:09:55 I don't see Get/SaveDeviceId used anywhere, which
rkc 2014/08/05 18:06:24 Get/SaveDeviceId are called from https://coderevie
not at google - send to devlin 2014/08/05 18:17:21 I see. That is never exposed to the extension, ri
rkc 2014/08/05 22:25:21 Spoke with jyasskin@ Removing all of this code.
+ PrefService* prefs = g_browser_process->local_state();
+ return prefs->GetString(prefs::kCopresenceDeviceId);
+}
+
+void CopresenceService::SaveDeviceId(const std::string& device_id) {
+ PrefService* prefs = g_browser_process->local_state();
+ prefs->SetString(prefs::kCopresenceDeviceId, device_id);
+}
+
+copresence::WhispernetClient* CopresenceService::GetWhispernetClient() {
+ return whispernet_client_.get();
+}
+
+template <>
+void
+BrowserContextKeyedAPIFactory<CopresenceService>::DeclareFactoryDependencies() {
+ DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
+}
+
+// CopresenceBatchExecuteFunction implementation:
+ExtensionFunction::ResponseAction CopresenceBatchExecuteFunction::Run() {
+ scoped_ptr<api::copresence::BatchExecute::Params> params(
+ api::copresence::BatchExecute::Params::Create(*args_));
+ EXTENSION_FUNCTION_VALIDATE(params.get());
+
+ SubscriptionToAppMap& apps_by_subscription_id =
+ GetCopresenceService(browser_context())->SubscriptionToAppsRegistry();
+
+ // Each batch execute will correspond to one ReportRequest protocol buffer.
+ copresence::ReportRequest request;
+ if (!PrepareReportRequestProto(params->operations,
+ extension_id(),
+ &apps_by_subscription_id,
+ &request))
+ return RespondNow(BadMessage());
not at google - send to devlin 2014/08/05 21:05:38 nit: multi-line condition should have {} around th
rkc 2014/08/05 22:25:22 Done.
+
+ GetCopresenceClient(browser_context())->ExecuteReportRequest(
+ request,
+ extension_id(),
+ base::Bind(&CopresenceBatchExecuteFunction::SendResult, this));
+ return RespondLater();
+}
+
+void CopresenceBatchExecuteFunction::SendResult(
+ copresence::CopresenceStatus status) {
+ api::copresence::BatchExecuteStatus api_status =
+ (status == copresence::SUCCESS)
+ ? api::copresence::BATCH_EXECUTE_STATUS_SUCCESS
+ : api::copresence::BATCH_EXECUTE_STATUS_FAILED;
+ Respond(
+ ArgumentList(api::copresence::BatchExecute::Results::Create(api_status)));
+}
+
+// CopresenceSetApiKeyFunction implementation:
+ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() {
+ scoped_ptr<api::copresence::SetApiKey::Params> params(
+ api::copresence::SetApiKey::Params::Create(*args_));
+ EXTENSION_FUNCTION_VALIDATE(params.get());
+
+ // TODO(rkc): Use the API key set by this function for this app.
+ // http://crbug.com/400617.
+ return RespondNow(NoArguments());
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698