| 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> |
| 8 |
| 7 #include <algorithm> | 9 #include <algorithm> |
| 8 #include "base/base64.h" | 10 #include "base/base64.h" |
| 9 #include "base/bind.h" | 11 #include "base/bind.h" |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 13 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 14 #include "components/gcm_driver/gcm_driver_desktop.h" | 16 #include "components/gcm_driver/gcm_driver_desktop.h" |
| 15 #include "crypto/random.h" | 17 #include "crypto/random.h" |
| 16 | 18 |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 } | 228 } |
| 227 | 229 |
| 228 void InstanceIDImpl::GetInstanceIDDataCompleted( | 230 void InstanceIDImpl::GetInstanceIDDataCompleted( |
| 229 const std::string& instance_id, | 231 const std::string& instance_id, |
| 230 const std::string& extra_data) { | 232 const std::string& extra_data) { |
| 231 id_ = instance_id; | 233 id_ = instance_id; |
| 232 | 234 |
| 233 if (extra_data.empty()) { | 235 if (extra_data.empty()) { |
| 234 creation_time_ = base::Time(); | 236 creation_time_ = base::Time(); |
| 235 } else { | 237 } else { |
| 236 int64 time_internal = 0LL; | 238 int64_t time_internal = 0LL; |
| 237 if (!base::StringToInt64(extra_data, &time_internal)) { | 239 if (!base::StringToInt64(extra_data, &time_internal)) { |
| 238 DVLOG(1) << "Failed to parse the time data: " + extra_data; | 240 DVLOG(1) << "Failed to parse the time data: " + extra_data; |
| 239 return; | 241 return; |
| 240 } | 242 } |
| 241 creation_time_ = base::Time::FromInternalValue(time_internal); | 243 creation_time_ = base::Time::FromInternalValue(time_internal); |
| 242 } | 244 } |
| 243 | 245 |
| 244 delayed_task_controller_.SetReady(); | 246 delayed_task_controller_.SetReady(); |
| 245 } | 247 } |
| 246 | 248 |
| 247 gcm::InstanceIDHandler* InstanceIDImpl::GetInstanceIDHandler() const { | 249 gcm::InstanceIDHandler* InstanceIDImpl::GetInstanceIDHandler() const { |
| 248 gcm::InstanceIDHandler* handler = gcm_driver_->GetInstanceIDHandler(); | 250 gcm::InstanceIDHandler* handler = gcm_driver_->GetInstanceIDHandler(); |
| 249 DCHECK(handler); | 251 DCHECK(handler); |
| 250 return handler; | 252 return handler; |
| 251 } | 253 } |
| 252 | 254 |
| 253 void InstanceIDImpl::EnsureIDGenerated() { | 255 void InstanceIDImpl::EnsureIDGenerated() { |
| 254 if (!id_.empty()) | 256 if (!id_.empty()) |
| 255 return; | 257 return; |
| 256 | 258 |
| 257 // Now produce the ID in the following steps: | 259 // Now produce the ID in the following steps: |
| 258 | 260 |
| 259 // 1) Generates the random number in 8 bytes which is required by the server. | 261 // 1) Generates the random number in 8 bytes which is required by the server. |
| 260 // We don't want to be strictly cryptographically secure. The server might | 262 // We don't want to be strictly cryptographically secure. The server might |
| 261 // reject the ID if there is a conflict or problem. | 263 // reject the ID if there is a conflict or problem. |
| 262 uint8 bytes[kInstanceIDByteLength]; | 264 uint8_t bytes[kInstanceIDByteLength]; |
| 263 crypto::RandBytes(bytes, sizeof(bytes)); | 265 crypto::RandBytes(bytes, sizeof(bytes)); |
| 264 | 266 |
| 265 // 2) Transforms the first 4 bits to 0x7. Note that this is required by the | 267 // 2) Transforms the first 4 bits to 0x7. Note that this is required by the |
| 266 // server. | 268 // server. |
| 267 bytes[0] &= 0x0f; | 269 bytes[0] &= 0x0f; |
| 268 bytes[0] |= 0x70; | 270 bytes[0] |= 0x70; |
| 269 | 271 |
| 270 // 3) Encode the value in Android-compatible base64 scheme: | 272 // 3) Encode the value in Android-compatible base64 scheme: |
| 271 // * URL safe: '/' replaced by '_' and '+' replaced by '-'. | 273 // * URL safe: '/' replaced by '_' and '+' replaced by '-'. |
| 272 // * No padding: any trailing '=' will be removed. | 274 // * No padding: any trailing '=' will be removed. |
| 273 base::Base64Encode( | 275 base::Base64Encode( |
| 274 base::StringPiece(reinterpret_cast<const char*>(bytes), sizeof(bytes)), | 276 base::StringPiece(reinterpret_cast<const char*>(bytes), sizeof(bytes)), |
| 275 &id_); | 277 &id_); |
| 276 std::replace(id_.begin(), id_.end(), '+', '-'); | 278 std::replace(id_.begin(), id_.end(), '+', '-'); |
| 277 std::replace(id_.begin(), id_.end(), '/', '_'); | 279 std::replace(id_.begin(), id_.end(), '/', '_'); |
| 278 id_.erase(std::remove(id_.begin(), id_.end(), '='), id_.end()); | 280 id_.erase(std::remove(id_.begin(), id_.end(), '='), id_.end()); |
| 279 | 281 |
| 280 creation_time_ = base::Time::Now(); | 282 creation_time_ = base::Time::Now(); |
| 281 | 283 |
| 282 // Save to the persistent store. | 284 // Save to the persistent store. |
| 283 GetInstanceIDHandler()->AddInstanceIDData( | 285 GetInstanceIDHandler()->AddInstanceIDData( |
| 284 app_id(), | 286 app_id(), |
| 285 id_, | 287 id_, |
| 286 base::Int64ToString(creation_time_.ToInternalValue())); | 288 base::Int64ToString(creation_time_.ToInternalValue())); |
| 287 } | 289 } |
| 288 | 290 |
| 289 } // namespace instance_id | 291 } // namespace instance_id |
| OLD | NEW |