| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_GNOME_X_H_ | 5 #ifndef CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_GNOME_X_H_ |
| 6 #define CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_GNOME_X_H_ | 6 #define CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_GNOME_X_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <gnome-keyring.h> | |
| 10 | |
| 11 #include <string> | 9 #include <string> |
| 12 | 10 |
| 13 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 14 #include "base/time.h" | 12 #include "base/time.h" |
| 15 #include "chrome/browser/password_manager/password_store_x.h" | 13 #include "chrome/browser/password_manager/password_store_x.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 17 | 15 |
| 18 class PrefService; | 16 class PrefService; |
| 19 | 17 |
| 18 namespace keyring_proxy { |
| 19 class KeyringProxyClient; |
| 20 } |
| 21 |
| 20 namespace webkit_glue { | 22 namespace webkit_glue { |
| 21 struct PasswordForm; | 23 struct PasswordForm; |
| 22 } | 24 } |
| 23 | 25 |
| 24 // Many of the gnome_keyring_* functions use variable arguments, which makes | 26 // NativeBackend implementation using GNOME Keyring. |
| 25 // them difficult if not impossible to truly wrap in C. Therefore, we use | 27 class NativeBackendGnome : public PasswordStoreX::NativeBackend { |
| 26 // appropriately-typed function pointers and scoping to make the fact that we | 28 public: |
| 27 // might be dynamically loading the library almost invisible. As a bonus, we | 29 static const char kGnomeKeyringAppString[]; |
| 28 // also get a simple way to mock the library for testing. Classes that inherit | |
| 29 // from GnomeKeyringLoader will use its versions of the gnome_keyring_* | |
| 30 // functions. Note that it has only static fields. | |
| 31 class GnomeKeyringLoader { | |
| 32 protected: | |
| 33 static bool LoadGnomeKeyring(); | |
| 34 | 30 |
| 35 // Call a given parameter with the name of each function we use from GNOME | |
| 36 // Keyring. Make sure to adjust the unit test if you change these. | |
| 37 #define GNOME_KEYRING_FOR_EACH_FUNC(F) \ | |
| 38 F(is_available) \ | |
| 39 F(store_password) \ | |
| 40 F(delete_password) \ | |
| 41 F(find_itemsv) \ | |
| 42 F(result_to_message) | |
| 43 | |
| 44 // Declare the actual function pointers that we'll use in client code. | |
| 45 #define GNOME_KEYRING_DECLARE_POINTER(name) \ | |
| 46 static typeof(&::gnome_keyring_##name) gnome_keyring_##name; | |
| 47 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_DECLARE_POINTER) | |
| 48 #undef GNOME_KEYRING_DECLARE_POINTER | |
| 49 | |
| 50 // Set to true if LoadGnomeKeyring() has already succeeded. | |
| 51 static bool keyring_loaded; | |
| 52 | |
| 53 private: | |
| 54 #if defined(DLOPEN_GNOME_KEYRING) | |
| 55 struct FunctionInfo { | |
| 56 const char* name; | |
| 57 void** pointer; | |
| 58 }; | |
| 59 | |
| 60 // Make it easy to initialize the function pointers in LoadGnomeKeyring(). | |
| 61 static const FunctionInfo functions[]; | |
| 62 #endif // defined(DLOPEN_GNOME_KEYRING) | |
| 63 }; | |
| 64 | |
| 65 // NativeBackend implementation using GNOME Keyring. | |
| 66 class NativeBackendGnome : public PasswordStoreX::NativeBackend, | |
| 67 public GnomeKeyringLoader { | |
| 68 public: | |
| 69 NativeBackendGnome(LocalProfileId id, PrefService* prefs); | 31 NativeBackendGnome(LocalProfileId id, PrefService* prefs); |
| 70 | 32 |
| 71 virtual ~NativeBackendGnome(); | 33 virtual ~NativeBackendGnome(); |
| 72 | 34 |
| 73 virtual bool Init() OVERRIDE; | 35 virtual bool Init() OVERRIDE; |
| 74 | 36 |
| 75 // Implements NativeBackend interface. | 37 // Implements NativeBackend interface. |
| 76 virtual bool AddLogin(const webkit_glue::PasswordForm& form) OVERRIDE; | 38 virtual bool AddLogin(const webkit_glue::PasswordForm& form) OVERRIDE; |
| 77 virtual bool UpdateLogin(const webkit_glue::PasswordForm& form) OVERRIDE; | 39 virtual bool UpdateLogin(const webkit_glue::PasswordForm& form) OVERRIDE; |
| 78 virtual bool RemoveLogin(const webkit_glue::PasswordForm& form) OVERRIDE; | 40 virtual bool RemoveLogin(const webkit_glue::PasswordForm& form) OVERRIDE; |
| 79 virtual bool RemoveLoginsCreatedBetween( | 41 virtual bool RemoveLoginsCreatedBetween( |
| 80 const base::Time& delete_begin, const base::Time& delete_end) OVERRIDE; | 42 const base::Time& delete_begin, const base::Time& delete_end) OVERRIDE; |
| 81 virtual bool GetLogins(const webkit_glue::PasswordForm& form, | 43 virtual bool GetLogins(const webkit_glue::PasswordForm& form, |
| 82 PasswordFormList* forms) OVERRIDE; | 44 PasswordFormList* forms) OVERRIDE; |
| 83 virtual bool GetLoginsCreatedBetween(const base::Time& get_begin, | 45 virtual bool GetLoginsCreatedBetween(const base::Time& get_begin, |
| 84 const base::Time& get_end, | 46 const base::Time& get_end, |
| 85 PasswordFormList* forms) OVERRIDE; | 47 PasswordFormList* forms) OVERRIDE; |
| 86 virtual bool GetAutofillableLogins(PasswordFormList* forms) OVERRIDE; | 48 virtual bool GetAutofillableLogins(PasswordFormList* forms) OVERRIDE; |
| 87 virtual bool GetBlacklistLogins(PasswordFormList* forms) OVERRIDE; | 49 virtual bool GetBlacklistLogins(PasswordFormList* forms) OVERRIDE; |
| 88 | 50 |
| 51 protected: |
| 52 // Takes ownership of |client|. Use for testing only. |
| 53 void InitForTesting(keyring_proxy::KeyringProxyClient* client); |
| 54 |
| 89 private: | 55 private: |
| 90 // Adds a login form without checking for one to replace first. | 56 // Adds a login form without checking for one to replace first. |
| 91 bool RawAddLogin(const webkit_glue::PasswordForm& form); | 57 bool RawAddLogin(const webkit_glue::PasswordForm& form); |
| 92 | 58 |
| 93 // Reads PasswordForms from the keyring with the given autofillability state. | 59 // Reads PasswordForms from the keyring with the given autofillability state. |
| 94 bool GetLoginsList(PasswordFormList* forms, bool autofillable); | 60 bool GetLoginsList(PasswordFormList* forms, bool autofillable); |
| 95 | 61 |
| 96 // Helper for GetLoginsCreatedBetween(). | 62 // Helper for GetLoginsCreatedBetween(). |
| 97 bool GetAllLogins(PasswordFormList* forms); | 63 bool GetAllLogins(PasswordFormList* forms); |
| 98 | 64 |
| 99 // Generates a profile-specific app string based on profile_id_. | 65 // Generates a profile-specific app string based on profile_id_. |
| 100 std::string GetProfileSpecificAppString() const; | 66 std::string GetProfileSpecificAppString() const; |
| 101 | 67 |
| 102 // Migrates non-profile-specific logins to be profile-specific. | 68 // Migrates non-profile-specific logins to be profile-specific. |
| 103 void MigrateToProfileSpecificLogins(); | 69 void MigrateToProfileSpecificLogins(); |
| 104 | 70 |
| 105 // The local profile id, used to generate the app string. | 71 // The local profile id, used to generate the app string. |
| 106 const LocalProfileId profile_id_; | 72 const LocalProfileId profile_id_; |
| 107 | 73 |
| 108 // The pref service to use for persistent migration settings. | 74 // The pref service to use for persistent migration settings. |
| 109 PrefService* prefs_; | 75 PrefService* prefs_; |
| 110 | 76 |
| 111 // The app string, possibly based on the local profile id. | 77 // The app string, possibly based on the local profile id. |
| 112 std::string app_string_; | 78 std::string app_string_; |
| 113 | 79 |
| 114 // True once MigrateToProfileSpecificLogins() has been attempted. | 80 // True once MigrateToProfileSpecificLogins() has been attempted. |
| 115 bool migrate_tried_; | 81 bool migrate_tried_; |
| 116 | 82 |
| 83 // The keyring proxy client handles communicating with an out-of-process |
| 84 // GNOME Keyring client, since to do it in-process we'd need to involve |
| 85 // the UI thread which can lead to deadlocks with password sync. |
| 86 scoped_ptr<keyring_proxy::KeyringProxyClient> proxy_client_; |
| 87 |
| 117 DISALLOW_COPY_AND_ASSIGN(NativeBackendGnome); | 88 DISALLOW_COPY_AND_ASSIGN(NativeBackendGnome); |
| 118 }; | 89 }; |
| 119 | 90 |
| 120 #endif // CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_GNOME_X_H_ | 91 #endif // CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_GNOME_X_H_ |
| OLD | NEW |