client_storage.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2021 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. #ifndef COMPONENTS_POLICY_TEST_SUPPORT_CLIENT_STORAGE_H_
  5. #define COMPONENTS_POLICY_TEST_SUPPORT_CLIENT_STORAGE_H_
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace policy {
  12. // Stores information about registered clients.
  13. class ClientStorage {
  14. public:
  15. struct ClientInfo {
  16. ClientInfo();
  17. ClientInfo(const ClientInfo& client_info);
  18. ClientInfo& operator=(const ClientInfo& client_info);
  19. ClientInfo(ClientInfo&& client_info);
  20. ClientInfo& operator=(ClientInfo&& client_info);
  21. ~ClientInfo();
  22. std::string device_id;
  23. std::string device_token;
  24. std::string machine_name;
  25. absl::optional<std::string> username;
  26. std::vector<std::string> state_keys;
  27. std::set<std::string> allowed_policy_types;
  28. };
  29. ClientStorage();
  30. ClientStorage(ClientStorage&& client_storage);
  31. ClientStorage& operator=(ClientStorage&& client_storage);
  32. ~ClientStorage();
  33. // Register client so the server returns policy without the client having to
  34. // make a registration call. This could be called at any time (before or after
  35. // starting the server).
  36. void RegisterClient(const ClientInfo& client_info);
  37. // Returns true if there is a client associated with |device_id|.
  38. bool HasClient(const std::string& device_id) const;
  39. // Returns the client info associated with |device_id|. Fails if there is no
  40. // such a client.
  41. const ClientInfo& GetClient(const std::string& device_id) const;
  42. // Returns the client info associated with |device_id| or nullptr if there is
  43. // no such a client.
  44. const ClientInfo* GetClientOrNull(const std::string& device_id) const;
  45. // Returns the client info associated with |state_key| or nullptr if there is
  46. // no such a client.
  47. const ClientInfo* LookupByStateKey(const std::string& state_key) const;
  48. // Returns true if deletion of client with token |device_token| succeeded.
  49. bool DeleteClient(const std::string& device_token);
  50. // Returns the number of clients registered.
  51. size_t GetNumberOfRegisteredClients() const;
  52. // Returns hashes for all state keys registered with the server, which, when
  53. // divied by |modulus|, result in the specified |remainder|.
  54. std::vector<std::string> GetMatchingStateKeyHashes(uint64_t modulus,
  55. uint64_t remainder) const;
  56. // Returns all the clients in the storage.
  57. std::vector<ClientInfo> GetAllClients();
  58. private:
  59. // Key: device ids.
  60. std::map<std::string, ClientInfo> clients_;
  61. // Maps device tokens to device IDs.
  62. std::map<std::string, std::string> registered_tokens_;
  63. };
  64. } // namespace policy
  65. #endif // COMPONENTS_POLICY_TEST_SUPPORT_CLIENT_STORAGE_H_