blob: 2886a4d4c68540b94ffb0e6ced0473771b3af102 [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"
13#include "base/macros.h"
14#include "base/memory/weak_ptr.h"
15#include "base/optional.h"
16#include "base/time/time.h"
17
18namespace chrome_browser_sharing {
19enum MessageType : int;
20class ResponseMessage;
21class SharingMessage;
22} // namespace chrome_browser_sharing
23
24namespace syncer {
Michael van Ouwerkerk01bcfbd2019-12-10 12:01:0225class DeviceInfo;
Himanshu Jajua69dbe52019-11-04 19:58:0526class LocalDeviceInfoProvider;
27} // namespace syncer
28
29class SharingFCMSender;
30class SharingSyncPreference;
Himanshu Jaju453c8bc2019-12-05 19:31:2131enum class SharingDevicePlatform;
Himanshu Jajua69dbe52019-11-04 19:58:0532enum class SharingSendMessageResult;
33
34class SharingMessageSender {
35 public:
36 using ResponseCallback = base::OnceCallback<void(
37 SharingSendMessageResult,
38 std::unique_ptr<chrome_browser_sharing::ResponseMessage>)>;
39
40 SharingMessageSender(
Richard Knoll367f2232019-11-14 18:11:3441 std::unique_ptr<SharingFCMSender> sharing_fcm_sender,
Himanshu Jajua69dbe52019-11-04 19:58:0542 SharingSyncPreference* sync_prefs,
43 syncer::LocalDeviceInfoProvider* local_device_info_provider);
44 virtual ~SharingMessageSender();
45
46 virtual void SendMessageToDevice(
Michael van Ouwerkerk01bcfbd2019-12-10 12:01:0247 const syncer::DeviceInfo& device,
Himanshu Jajua69dbe52019-11-04 19:58:0548 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,
Himanshu Jaju12a74df2020-01-03 11:26:0261 SharingDevicePlatform receiver_device_platform,
Himanshu Jajua69dbe52019-11-04 19:58:0562 SharingSendMessageResult result,
63 base::Optional<std::string> message_id);
64
65 void InvokeSendMessageCallback(
66 const std::string& message_guid,
67 chrome_browser_sharing::MessageType message_type,
Himanshu Jaju12a74df2020-01-03 11:26:0268 SharingDevicePlatform receiver_device_platform,
Himanshu Jajua69dbe52019-11-04 19:58:0569 SharingSendMessageResult result,
70 std::unique_ptr<chrome_browser_sharing::ResponseMessage> response);
71
Richard Knoll367f2232019-11-14 18:11:3472 std::unique_ptr<SharingFCMSender> fcm_sender_;
Himanshu Jajua69dbe52019-11-04 19:58:0573 SharingSyncPreference* sync_prefs_;
74 syncer::LocalDeviceInfoProvider* local_device_info_provider_;
75
76 // Map of random GUID to SendMessageCallback.
77 std::map<std::string, ResponseCallback> send_message_callbacks_;
78 // Map of FCM message_id to time at start of send message request to FCM.
79 std::map<std::string, base::TimeTicks> send_message_times_;
80 // Map of FCM message_id to random GUID.
81 std::map<std::string, std::string> message_guids_;
Himanshu Jaju12a74df2020-01-03 11:26:0282 // Map of FCM message_id to platform of receiver device for metrics.
Himanshu Jaju453c8bc2019-12-05 19:31:2183 std::map<std::string, SharingDevicePlatform> receiver_device_platform_;
Himanshu Jajua69dbe52019-11-04 19:58:0584
85 base::WeakPtrFactory<SharingMessageSender> weak_ptr_factory_{this};
86
87 DISALLOW_COPY_AND_ASSIGN(SharingMessageSender);
88};
89
90#endif // CHROME_BROWSER_SHARING_SHARING_MESSAGE_SENDER_H_