Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Hira Mahmood | 0f3c35a8 | 2024-07-31 15:43:26 | [diff] [blame] | 5 | #ifndef COMPONENTS_SHARING_MESSAGE_SHARING_MESSAGE_SENDER_H_ |
| 6 | #define COMPONENTS_SHARING_MESSAGE_SHARING_MESSAGE_SENDER_H_ |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 7 | |
| 8 | #include <map> |
Richard Knoll | 367f223 | 2019-11-14 18:11:34 | [diff] [blame] | 9 | #include <memory> |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 10 | #include <optional> |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 11 | #include <string> |
| 12 | |
Avi Drissman | 9269d4ed | 2023-01-07 01:38:06 | [diff] [blame] | 13 | #include "base/functional/callback.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 14 | #include "base/memory/raw_ptr.h" |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
Hira Mahmood | 0f3c35a8 | 2024-07-31 15:43:26 | [diff] [blame] | 16 | #include "base/task/single_thread_task_runner.h" |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 17 | #include "base/time/time.h" |
Hira Mahmood | 0f3c35a8 | 2024-07-31 15:43:26 | [diff] [blame] | 18 | #include "components/sharing_message/sharing_target_device_info.h" |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 19 | |
Hira Mahmood | e16b75f0 | 2024-07-18 17:46:43 | [diff] [blame] | 20 | namespace components_sharing_message { |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 21 | class ResponseMessage; |
| 22 | class SharingMessage; |
Hira Mahmood | e16b75f0 | 2024-07-18 17:46:43 | [diff] [blame] | 23 | } // namespace components_sharing_message |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 24 | |
Hira Mahmood | 50818ce2 | 2024-08-08 19:39:51 | [diff] [blame] | 25 | namespace sharing_message { |
| 26 | enum MessageType : int; |
| 27 | } // namespace sharing_message |
| 28 | |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 29 | namespace syncer { |
| 30 | class LocalDeviceInfoProvider; |
| 31 | } // namespace syncer |
| 32 | |
Hira Mahmood | 50818ce2 | 2024-08-08 19:39:51 | [diff] [blame] | 33 | namespace sync_pb { |
| 34 | class UnencryptedSharingMessage; |
| 35 | } // namespace sync_pb |
| 36 | |
Alex Chau | 5bfcccb | 2020-02-04 18:41:07 | [diff] [blame] | 37 | enum class SharingChannelType; |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 38 | class SharingFCMSender; |
Himanshu Jaju | 453c8bc | 2019-12-05 19:31:21 | [diff] [blame] | 39 | enum class SharingDevicePlatform; |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 40 | enum class SharingSendMessageResult; |
| 41 | |
| 42 | class SharingMessageSender { |
| 43 | public: |
| 44 | using ResponseCallback = base::OnceCallback<void( |
| 45 | SharingSendMessageResult, |
Hira Mahmood | e16b75f0 | 2024-07-18 17:46:43 | [diff] [blame] | 46 | std::unique_ptr<components_sharing_message::ResponseMessage>)>; |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 47 | |
Richard Knoll | d17e406 | 2020-01-14 21:23:14 | [diff] [blame] | 48 | // Delegate class used to swap the actual message sending implementation. |
| 49 | class SendMessageDelegate { |
| 50 | public: |
| 51 | using SendMessageCallback = |
Alex Chau | 5bfcccb | 2020-02-04 18:41:07 | [diff] [blame] | 52 | base::OnceCallback<void(SharingSendMessageResult result, |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 53 | std::optional<std::string> message_id, |
Alex Chau | 5bfcccb | 2020-02-04 18:41:07 | [diff] [blame] | 54 | SharingChannelType channel_type)>; |
Richard Knoll | d17e406 | 2020-01-14 21:23:14 | [diff] [blame] | 55 | virtual ~SendMessageDelegate() = default; |
| 56 | |
| 57 | virtual void DoSendMessageToDevice( |
Mikel Astiz | 3809676 | 2024-01-05 16:23:18 | [diff] [blame] | 58 | const SharingTargetDeviceInfo& device, |
Richard Knoll | d17e406 | 2020-01-14 21:23:14 | [diff] [blame] | 59 | base::TimeDelta time_to_live, |
Hira Mahmood | e16b75f0 | 2024-07-18 17:46:43 | [diff] [blame] | 60 | components_sharing_message::SharingMessage message, |
Richard Knoll | d17e406 | 2020-01-14 21:23:14 | [diff] [blame] | 61 | SendMessageCallback callback) = 0; |
Hira Mahmood | 50818ce2 | 2024-08-08 19:39:51 | [diff] [blame] | 62 | |
| 63 | virtual void DoSendUnencryptedMessageToDevice( |
| 64 | const SharingTargetDeviceInfo& device, |
| 65 | sync_pb::UnencryptedSharingMessage message, |
| 66 | SendMessageCallback callback) = 0; |
Rushan Suleymanov | 3eae9f54 | 2024-12-10 21:18:32 | [diff] [blame] | 67 | |
| 68 | virtual void ClearPendingMessages() = 0; |
Richard Knoll | d17e406 | 2020-01-14 21:23:14 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | // Delegate type used to send a message. |
Hira Mahmood | 50818ce2 | 2024-08-08 19:39:51 | [diff] [blame] | 72 | enum class DelegateType { kFCM, kWebRtc, kIOSPush }; |
Richard Knoll | d17e406 | 2020-01-14 21:23:14 | [diff] [blame] | 73 | |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 74 | SharingMessageSender( |
Hira Mahmood | 0f3c35a8 | 2024-07-31 15:43:26 | [diff] [blame] | 75 | syncer::LocalDeviceInfoProvider* local_device_info_provider, |
| 76 | scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
Richard Knoll | d17e406 | 2020-01-14 21:23:14 | [diff] [blame] | 77 | SharingMessageSender(const SharingMessageSender&) = delete; |
| 78 | SharingMessageSender& operator=(const SharingMessageSender&) = delete; |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 79 | virtual ~SharingMessageSender(); |
| 80 | |
Yi Gu | 1211d82 | 2021-03-23 16:21:30 | [diff] [blame] | 81 | virtual base::OnceClosure SendMessageToDevice( |
Mikel Astiz | 3809676 | 2024-01-05 16:23:18 | [diff] [blame] | 82 | const SharingTargetDeviceInfo& device, |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 83 | base::TimeDelta response_timeout, |
Hira Mahmood | e16b75f0 | 2024-07-18 17:46:43 | [diff] [blame] | 84 | components_sharing_message::SharingMessage message, |
Richard Knoll | d17e406 | 2020-01-14 21:23:14 | [diff] [blame] | 85 | DelegateType delegate_type, |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 86 | ResponseCallback callback); |
| 87 | |
Hira Mahmood | 50818ce2 | 2024-08-08 19:39:51 | [diff] [blame] | 88 | virtual base::OnceClosure SendUnencryptedMessageToDevice( |
| 89 | const SharingTargetDeviceInfo& device, |
| 90 | sync_pb::UnencryptedSharingMessage message, |
| 91 | DelegateType delegate_type, |
| 92 | ResponseCallback callback); |
| 93 | |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 94 | virtual void OnAckReceived( |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 95 | const std::string& message_id, |
Hira Mahmood | e16b75f0 | 2024-07-18 17:46:43 | [diff] [blame] | 96 | std::unique_ptr<components_sharing_message::ResponseMessage> response); |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 97 | |
Richard Knoll | d17e406 | 2020-01-14 21:23:14 | [diff] [blame] | 98 | // Registers the given |delegate| to send messages when SendMessageToDevice is |
| 99 | // called with |type|. |
| 100 | void RegisterSendDelegate(DelegateType type, |
| 101 | std::unique_ptr<SendMessageDelegate> delegate); |
| 102 | |
Rushan Suleymanov | 3eae9f54 | 2024-12-10 21:18:32 | [diff] [blame] | 103 | // Clears all pending messages for all delegates. |
| 104 | void ClearPendingMessages(); |
| 105 | |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 106 | // Returns SharingFCMSender for testing. |
| 107 | SharingFCMSender* GetFCMSenderForTesting() const; |
| 108 | |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 109 | private: |
Richard Knoll | ae2f49f | 2020-01-15 18:18:53 | [diff] [blame] | 110 | struct SentMessageMetadata { |
| 111 | SentMessageMetadata(ResponseCallback callback, |
| 112 | base::TimeTicks timestamp, |
Hira Mahmood | 50818ce2 | 2024-08-08 19:39:51 | [diff] [blame] | 113 | sharing_message::MessageType type, |
Richard Knoll | ae2f49f | 2020-01-15 18:18:53 | [diff] [blame] | 114 | SharingDevicePlatform receiver_device_platform, |
Alex Chau | 5bfcccb | 2020-02-04 18:41:07 | [diff] [blame] | 115 | int trace_id, |
Michael van Ouwerkerk | a7bd20d | 2020-02-05 18:53:37 | [diff] [blame] | 116 | SharingChannelType channel_type, |
| 117 | base::TimeDelta receiver_pulse_interval); |
Richard Knoll | ae2f49f | 2020-01-15 18:18:53 | [diff] [blame] | 118 | SentMessageMetadata(SentMessageMetadata&& other); |
| 119 | SentMessageMetadata& operator=(SentMessageMetadata&& other); |
| 120 | ~SentMessageMetadata(); |
| 121 | |
| 122 | ResponseCallback callback; |
| 123 | base::TimeTicks timestamp; |
Hira Mahmood | 50818ce2 | 2024-08-08 19:39:51 | [diff] [blame] | 124 | sharing_message::MessageType type; |
Richard Knoll | ae2f49f | 2020-01-15 18:18:53 | [diff] [blame] | 125 | SharingDevicePlatform receiver_device_platform; |
Michael van Ouwerkerk | ce7d4ed | 2020-01-24 18:15:11 | [diff] [blame] | 126 | int trace_id; |
Alex Chau | 5bfcccb | 2020-02-04 18:41:07 | [diff] [blame] | 127 | SharingChannelType channel_type; |
Michael van Ouwerkerk | a7bd20d | 2020-02-05 18:53:37 | [diff] [blame] | 128 | base::TimeDelta receiver_pulse_interval; |
Richard Knoll | ae2f49f | 2020-01-15 18:18:53 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | void OnMessageSent(const std::string& message_guid, |
Hira Mahmood | 44a3d73f | 2024-09-16 15:33:08 | [diff] [blame] | 132 | sharing_message::MessageType message_type, |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 133 | SharingSendMessageResult result, |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 134 | std::optional<std::string> message_id, |
Alex Chau | 5bfcccb | 2020-02-04 18:41:07 | [diff] [blame] | 135 | SharingChannelType channel_type); |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 136 | |
| 137 | void InvokeSendMessageCallback( |
| 138 | const std::string& message_guid, |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 139 | SharingSendMessageResult result, |
Hira Mahmood | e16b75f0 | 2024-07-18 17:46:43 | [diff] [blame] | 140 | std::unique_ptr<components_sharing_message::ResponseMessage> response); |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 141 | |
Hira Mahmood | 50818ce2 | 2024-08-08 19:39:51 | [diff] [blame] | 142 | SendMessageDelegate* MaybeGetSendMessageDelegate( |
| 143 | const SharingTargetDeviceInfo& device, |
| 144 | sharing_message::MessageType message_type, |
| 145 | int trace_id, |
| 146 | const std::string& message_guid, |
| 147 | DelegateType delegate_type); |
| 148 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 149 | raw_ptr<syncer::LocalDeviceInfoProvider> local_device_info_provider_; |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 150 | |
Hira Mahmood | 0f3c35a8 | 2024-07-31 15:43:26 | [diff] [blame] | 151 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 152 | |
Richard Knoll | ae2f49f | 2020-01-15 18:18:53 | [diff] [blame] | 153 | // Map of random GUID to SentMessageMetadata. |
| 154 | std::map<std::string, SentMessageMetadata> message_metadata_; |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 155 | // Map of FCM message_id to random GUID. |
| 156 | std::map<std::string, std::string> message_guids_; |
Richard Knoll | d3d2d1db | 2020-01-15 19:34:30 | [diff] [blame] | 157 | // Map of FCM message_id to received ACK response messages. |
| 158 | std::map<std::string, |
Hira Mahmood | e16b75f0 | 2024-07-18 17:46:43 | [diff] [blame] | 159 | std::unique_ptr<components_sharing_message::ResponseMessage>> |
Richard Knoll | d3d2d1db | 2020-01-15 19:34:30 | [diff] [blame] | 160 | cached_ack_response_messages_; |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 161 | |
Richard Knoll | d17e406 | 2020-01-14 21:23:14 | [diff] [blame] | 162 | // Registered delegates to send messages. |
| 163 | std::map<DelegateType, std::unique_ptr<SendMessageDelegate>> send_delegates_; |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 164 | |
Richard Knoll | d17e406 | 2020-01-14 21:23:14 | [diff] [blame] | 165 | base::WeakPtrFactory<SharingMessageSender> weak_ptr_factory_{this}; |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 166 | }; |
| 167 | |
Hira Mahmood | 0f3c35a8 | 2024-07-31 15:43:26 | [diff] [blame] | 168 | #endif // COMPONENTS_SHARING_MESSAGE_SHARING_MESSAGE_SENDER_H_ |