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

Unified Diff: components/proximity_auth/cryptauth/cryptauth_gcm_manager_impl.cc

Issue 1228763002: Introduce CryptAuthGCMManager, which handles GCM interactions for CryptAuth. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 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: components/proximity_auth/cryptauth/cryptauth_gcm_manager_impl.cc
diff --git a/components/proximity_auth/cryptauth/cryptauth_gcm_manager_impl.cc b/components/proximity_auth/cryptauth/cryptauth_gcm_manager_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..548e9d6669bc222d224747cac10bc412f0471a20
--- /dev/null
+++ b/components/proximity_auth/cryptauth/cryptauth_gcm_manager_impl.cc
@@ -0,0 +1,147 @@
+// Copyright 2015 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 "components/proximity_auth/cryptauth/cryptauth_gcm_manager_impl.h"
+
+#include "base/bind.h"
+#include "base/prefs/pref_service.h"
+#include "base/strings/string_util.h"
+#include "components/gcm_driver/gcm_driver.h"
+#include "components/proximity_auth/cryptauth/pref_names.h"
+#include "components/proximity_auth/logging/logging.h"
+
+namespace proximity_auth {
+
+namespace {
+
+// The GCM app id identifies the client.
+const char kCryptAuthGCMAppId[] = "com.google.chrome.cryptauth";
+
+// The GCM sender id identifies the CryptAuth server.
+const char kCryptAuthGCMSenderId[] = "381449029288";
+
+// The 'registrationTickleType' key-value pair is present in GCM push
+// messages. The values correspond to a server-side enum.
+const char kRegistrationTickleTypeKey[] = "registrationTickleType";
+const char kRegistrationTickleTypeForceEnrollment[] = "1";
+const char kRegistrationTickleTypeUpdateEnrollment[] = "2";
+const char kRegistrationTickleTypeDevicesSync[] = "3";
+
+} // namespace
+
+CryptAuthGCMManagerImpl::CryptAuthGCMManagerImpl(gcm::GCMDriver* gcm_driver,
+ PrefService* pref_service)
+ : gcm_driver_(gcm_driver),
+ pref_service_(pref_service),
+ registration_in_progress_(false),
+ weak_ptr_factory_(this) {
+}
+
+CryptAuthGCMManagerImpl::~CryptAuthGCMManagerImpl() {
+ if (gcm_driver_->GetAppHandler(kCryptAuthGCMAppId) == this)
+ gcm_driver_->RemoveAppHandler(kCryptAuthGCMAppId);
+}
+
+void CryptAuthGCMManagerImpl::StartListening() {
+ if (gcm_driver_->GetAppHandler(kCryptAuthGCMAppId) == this) {
+ PA_LOG(INFO) << "GCM app handler already added";
+ return;
+ }
+
+ gcm_driver_->AddAppHandler(kCryptAuthGCMAppId, this);
+}
+
+void CryptAuthGCMManagerImpl::RegisterWithGCM() {
+ if (registration_in_progress_) {
+ PA_LOG(INFO) << "GCM Registration is already in progress";
+ return;
+ }
+
+ PA_LOG(INFO) << "Beginning GCM registration...";
+ registration_in_progress_ = true;
+
+ std::vector<std::string> sender_ids(1, kCryptAuthGCMSenderId);
+ gcm_driver_->Register(
+ kCryptAuthGCMAppId, sender_ids,
+ base::Bind(&CryptAuthGCMManagerImpl::OnRegistrationCompleted,
+ weak_ptr_factory_.GetWeakPtr()));
sacomoto 2015/07/10 10:54:14 See comment in cryptauth_gcm_manager_impl_unittest
Tim Song 2015/07/10 20:52:04 Acknowledged.
+}
+
+std::string CryptAuthGCMManagerImpl::GetRegistrationId() {
+ return pref_service_->GetString(prefs::kCryptAuthGCMRegistrationId);
+}
+
+void CryptAuthGCMManagerImpl::AddObserver(Observer* observer) {
+ observers_.AddObserver(observer);
+}
+
+void CryptAuthGCMManagerImpl::RemoveObserver(Observer* observer) {
+ observers_.RemoveObserver(observer);
+}
+
+void CryptAuthGCMManagerImpl::ShutdownHandler() {
+}
+
+void CryptAuthGCMManagerImpl::OnMessage(
+ const std::string& app_id,
+ const gcm::GCMClient::IncomingMessage& message) {
+ std::vector<std::string> fields;
+ for (const auto& kv : message.data) {
+ fields.push_back(std::string(kv.first) + ": " + std::string(kv.second));
+ }
+
+ PA_LOG(INFO) << "GCM message received:\n"
+ << " sender_id: " << message.sender_id << "\n"
+ << " collapse_key: " << message.collapse_key << "\n"
+ << " data:\n " << JoinString(fields, "\n ");
+
+ if (message.data.find(kRegistrationTickleTypeKey) == message.data.end()) {
+ PA_LOG(WARNING) << "GCM message does not contain 'registrationTickleType'.";
+ } else {
+ std::string tickle_type = message.data.at(kRegistrationTickleTypeKey);
+ if (tickle_type == kRegistrationTickleTypeForceEnrollment ||
+ tickle_type == kRegistrationTickleTypeUpdateEnrollment) {
+ // These tickle types correspond to re-enrollment messages.
+ FOR_EACH_OBSERVER(Observer, observers_, OnReenrollMessage());
+ } else if (tickle_type == kRegistrationTickleTypeDevicesSync) {
+ FOR_EACH_OBSERVER(Observer, observers_, OnResyncMessage());
+ } else {
+ PA_LOG(WARNING) << "Unknown tickle type in GCM message.";
+ }
+ }
+}
+
+void CryptAuthGCMManagerImpl::OnMessagesDeleted(const std::string& app_id) {
+}
+
+void CryptAuthGCMManagerImpl::OnSendError(
+ const std::string& app_id,
+ const gcm::GCMClient::SendErrorDetails& details) {
+ NOTREACHED();
+}
+
+void CryptAuthGCMManagerImpl::OnSendAcknowledged(
+ const std::string& app_id,
+ const std::string& message_id) {
+ NOTREACHED();
+}
+
+void CryptAuthGCMManagerImpl::OnRegistrationCompleted(
+ const std::string& registration_id,
+ gcm::GCMClient::Result result) {
+ registration_in_progress_ = false;
+ if (result != gcm::GCMClient::SUCCESS) {
+ PA_LOG(WARNING) << "GCM registration failed with result="
+ << static_cast<int>(result);
+ FOR_EACH_OBSERVER(Observer, observers_, OnGCMRegistrationResult(false));
+ return;
+ }
+
+ PA_LOG(INFO) << "GCM registration success, registration_id="
+ << registration_id;
+ pref_service_->SetString(prefs::kCryptAuthGCMRegistrationId, registration_id);
+ FOR_EACH_OBSERVER(Observer, observers_, OnGCMRegistrationResult(true));
+}
+
+} // namespace proximity_auth

Powered by Google App Engine
This is Rietveld 408576698