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_LOCAL_DEVICE_DATA_NEARBY_SHARE_LOCAL_DEVICE_DATA_MANAGER_IMPL_H_
6 #define CHROME_BROWSER_NEARBY_SHARING_LOCAL_DEVICE_DATA_NEARBY_SHARE_LOCAL_DEVICE_DATA_MANAGER_IMPL_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/callback.h"
13 #include "base/optional.h"
14 #include "chrome/browser/nearby_sharing/local_device_data/nearby_share_local_device_data_manager.h"
15 #include "chrome/browser/nearby_sharing/proto/device_rpc.pb.h"
16 #include "chrome/browser/nearby_sharing/proto/rpc_resources.pb.h"
17 #include "chrome/browser/ui/webui/nearby_share/public/mojom/nearby_share_settings.mojom.h"
18 
19 class NearbyShareClientFactory;
20 class NearbyShareDeviceDataUpdater;
21 class NearbyShareScheduler;
22 class PrefService;
23 
24 // Implementation of NearbyShareLocalDeviceDataManager that persists device data
25 // in prefs. All RPC-related calls are guarded by a timeout, so callbacks are
26 // guaranteed to be invoked. In addition to supporting on-demand device-data
27 // downloads, this implementation schedules periodic downloads of device
28 // data--full name and icon URL--from the server.
29 class NearbyShareLocalDeviceDataManagerImpl
30     : public NearbyShareLocalDeviceDataManager {
31  public:
32   class Factory {
33    public:
34     static std::unique_ptr<NearbyShareLocalDeviceDataManager> Create(
35         PrefService* pref_service,
36         NearbyShareClientFactory* http_client_factory,
37         const std::string& default_device_name);
38     static void SetFactoryForTesting(Factory* test_factory);
39 
40    protected:
41     virtual ~Factory();
42     virtual std::unique_ptr<NearbyShareLocalDeviceDataManager> CreateInstance(
43         PrefService* pref_service,
44         NearbyShareClientFactory* http_client_factory,
45         const std::string& default_device_name) = 0;
46 
47    private:
48     static Factory* test_factory_;
49   };
50 
51   ~NearbyShareLocalDeviceDataManagerImpl() override;
52 
53  private:
54   NearbyShareLocalDeviceDataManagerImpl(
55       PrefService* pref_service,
56       NearbyShareClientFactory* http_client_factory,
57       const std::string& default_device_name);
58 
59   // NearbyShareLocalDeviceDataManager:
60   std::string GetId() override;
61   std::string GetDeviceName() const override;
62   base::Optional<std::string> GetFullName() const override;
63   base::Optional<std::string> GetIconUrl() const override;
64   nearby_share::mojom::DeviceNameValidationResult ValidateDeviceName(
65       const std::string& name) override;
66   nearby_share::mojom::DeviceNameValidationResult SetDeviceName(
67       const std::string& name) override;
68   void DownloadDeviceData() override;
69   void UploadContacts(std::vector<nearbyshare::proto::Contact> contacts,
70                       UploadCompleteCallback callback) override;
71   void UploadCertificates(
72       std::vector<nearbyshare::proto::PublicCertificate> certificates,
73       UploadCompleteCallback callback) override;
74   void OnStart() override;
75   void OnStop() override;
76 
77   void OnDownloadDeviceDataRequested();
78   void OnDownloadDeviceDataFinished(
79       const base::Optional<nearbyshare::proto::UpdateDeviceResponse>& response);
80   void OnUploadContactsFinished(
81       UploadCompleteCallback callback,
82       const base::Optional<nearbyshare::proto::UpdateDeviceResponse>& response);
83   void OnUploadCertificatesFinished(
84       UploadCompleteCallback callback,
85       const base::Optional<nearbyshare::proto::UpdateDeviceResponse>& response);
86   void HandleUpdateDeviceResponse(
87       const base::Optional<nearbyshare::proto::UpdateDeviceResponse>& response);
88 
89   PrefService* pref_service_ = nullptr;
90   std::unique_ptr<NearbyShareDeviceDataUpdater> device_data_updater_;
91   std::unique_ptr<NearbyShareScheduler> download_device_data_scheduler_;
92 };
93 
94 #endif  // CHROME_BROWSER_NEARBY_SHARING_LOCAL_DEVICE_DATA_NEARBY_SHARE_LOCAL_DEVICE_DATA_MANAGER_IMPL_H_
95