| 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 "base/base64.h" | 10 #include "base/base64.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 } // namespace | 47 } // namespace |
| 48 | 48 |
| 49 // static | 49 // static |
| 50 scoped_ptr<InstanceID> InstanceID::Create(const std::string& app_id, | 50 scoped_ptr<InstanceID> InstanceID::Create(const std::string& app_id, |
| 51 gcm::InstanceIDHandler* handler) { | 51 gcm::InstanceIDHandler* handler) { |
| 52 return make_scoped_ptr(new InstanceIDImpl(app_id, handler)); | 52 return make_scoped_ptr(new InstanceIDImpl(app_id, handler)); |
| 53 } | 53 } |
| 54 | 54 |
| 55 InstanceIDImpl::InstanceIDImpl(const std::string& app_id, | 55 InstanceIDImpl::InstanceIDImpl(const std::string& app_id, |
| 56 gcm::InstanceIDHandler* handler) | 56 gcm::InstanceIDHandler* handler) |
| 57 : InstanceID(app_id, handler), weak_ptr_factory_(this) { | 57 : InstanceID(app_id), handler_(handler), weak_ptr_factory_(this) { |
| 58 handler->GetInstanceIDData( | 58 DCHECK(handler_); |
| 59 handler_->GetInstanceIDData( |
| 59 app_id, base::Bind(&InstanceIDImpl::GetInstanceIDDataCompleted, | 60 app_id, base::Bind(&InstanceIDImpl::GetInstanceIDDataCompleted, |
| 60 weak_ptr_factory_.GetWeakPtr())); | 61 weak_ptr_factory_.GetWeakPtr())); |
| 61 } | 62 } |
| 62 | 63 |
| 63 InstanceIDImpl::~InstanceIDImpl() { | 64 InstanceIDImpl::~InstanceIDImpl() { |
| 64 } | 65 } |
| 65 | 66 |
| 66 void InstanceIDImpl::GetID(const GetIDCallback& callback) { | 67 void InstanceIDImpl::GetID(const GetIDCallback& callback) { |
| 67 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { | 68 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { |
| 68 delayed_task_controller_.AddTask( | 69 delayed_task_controller_.AddTask( |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 DoGetToken(authorized_entity, scope, options, callback); | 120 DoGetToken(authorized_entity, scope, options, callback); |
| 120 } | 121 } |
| 121 | 122 |
| 122 void InstanceIDImpl::DoGetToken( | 123 void InstanceIDImpl::DoGetToken( |
| 123 const std::string& authorized_entity, | 124 const std::string& authorized_entity, |
| 124 const std::string& scope, | 125 const std::string& scope, |
| 125 const std::map<std::string, std::string>& options, | 126 const std::map<std::string, std::string>& options, |
| 126 const GetTokenCallback& callback) { | 127 const GetTokenCallback& callback) { |
| 127 EnsureIDGenerated(); | 128 EnsureIDGenerated(); |
| 128 | 129 |
| 129 handler()->GetToken(app_id(), authorized_entity, scope, options, | 130 handler_->GetToken(app_id(), authorized_entity, scope, options, |
| 130 base::Bind(&InstanceIDImpl::OnGetTokenCompleted, | 131 base::Bind(&InstanceIDImpl::OnGetTokenCompleted, |
| 131 weak_ptr_factory_.GetWeakPtr(), callback)); | 132 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 132 } | 133 } |
| 133 | 134 |
| 134 void InstanceIDImpl::DeleteToken(const std::string& authorized_entity, | 135 void InstanceIDImpl::DeleteToken(const std::string& authorized_entity, |
| 135 const std::string& scope, | 136 const std::string& scope, |
| 136 const DeleteTokenCallback& callback) { | 137 const DeleteTokenCallback& callback) { |
| 137 DCHECK(!authorized_entity.empty()); | 138 DCHECK(!authorized_entity.empty()); |
| 138 DCHECK(!scope.empty()); | 139 DCHECK(!scope.empty()); |
| 139 | 140 |
| 140 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { | 141 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { |
| 141 delayed_task_controller_.AddTask( | 142 delayed_task_controller_.AddTask( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 153 void InstanceIDImpl::DoDeleteToken( | 154 void InstanceIDImpl::DoDeleteToken( |
| 154 const std::string& authorized_entity, | 155 const std::string& authorized_entity, |
| 155 const std::string& scope, | 156 const std::string& scope, |
| 156 const DeleteTokenCallback& callback) { | 157 const DeleteTokenCallback& callback) { |
| 157 // Nothing to delete if the ID has not been generated. | 158 // Nothing to delete if the ID has not been generated. |
| 158 if (id_.empty()) { | 159 if (id_.empty()) { |
| 159 callback.Run(InstanceID::INVALID_PARAMETER); | 160 callback.Run(InstanceID::INVALID_PARAMETER); |
| 160 return; | 161 return; |
| 161 } | 162 } |
| 162 | 163 |
| 163 handler()->DeleteToken(app_id(), authorized_entity, scope, | 164 handler_->DeleteToken(app_id(), authorized_entity, scope, |
| 164 base::Bind(&InstanceIDImpl::OnDeleteTokenCompleted, | 165 base::Bind(&InstanceIDImpl::OnDeleteTokenCompleted, |
| 165 weak_ptr_factory_.GetWeakPtr(), callback)); | 166 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 166 } | 167 } |
| 167 | 168 |
| 168 void InstanceIDImpl::DeleteID(const DeleteIDCallback& callback) { | 169 void InstanceIDImpl::DeleteID(const DeleteIDCallback& callback) { |
| 169 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { | 170 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { |
| 170 delayed_task_controller_.AddTask( | 171 delayed_task_controller_.AddTask( |
| 171 base::Bind(&InstanceIDImpl::DoDeleteID, | 172 base::Bind(&InstanceIDImpl::DoDeleteID, |
| 172 weak_ptr_factory_.GetWeakPtr(), | 173 weak_ptr_factory_.GetWeakPtr(), |
| 173 callback)); | 174 callback)); |
| 174 return; | 175 return; |
| 175 } | 176 } |
| 176 | 177 |
| 177 DoDeleteID(callback); | 178 DoDeleteID(callback); |
| 178 } | 179 } |
| 179 | 180 |
| 180 void InstanceIDImpl::DoDeleteID(const DeleteIDCallback& callback) { | 181 void InstanceIDImpl::DoDeleteID(const DeleteIDCallback& callback) { |
| 181 // Nothing to do if ID has not been generated. | 182 // Nothing to do if ID has not been generated. |
| 182 if (id_.empty()) { | 183 if (id_.empty()) { |
| 183 callback.Run(InstanceID::SUCCESS); | 184 callback.Run(InstanceID::SUCCESS); |
| 184 return; | 185 return; |
| 185 } | 186 } |
| 186 | 187 |
| 187 handler()->DeleteAllTokensForApp( | 188 handler_->DeleteAllTokensForApp( |
| 188 app_id(), base::Bind(&InstanceIDImpl::OnDeleteIDCompleted, | 189 app_id(), base::Bind(&InstanceIDImpl::OnDeleteIDCompleted, |
| 189 weak_ptr_factory_.GetWeakPtr(), callback)); | 190 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 190 | 191 |
| 191 handler()->RemoveInstanceIDData(app_id()); | 192 handler_->RemoveInstanceIDData(app_id()); |
| 192 | 193 |
| 193 id_.clear(); | 194 id_.clear(); |
| 194 creation_time_ = base::Time(); | 195 creation_time_ = base::Time(); |
| 195 } | 196 } |
| 196 | 197 |
| 197 void InstanceIDImpl::OnGetTokenCompleted(const GetTokenCallback& callback, | 198 void InstanceIDImpl::OnGetTokenCompleted(const GetTokenCallback& callback, |
| 198 const std::string& token, | 199 const std::string& token, |
| 199 gcm::GCMClient::Result result) { | 200 gcm::GCMClient::Result result) { |
| 200 callback.Run(token, GCMClientResultToInstanceIDResult(result)); | 201 callback.Run(token, GCMClientResultToInstanceIDResult(result)); |
| 201 } | 202 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 base::Base64Encode( | 255 base::Base64Encode( |
| 255 base::StringPiece(reinterpret_cast<const char*>(bytes), sizeof(bytes)), | 256 base::StringPiece(reinterpret_cast<const char*>(bytes), sizeof(bytes)), |
| 256 &id_); | 257 &id_); |
| 257 std::replace(id_.begin(), id_.end(), '+', '-'); | 258 std::replace(id_.begin(), id_.end(), '+', '-'); |
| 258 std::replace(id_.begin(), id_.end(), '/', '_'); | 259 std::replace(id_.begin(), id_.end(), '/', '_'); |
| 259 id_.erase(std::remove(id_.begin(), id_.end(), '='), id_.end()); | 260 id_.erase(std::remove(id_.begin(), id_.end(), '='), id_.end()); |
| 260 | 261 |
| 261 creation_time_ = base::Time::Now(); | 262 creation_time_ = base::Time::Now(); |
| 262 | 263 |
| 263 // Save to the persistent store. | 264 // Save to the persistent store. |
| 264 handler()->AddInstanceIDData( | 265 handler_->AddInstanceIDData( |
| 265 app_id(), id_, base::Int64ToString(creation_time_.ToInternalValue())); | 266 app_id(), id_, base::Int64ToString(creation_time_.ToInternalValue())); |
| 266 } | 267 } |
| 267 | 268 |
| 268 } // namespace instance_id | 269 } // namespace instance_id |
| OLD | NEW |