OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 SERVICES_AUTHENTICATION_ACCOUNTS_DB_MANAGER_H_ |
| 6 #define SERVICES_AUTHENTICATION_ACCOUNTS_DB_MANAGER_H_ |
| 7 |
| 8 #include <type_traits> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "mojo/services/files/interfaces/files.mojom.h" |
| 12 |
| 13 namespace authentication { |
| 14 |
| 15 // TODO(ukode): Stuff copied from mojo/public/cpp/bindings/lib/template_util.h. |
| 16 typedef char YesType; |
| 17 |
| 18 struct NoType { |
| 19 YesType dummy[2]; |
| 20 }; |
| 21 |
| 22 template <typename T> |
| 23 struct IsMoveOnlyType { |
| 24 template <typename U> |
| 25 static YesType Test(const typename U::MoveOnlyTypeForCPP03*); |
| 26 |
| 27 template <typename U> |
| 28 static NoType Test(...); |
| 29 |
| 30 static const bool value = |
| 31 sizeof(Test<T>(0)) == sizeof(YesType) && !std::is_const<T>::value; |
| 32 }; |
| 33 |
| 34 template <typename T> |
| 35 typename std::enable_if<!IsMoveOnlyType<T>::value, T>::type& Forward(T& t) { |
| 36 return t; |
| 37 } |
| 38 |
| 39 template <typename T> |
| 40 typename std::enable_if<IsMoveOnlyType<T>::value, T>::type Forward(T& t) { |
| 41 return t.Pass(); |
| 42 } |
| 43 // TODO(ukode): (End of stuff copied from template_util.h.) |
| 44 |
| 45 template <typename T1> |
| 46 mojo::Callback<void(T1)> Capture(T1* t1) { |
| 47 return [t1](T1 got_t1) { *t1 = Forward(got_t1); }; |
| 48 } |
| 49 |
| 50 template <typename T1, typename T2> |
| 51 mojo::Callback<void(T1, T2)> Capture(T1* t1, T2* t2) { |
| 52 return [t1, t2](T1 got_t1, T2 got_t2) { |
| 53 *t1 = Forward(got_t1); |
| 54 *t2 = Forward(got_t2); |
| 55 }; |
| 56 } |
| 57 |
| 58 // Implementation of user account management service on systems like FNL. This |
| 59 // uses native mojo files service as the underlying mechanism to store user |
| 60 // credentials and supports operations such as to add a new user account, update |
| 61 // existing user credentials and fetching current credentials for a given user. |
| 62 class AccountsDbManager { |
| 63 public: |
| 64 AccountsDbManager(); |
| 65 explicit AccountsDbManager(mojo::files::FilesPtr files); |
| 66 ~AccountsDbManager(); |
| 67 // Updates account credentials for a given user. |
| 68 bool UpdateAccount(const mojo::String& username, |
| 69 const mojo::String& account_data); |
| 70 // Fetches account credentials for a given user. |
| 71 void GetAccountDataForUser(const mojo::String& username, |
| 72 mojo::String& user_data); |
| 73 // Fetches all user credentials. |
| 74 mojo::Array<uint8_t> FetchAllAccounts(); |
| 75 |
| 76 private: |
| 77 // Generates new contents of the accounts database during an update operation. |
| 78 // Performs one of the two operations: |
| 79 // 1. If the user already exists, updates the existing record in database. |
| 80 // 2. If its a new user, adds a new record to the existing database content. |
| 81 void GetUpdatedDbContents(const mojo::String& username, |
| 82 const mojo::String& new_account_data, |
| 83 const bool user_exists, |
| 84 mojo::String& new_db_contents); |
| 85 |
| 86 // Directory pointer to the stored accounts db file. |
| 87 mojo::files::DirectoryPtr directory_; |
| 88 // Stores the cached account data in between account operations. |
| 89 std::string contents_; |
| 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(AccountsDbManager); |
| 92 }; |
| 93 |
| 94 } // namespace authentication |
| 95 |
| 96 #endif // SERVICES_AUTHENTICATION_ACCOUNTS_DB_MANAGER_H_ |
OLD | NEW |