OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/gcm_driver/instance_id/instance_id_impl.h" | 5 #include "components/gcm_driver/instance_id/instance_id_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 default: | 42 default: |
43 NOTREACHED() << "Unexpected value of result cannot be converted: " | 43 NOTREACHED() << "Unexpected value of result cannot be converted: " |
44 << result; | 44 << result; |
45 } | 45 } |
46 return InstanceID::UNKNOWN_ERROR; | 46 return InstanceID::UNKNOWN_ERROR; |
47 } | 47 } |
48 | 48 |
49 } // namespace | 49 } // namespace |
50 | 50 |
51 // static | 51 // static |
52 std::unique_ptr<InstanceID> InstanceID::Create( | 52 std::unique_ptr<InstanceID> InstanceID::Create(const std::string& app_id, |
53 const std::string& app_id, | 53 gcm::GCMDriver* gcm_driver) { |
54 gcm::InstanceIDHandler* handler) { | 54 return base::WrapUnique(new InstanceIDImpl(app_id, gcm_driver)); |
55 return base::WrapUnique(new InstanceIDImpl(app_id, handler)); | |
56 } | 55 } |
57 | 56 |
58 InstanceIDImpl::InstanceIDImpl(const std::string& app_id, | 57 InstanceIDImpl::InstanceIDImpl(const std::string& app_id, |
59 gcm::InstanceIDHandler* handler) | 58 gcm::GCMDriver* gcm_driver) |
60 : InstanceID(app_id), handler_(handler), weak_ptr_factory_(this) { | 59 : InstanceID(app_id, gcm_driver), weak_ptr_factory_(this) { |
61 DCHECK(handler_); | 60 Handler()->GetInstanceIDData( |
62 handler_->GetInstanceIDData( | |
63 app_id, base::Bind(&InstanceIDImpl::GetInstanceIDDataCompleted, | 61 app_id, base::Bind(&InstanceIDImpl::GetInstanceIDDataCompleted, |
64 weak_ptr_factory_.GetWeakPtr())); | 62 weak_ptr_factory_.GetWeakPtr())); |
65 } | 63 } |
66 | 64 |
67 InstanceIDImpl::~InstanceIDImpl() { | 65 InstanceIDImpl::~InstanceIDImpl() { |
68 } | 66 } |
69 | 67 |
70 void InstanceIDImpl::GetID(const GetIDCallback& callback) { | 68 void InstanceIDImpl::GetID(const GetIDCallback& callback) { |
71 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { | 69 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { |
72 delayed_task_controller_.AddTask( | 70 delayed_task_controller_.AddTask( |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 DoGetToken(authorized_entity, scope, options, callback); | 121 DoGetToken(authorized_entity, scope, options, callback); |
124 } | 122 } |
125 | 123 |
126 void InstanceIDImpl::DoGetToken( | 124 void InstanceIDImpl::DoGetToken( |
127 const std::string& authorized_entity, | 125 const std::string& authorized_entity, |
128 const std::string& scope, | 126 const std::string& scope, |
129 const std::map<std::string, std::string>& options, | 127 const std::map<std::string, std::string>& options, |
130 const GetTokenCallback& callback) { | 128 const GetTokenCallback& callback) { |
131 EnsureIDGenerated(); | 129 EnsureIDGenerated(); |
132 | 130 |
133 handler_->GetToken(app_id(), authorized_entity, scope, options, | 131 Handler()->GetToken(app_id(), authorized_entity, scope, options, |
134 base::Bind(&InstanceIDImpl::OnGetTokenCompleted, | 132 base::Bind(&InstanceIDImpl::OnGetTokenCompleted, |
135 weak_ptr_factory_.GetWeakPtr(), callback)); | 133 weak_ptr_factory_.GetWeakPtr(), callback)); |
136 } | 134 } |
137 | 135 |
138 void InstanceIDImpl::DeleteToken(const std::string& authorized_entity, | 136 void InstanceIDImpl::DeleteTokenImpl(const std::string& authorized_entity, |
139 const std::string& scope, | 137 const std::string& scope, |
140 const DeleteTokenCallback& callback) { | 138 const DeleteTokenCallback& callback) { |
141 DCHECK(!authorized_entity.empty()); | 139 DCHECK(!authorized_entity.empty()); |
142 DCHECK(!scope.empty()); | 140 DCHECK(!scope.empty()); |
143 | 141 |
144 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { | 142 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { |
145 delayed_task_controller_.AddTask( | 143 delayed_task_controller_.AddTask( |
146 base::Bind(&InstanceIDImpl::DoDeleteToken, | 144 base::Bind(&InstanceIDImpl::DoDeleteToken, |
147 weak_ptr_factory_.GetWeakPtr(), | 145 weak_ptr_factory_.GetWeakPtr(), |
148 authorized_entity, | 146 authorized_entity, |
149 scope, | 147 scope, |
150 callback)); | 148 callback)); |
151 return; | 149 return; |
152 } | 150 } |
153 | 151 |
154 DoDeleteToken(authorized_entity, scope, callback); | 152 DoDeleteToken(authorized_entity, scope, callback); |
155 } | 153 } |
156 | 154 |
157 void InstanceIDImpl::DoDeleteToken( | 155 void InstanceIDImpl::DoDeleteToken( |
158 const std::string& authorized_entity, | 156 const std::string& authorized_entity, |
159 const std::string& scope, | 157 const std::string& scope, |
160 const DeleteTokenCallback& callback) { | 158 const DeleteTokenCallback& callback) { |
161 // Nothing to delete if the ID has not been generated. | 159 // Nothing to delete if the ID has not been generated. |
162 if (id_.empty()) { | 160 if (id_.empty()) { |
163 callback.Run(InstanceID::INVALID_PARAMETER); | 161 callback.Run(InstanceID::INVALID_PARAMETER); |
164 return; | 162 return; |
165 } | 163 } |
166 | 164 |
167 handler_->DeleteToken(app_id(), authorized_entity, scope, | 165 Handler()->DeleteToken(app_id(), authorized_entity, scope, |
168 base::Bind(&InstanceIDImpl::OnDeleteTokenCompleted, | 166 base::Bind(&InstanceIDImpl::OnDeleteTokenCompleted, |
169 weak_ptr_factory_.GetWeakPtr(), callback)); | 167 weak_ptr_factory_.GetWeakPtr(), callback)); |
170 } | 168 } |
171 | 169 |
172 void InstanceIDImpl::DeleteID(const DeleteIDCallback& callback) { | 170 void InstanceIDImpl::DeleteIDImpl(const DeleteIDCallback& callback) { |
173 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { | 171 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { |
174 delayed_task_controller_.AddTask( | 172 delayed_task_controller_.AddTask( |
175 base::Bind(&InstanceIDImpl::DoDeleteID, | 173 base::Bind(&InstanceIDImpl::DoDeleteID, |
176 weak_ptr_factory_.GetWeakPtr(), | 174 weak_ptr_factory_.GetWeakPtr(), |
177 callback)); | 175 callback)); |
178 return; | 176 return; |
179 } | 177 } |
180 | 178 |
181 DoDeleteID(callback); | 179 DoDeleteID(callback); |
182 } | 180 } |
183 | 181 |
184 void InstanceIDImpl::DoDeleteID(const DeleteIDCallback& callback) { | 182 void InstanceIDImpl::DoDeleteID(const DeleteIDCallback& callback) { |
185 // Nothing to do if ID has not been generated. | 183 // Nothing to do if ID has not been generated. |
186 if (id_.empty()) { | 184 if (id_.empty()) { |
187 callback.Run(InstanceID::SUCCESS); | 185 callback.Run(InstanceID::SUCCESS); |
188 return; | 186 return; |
189 } | 187 } |
190 | 188 |
191 handler_->DeleteAllTokensForApp( | 189 Handler()->DeleteAllTokensForApp( |
192 app_id(), base::Bind(&InstanceIDImpl::OnDeleteIDCompleted, | 190 app_id(), base::Bind(&InstanceIDImpl::OnDeleteIDCompleted, |
193 weak_ptr_factory_.GetWeakPtr(), callback)); | 191 weak_ptr_factory_.GetWeakPtr(), callback)); |
194 | 192 |
195 handler_->RemoveInstanceIDData(app_id()); | 193 Handler()->RemoveInstanceIDData(app_id()); |
196 | 194 |
197 id_.clear(); | 195 id_.clear(); |
198 creation_time_ = base::Time(); | 196 creation_time_ = base::Time(); |
199 } | 197 } |
200 | 198 |
201 void InstanceIDImpl::OnGetTokenCompleted(const GetTokenCallback& callback, | 199 void InstanceIDImpl::OnGetTokenCompleted(const GetTokenCallback& callback, |
202 const std::string& token, | 200 const std::string& token, |
203 gcm::GCMClient::Result result) { | 201 gcm::GCMClient::Result result) { |
204 callback.Run(token, GCMClientResultToInstanceIDResult(result)); | 202 callback.Run(token, GCMClientResultToInstanceIDResult(result)); |
205 } | 203 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 base::Base64Encode( | 256 base::Base64Encode( |
259 base::StringPiece(reinterpret_cast<const char*>(bytes), sizeof(bytes)), | 257 base::StringPiece(reinterpret_cast<const char*>(bytes), sizeof(bytes)), |
260 &id_); | 258 &id_); |
261 std::replace(id_.begin(), id_.end(), '+', '-'); | 259 std::replace(id_.begin(), id_.end(), '+', '-'); |
262 std::replace(id_.begin(), id_.end(), '/', '_'); | 260 std::replace(id_.begin(), id_.end(), '/', '_'); |
263 id_.erase(std::remove(id_.begin(), id_.end(), '='), id_.end()); | 261 id_.erase(std::remove(id_.begin(), id_.end(), '='), id_.end()); |
264 | 262 |
265 creation_time_ = base::Time::Now(); | 263 creation_time_ = base::Time::Now(); |
266 | 264 |
267 // Save to the persistent store. | 265 // Save to the persistent store. |
268 handler_->AddInstanceIDData( | 266 Handler()->AddInstanceIDData( |
269 app_id(), id_, base::Int64ToString(creation_time_.ToInternalValue())); | 267 app_id(), id_, base::Int64ToString(creation_time_.ToInternalValue())); |
270 } | 268 } |
271 | 269 |
| 270 gcm::InstanceIDHandler* InstanceIDImpl::Handler() { |
| 271 gcm::InstanceIDHandler* handler = |
| 272 gcm_driver()->GetInstanceIDHandlerInternal(); |
| 273 DCHECK(handler); |
| 274 return handler; |
| 275 } |
| 276 |
272 } // namespace instance_id | 277 } // namespace instance_id |
OLD | NEW |