OLD | NEW |
| (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/strings/string_util.h" | |
9 #include "components/gcm_driver/gcm_driver.h" | |
10 #include "components/prefs/pref_service.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(const std::string& app_id, | |
87 const gcm::IncomingMessage& message) { | |
88 std::vector<std::string> fields; | |
89 for (const auto& kv : message.data) { | |
90 fields.push_back(std::string(kv.first) + ": " + std::string(kv.second)); | |
91 } | |
92 | |
93 PA_LOG(INFO) << "GCM message received:\n" | |
94 << " sender_id: " << message.sender_id << "\n" | |
95 << " collapse_key: " << message.collapse_key << "\n" | |
96 << " data:\n " << base::JoinString(fields, "\n "); | |
97 | |
98 if (message.data.find(kRegistrationTickleTypeKey) == message.data.end()) { | |
99 PA_LOG(WARNING) << "GCM message does not contain 'registrationTickleType'."; | |
100 } else { | |
101 std::string tickle_type = message.data.at(kRegistrationTickleTypeKey); | |
102 if (tickle_type == kRegistrationTickleTypeForceEnrollment || | |
103 tickle_type == kRegistrationTickleTypeUpdateEnrollment) { | |
104 // These tickle types correspond to re-enrollment messages. | |
105 for (auto& observer : observers_) | |
106 observer.OnReenrollMessage(); | |
107 } else if (tickle_type == kRegistrationTickleTypeDevicesSync) { | |
108 for (auto& observer : observers_) | |
109 observer.OnResyncMessage(); | |
110 } else { | |
111 PA_LOG(WARNING) << "Unknown tickle type in GCM message."; | |
112 } | |
113 } | |
114 } | |
115 | |
116 void CryptAuthGCMManagerImpl::OnMessagesDeleted(const std::string& app_id) { | |
117 } | |
118 | |
119 void CryptAuthGCMManagerImpl::OnSendError( | |
120 const std::string& app_id, | |
121 const gcm::GCMClient::SendErrorDetails& details) { | |
122 NOTREACHED(); | |
123 } | |
124 | |
125 void CryptAuthGCMManagerImpl::OnSendAcknowledged( | |
126 const std::string& app_id, | |
127 const std::string& message_id) { | |
128 NOTREACHED(); | |
129 } | |
130 | |
131 void CryptAuthGCMManagerImpl::OnRegistrationCompleted( | |
132 const std::string& registration_id, | |
133 gcm::GCMClient::Result result) { | |
134 registration_in_progress_ = false; | |
135 if (result != gcm::GCMClient::SUCCESS) { | |
136 PA_LOG(WARNING) << "GCM registration failed with result=" | |
137 << static_cast<int>(result); | |
138 for (auto& observer : observers_) | |
139 observer.OnGCMRegistrationResult(false); | |
140 return; | |
141 } | |
142 | |
143 PA_LOG(INFO) << "GCM registration success, registration_id=" | |
144 << registration_id; | |
145 pref_service_->SetString(prefs::kCryptAuthGCMRegistrationId, registration_id); | |
146 for (auto& observer : observers_) | |
147 observer.OnGCMRegistrationResult(true); | |
148 } | |
149 | |
150 } // namespace proximity_auth | |
OLD | NEW |