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