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