File: wifi_configuration_bridge.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (184 lines) | stat: -rw-r--r-- 7,786 bytes parent folder | download | duplicates (5)
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
181
182
183
184
// 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 CHROMEOS_ASH_COMPONENTS_SYNC_WIFI_WIFI_CONFIGURATION_BRIDGE_H_
#define CHROMEOS_ASH_COMPONENTS_SYNC_WIFI_WIFI_CONFIGURATION_BRIDGE_H_

#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h"
#include "chromeos/ash/components/network/network_configuration_observer.h"
#include "chromeos/ash/components/network/network_metadata_observer.h"
#include "chromeos/ash/components/sync_wifi/network_identifier.h"
#include "components/sync/base/data_type.h"
#include "components/sync/model/data_type_store.h"
#include "components/sync/model/data_type_sync_bridge.h"

class PrefRegistrySimple;
class PrefService;

namespace syncer {
class DataTypeLocalChangeProcessor;
}  // namespace syncer

namespace ash::timer_factory {
class TimerFactory;
}  // namespace ash::timer_factory

namespace ash {

class NetworkConfigurationHandler;
class NetworkMetadataStore;

namespace sync_wifi {

const char kIsFirstRun[] = "sync_wifi.is_first_run";
const char kHasFixedAutoconnect[] = "sync_wifi.has_fixed_autoconnect";

class LocalNetworkCollector;
class SyncedNetworkMetricsLogger;
class SyncedNetworkUpdater;

// Receives updates to network configurations from the Chrome sync back end and
// from the system network stack and keeps both lists in sync.
class WifiConfigurationBridge : public syncer::DataTypeSyncBridge,
                                public NetworkConfigurationObserver,
                                public NetworkMetadataObserver {
 public:
  WifiConfigurationBridge(
      SyncedNetworkUpdater* synced_network_updater,
      LocalNetworkCollector* local_network_collector,
      NetworkConfigurationHandler* network_configuration_handler,
      SyncedNetworkMetricsLogger* metrics_recorder,
      ash::timer_factory::TimerFactory* timer_factory,
      PrefService* pref_service,
      std::unique_ptr<syncer::DataTypeLocalChangeProcessor> change_processor,
      syncer::OnceDataTypeStoreFactory create_store_callback);

  WifiConfigurationBridge(const WifiConfigurationBridge&) = delete;
  WifiConfigurationBridge& operator=(const WifiConfigurationBridge&) = delete;

  ~WifiConfigurationBridge() override;

  static void RegisterPrefs(PrefRegistrySimple* registry);

  // syncer::DataTypeSyncBridge:
  std::unique_ptr<syncer::MetadataChangeList> CreateMetadataChangeList()
      override;
  std::optional<syncer::ModelError> MergeFullSyncData(
      std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
      syncer::EntityChangeList entity_data) override;
  std::optional<syncer::ModelError> ApplyIncrementalSyncChanges(
      std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
      syncer::EntityChangeList entity_changes) override;
  std::unique_ptr<syncer::DataBatch> GetDataForCommit(
      StorageKeyList storage_keys) override;
  std::unique_ptr<syncer::DataBatch> GetAllDataForDebugging() override;
  std::string GetClientTag(
      const syncer::EntityData& entity_data) const override;
  std::string GetStorageKey(
      const syncer::EntityData& entity_data) const override;
  void ApplyDisableSyncChanges(std::unique_ptr<syncer::MetadataChangeList>
                                   delete_metadata_change_list) override;

  // NetworkMetadataObserver:
  void OnFirstConnectionToNetwork(const std::string& guid) override;
  void OnNetworkCreated(const std::string& guid) override;
  void OnNetworkUpdate(const std::string& guid,
                       const base::Value::Dict* set_properties) override;

  // NetworkConfigurationObserver::
  void OnBeforeConfigurationRemoved(const std::string& service_path,
                                    const std::string& guid) override;
  void OnConfigurationRemoved(const std::string& service_path,
                              const std::string& guid) override;
  void OnShuttingDown() override;

  // Comes from |entries_| the in-memory map.
  std::vector<NetworkIdentifier> GetAllIdsForTesting();

  void SetNetworkMetadataStore(
      base::WeakPtr<NetworkMetadataStore> network_metadata_store);

 private:
  void Commit(std::unique_ptr<syncer::DataTypeStore::WriteBatch> batch);

  // Callbacks for DataTypeStore.
  void OnStoreCreated(const std::optional<syncer::ModelError>& error,
                      std::unique_ptr<syncer::DataTypeStore> store);
  void OnReadAllData(
      const std::optional<syncer::ModelError>& error,
      std::unique_ptr<syncer::DataTypeStore::RecordList> records);
  void OnReadAllMetadata(const std::optional<syncer::ModelError>& error,
                         std::unique_ptr<syncer::MetadataBatch> metadata_batch);
  void OnCommit(const std::optional<syncer::ModelError>& error);

  void OnGetAllSyncableNetworksResult(
      std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
      syncer::EntityChangeList change_list,
      std::vector<sync_pb::WifiConfigurationSpecifics> local_network_list);

  void SaveNetworkToSync(
      std::optional<sync_pb::WifiConfigurationSpecifics> proto);
  void RemoveNetworkFromSync(const std::string& storage_key);

  // Starts an async request to serialize a network to a proto and save to sync.
  void OnNetworkConfiguredDelayComplete(const std::string& network_guid);

  bool IsLastUpdateFromSync(const std::string& network_guid);

  void FixAutoconnect();
  void OnFixAutoconnectComplete();

  // An in-memory list of the proto's that mirrors what is on the sync server.
  // This gets updated when changes are received from the server and after local
  // changes have been committed.  On initialization of this class, it is
  // populated with the contents of |store_|.
  base::flat_map<std::string, sync_pb::WifiConfigurationSpecifics> entries_;

  // Map of network |guid| to |storage_key|.  After a network is deleted, we
  // no longer have access to its metadata so this stores the necessary
  // information to delete it from sync.
  base::flat_map<std::string, std::string> pending_deletes_;

  // Holds on to timers that are started immediately after a network is
  // configured so we can wait until the first connection attempt is complete.
  base::flat_map<std::string, std::unique_ptr<base::OneShotTimer>>
      network_guid_to_timer_map_;

  // Map of storage_key to proto which tracks networks that should be synced
  // once the service is ready.  This is keyed on network_id to ensure that the
  // most recent change is kept if there are multiple changes to the same
  // network.
  base::flat_map<std::string,
                 std::optional<sync_pb::WifiConfigurationSpecifics>>
      networks_to_sync_when_ready_;

  // The on disk store of WifiConfigurationSpecifics protos that mirrors what
  // is on the sync server.  This gets updated when changes are received from
  // the server and after local changes have been committed to the server.
  std::unique_ptr<syncer::DataTypeStore> store_;

  raw_ptr<SyncedNetworkUpdater, DanglingUntriaged> synced_network_updater_;
  raw_ptr<LocalNetworkCollector, DanglingUntriaged> local_network_collector_;
  raw_ptr<NetworkConfigurationHandler> network_configuration_handler_;
  raw_ptr<SyncedNetworkMetricsLogger, DanglingUntriaged> metrics_recorder_;
  raw_ptr<ash::timer_factory::TimerFactory, DanglingUntriaged> timer_factory_;
  raw_ptr<PrefService, DanglingUntriaged> pref_service_;
  base::WeakPtr<NetworkMetadataStore> network_metadata_store_;

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

}  // namespace sync_wifi

}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_SYNC_WIFI_WIFI_CONFIGURATION_BRIDGE_H_