| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 REMOTING_PROTOCOL_PAIRING_REGISTRY_H_ | 5 #ifndef REMOTING_PROTOCOL_PAIRING_REGISTRY_H_ |
| 6 #define REMOTING_PROTOCOL_PAIRING_REGISTRY_H_ | 6 #define REMOTING_PROTOCOL_PAIRING_REGISTRY_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> |
| 9 #include <queue> | 10 #include <queue> |
| 10 #include <string> | 11 #include <string> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/callback.h" | 14 #include "base/callback.h" |
| 14 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 | 19 |
| 20 namespace base { | 20 namespace base { |
| 21 class DictionaryValue; | 21 class DictionaryValue; |
| 22 class ListValue; | 22 class ListValue; |
| 23 class SingleThreadTaskRunner; | 23 class SingleThreadTaskRunner; |
| 24 } // namespace base | 24 } // namespace base |
| 25 | 25 |
| 26 namespace tracked_objects { | 26 namespace tracked_objects { |
| 27 class Location; | 27 class Location; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 46 Pairing(const base::Time& created_time, | 46 Pairing(const base::Time& created_time, |
| 47 const std::string& client_name, | 47 const std::string& client_name, |
| 48 const std::string& client_id, | 48 const std::string& client_id, |
| 49 const std::string& shared_secret); | 49 const std::string& shared_secret); |
| 50 Pairing(const Pairing& other); | 50 Pairing(const Pairing& other); |
| 51 ~Pairing(); | 51 ~Pairing(); |
| 52 | 52 |
| 53 static Pairing Create(const std::string& client_name); | 53 static Pairing Create(const std::string& client_name); |
| 54 static Pairing CreateFromValue(const base::DictionaryValue& pairing); | 54 static Pairing CreateFromValue(const base::DictionaryValue& pairing); |
| 55 | 55 |
| 56 scoped_ptr<base::DictionaryValue> ToValue() const; | 56 std::unique_ptr<base::DictionaryValue> ToValue() const; |
| 57 | 57 |
| 58 bool operator==(const Pairing& other) const; | 58 bool operator==(const Pairing& other) const; |
| 59 | 59 |
| 60 bool is_valid() const; | 60 bool is_valid() const; |
| 61 | 61 |
| 62 base::Time created_time() const { return created_time_; } | 62 base::Time created_time() const { return created_time_; } |
| 63 std::string client_id() const { return client_id_; } | 63 std::string client_id() const { return client_id_; } |
| 64 std::string client_name() const { return client_name_; } | 64 std::string client_name() const { return client_name_; } |
| 65 std::string shared_secret() const { return shared_secret_; } | 65 std::string shared_secret() const { return shared_secret_; } |
| 66 | 66 |
| 67 private: | 67 private: |
| 68 base::Time created_time_; | 68 base::Time created_time_; |
| 69 std::string client_name_; | 69 std::string client_name_; |
| 70 std::string client_id_; | 70 std::string client_id_; |
| 71 std::string shared_secret_; | 71 std::string shared_secret_; |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 // Mapping from client id to pairing information. | 74 // Mapping from client id to pairing information. |
| 75 typedef std::map<std::string, Pairing> PairedClients; | 75 typedef std::map<std::string, Pairing> PairedClients; |
| 76 | 76 |
| 77 // Delegate callbacks. | 77 // Delegate callbacks. |
| 78 typedef base::Callback<void(bool success)> DoneCallback; | 78 typedef base::Callback<void(bool success)> DoneCallback; |
| 79 typedef base::Callback<void(scoped_ptr<base::ListValue> pairings)> | 79 typedef base::Callback<void(std::unique_ptr<base::ListValue> pairings)> |
| 80 GetAllPairingsCallback; | 80 GetAllPairingsCallback; |
| 81 typedef base::Callback<void(Pairing pairing)> GetPairingCallback; | 81 typedef base::Callback<void(Pairing pairing)> GetPairingCallback; |
| 82 | 82 |
| 83 static const char kCreatedTimeKey[]; | 83 static const char kCreatedTimeKey[]; |
| 84 static const char kClientIdKey[]; | 84 static const char kClientIdKey[]; |
| 85 static const char kClientNameKey[]; | 85 static const char kClientNameKey[]; |
| 86 static const char kSharedSecretKey[]; | 86 static const char kSharedSecretKey[]; |
| 87 | 87 |
| 88 // Interface representing the persistent storage back-end. | 88 // Interface representing the persistent storage back-end. |
| 89 class Delegate { | 89 class Delegate { |
| 90 public: | 90 public: |
| 91 virtual ~Delegate() {} | 91 virtual ~Delegate() {} |
| 92 | 92 |
| 93 // Retrieves all JSON-encoded pairings from persistent storage. | 93 // Retrieves all JSON-encoded pairings from persistent storage. |
| 94 virtual scoped_ptr<base::ListValue> LoadAll() = 0; | 94 virtual std::unique_ptr<base::ListValue> LoadAll() = 0; |
| 95 | 95 |
| 96 // Deletes all pairings in persistent storage. | 96 // Deletes all pairings in persistent storage. |
| 97 virtual bool DeleteAll() = 0; | 97 virtual bool DeleteAll() = 0; |
| 98 | 98 |
| 99 // Retrieves the pairing identified by |client_id|. | 99 // Retrieves the pairing identified by |client_id|. |
| 100 virtual Pairing Load(const std::string& client_id) = 0; | 100 virtual Pairing Load(const std::string& client_id) = 0; |
| 101 | 101 |
| 102 // Saves |pairing| to persistent storage. | 102 // Saves |pairing| to persistent storage. |
| 103 virtual bool Save(const Pairing& pairing) = 0; | 103 virtual bool Save(const Pairing& pairing) = 0; |
| 104 | 104 |
| 105 // Deletes the pairing identified by |client_id|. | 105 // Deletes the pairing identified by |client_id|. |
| 106 virtual bool Delete(const std::string& client_id) = 0; | 106 virtual bool Delete(const std::string& client_id) = 0; |
| 107 }; | 107 }; |
| 108 | 108 |
| 109 PairingRegistry( | 109 PairingRegistry( |
| 110 scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner, | 110 scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner, |
| 111 scoped_ptr<Delegate> delegate); | 111 std::unique_ptr<Delegate> delegate); |
| 112 | 112 |
| 113 // Creates a pairing for a new client and saves it to disk. | 113 // Creates a pairing for a new client and saves it to disk. |
| 114 // | 114 // |
| 115 // TODO(jamiewalch): Plumb the Save callback into the RequestPairing flow | 115 // TODO(jamiewalch): Plumb the Save callback into the RequestPairing flow |
| 116 // so that the client isn't sent the pairing information until it has been | 116 // so that the client isn't sent the pairing information until it has been |
| 117 // saved. | 117 // saved. |
| 118 Pairing CreatePairing(const std::string& client_name); | 118 Pairing CreatePairing(const std::string& client_name); |
| 119 | 119 |
| 120 // Gets the pairing for the specified client id. See the corresponding | 120 // Gets the pairing for the specified client id. See the corresponding |
| 121 // Delegate method for details. If none is found, the callback is invoked | 121 // Delegate method for details. If none is found, the callback is invoked |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 const protocol::PairingRegistry::DoneCallback& callback); | 168 const protocol::PairingRegistry::DoneCallback& callback); |
| 169 | 169 |
| 170 // "Trampoline" callbacks that schedule the next pending request and then | 170 // "Trampoline" callbacks that schedule the next pending request and then |
| 171 // invoke the original caller-supplied callback. | 171 // invoke the original caller-supplied callback. |
| 172 void InvokeDoneCallbackAndScheduleNext( | 172 void InvokeDoneCallbackAndScheduleNext( |
| 173 const DoneCallback& callback, bool success); | 173 const DoneCallback& callback, bool success); |
| 174 void InvokeGetPairingCallbackAndScheduleNext( | 174 void InvokeGetPairingCallbackAndScheduleNext( |
| 175 const GetPairingCallback& callback, Pairing pairing); | 175 const GetPairingCallback& callback, Pairing pairing); |
| 176 void InvokeGetAllPairingsCallbackAndScheduleNext( | 176 void InvokeGetAllPairingsCallbackAndScheduleNext( |
| 177 const GetAllPairingsCallback& callback, | 177 const GetAllPairingsCallback& callback, |
| 178 scoped_ptr<base::ListValue> pairings); | 178 std::unique_ptr<base::ListValue> pairings); |
| 179 | 179 |
| 180 // Sanitize |pairings| by parsing each entry and removing the secret from it. | 180 // Sanitize |pairings| by parsing each entry and removing the secret from it. |
| 181 void SanitizePairings(const GetAllPairingsCallback& callback, | 181 void SanitizePairings(const GetAllPairingsCallback& callback, |
| 182 scoped_ptr<base::ListValue> pairings); | 182 std::unique_ptr<base::ListValue> pairings); |
| 183 | 183 |
| 184 // Queue management methods. | 184 // Queue management methods. |
| 185 void ServiceOrQueueRequest(const base::Closure& request); | 185 void ServiceOrQueueRequest(const base::Closure& request); |
| 186 void ServiceNextRequest(); | 186 void ServiceNextRequest(); |
| 187 | 187 |
| 188 // Task runner on which all public methods of this class should be called. | 188 // Task runner on which all public methods of this class should be called. |
| 189 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; | 189 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; |
| 190 | 190 |
| 191 // Task runner used to run blocking calls to the delegate. A single thread | 191 // Task runner used to run blocking calls to the delegate. A single thread |
| 192 // task runner is used to guarantee that one one method of the delegate is | 192 // task runner is used to guarantee that one one method of the delegate is |
| 193 // called at a time. | 193 // called at a time. |
| 194 scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner_; | 194 scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner_; |
| 195 | 195 |
| 196 scoped_ptr<Delegate> delegate_; | 196 std::unique_ptr<Delegate> delegate_; |
| 197 | 197 |
| 198 std::queue<base::Closure> pending_requests_; | 198 std::queue<base::Closure> pending_requests_; |
| 199 | 199 |
| 200 DISALLOW_COPY_AND_ASSIGN(PairingRegistry); | 200 DISALLOW_COPY_AND_ASSIGN(PairingRegistry); |
| 201 }; | 201 }; |
| 202 | 202 |
| 203 } // namespace protocol | 203 } // namespace protocol |
| 204 } // namespace remoting | 204 } // namespace remoting |
| 205 | 205 |
| 206 #endif // REMOTING_PROTOCOL_PAIRING_REGISTRY_H_ | 206 #endif // REMOTING_PROTOCOL_PAIRING_REGISTRY_H_ |
| OLD | NEW |