blob: 0d5b76918b1c3a60f55f75449c7e6fdf39b17109 [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,
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 Knoll367f2232019-11-14 18:11:3470 std::unique_ptr<SharingFCMSender> fcm_sender_;
Himanshu Jajua69dbe52019-11-04 19:58:0571 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 Jaju453c8bc2019-12-05 19:31:2180 // Map of random message guid to platform of receiver device for metrics.
81 std::map<std::string, SharingDevicePlatform> receiver_device_platform_;
Himanshu Jajua69dbe52019-11-04 19:58:0582
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_