Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 1 | // Copyright 2019 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 CHROME_BROWSER_SHARING_SHARING_MESSAGE_SENDER_H_ |
| 6 | #define CHROME_BROWSER_SHARING_SHARING_MESSAGE_SENDER_H_ |
| 7 | |
| 8 | #include <map> |
Richard Knoll | 367f223 | 2019-11-14 18:11:34 | [diff] [blame] | 9 | #include <memory> |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 10 | #include <string> |
| 11 | |
| 12 | #include "base/callback.h" |
| 13 | #include "base/macros.h" |
| 14 | #include "base/memory/weak_ptr.h" |
| 15 | #include "base/optional.h" |
| 16 | #include "base/time/time.h" |
| 17 | |
| 18 | namespace chrome_browser_sharing { |
| 19 | enum MessageType : int; |
| 20 | class ResponseMessage; |
| 21 | class SharingMessage; |
| 22 | } // namespace chrome_browser_sharing |
| 23 | |
| 24 | namespace syncer { |
Michael van Ouwerkerk | 01bcfbd | 2019-12-10 12:01:02 | [diff] [blame^] | 25 | class DeviceInfo; |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 26 | class LocalDeviceInfoProvider; |
| 27 | } // namespace syncer |
| 28 | |
| 29 | class SharingFCMSender; |
| 30 | class SharingSyncPreference; |
Himanshu Jaju | 453c8bc | 2019-12-05 19:31:21 | [diff] [blame] | 31 | enum class SharingDevicePlatform; |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 32 | enum class SharingSendMessageResult; |
| 33 | |
| 34 | class SharingMessageSender { |
| 35 | public: |
| 36 | using ResponseCallback = base::OnceCallback<void( |
| 37 | SharingSendMessageResult, |
| 38 | std::unique_ptr<chrome_browser_sharing::ResponseMessage>)>; |
| 39 | |
| 40 | SharingMessageSender( |
Richard Knoll | 367f223 | 2019-11-14 18:11:34 | [diff] [blame] | 41 | std::unique_ptr<SharingFCMSender> sharing_fcm_sender, |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 42 | SharingSyncPreference* sync_prefs, |
| 43 | syncer::LocalDeviceInfoProvider* local_device_info_provider); |
| 44 | virtual ~SharingMessageSender(); |
| 45 | |
| 46 | virtual void SendMessageToDevice( |
Michael van Ouwerkerk | 01bcfbd | 2019-12-10 12:01:02 | [diff] [blame^] | 47 | const syncer::DeviceInfo& device, |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 48 | base::TimeDelta response_timeout, |
| 49 | chrome_browser_sharing::SharingMessage message, |
| 50 | ResponseCallback callback); |
| 51 | |
| 52 | virtual void OnAckReceived( |
| 53 | chrome_browser_sharing::MessageType message_type, |
| 54 | const std::string& message_id, |
| 55 | std::unique_ptr<chrome_browser_sharing::ResponseMessage> response); |
| 56 | |
| 57 | private: |
| 58 | void OnMessageSent(base::TimeTicks start_time, |
| 59 | const std::string& message_guid, |
| 60 | chrome_browser_sharing::MessageType message_type, |
| 61 | SharingSendMessageResult result, |
| 62 | base::Optional<std::string> message_id); |
| 63 | |
| 64 | void InvokeSendMessageCallback( |
| 65 | const std::string& message_guid, |
| 66 | chrome_browser_sharing::MessageType message_type, |
| 67 | SharingSendMessageResult result, |
| 68 | std::unique_ptr<chrome_browser_sharing::ResponseMessage> response); |
| 69 | |
Richard Knoll | 367f223 | 2019-11-14 18:11:34 | [diff] [blame] | 70 | std::unique_ptr<SharingFCMSender> fcm_sender_; |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 71 | SharingSyncPreference* sync_prefs_; |
| 72 | syncer::LocalDeviceInfoProvider* local_device_info_provider_; |
| 73 | |
| 74 | // Map of random GUID to SendMessageCallback. |
| 75 | std::map<std::string, ResponseCallback> send_message_callbacks_; |
| 76 | // Map of FCM message_id to time at start of send message request to FCM. |
| 77 | std::map<std::string, base::TimeTicks> send_message_times_; |
| 78 | // Map of FCM message_id to random GUID. |
| 79 | std::map<std::string, std::string> message_guids_; |
Himanshu Jaju | 453c8bc | 2019-12-05 19:31:21 | [diff] [blame] | 80 | // Map of random message guid to platform of receiver device for metrics. |
| 81 | std::map<std::string, SharingDevicePlatform> receiver_device_platform_; |
Himanshu Jaju | a69dbe5 | 2019-11-04 19:58:05 | [diff] [blame] | 82 | |
| 83 | base::WeakPtrFactory<SharingMessageSender> weak_ptr_factory_{this}; |
| 84 | |
| 85 | DISALLOW_COPY_AND_ASSIGN(SharingMessageSender); |
| 86 | }; |
| 87 | |
| 88 | #endif // CHROME_BROWSER_SHARING_SHARING_MESSAGE_SENDER_H_ |