instance_id_impl.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_IMPL_H_
  5. #define COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_IMPL_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/time/time.h"
  11. #include "components/gcm_driver/gcm_client.h"
  12. #include "components/gcm_driver/gcm_delayed_task_controller.h"
  13. #include "components/gcm_driver/instance_id/instance_id.h"
  14. namespace gcm {
  15. class GCMDriver;
  16. class InstanceIDHandler;
  17. } // namespace gcm
  18. namespace instance_id {
  19. // InstanceID implementation for desktop and iOS.
  20. class InstanceIDImpl : public InstanceID {
  21. public:
  22. InstanceIDImpl(const std::string& app_id, gcm::GCMDriver* gcm_driver);
  23. InstanceIDImpl(const InstanceIDImpl&) = delete;
  24. InstanceIDImpl& operator=(const InstanceIDImpl&) = delete;
  25. ~InstanceIDImpl() override;
  26. // InstanceID:
  27. void GetID(GetIDCallback callback) override;
  28. void GetCreationTime(GetCreationTimeCallback callback) override;
  29. void GetToken(const std::string& authorized_entity,
  30. const std::string& scope,
  31. base::TimeDelta time_to_live,
  32. std::set<Flags> flags,
  33. GetTokenCallback callback) override;
  34. void ValidateToken(const std::string& authorized_entity,
  35. const std::string& scope,
  36. const std::string& token,
  37. ValidateTokenCallback callback) override;
  38. void DeleteTokenImpl(const std::string& authorized_entity,
  39. const std::string& scope,
  40. DeleteTokenCallback callback) override;
  41. void DeleteIDImpl(DeleteIDCallback callback) override;
  42. private:
  43. void EnsureIDGenerated();
  44. void OnGetTokenCompleted(GetTokenCallback callback,
  45. const std::string& token,
  46. gcm::GCMClient::Result result);
  47. void OnDeleteTokenCompleted(DeleteTokenCallback callback,
  48. gcm::GCMClient::Result result);
  49. void OnDeleteIDCompleted(DeleteIDCallback callback,
  50. gcm::GCMClient::Result result);
  51. void GetInstanceIDDataCompleted(const std::string& instance_id,
  52. const std::string& extra_data);
  53. void DoGetID(GetIDCallback callback);
  54. void DoGetCreationTime(GetCreationTimeCallback callback);
  55. void DoGetToken(const std::string& authorized_entity,
  56. const std::string& scope,
  57. base::TimeDelta time_to_live,
  58. GetTokenCallback callback);
  59. void DoValidateToken(const std::string& authorized_entity,
  60. const std::string& scope,
  61. const std::string& token,
  62. ValidateTokenCallback callback);
  63. void DoDeleteToken(const std::string& authorized_entity,
  64. const std::string& scope,
  65. DeleteTokenCallback callback);
  66. void DoDeleteID(DeleteIDCallback callback);
  67. gcm::InstanceIDHandler* Handler();
  68. // Asynchronously runs task once delayed_task_controller_ is ready.
  69. void RunWhenReady(base::OnceClosure task);
  70. gcm::GCMDelayedTaskController delayed_task_controller_;
  71. // The generated Instance ID.
  72. std::string id_;
  73. // The time when the Instance ID has been generated.
  74. base::Time creation_time_;
  75. base::WeakPtrFactory<InstanceIDImpl> weak_ptr_factory_{this};
  76. };
  77. } // namespace instance_id
  78. #endif // COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_IMPL_H_