blob: cd835b38655183614681592474ad89ca95ae6bfe [file] [log] [blame]
Himanshu Jajua69dbe52019-11-04 19:58:051// 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 Knoll367f2232019-11-14 18:11:349#include <memory>
Himanshu Jajua69dbe52019-11-04 19:58:0510#include <string>
11
12#include "base/callback.h"
Himanshu Jajua69dbe52019-11-04 19:58:0513#include "base/memory/weak_ptr.h"
14#include "base/optional.h"
15#include "base/time/time.h"
16
17namespace chrome_browser_sharing {
18enum MessageType : int;
19class ResponseMessage;
20class SharingMessage;
21} // namespace chrome_browser_sharing
22
23namespace syncer {
Michael van Ouwerkerk01bcfbd2019-12-10 12:01:0224class DeviceInfo;
Himanshu Jajua69dbe52019-11-04 19:58:0525class LocalDeviceInfoProvider;
26} // namespace syncer
27
Himanshu Jajua69dbe52019-11-04 19:58:0528class SharingSyncPreference;
Himanshu Jaju453c8bc2019-12-05 19:31:2129enum class SharingDevicePlatform;
Himanshu Jajua69dbe52019-11-04 19:58:0530enum class SharingSendMessageResult;
31
32class SharingMessageSender {
33 public:
34 using ResponseCallback = base::OnceCallback<void(
35 SharingSendMessageResult,
36 std::unique_ptr<chrome_browser_sharing::ResponseMessage>)>;
37
Richard Knolld17e4062020-01-14 21:23:1438 // Delegate class used to swap the actual message sending implementation.
39 class SendMessageDelegate {
40 public:
41 using SendMessageCallback =
42 base::OnceCallback<void(SharingSendMessageResult,
43 base::Optional<std::string>)>;
44 virtual ~SendMessageDelegate() = default;
45
46 virtual void DoSendMessageToDevice(
47 const syncer::DeviceInfo& device,
48 base::TimeDelta time_to_live,
49 chrome_browser_sharing::SharingMessage message,
50 SendMessageCallback callback) = 0;
51 };
52
53 // Delegate type used to send a message.
54 enum class DelegateType {
55 kFCM,
56 };
57
Himanshu Jajua69dbe52019-11-04 19:58:0558 SharingMessageSender(
Himanshu Jajua69dbe52019-11-04 19:58:0559 SharingSyncPreference* sync_prefs,
60 syncer::LocalDeviceInfoProvider* local_device_info_provider);
Richard Knolld17e4062020-01-14 21:23:1461 SharingMessageSender(const SharingMessageSender&) = delete;
62 SharingMessageSender& operator=(const SharingMessageSender&) = delete;
Himanshu Jajua69dbe52019-11-04 19:58:0563 virtual ~SharingMessageSender();
64
65 virtual void SendMessageToDevice(
Michael van Ouwerkerk01bcfbd2019-12-10 12:01:0266 const syncer::DeviceInfo& device,
Himanshu Jajua69dbe52019-11-04 19:58:0567 base::TimeDelta response_timeout,
68 chrome_browser_sharing::SharingMessage message,
Richard Knolld17e4062020-01-14 21:23:1469 DelegateType delegate_type,
Himanshu Jajua69dbe52019-11-04 19:58:0570 ResponseCallback callback);
71
72 virtual void OnAckReceived(
73 chrome_browser_sharing::MessageType message_type,
74 const std::string& message_id,
75 std::unique_ptr<chrome_browser_sharing::ResponseMessage> response);
76
Richard Knolld17e4062020-01-14 21:23:1477 // Registers the given |delegate| to send messages when SendMessageToDevice is
78 // called with |type|.
79 void RegisterSendDelegate(DelegateType type,
80 std::unique_ptr<SendMessageDelegate> delegate);
81
Himanshu Jajua69dbe52019-11-04 19:58:0582 private:
83 void OnMessageSent(base::TimeTicks start_time,
84 const std::string& message_guid,
85 chrome_browser_sharing::MessageType message_type,
Himanshu Jaju12a74df2020-01-03 11:26:0286 SharingDevicePlatform receiver_device_platform,
Himanshu Jajua69dbe52019-11-04 19:58:0587 SharingSendMessageResult result,
88 base::Optional<std::string> message_id);
89
90 void InvokeSendMessageCallback(
91 const std::string& message_guid,
92 chrome_browser_sharing::MessageType message_type,
Himanshu Jaju12a74df2020-01-03 11:26:0293 SharingDevicePlatform receiver_device_platform,
Himanshu Jajua69dbe52019-11-04 19:58:0594 SharingSendMessageResult result,
95 std::unique_ptr<chrome_browser_sharing::ResponseMessage> response);
96
Himanshu Jajua69dbe52019-11-04 19:58:0597 SharingSyncPreference* sync_prefs_;
98 syncer::LocalDeviceInfoProvider* local_device_info_provider_;
99
100 // Map of random GUID to SendMessageCallback.
101 std::map<std::string, ResponseCallback> send_message_callbacks_;
102 // Map of FCM message_id to time at start of send message request to FCM.
103 std::map<std::string, base::TimeTicks> send_message_times_;
104 // Map of FCM message_id to random GUID.
105 std::map<std::string, std::string> message_guids_;
Himanshu Jaju12a74df2020-01-03 11:26:02106 // Map of FCM message_id to platform of receiver device for metrics.
Himanshu Jaju453c8bc2019-12-05 19:31:21107 std::map<std::string, SharingDevicePlatform> receiver_device_platform_;
Himanshu Jajua69dbe52019-11-04 19:58:05108
Richard Knolld17e4062020-01-14 21:23:14109 // Registered delegates to send messages.
110 std::map<DelegateType, std::unique_ptr<SendMessageDelegate>> send_delegates_;
Himanshu Jajua69dbe52019-11-04 19:58:05111
Richard Knolld17e4062020-01-14 21:23:14112 base::WeakPtrFactory<SharingMessageSender> weak_ptr_factory_{this};
Himanshu Jajua69dbe52019-11-04 19:58:05113};
114
115#endif // CHROME_BROWSER_SHARING_SHARING_MESSAGE_SENDER_H_