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

Side by Side 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: fixes 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/proximity_auth/cryptauth/cryptauth_gcm_manager_impl.h"
6
7 #include "base/bind.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/string_util.h"
10 #include "components/gcm_driver/gcm_driver.h"
11 #include "components/proximity_auth/cryptauth/pref_names.h"
12 #include "components/proximity_auth/logging/logging.h"
13
14 namespace proximity_auth {
15
16 namespace {
17
18 // The GCM app id identifies the client.
19 const char kCryptAuthGCMAppId[] = "com.google.chrome.cryptauth";
20
21 // The GCM sender id identifies the CryptAuth server.
22 const char kCryptAuthGCMSenderId[] = "381449029288";
23
24 // The 'registrationTickleType' key-value pair is present in GCM push
25 // messages. The values correspond to a server-side enum.
26 const char kRegistrationTickleTypeKey[] = "registrationTickleType";
27 const char kRegistrationTickleTypeForceEnrollment[] = "1";
28 const char kRegistrationTickleTypeUpdateEnrollment[] = "2";
29 const char kRegistrationTickleTypeDevicesSync[] = "3";
30
31 } // namespace
32
33 CryptAuthGCMManagerImpl::CryptAuthGCMManagerImpl(gcm::GCMDriver* gcm_driver,
34 PrefService* pref_service)
35 : gcm_driver_(gcm_driver),
36 pref_service_(pref_service),
37 registration_in_progress_(false),
38 weak_ptr_factory_(this) {
39 }
40
41 CryptAuthGCMManagerImpl::~CryptAuthGCMManagerImpl() {
42 if (gcm_driver_->GetAppHandler(kCryptAuthGCMAppId) == this)
43 gcm_driver_->RemoveAppHandler(kCryptAuthGCMAppId);
44 }
45
46 void CryptAuthGCMManagerImpl::StartListening() {
47 if (gcm_driver_->GetAppHandler(kCryptAuthGCMAppId) == this) {
48 PA_LOG(INFO) << "GCM app handler already added";
49 return;
50 }
51
52 gcm_driver_->AddAppHandler(kCryptAuthGCMAppId, this);
53 }
54
55 void CryptAuthGCMManagerImpl::RegisterWithGCM() {
56 if (registration_in_progress_) {
57 PA_LOG(INFO) << "GCM Registration is already in progress";
58 return;
59 }
60
61 PA_LOG(INFO) << "Beginning GCM registration...";
62 registration_in_progress_ = true;
63
64 std::vector<std::string> sender_ids(1, kCryptAuthGCMSenderId);
65 gcm_driver_->Register(
66 kCryptAuthGCMAppId, sender_ids,
67 base::Bind(&CryptAuthGCMManagerImpl::OnRegistrationCompleted,
68 weak_ptr_factory_.GetWeakPtr()));
69 }
70
71 std::string CryptAuthGCMManagerImpl::GetRegistrationId() {
72 return pref_service_->GetString(prefs::kCryptAuthGCMRegistrationId);
73 }
74
75 void CryptAuthGCMManagerImpl::AddObserver(Observer* observer) {
76 observers_.AddObserver(observer);
77 }
78
79 void CryptAuthGCMManagerImpl::RemoveObserver(Observer* observer) {
80 observers_.RemoveObserver(observer);
81 }
82
83 void CryptAuthGCMManagerImpl::ShutdownHandler() {
84 }
85
86 void CryptAuthGCMManagerImpl::OnMessage(
87 const std::string& app_id,
88 const gcm::GCMClient::IncomingMessage& message) {
89 std::vector<std::string> fields;
90 for (const auto& kv : message.data) {
91 fields.push_back(std::string(kv.first) + ": " + std::string(kv.second));
92 }
93
94 PA_LOG(INFO) << "GCM message received:\n"
95 << " sender_id: " << message.sender_id << "\n"
96 << " collapse_key: " << message.collapse_key << "\n"
97 << " data:\n " << JoinString(fields, "\n ");
98
99 if (message.data.find(kRegistrationTickleTypeKey) == message.data.end()) {
100 PA_LOG(WARNING) << "GCM message does not contain 'registrationTickleType'.";
101 } else {
102 std::string tickle_type = message.data.at(kRegistrationTickleTypeKey);
103 if (tickle_type == kRegistrationTickleTypeForceEnrollment ||
104 tickle_type == kRegistrationTickleTypeUpdateEnrollment) {
105 // These tickle types correspond to re-enrollment messages.
106 FOR_EACH_OBSERVER(Observer, observers_, OnReenrollMessage());
107 } else if (tickle_type == kRegistrationTickleTypeDevicesSync) {
108 FOR_EACH_OBSERVER(Observer, observers_, OnResyncMessage());
109 } else {
110 PA_LOG(WARNING) << "Unknown tickle type in GCM message.";
111 }
112 }
113 }
114
115 void CryptAuthGCMManagerImpl::OnMessagesDeleted(const std::string& app_id) {
116 }
117
118 void CryptAuthGCMManagerImpl::OnSendError(
119 const std::string& app_id,
120 const gcm::GCMClient::SendErrorDetails& details) {
121 NOTREACHED();
122 }
123
124 void CryptAuthGCMManagerImpl::OnSendAcknowledged(
125 const std::string& app_id,
126 const std::string& message_id) {
127 NOTREACHED();
128 }
129
130 void CryptAuthGCMManagerImpl::OnRegistrationCompleted(
131 const std::string& registration_id,
132 gcm::GCMClient::Result result) {
133 registration_in_progress_ = false;
134 if (result != gcm::GCMClient::SUCCESS) {
135 PA_LOG(WARNING) << "GCM registration failed with result="
136 << static_cast<int>(result);
137 FOR_EACH_OBSERVER(Observer, observers_, OnGCMRegistrationResult(false));
138 return;
139 }
140
141 PA_LOG(INFO) << "GCM registration success, registration_id="
142 << registration_id;
143 pref_service_->SetString(prefs::kCryptAuthGCMRegistrationId, registration_id);
144 FOR_EACH_OBSERVER(Observer, observers_, OnGCMRegistrationResult(true));
145 }
146
147 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698