OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_PASSWORD_MANAGER_KEYRING_PROXY_KEYRING_PROXY_CLIENT_H_ |
| 6 #define CHROME_BROWSER_PASSWORD_MANAGER_KEYRING_PROXY_KEYRING_PROXY_CLIENT_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <gnome-keyring.h> |
| 10 |
| 11 #include <map> |
| 12 #include <string> |
| 13 #include <vector> |
| 14 |
| 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/synchronization/lock.h" |
| 17 #include "base/synchronization/waitable_event.h" |
| 18 |
| 19 namespace webkit { |
| 20 namespace forms { |
| 21 struct PasswordForm; |
| 22 } |
| 23 } |
| 24 |
| 25 namespace keyring_proxy { |
| 26 |
| 27 // Used internally by KeyringProxyClient to watch the file descriptor for data |
| 28 // to read. Done in a separate class so it can be refcounted and shut down |
| 29 // safely on the file thread after pending callbacks are done. |
| 30 class KeyringProxyFDWatcher; |
| 31 |
| 32 class KeyringProxyClient { |
| 33 public: |
| 34 // This is the name of the binary for the proxy itself. |
| 35 static const char kProxyBinary[]; |
| 36 |
| 37 // Save some typing with convenient short types for password forms. |
| 38 typedef webkit::forms::PasswordForm PasswordForm; |
| 39 typedef std::vector<PasswordForm*> PasswordFormList; |
| 40 |
| 41 // Every request to the proxy has a context, and internally also an ID. |
| 42 struct RequestContext { |
| 43 base::WaitableEvent event; |
| 44 GnomeKeyringResult result_code; |
| 45 PasswordFormList* result_list; // Not used for AddLogin() or RemoveLogin(). |
| 46 |
| 47 RequestContext() |
| 48 : event(false, false), result_code(GNOME_KEYRING_RESULT_CANCELLED), |
| 49 result_list(NULL) {} |
| 50 explicit RequestContext(PasswordFormList* list) |
| 51 : event(false, false), result_code(GNOME_KEYRING_RESULT_CANCELLED), |
| 52 result_list(list) {} |
| 53 }; |
| 54 |
| 55 // The client is constructed and destructed on the UI thread. |
| 56 KeyringProxyClient(); |
| 57 ~KeyringProxyClient(); |
| 58 |
| 59 // Starts a keyring proxy process. Returns true on fork() success; the process |
| 60 // itself may still fail to start or fail later. Use connected() to check it. |
| 61 // Safe to call on the UI thread after instantiation, but if called again to |
| 62 // reconnect later, should be called on the DB thread. |
| 63 bool Connect(); |
| 64 |
| 65 // Returns true if the keyring proxy is still connected as far as we know. |
| 66 bool connected() const { return !!fd_watcher_.get(); } |
| 67 |
| 68 // These are safe to call on the DB thread. The request is complete when the |
| 69 // waitable event in the provided request context becomes ready. The actual |
| 70 // callbacks that happen when data is received from the proxy will happen on |
| 71 // the file thread, where they will signal the waitable events. This is done |
| 72 // because we want to parallelize multiple requests, but the password store |
| 73 // APIs on the DB thread are synchronous and so must wait for the replies. |
| 74 void AddLogin(const PasswordForm& form, |
| 75 const std::string& app_string, |
| 76 RequestContext* context); |
| 77 void AddLoginSearch(const PasswordForm& form, |
| 78 const std::string& app_string, |
| 79 RequestContext* context); |
| 80 void UpdateLoginSearch(const PasswordForm& form, |
| 81 const std::string& app_string, |
| 82 RequestContext* context); |
| 83 void RemoveLogin(const PasswordForm& form, |
| 84 const std::string& app_string, |
| 85 RequestContext* context); |
| 86 void GetLogins(const PasswordForm& form, |
| 87 const std::string& app_string, |
| 88 RequestContext* context); |
| 89 void GetLoginsList(bool blacklisted_by_user, |
| 90 const std::string& app_string, |
| 91 RequestContext* context); |
| 92 void GetAllLogins(const std::string& app_string, |
| 93 RequestContext* context); |
| 94 |
| 95 // Launches the keyring proxy and returns the IPC file descriptor to it. |
| 96 static int LaunchKeyringProxy(); |
| 97 |
| 98 protected: |
| 99 // This is the type of the messages actually exchanged with the proxy. |
| 100 typedef std::vector<std::string> ProxyMessage; |
| 101 |
| 102 // Connects to the given file descriptor instead of launching the proxy. |
| 103 // Will still close it, but will only listen for reads if requested. |
| 104 void ConnectForTesting(int fd, bool watch_for_reads); |
| 105 |
| 106 // Cancel all outstanding requests and unblock waiters. Must already hold |
| 107 // |request_lock_| to call CancelAllRequests(). Protected for testing. |
| 108 void CancelAllRequests(); |
| 109 |
| 110 // These are called by KeyringProxyFDWatcher on the file thread. |
| 111 void HandleProxyReply(const ProxyMessage& reply); |
| 112 void HandleProxyError(bool eof); |
| 113 |
| 114 private: |
| 115 friend class KeyringProxyFDWatcher; |
| 116 |
| 117 // Must already hold |request_lock_| to call GetRequestId(). |
| 118 int GetRequestId(); |
| 119 // Send a request of type |type| and add an entry to |proxy_requests_| for it. |
| 120 void SendRequest( |
| 121 char type, const ProxyMessage& request, RequestContext* context); |
| 122 |
| 123 // Cancel a single request context, unblocking the waiter. |
| 124 void CancelRequest(RequestContext* context); |
| 125 |
| 126 scoped_refptr<KeyringProxyFDWatcher> fd_watcher_; |
| 127 |
| 128 // The next proxy request ID to try. |
| 129 int next_request_id_; |
| 130 |
| 131 // A map from proxy request IDs to proxy requests. Guarded by |request_lock_|. |
| 132 std::map<int, RequestContext*> proxy_requests_; |
| 133 |
| 134 // Guards |proxy_requests_| since it is accessed on the DB and file threads. |
| 135 base::Lock request_lock_; |
| 136 |
| 137 DISALLOW_COPY_AND_ASSIGN(KeyringProxyClient); |
| 138 }; |
| 139 |
| 140 } // namespace keyring_proxy |
| 141 |
| 142 #endif // CHROME_BROWSER_PASSWORD_MANAGER_KEYRING_PROXY_KEYRING_PROXY_CLIENT_H_ |
OLD | NEW |