jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 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_H_ |
| 6 | #define COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_H_ |
| 7 | |
| 8 | #include <map> |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 9 | #include <memory> |
Richard Knoll | 882414b8 | 2019-08-05 15:19:48 | [diff] [blame] | 10 | #include <set> |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 11 | #include <string> |
| 12 | |
| 13 | #include "base/callback.h" |
| 14 | #include "base/macros.h" |
johnme | a00cc8ae | 2016-06-02 13:58:04 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 16 | #include "base/time/time.h" |
| 17 | |
jianli | 2104ce61 | 2015-05-06 00:24:34 | [diff] [blame] | 18 | namespace gcm { |
johnme | a00cc8ae | 2016-06-02 13:58:04 | [diff] [blame] | 19 | class GCMDriver; |
jianli | 2104ce61 | 2015-05-06 00:24:34 | [diff] [blame] | 20 | } // namespace gcm |
| 21 | |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 22 | namespace instance_id { |
| 23 | |
johnme | a00cc8ae | 2016-06-02 13:58:04 | [diff] [blame] | 24 | extern const char kGCMScope[]; |
| 25 | |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 26 | // Encapsulates Instance ID functionalities that need to be implemented for |
johnme | 6ab9853 | 2016-04-27 18:57:10 | [diff] [blame] | 27 | // different platforms. One instance is created per application. Life of |
| 28 | // Instance ID is managed by the InstanceIDDriver. |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 29 | class InstanceID { |
| 30 | public: |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 31 | // Used in UMA. Can add enum values, but never renumber or delete and reuse. |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 32 | enum Result { |
| 33 | // Successful operation. |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 34 | SUCCESS = 0, |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 35 | // Invalid parameter. |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 36 | INVALID_PARAMETER = 1, |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 37 | // Instance ID is disabled. |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 38 | DISABLED = 2, |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 39 | // Previous asynchronous operation is still pending to finish. |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 40 | ASYNC_OPERATION_PENDING = 3, |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 41 | // Network socket error. |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 42 | NETWORK_ERROR = 4, |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 43 | // Problem at the server. |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 44 | SERVER_ERROR = 5, |
| 45 | // 6 is omitted, in case we ever merge this enum with GCMClient::Result. |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 46 | // Other errors. |
johnme | ef71bd0 | 2017-02-09 17:45:56 | [diff] [blame] | 47 | UNKNOWN_ERROR = 7, |
| 48 | |
| 49 | // Used for UMA. Keep LAST_RESULT up to date and sync with histograms.xml. |
| 50 | LAST_RESULT = UNKNOWN_ERROR |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 51 | }; |
| 52 | |
Richard Knoll | 882414b8 | 2019-08-05 15:19:48 | [diff] [blame] | 53 | // 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 | |
johnme | 6ab9853 | 2016-04-27 18:57:10 | [diff] [blame] | 65 | // Asynchronous callbacks. Must not synchronously delete |this| (using |
| 66 | // InstanceIDDriver::RemoveInstanceID). |
johnme | 6576ecf | 2017-04-03 19:26:28 | [diff] [blame] | 67 | 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 = |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 73 | base::OnceCallback<void(const std::string& token, Result result)>; |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 74 | using ValidateTokenCallback = base::OnceCallback<void(bool is_valid)>; |
johnme | 6576ecf | 2017-04-03 19:26:28 | [diff] [blame] | 75 | using GetEncryptionInfoCallback = |
Peter Beverloo | 9635b66 | 2019-07-10 19:05:18 | [diff] [blame] | 76 | base::OnceCallback<void(std::string p256dh, std::string auth_secret)>; |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 77 | using DeleteTokenCallback = base::OnceCallback<void(Result result)>; |
| 78 | using DeleteIDCallback = base::OnceCallback<void(Result result)>; |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 79 | |
jianli | 22a3673 | 2015-05-05 00:01:56 | [diff] [blame] | 80 | static const int kInstanceIDByteLength = 8; |
| 81 | |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 82 | // Creator. Should only be used by InstanceIDDriver::GetInstanceID. |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 83 | // |app_id|: identifies the application that uses the Instance ID. |
johnme | 54a3e148 | 2016-03-11 19:13:22 | [diff] [blame] | 84 | // |handler|: provides the GCM functionality needed to support Instance ID. |
johnme | 2f8daf9 | 2016-04-15 18:17:44 | [diff] [blame] | 85 | // Must outlive this class. On Android, this can be null instead. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 86 | static std::unique_ptr<InstanceID> CreateInternal(const std::string& app_id, |
| 87 | gcm::GCMDriver* gcm_driver); |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 88 | |
| 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. |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 96 | virtual void GetID(const GetIDCallback& callback) = 0; |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 97 | |
| 98 | // Returns the time when the InstanceID has been generated. |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 99 | virtual void GetCreationTime(const GetCreationTimeCallback& callback) = 0; |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 100 | |
jianli | 3c23264 | 2015-05-05 00:28:27 | [diff] [blame] | 101 | // 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. |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 106 | // |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 Knoll | 882414b8 | 2019-08-05 15:19:48 | [diff] [blame] | 111 | // |flags|: Flags used to create this token. |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 112 | // |callback|: to be called once the asynchronous operation is done. |
jianli | 3c23264 | 2015-05-05 00:28:27 | [diff] [blame] | 113 | virtual void GetToken(const std::string& authorized_entity, |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 114 | const std::string& scope, |
| 115 | const std::map<std::string, std::string>& options, |
Richard Knoll | 882414b8 | 2019-08-05 15:19:48 | [diff] [blame] | 116 | std::set<Flags> flags, |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 117 | GetTokenCallback callback) = 0; |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 118 | |
johnme | 6576ecf | 2017-04-03 19:26:28 | [diff] [blame] | 119 | // 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, |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 124 | ValidateTokenCallback callback) = 0; |
johnme | 6576ecf | 2017-04-03 19:26:28 | [diff] [blame] | 125 | |
johnme | a00cc8ae | 2016-06-02 13:58:04 | [diff] [blame] | 126 | // 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. |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 130 | // |callback|: to be called once the asynchronous operation is done. |
Alex Chau | 266f677 | 2019-07-25 20:43:02 | [diff] [blame] | 131 | virtual void GetEncryptionInfo(const std::string& authorized_entity, |
| 132 | GetEncryptionInfoCallback callback); |
johnme | a00cc8ae | 2016-06-02 13:58:04 | [diff] [blame] | 133 | |
| 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 Chau | 81fe8c31 | 2019-07-15 12:09:25 | [diff] [blame] | 138 | virtual void DeleteToken(const std::string& authorized_entity, |
| 139 | const std::string& scope, |
| 140 | DeleteTokenCallback callback); |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 141 | |
| 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. |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 145 | void DeleteID(DeleteIDCallback callback); |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 146 | |
| 147 | std::string app_id() const { return app_id_; } |
| 148 | |
| 149 | protected: |
johnme | a00cc8ae | 2016-06-02 13:58:04 | [diff] [blame] | 150 | 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, |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 155 | DeleteTokenCallback callback) = 0; |
| 156 | virtual void DeleteIDImpl(DeleteIDCallback callback) = 0; |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 157 | |
| 158 | void NotifyTokenRefresh(bool update_id); |
| 159 | |
johnme | a00cc8ae | 2016-06-02 13:58:04 | [diff] [blame] | 160 | gcm::GCMDriver* gcm_driver() { return gcm_driver_; } |
| 161 | |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 162 | private: |
johnme | a00cc8ae | 2016-06-02 13:58:04 | [diff] [blame] | 163 | void DidDelete(const std::string& authorized_entity, |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 164 | base::OnceCallback<void(Result result)> callback, |
johnme | a00cc8ae | 2016-06-02 13:58:04 | [diff] [blame] | 165 | Result result); |
| 166 | |
| 167 | // Owned by GCMProfileServiceFactory, which is a dependency of |
| 168 | // InstanceIDProfileServiceFactory, which owns this. |
| 169 | gcm::GCMDriver* gcm_driver_; |
| 170 | |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 171 | std::string app_id_; |
| 172 | TokenRefreshCallback token_refresh_callback_; |
| 173 | |
Jeremy Roman | 5c341f6d | 2019-07-15 15:56:10 | [diff] [blame] | 174 | base::WeakPtrFactory<InstanceID> weak_ptr_factory_{this}; |
johnme | a00cc8ae | 2016-06-02 13:58:04 | [diff] [blame] | 175 | |
jianli | 4089003 | 2015-04-29 21:55:34 | [diff] [blame] | 176 | DISALLOW_COPY_AND_ASSIGN(InstanceID); |
| 177 | }; |
| 178 | |
| 179 | } // namespace instance_id |
| 180 | |
| 181 | #endif // COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_H_ |