1 // Copyright 2020 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_NEARBY_SHARING_NEARBY_RECEIVE_MANAGER_H_
6 #define CHROME_BROWSER_NEARBY_SHARING_NEARBY_RECEIVE_MANAGER_H_
7 
8 #include "base/unguessable_token.h"
9 #include "chrome/browser/nearby_sharing/attachment.h"
10 #include "chrome/browser/nearby_sharing/nearby_sharing_service.h"
11 #include "chrome/browser/nearby_sharing/transfer_update_callback.h"
12 #include "chrome/browser/ui/webui/nearby_share/nearby_share.mojom.h"
13 #include "mojo/public/cpp/bindings/remote.h"
14 
15 // |NearbyReceiveManager| is a mojo implementation that is bound in os-settings
16 // to allow the user to enter high-visibility advertising and accept incoming
17 // connections. Entering high visibility mode causes this object to register
18 // itself as a foreground receive surface with the |NearbySharingService| and
19 // listen for incoming share attempts. The client can register an observer to
20 // receive notifications when high visibility mode state has changed or when a
21 // share request has come in. The client can then accept or reject the share.
22 // This is a transient object and only lives while os-settings has it bound.
23 class NearbyReceiveManager : public nearby_share::mojom::ReceiveManager,
24                              public TransferUpdateCallback,
25                              public NearbySharingService::Observer {
26  public:
27   explicit NearbyReceiveManager(NearbySharingService* nearby_sharing_service);
28   ~NearbyReceiveManager() override;
29 
30   // TransferUpdateCallback:
31   void OnTransferUpdate(const ShareTarget& share_target,
32                         const TransferMetadata& transfer_metadata) override;
33 
34   // nearby_share::mojom::ReceiveManager:
35   void AddReceiveObserver(
36       ::mojo::PendingRemote<nearby_share::mojom::ReceiveObserver> observer)
37       override;
38   void IsInHighVisibility(IsInHighVisibilityCallback callback) override;
39   void RegisterForegroundReceiveSurface(
40       RegisterForegroundReceiveSurfaceCallback callback) override;
41   void UnregisterForegroundReceiveSurface(
42       UnregisterForegroundReceiveSurfaceCallback callback) override;
43   void Accept(const base::UnguessableToken& share_target_id,
44               AcceptCallback callback) override;
45   void Reject(const base::UnguessableToken& share_target_id,
46               RejectCallback callback) override;
47 
48   // NearbySharingService::Observer
49   void OnHighVisibilityChanged(bool in_high_visibility) override;
OnShutdown()50   void OnShutdown() override {}
51 
52  private:
53   void NotifyOnTransferUpdate(const ShareTarget& share_target,
54                               const TransferMetadata& metadata);
55 
56   NearbySharingService* nearby_sharing_service_;
57 
58   base::flat_map<base::UnguessableToken, ShareTarget> share_targets_map_;
59   mojo::RemoteSet<nearby_share::mojom::ReceiveObserver> observers_set_;
60 };
61 
62 #endif  // CHROME_BROWSER_NEARBY_SHARING_NEARBY_RECEIVE_MANAGER_H_
63