alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS 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 FEDERATED_STORAGE_MANAGER_IMPL_H_ |
| 6 | #define FEDERATED_STORAGE_MANAGER_IMPL_H_ |
| 7 | |
| 8 | #include "federated/storage_manager.h" |
| 9 | |
alanlxl | 9d26c1c | 2020-08-21 13:42:36 +1000 | [diff] [blame] | 10 | #include <memory> |
alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame] | 11 | #include <string> |
alanlxl | 9d26c1c | 2020-08-21 13:42:36 +1000 | [diff] [blame] | 12 | |
alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame] | 13 | #include <base/sequence_checker.h> |
| 14 | |
alanlxl | 9d26c1c | 2020-08-21 13:42:36 +1000 | [diff] [blame] | 15 | #include "federated/example_database.h" |
| 16 | #include "federated/session_manager_observer_interface.h" |
| 17 | #include "federated/session_manager_proxy.h" |
| 18 | |
alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame] | 19 | namespace federated { |
| 20 | |
alanlxl | 9d26c1c | 2020-08-21 13:42:36 +1000 | [diff] [blame] | 21 | class SessionManagerProxy; |
| 22 | |
| 23 | class StorageManagerImpl : public StorageManager, |
| 24 | public SessionManagerObserverInterface { |
alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame] | 25 | public: |
| 26 | StorageManagerImpl() = default; |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 27 | StorageManagerImpl(const StorageManagerImpl&) = delete; |
| 28 | StorageManagerImpl& operator=(const StorageManagerImpl&) = delete; |
| 29 | |
alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame] | 30 | ~StorageManagerImpl() override = default; |
| 31 | |
alanlxl | 9d26c1c | 2020-08-21 13:42:36 +1000 | [diff] [blame] | 32 | // StorageManager: |
| 33 | void InitializeSessionManagerProxy(dbus::Bus* bus) override; |
alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame] | 34 | bool OnExampleReceived(const std::string& client_name, |
| 35 | const std::string& serialized_example) override; |
alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame] | 36 | bool PrepareStreamingForClient(const std::string& client_name) override; |
alanlxl | 9d26c1c | 2020-08-21 13:42:36 +1000 | [diff] [blame] | 37 | bool GetNextExample(std::string* example, bool* end_of_iterator) override; |
| 38 | bool CloseStreaming(bool clean_examples) override; |
| 39 | |
| 40 | // SessionManagerObserverInterface: |
| 41 | void OnSessionStarted() override; |
| 42 | void OnSessionStopped() override; |
alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame] | 43 | |
| 44 | private: |
alanlxl | 9d26c1c | 2020-08-21 13:42:36 +1000 | [diff] [blame] | 45 | friend class StorageManagerImplTest; |
| 46 | |
| 47 | void set_example_database_for_testing(ExampleDatabase* example_database) { |
| 48 | example_database_.reset(example_database); |
| 49 | } |
| 50 | |
| 51 | void ConnectToDatabaseIfNecessary(); |
| 52 | |
| 53 | // Session manager that notifies session state changes. |
| 54 | std::unique_ptr<SessionManagerProxy> session_manager_proxy_; |
| 55 | |
| 56 | // The database connection. |
| 57 | std::unique_ptr<ExampleDatabase> example_database_; |
| 58 | |
| 59 | // Current login user hash. The database is connected to |
| 60 | // /run/daemon-store/federated/<sanitized_username_>/examples.db. |
| 61 | std::string sanitized_username_; |
| 62 | |
| 63 | // Which client it is streaming examples for. |
| 64 | std::string streaming_client_name_; |
| 65 | |
| 66 | // The last seen (i.e. largest) example id for the streaming_client_name_, |
| 67 | // after training job succeeds, examples of this client with id <= |
| 68 | // last_seen_example_id_ should be removed from the database. |
| 69 | int64_t last_seen_example_id_; |
| 70 | |
alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame] | 71 | SEQUENCE_CHECKER(sequence_checker_); |
alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | } // namespace federated |
| 75 | |
| 76 | #endif // FEDERATED_STORAGE_MANAGER_IMPL_H_ |