File: sharing_fcm_sender.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (180 lines) | stat: -rw-r--r-- 7,443 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SHARING_MESSAGE_SHARING_FCM_SENDER_H_
#define COMPONENTS_SHARING_MESSAGE_SHARING_FCM_SENDER_H_

#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "components/sharing_message/proto/sharing_message.pb.h"
#include "components/sharing_message/sharing_message_sender.h"
#include "components/sharing_message/sharing_send_message_result.h"
#include "components/sync/model/syncable_service.h"
#include "components/sync/protocol/unencrypted_sharing_message.pb.h"
#include "components/sync/service/sync_service_observer.h"
#include "components/sync_device_info/device_info.h"

namespace gcm {
class GCMDriver;
enum class GCMEncryptionResult;
}  // namespace gcm

namespace syncer {
class DeviceInfoTracker;
class LocalDeviceInfoProvider;
class SyncService;
}  // namespace syncer

namespace sync_pb {
class SharingMessageCommitError;
}

enum class SharingChannelType;
class SharingMessageBridge;
class SharingSyncPreference;

// Responsible for sending FCM messages within Sharing infrastructure.
class SharingFCMSender : public SharingMessageSender::SendMessageDelegate,
                         public syncer::SyncServiceObserver {
 public:
  using SharingMessage = components_sharing_message::SharingMessage;
  using SendMessageCallback =
      base::OnceCallback<void(SharingSendMessageResult result,
                              std::optional<std::string> message_id,
                              SharingChannelType channel_type)>;

  SharingFCMSender(
      SharingMessageBridge* sharing_message_bridge,
      SharingSyncPreference* sync_preference,
      gcm::GCMDriver* gcm_driver,
      const syncer::DeviceInfoTracker* device_info_tracker,
      const syncer::LocalDeviceInfoProvider* local_device_info_provider,
      syncer::SyncService* sync_service,
      syncer::SyncableService::StartSyncFlare start_sync_flare);
  SharingFCMSender(const SharingFCMSender&) = delete;
  SharingFCMSender& operator=(const SharingFCMSender&) = delete;
  ~SharingFCMSender() override;

  // Sends a |message| to device identified by |fcm_configuration|, which
  // expires after |time_to_live| seconds. |callback| will be invoked with
  // message_id if asynchronous operation succeeded, or std::nullopt if
  // operation failed.
  virtual void SendMessageToFcmTarget(
      const components_sharing_message::FCMChannelConfiguration&
          fcm_configuration,
      base::TimeDelta time_to_live,
      SharingMessage message,
      SendMessageCallback callback);

  // Sends a |message| to device identified by |server_channel|, |callback| will
  // be invoked with message_id if asynchronous operation succeeded, or
  // std::nullopt if operation failed.
  virtual void SendMessageToServerTarget(
      const components_sharing_message::ServerChannelConfiguration&
          server_channel,
      SharingMessage message,
      SendMessageCallback callback);

  // Removes any pending messages that are waiting for the sync service to
  // initialize. This must be called if the device is unregistered.
  void ClearPendingMessages() override;

  // syncer::SyncServiceObserver overrides.
  void OnStateChanged(syncer::SyncService* sync_service) override;
  void OnSyncShutdown(syncer::SyncService* sync_service) override;

  // Used to inject fake SharingMessageBridge in integration tests.
  void SetSharingMessageBridgeForTesting(
      SharingMessageBridge* sharing_message_bridge);

 protected:
  // SharingMessageSender::SendMessageDelegate:
  void DoSendMessageToDevice(const SharingTargetDeviceInfo& device,
                             base::TimeDelta time_to_live,
                             SharingMessage message,
                             SendMessageCallback callback) override;
  void DoSendUnencryptedMessageToDevice(
      const SharingTargetDeviceInfo& device,
      sync_pb::UnencryptedSharingMessage message,
      SendMessageCallback callback) override;

 private:
  using MessageSender = base::OnceCallback<void(std::string message,
                                                SendMessageCallback callback)>;

  void EncryptMessage(const std::string& authorized_entity,
                      const std::string& p256dh,
                      const std::string& auth_secret,
                      const SharingMessage& message,
                      SharingChannelType channel_type,
                      SendMessageCallback callback,
                      MessageSender message_sender);

  void OnMessageEncrypted(SharingChannelType channel_type,
                          SendMessageCallback callback,
                          MessageSender message_sender,
                          gcm::GCMEncryptionResult result,
                          std::string message);

  void DoSendMessageToSenderIdTarget(const std::string& fcm_token,
                                     base::TimeDelta time_to_live,
                                     const std::string& message_id,
                                     std::string message,
                                     SendMessageCallback callback);

  void DoSendMessageToServerTarget(const std::string& server_channel,
                                   const std::string& message_id,
                                   std::string message,
                                   SendMessageCallback callback);

  void OnMessageSentViaSync(SendMessageCallback callback,
                            const std::string& message_id,
                            SharingChannelType channel_type,
                            const sync_pb::SharingMessageCommitError& error);

  bool SetMessageSenderInfo(SharingMessage* message);

  raw_ptr<SharingMessageBridge> sharing_message_bridge_;
  const raw_ptr<SharingSyncPreference> sync_preference_;
  const raw_ptr<gcm::GCMDriver, AcrossTasksDanglingUntriaged> gcm_driver_;
  const raw_ptr<const syncer::DeviceInfoTracker> device_info_tracker_;
  const raw_ptr<const syncer::LocalDeviceInfoProvider>
      local_device_info_provider_;
  const raw_ptr<syncer::SyncService> sync_service_;
  syncer::SyncableService::StartSyncFlare start_sync_flare_;
  base::ScopedObservation<syncer::SyncService, SharingFCMSender>
      sync_service_observation_{this};

  // Pending messages that are waiting for the sync service to initialize.
  struct PendingMessage {
    PendingMessage(
        components_sharing_message::FCMChannelConfiguration fcm_configuration,
        base::TimeDelta time_to_live,
        SharingMessage message,
        SendMessageCallback callback);
    PendingMessage(const PendingMessage& other) = delete;
    PendingMessage& operator=(const PendingMessage& other) = delete;
    PendingMessage(PendingMessage&& other);
    PendingMessage& operator=(PendingMessage&& other);
    ~PendingMessage();

    components_sharing_message::FCMChannelConfiguration fcm_configuration;
    base::TimeDelta time_to_live;
    SharingMessage message;
    SendMessageCallback callback;
  };
  std::vector<PendingMessage> pending_messages_;

  base::WeakPtrFactory<SharingFCMSender> weak_ptr_factory_{this};
};

#endif  // COMPONENTS_SHARING_MESSAGE_SHARING_FCM_SENDER_H_