Chromium Code Reviews| 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 GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ | 5 #ifndef GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ |
| 6 #define GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ | 6 #define GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "google_apis/gcm/base/gcm_export.h" | 16 #include "google_apis/gcm/base/gcm_export.h" |
| 17 #include "google_apis/gcm/base/mcs_message.h" | 17 #include "google_apis/gcm/base/mcs_message.h" |
| 18 #include "google_apis/gcm/engine/connection_handler.h" | 18 #include "google_apis/gcm/engine/connection_handler.h" |
| 19 #include "google_apis/gcm/engine/heartbeat_manager.h" | 19 #include "google_apis/gcm/engine/heartbeat_manager.h" |
| 20 #include "google_apis/gcm/engine/rmq_store.h" | 20 #include "google_apis/gcm/engine/rmq_store.h" |
| 21 | 21 |
| 22 namespace base { | |
| 23 class Clock; | |
| 24 } // namespace base | |
| 25 | |
| 22 namespace google { | 26 namespace google { |
| 23 namespace protobuf { | 27 namespace protobuf { |
| 24 class MessageLite; | 28 class MessageLite; |
| 25 } // namespace protobuf | 29 } // namespace protobuf |
| 26 } // namespace google | 30 } // namespace google |
| 27 | 31 |
| 28 namespace mcs_proto { | 32 namespace mcs_proto { |
| 29 class LoginRequest; | 33 class LoginRequest; |
| 30 } | 34 } |
| 31 | 35 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 57 // Callback when a message is received. | 61 // Callback when a message is received. |
| 58 typedef base::Callback<void(const MCSMessage& message)> | 62 typedef base::Callback<void(const MCSMessage& message)> |
| 59 OnMessageReceivedCallback; | 63 OnMessageReceivedCallback; |
| 60 // Callback when a message is sent (and receipt has been acknowledged by | 64 // Callback when a message is sent (and receipt has been acknowledged by |
| 61 // the MCS endpoint). | 65 // the MCS endpoint). |
| 62 // TODO(zea): pass some sort of structure containing more details about | 66 // TODO(zea): pass some sort of structure containing more details about |
| 63 // send failures. | 67 // send failures. |
| 64 typedef base::Callback<void(const std::string& message_id)> | 68 typedef base::Callback<void(const std::string& message_id)> |
| 65 OnMessageSentCallback; | 69 OnMessageSentCallback; |
| 66 | 70 |
| 67 MCSClient(ConnectionFactory* connection_factory, RMQStore* rmq_store); | 71 MCSClient(base::Clock* clock, |
| 72 ConnectionFactory* connection_factory, | |
| 73 RMQStore* rmq_store); | |
| 68 virtual ~MCSClient(); | 74 virtual ~MCSClient(); |
| 69 | 75 |
| 70 // Initialize the client. Will load any previous id/token information as well | 76 // Initialize the client. Will load any previous id/token information as well |
| 71 // as unacknowledged message information from the RMQ storage, if it exists, | 77 // as unacknowledged message information from the RMQ storage, if it exists, |
| 72 // passing the id/token information back via |initialization_callback| along | 78 // passing the id/token information back via |initialization_callback| along |
| 73 // with a |success == true| result. If no RMQ information is present (and | 79 // with a |success == true| result. If no RMQ information is present (and |
| 74 // this is therefore a fresh client), a clean RMQ store will be created and | 80 // this is therefore a fresh client), a clean RMQ store will be created and |
| 75 // values of 0 will be returned via |initialization_callback| with | 81 // values of 0 will be returned via |initialization_callback| with |
| 76 // |success == true|. | 82 // |success == true|. |
| 77 /// If an error loading the RMQ store is encountered, | 83 /// If an error loading the RMQ store is encountered, |
| 78 // |initialization_callback| will be invoked with |success == false|. | 84 // |initialization_callback| will be invoked with |success == false|. |
| 79 void Initialize(const InitializationCompleteCallback& initialization_callback, | 85 void Initialize(const InitializationCompleteCallback& initialization_callback, |
| 80 const OnMessageReceivedCallback& message_received_callback, | 86 const OnMessageReceivedCallback& message_received_callback, |
| 81 const OnMessageSentCallback& message_sent_callback, | 87 const OnMessageSentCallback& message_sent_callback, |
| 82 const RMQStore::LoadResult& load_result); | 88 const RMQStore::LoadResult& load_result); |
| 83 | 89 |
| 84 // Logs the client into the server. Client must be initialized. | 90 // Logs the client into the server. Client must be initialized. |
| 85 // |android_id| and |security_token| are optional if this is not a new | 91 // |android_id| and |security_token| are optional if this is not a new |
| 86 // client, else they must be non-zero. | 92 // client, else they must be non-zero. |
| 87 // Successful login will result in |message_received_callback| being invoked | 93 // Successful login will result in |message_received_callback| being invoked |
| 88 // with a valid LoginResponse. | 94 // with a valid LoginResponse. |
| 89 // Login failure (typically invalid id/token) will shut down the client, and | 95 // Login failure (typically invalid id/token) will shut down the client, and |
| 90 // |initialization_callback| to be invoked with |success = false|. | 96 // |initialization_callback| to be invoked with |success = false|. |
| 91 void Login(uint64 android_id, uint64 security_token); | 97 void Login(uint64 android_id, uint64 security_token); |
| 92 | 98 |
| 93 // Sends a message, with or without reliable message queueing (RMQ) support. | 99 // Sends a message, with or without reliable message queueing (RMQ) support. |
| 94 // Will asynchronously invoke the OnMessageSent callback regardless. | 100 // Will asynchronously invoke the OnMessageSent callback regardless. |
| 95 // TODO(zea): support TTL. | 101 // Whether to use RMQ depends on whether the protobuf has |ttl| set or not. |
| 96 void SendMessage(const MCSMessage& message, bool use_rmq); | 102 // |ttl == 0| denotes the message should only be sent if the connection is |
| 103 // open. |ttl > 0| will keep the message saved for |ttl| seconds, after which | |
| 104 // it will be dropped if it was unable to be sent. When a message is dropped, | |
| 105 // OnMessageSent is invoked with a TTL expiration error. | |
|
fgorski
2013/12/28 01:15:08
do you mean message_sent_callback_?
Nicolas Zea
2013/12/30 21:46:19
Done.
| |
| 106 void SendMessage(const MCSMessage& message); | |
| 97 | 107 |
| 98 // Disconnects the client and permanently destroys the persistent RMQ store. | 108 // Disconnects the client and permanently destroys the persistent RMQ store. |
| 99 // WARNING: This is permanent, and the client must be recreated with new | 109 // WARNING: This is permanent, and the client must be recreated with new |
| 100 // credentials afterwards. | 110 // credentials afterwards. |
| 101 void Destroy(); | 111 void Destroy(); |
| 102 | 112 |
| 103 // Returns the current state of the client. | 113 // Returns the current state of the client. |
| 104 State state() const { return state_; } | 114 State state() const { return state_; } |
| 105 | 115 |
| 106 private: | 116 private: |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 // acknowledgment of receipt of messages. | 158 // acknowledgment of receipt of messages. |
| 149 void HandleServerConfirmedReceipt(StreamId device_stream_id); | 159 void HandleServerConfirmedReceipt(StreamId device_stream_id); |
| 150 | 160 |
| 151 // Generates a new persistent id for messages. | 161 // Generates a new persistent id for messages. |
| 152 // Virtual for testing. | 162 // Virtual for testing. |
| 153 virtual PersistentId GetNextPersistentId(); | 163 virtual PersistentId GetNextPersistentId(); |
| 154 | 164 |
| 155 // Helper for the heartbeat manager to signal a connection reset. | 165 // Helper for the heartbeat manager to signal a connection reset. |
| 156 void OnConnectionResetByHeartbeat(); | 166 void OnConnectionResetByHeartbeat(); |
| 157 | 167 |
| 168 // Clock for enforcing TTL. Passed in for testing. | |
| 169 base::Clock* const clock_; | |
| 170 | |
| 158 // Client state. | 171 // Client state. |
| 159 State state_; | 172 State state_; |
| 160 | 173 |
| 161 // Callbacks for owner. | 174 // Callbacks for owner. |
| 162 InitializationCompleteCallback initialization_callback_; | 175 InitializationCompleteCallback initialization_callback_; |
| 163 OnMessageReceivedCallback message_received_callback_; | 176 OnMessageReceivedCallback message_received_callback_; |
| 164 OnMessageSentCallback message_sent_callback_; | 177 OnMessageSentCallback message_sent_callback_; |
| 165 | 178 |
| 166 // The android id and security token in use by this device. | 179 // The android id and security token in use by this device. |
| 167 uint64 android_id_; | 180 uint64 android_id_; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 HeartbeatManager heartbeat_manager_; | 229 HeartbeatManager heartbeat_manager_; |
| 217 | 230 |
| 218 base::WeakPtrFactory<MCSClient> weak_ptr_factory_; | 231 base::WeakPtrFactory<MCSClient> weak_ptr_factory_; |
| 219 | 232 |
| 220 DISALLOW_COPY_AND_ASSIGN(MCSClient); | 233 DISALLOW_COPY_AND_ASSIGN(MCSClient); |
| 221 }; | 234 }; |
| 222 | 235 |
| 223 } // namespace gcm | 236 } // namespace gcm |
| 224 | 237 |
| 225 #endif // GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ | 238 #endif // GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ |
| OLD | NEW |