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