blob: 628fe9ec7345a4a6b2771ec7d1e7446d014adac2 [file] [log] [blame]
jianli40890032015-04-29 21:55:341// 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_H_
6#define COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_H_
7
8#include <map>
dchenga77e28eb2016-04-21 21:34:379#include <memory>
Richard Knoll882414b82019-08-05 15:19:4810#include <set>
jianli40890032015-04-29 21:55:3411#include <string>
12
13#include "base/callback.h"
14#include "base/macros.h"
johnmea00cc8ae2016-06-02 13:58:0415#include "base/memory/weak_ptr.h"
jianli40890032015-04-29 21:55:3416#include "base/time/time.h"
17
jianli2104ce612015-05-06 00:24:3418namespace gcm {
johnmea00cc8ae2016-06-02 13:58:0419class GCMDriver;
jianli2104ce612015-05-06 00:24:3420} // namespace gcm
21
jianli40890032015-04-29 21:55:3422namespace instance_id {
23
johnmea00cc8ae2016-06-02 13:58:0424extern const char kGCMScope[];
25
jianli40890032015-04-29 21:55:3426// Encapsulates Instance ID functionalities that need to be implemented for
johnme6ab98532016-04-27 18:57:1027// different platforms. One instance is created per application. Life of
28// Instance ID is managed by the InstanceIDDriver.
jianli40890032015-04-29 21:55:3429class InstanceID {
30 public:
johnmeef71bd02017-02-09 17:45:5631 // Used in UMA. Can add enum values, but never renumber or delete and reuse.
jianli40890032015-04-29 21:55:3432 enum Result {
33 // Successful operation.
johnmeef71bd02017-02-09 17:45:5634 SUCCESS = 0,
jianli40890032015-04-29 21:55:3435 // Invalid parameter.
johnmeef71bd02017-02-09 17:45:5636 INVALID_PARAMETER = 1,
jianli40890032015-04-29 21:55:3437 // Instance ID is disabled.
johnmeef71bd02017-02-09 17:45:5638 DISABLED = 2,
jianli7a0c9b62015-05-26 23:24:4739 // Previous asynchronous operation is still pending to finish.
johnmeef71bd02017-02-09 17:45:5640 ASYNC_OPERATION_PENDING = 3,
jianli40890032015-04-29 21:55:3441 // Network socket error.
johnmeef71bd02017-02-09 17:45:5642 NETWORK_ERROR = 4,
jianli40890032015-04-29 21:55:3443 // Problem at the server.
johnmeef71bd02017-02-09 17:45:5644 SERVER_ERROR = 5,
45 // 6 is omitted, in case we ever merge this enum with GCMClient::Result.
jianli40890032015-04-29 21:55:3446 // Other errors.
johnmeef71bd02017-02-09 17:45:5647 UNKNOWN_ERROR = 7,
48
49 // Used for UMA. Keep LAST_RESULT up to date and sync with histograms.xml.
50 LAST_RESULT = UNKNOWN_ERROR
jianli40890032015-04-29 21:55:3451 };
52
Richard Knoll882414b82019-08-05 15:19:4853 // Flags to be used to create a token. These might be platform specific.
54 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.gcm_driver
55 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: InstanceIDFlags
56 enum class Flags {
57 // Whether delivery of received messages should be deferred until there is a
58 // visible activity. Only applicable for Android.
59 kIsLazy = 1 << 0,
60 // Whether delivery of received messages should bypass the background task
61 // scheduler. Only applicable for high priority messages on Android.
62 kBypassScheduler = 1 << 1,
63 };
64
johnme6ab98532016-04-27 18:57:1065 // Asynchronous callbacks. Must not synchronously delete |this| (using
66 // InstanceIDDriver::RemoveInstanceID).
johnme6576ecf2017-04-03 19:26:2867 using TokenRefreshCallback =
68 base::Callback<void(const std::string& app_id, bool update_id)>;
69 using GetIDCallback = base::Callback<void(const std::string& id)>;
70 using GetCreationTimeCallback =
71 base::Callback<void(const base::Time& creation_time)>;
72 using GetTokenCallback =
danakjb534bf72019-05-02 17:10:1473 base::OnceCallback<void(const std::string& token, Result result)>;
danakjf4b9e942019-11-29 15:43:0474 using ValidateTokenCallback = base::OnceCallback<void(bool is_valid)>;
johnme6576ecf2017-04-03 19:26:2875 using GetEncryptionInfoCallback =
Peter Beverloo9635b662019-07-10 19:05:1876 base::OnceCallback<void(std::string p256dh, std::string auth_secret)>;
danakjb534bf72019-05-02 17:10:1477 using DeleteTokenCallback = base::OnceCallback<void(Result result)>;
78 using DeleteIDCallback = base::OnceCallback<void(Result result)>;
jianli40890032015-04-29 21:55:3479
jianli22a36732015-05-05 00:01:5680 static const int kInstanceIDByteLength = 8;
81
johnme627dc8c72016-08-19 21:49:3982 // Creator. Should only be used by InstanceIDDriver::GetInstanceID.
jianli40890032015-04-29 21:55:3483 // |app_id|: identifies the application that uses the Instance ID.
johnme54a3e1482016-03-11 19:13:2284 // |handler|: provides the GCM functionality needed to support Instance ID.
johnme2f8daf92016-04-15 18:17:4485 // Must outlive this class. On Android, this can be null instead.
johnme627dc8c72016-08-19 21:49:3986 static std::unique_ptr<InstanceID> CreateInternal(const std::string& app_id,
87 gcm::GCMDriver* gcm_driver);
jianli40890032015-04-29 21:55:3488
89 virtual ~InstanceID();
90
91 // Sets the callback that will be invoked when the token refresh event needs
92 // to be triggered.
93 void SetTokenRefreshCallback(const TokenRefreshCallback& callback);
94
95 // Returns the Instance ID.
jianli10018b2d2015-05-11 21:14:1396 virtual void GetID(const GetIDCallback& callback) = 0;
jianli40890032015-04-29 21:55:3497
98 // Returns the time when the InstanceID has been generated.
jianli10018b2d2015-05-11 21:14:1399 virtual void GetCreationTime(const GetCreationTimeCallback& callback) = 0;
jianli40890032015-04-29 21:55:34100
jianli3c232642015-05-05 00:28:27101 // Retrieves a token that allows the authorized entity to access the service
102 // defined as "scope".
103 // |authorized_entity|: identifies the entity that is authorized to access
104 // resources associated with this Instance ID. It can be
105 // another Instance ID or a project ID.
jianli40890032015-04-29 21:55:34106 // |scope|: identifies authorized actions that the authorized entity can take.
107 // E.g. for sending GCM messages, "GCM" scope should be used.
108 // |options|: allows including a small number of string key/value pairs that
109 // will be associated with the token and may be used in processing
110 // the request.
Richard Knoll882414b82019-08-05 15:19:48111 // |flags|: Flags used to create this token.
jianli40890032015-04-29 21:55:34112 // |callback|: to be called once the asynchronous operation is done.
jianli3c232642015-05-05 00:28:27113 virtual void GetToken(const std::string& authorized_entity,
jianli40890032015-04-29 21:55:34114 const std::string& scope,
115 const std::map<std::string, std::string>& options,
Richard Knoll882414b82019-08-05 15:19:48116 std::set<Flags> flags,
danakjb534bf72019-05-02 17:10:14117 GetTokenCallback callback) = 0;
jianli40890032015-04-29 21:55:34118
johnme6576ecf2017-04-03 19:26:28119 // Checks that the provided |token| matches the stored token for (|app_id()|,
120 // |authorized_entity|, |scope|).
121 virtual void ValidateToken(const std::string& authorized_entity,
122 const std::string& scope,
123 const std::string& token,
danakjf4b9e942019-11-29 15:43:04124 ValidateTokenCallback callback) = 0;
johnme6576ecf2017-04-03 19:26:28125
johnmea00cc8ae2016-06-02 13:58:04126 // Get the public encryption key and authentication secret associated with a
127 // GCM-scoped token. If encryption info is not yet associated, it will be
128 // created.
129 // |authorized_entity|: the authorized entity passed when obtaining the token.
jianli40890032015-04-29 21:55:34130 // |callback|: to be called once the asynchronous operation is done.
Alex Chau266f6772019-07-25 20:43:02131 virtual void GetEncryptionInfo(const std::string& authorized_entity,
132 GetEncryptionInfoCallback callback);
johnmea00cc8ae2016-06-02 13:58:04133
134 // Revokes a granted token.
135 // |authorized_entity|: the authorized entity passed when obtaining the token.
136 // |scope|: the scope that was passed when obtaining the token.
137 // |callback|: to be called once the asynchronous operation is done.
Alex Chau81fe8c312019-07-15 12:09:25138 virtual void DeleteToken(const std::string& authorized_entity,
139 const std::string& scope,
140 DeleteTokenCallback callback);
jianli40890032015-04-29 21:55:34141
142 // Resets the app instance identifier and revokes all tokens associated with
143 // it.
144 // |callback|: to be called once the asynchronous operation is done.
danakjb534bf72019-05-02 17:10:14145 void DeleteID(DeleteIDCallback callback);
jianli40890032015-04-29 21:55:34146
147 std::string app_id() const { return app_id_; }
148
149 protected:
johnmea00cc8ae2016-06-02 13:58:04150 InstanceID(const std::string& app_id, gcm::GCMDriver* gcm_driver);
151
152 // Platform-specific implementations.
153 virtual void DeleteTokenImpl(const std::string& authorized_entity,
154 const std::string& scope,
danakjb534bf72019-05-02 17:10:14155 DeleteTokenCallback callback) = 0;
156 virtual void DeleteIDImpl(DeleteIDCallback callback) = 0;
jianli40890032015-04-29 21:55:34157
158 void NotifyTokenRefresh(bool update_id);
159
johnmea00cc8ae2016-06-02 13:58:04160 gcm::GCMDriver* gcm_driver() { return gcm_driver_; }
161
jianli40890032015-04-29 21:55:34162 private:
johnmea00cc8ae2016-06-02 13:58:04163 void DidDelete(const std::string& authorized_entity,
danakjb534bf72019-05-02 17:10:14164 base::OnceCallback<void(Result result)> callback,
johnmea00cc8ae2016-06-02 13:58:04165 Result result);
166
167 // Owned by GCMProfileServiceFactory, which is a dependency of
168 // InstanceIDProfileServiceFactory, which owns this.
169 gcm::GCMDriver* gcm_driver_;
170
jianli40890032015-04-29 21:55:34171 std::string app_id_;
172 TokenRefreshCallback token_refresh_callback_;
173
Jeremy Roman5c341f6d2019-07-15 15:56:10174 base::WeakPtrFactory<InstanceID> weak_ptr_factory_{this};
johnmea00cc8ae2016-06-02 13:58:04175
jianli40890032015-04-29 21:55:34176 DISALLOW_COPY_AND_ASSIGN(InstanceID);
177};
178
179} // namespace instance_id
180
181#endif // COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_H_