OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 COMPONENTS_OS_CRYPT_LIBSECRET_UTIL_POSIX_H_ |
| 6 #define COMPONENTS_OS_CRYPT_LIBSECRET_UTIL_POSIX_H_ |
| 7 |
| 8 #include <libsecret/secret.h> |
| 9 |
| 10 #include <list> |
| 11 #include <string> |
| 12 |
| 13 #include "base/macros.h" |
| 14 |
| 15 // Utility for dynamically loading libsecret. |
| 16 class LibsecretLoader { |
| 17 public: |
| 18 static decltype(&::secret_password_store_sync) secret_password_store_sync; |
| 19 static decltype(&::secret_service_search_sync) secret_service_search_sync; |
| 20 static decltype(&::secret_password_clear_sync) secret_password_clear_sync; |
| 21 static decltype(&::secret_item_get_secret) secret_item_get_secret; |
| 22 static decltype(&::secret_value_get_text) secret_value_get_text; |
| 23 static decltype(&::secret_item_get_attributes) secret_item_get_attributes; |
| 24 static decltype(&::secret_item_load_secret_sync) secret_item_load_secret_sync; |
| 25 static decltype(&::secret_value_unref) secret_value_unref; |
| 26 |
| 27 // Loads the libsecret library and checks that it responds to queries. |
| 28 // Returns false if either step fails. |
| 29 // Repeated calls check the responsiveness every time, but do not load the |
| 30 // the library again if already successful. |
| 31 static bool EnsureLibsecretLoaded(); |
| 32 |
| 33 protected: |
| 34 static bool libsecret_loaded_; |
| 35 |
| 36 private: |
| 37 struct FunctionInfo { |
| 38 const char* name; |
| 39 void** pointer; |
| 40 }; |
| 41 |
| 42 static const FunctionInfo kFunctions[]; |
| 43 |
| 44 // Load the libsecret binaries. Returns true on success. |
| 45 // If successful, the result is cached and the function can be safely called |
| 46 // multiple times. |
| 47 // Checking |LibsecretIsAvailable| is necessary after this to verify that the |
| 48 // service responds to queries. |
| 49 static bool LoadLibsecret(); |
| 50 |
| 51 // True if the libsecret binaries have been loaded and the library responds |
| 52 // to queries. |
| 53 static bool LibsecretIsAvailable(); |
| 54 |
| 55 DISALLOW_IMPLICIT_CONSTRUCTORS(LibsecretLoader); |
| 56 }; |
| 57 |
| 58 class LibsecretAttributesBuilder { |
| 59 public: |
| 60 LibsecretAttributesBuilder(); |
| 61 ~LibsecretAttributesBuilder(); |
| 62 |
| 63 void Append(const std::string& name, const std::string& value); |
| 64 |
| 65 void Append(const std::string& name, int64_t value); |
| 66 |
| 67 // GHashTable, its keys and values returned from Get() are destroyed in |
| 68 // |LibsecretAttributesBuilder| destructor. |
| 69 GHashTable* Get() { return attrs_; } |
| 70 |
| 71 private: |
| 72 // |name_values_| is a storage for strings referenced in |attrs_|. |
| 73 // TODO(crbug.com/607950): Make implementation more robust by not depending on |
| 74 // the implementation details of containers. External objects keep references |
| 75 // to the objects stored in this container. Using a vector here will fail the |
| 76 // ASan tests, because it may move the objects and break the references. |
| 77 std::list<std::string> name_values_; |
| 78 GHashTable* attrs_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(LibsecretAttributesBuilder); |
| 81 }; |
| 82 |
| 83 #endif // COMPONENTS_OS_CRYPT_LIBSECRET_UTIL_POSIX_H_ |
OLD | NEW |