instance_id.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include "components/gcm_driver/instance_id/instance_id.h"
  5. #include "base/bind.h"
  6. #include "components/gcm_driver/gcm_driver.h"
  7. namespace instance_id {
  8. // A common use case for InstanceID tokens is to authorize and route push
  9. // messages sent via Google Cloud Messaging (replacing the earlier registration
  10. // IDs). To get such a GCM-enabled token, pass this scope to getToken.
  11. // Must match Java GoogleCloudMessaging.INSTANCE_ID_SCOPE.
  12. const char kGCMScope[] = "GCM";
  13. InstanceID::InstanceID(const std::string& app_id, gcm::GCMDriver* gcm_driver)
  14. : gcm_driver_(gcm_driver), app_id_(app_id) {}
  15. InstanceID::~InstanceID() {}
  16. void InstanceID::GetEncryptionInfo(const std::string& authorized_entity,
  17. GetEncryptionInfoCallback callback) {
  18. gcm_driver_->GetEncryptionProviderInternal()->GetEncryptionInfo(
  19. app_id_, authorized_entity, std::move(callback));
  20. }
  21. void InstanceID::DeleteToken(const std::string& authorized_entity,
  22. const std::string& scope,
  23. DeleteTokenCallback callback) {
  24. // Tokens with GCM scope act as Google Cloud Messaging registrations, so may
  25. // have associated encryption information in the GCMKeyStore. This needs to be
  26. // cleared when the token is deleted.
  27. DeleteTokenCallback wrapped_callback =
  28. scope == kGCMScope
  29. ? base::BindOnce(&InstanceID::DidDelete,
  30. weak_ptr_factory_.GetWeakPtr(), authorized_entity,
  31. std::move(callback))
  32. : std::move(callback);
  33. DeleteTokenImpl(authorized_entity, scope, std::move(wrapped_callback));
  34. }
  35. void InstanceID::DeleteID(DeleteIDCallback callback) {
  36. // Use "*" as authorized_entity to remove any encryption info for all tokens.
  37. DeleteIDImpl(
  38. base::BindOnce(&InstanceID::DidDelete, weak_ptr_factory_.GetWeakPtr(),
  39. "*" /* authorized_entity */, std::move(callback)));
  40. }
  41. void InstanceID::DidDelete(const std::string& authorized_entity,
  42. base::OnceCallback<void(Result result)> callback,
  43. Result result) {
  44. gcm_driver_->GetEncryptionProviderInternal()->RemoveEncryptionInfo(
  45. app_id_, authorized_entity, base::BindOnce(std::move(callback), result));
  46. }
  47. } // namespace instance_id