| 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/gcm_store.h" | 19 #include "google_apis/gcm/engine/gcm_store.h" |
| 20 #include "google_apis/gcm/engine/heartbeat_manager.h" | 20 #include "google_apis/gcm/engine/heartbeat_manager.h" |
| 21 #include "google_apis/gcm/gcm_stats_recorder.h" |
| 21 | 22 |
| 22 namespace base { | 23 namespace base { |
| 23 class Clock; | 24 class Clock; |
| 24 } // namespace base | 25 } // namespace base |
| 25 | 26 |
| 26 namespace google { | 27 namespace google { |
| 27 namespace protobuf { | 28 namespace protobuf { |
| 28 class MessageLite; | 29 class MessageLite; |
| 29 } // namespace protobuf | 30 } // namespace protobuf |
| 30 } // namespace google | 31 } // namespace google |
| 31 | 32 |
| 32 namespace mcs_proto { | 33 namespace mcs_proto { |
| 33 class LoginRequest; | 34 class LoginRequest; |
| 34 } | 35 } |
| 35 | 36 |
| 36 namespace gcm { | 37 namespace gcm { |
| 37 | 38 |
| 38 class CollapseKey; | 39 class CollapseKey; |
| 39 class ConnectionFactory; | 40 class ConnectionFactory; |
| 41 class GCMStatsRecorder; |
| 40 struct ReliablePacketInfo; | 42 struct ReliablePacketInfo; |
| 41 | 43 |
| 42 // An MCS client. This client is in charge of all communications with an | 44 // An MCS client. This client is in charge of all communications with an |
| 43 // MCS endpoint, and is capable of reliably sending/receiving GCM messages. | 45 // MCS endpoint, and is capable of reliably sending/receiving GCM messages. |
| 44 // NOTE: Not thread safe. This class should live on the same thread as that | 46 // NOTE: Not thread safe. This class should live on the same thread as that |
| 45 // network requests are performed on. | 47 // network requests are performed on. |
| 46 class GCM_EXPORT MCSClient { | 48 class GCM_EXPORT MCSClient { |
| 47 public: | 49 public: |
| 48 // Any change made to this enum should have corresponding change in the | 50 // Any change made to this enum should have corresponding change in the |
| 49 // GetStateString(...) function. | 51 // GetStateString(...) function. |
| 50 enum State { | 52 enum State { |
| 51 UNINITIALIZED, // Uninitialized. | 53 UNINITIALIZED, // Uninitialized. |
| 52 LOADED, // GCM Load finished, waiting to connect. | 54 LOADED, // GCM Load finished, waiting to connect. |
| 53 CONNECTING, // Connection in progress. | 55 CONNECTING, // Connection in progress. |
| 54 CONNECTED, // Connected and running. | 56 CONNECTED, // Connected and running. |
| 55 }; | 57 }; |
| 56 | 58 |
| 59 // Any change made to this enum should have corresponding change in the |
| 60 // GetMessageSendStatusString(...) function in mcs_client.cc. |
| 57 enum MessageSendStatus { | 61 enum MessageSendStatus { |
| 58 // Message was queued succcessfully. | 62 // Message was queued succcessfully. |
| 59 QUEUED, | 63 QUEUED, |
| 60 // Message was sent to the server and the ACK was received. | 64 // Message was sent to the server and the ACK was received. |
| 61 SENT, | 65 SENT, |
| 62 // Message not saved, because total queue size limit reached. | 66 // Message not saved, because total queue size limit reached. |
| 63 QUEUE_SIZE_LIMIT_REACHED, | 67 QUEUE_SIZE_LIMIT_REACHED, |
| 64 // Messgae not saved, because app queue size limit reached. | 68 // Message not saved, because app queue size limit reached. |
| 65 APP_QUEUE_SIZE_LIMIT_REACHED, | 69 APP_QUEUE_SIZE_LIMIT_REACHED, |
| 66 // Message too large to send. | 70 // Message too large to send. |
| 67 MESSAGE_TOO_LARGE, | 71 MESSAGE_TOO_LARGE, |
| 68 // Message not send becuase of TTL = 0 and no working connection. | 72 // Message not send becuase of TTL = 0 and no working connection. |
| 69 NO_CONNECTION_ON_ZERO_TTL, | 73 NO_CONNECTION_ON_ZERO_TTL, |
| 70 // Message exceeded TTL. | 74 // Message exceeded TTL. |
| 71 TTL_EXCEEDED | 75 TTL_EXCEEDED, |
| 76 |
| 77 // NOTE: always keep this entry at the end. Add new status types only |
| 78 // immediately above this line. Make sure to update the corresponding |
| 79 // histogram enum accordingly. |
| 80 SEND_STATUS_COUNT |
| 72 }; | 81 }; |
| 73 | 82 |
| 74 // Callback for MCSClient's error conditions. | 83 // Callback for MCSClient's error conditions. |
| 75 // TODO(fgorski): Keeping it as a callback with intention to add meaningful | 84 // TODO(fgorski): Keeping it as a callback with intention to add meaningful |
| 76 // error information. | 85 // error information. |
| 77 typedef base::Callback<void()> ErrorCallback; | 86 typedef base::Callback<void()> ErrorCallback; |
| 78 // Callback when a message is received. | 87 // Callback when a message is received. |
| 79 typedef base::Callback<void(const MCSMessage& message)> | 88 typedef base::Callback<void(const MCSMessage& message)> |
| 80 OnMessageReceivedCallback; | 89 OnMessageReceivedCallback; |
| 81 // Callback when a message is sent (and receipt has been acknowledged by | 90 // Callback when a message is sent (and receipt has been acknowledged by |
| 82 // the MCS endpoint). | 91 // the MCS endpoint). |
| 83 typedef base::Callback< | 92 typedef base::Callback< |
| 84 void(int64 user_serial_number, | 93 void(int64 user_serial_number, |
| 85 const std::string& app_id, | 94 const std::string& app_id, |
| 86 const std::string& message_id, | 95 const std::string& message_id, |
| 87 MessageSendStatus status)> OnMessageSentCallback; | 96 MessageSendStatus status)> OnMessageSentCallback; |
| 88 | 97 |
| 89 MCSClient(const std::string& version_string, | 98 MCSClient(const std::string& version_string, |
| 90 base::Clock* clock, | 99 base::Clock* clock, |
| 91 ConnectionFactory* connection_factory, | 100 ConnectionFactory* connection_factory, |
| 92 GCMStore* gcm_store); | 101 GCMStore* gcm_store, |
| 102 GCMStatsRecorder* recorder); |
| 93 virtual ~MCSClient(); | 103 virtual ~MCSClient(); |
| 94 | 104 |
| 95 // Initialize the client. Will load any previous id/token information as well | 105 // Initialize the client. Will load any previous id/token information as well |
| 96 // as unacknowledged message information from the GCM storage, if it exists, | 106 // as unacknowledged message information from the GCM storage, if it exists, |
| 97 // passing the id/token information back via |initialization_callback| along | 107 // passing the id/token information back via |initialization_callback| along |
| 98 // with a |success == true| result. If no GCM information is present (and | 108 // with a |success == true| result. If no GCM information is present (and |
| 99 // this is therefore a fresh client), a clean GCM store will be created and | 109 // this is therefore a fresh client), a clean GCM store will be created and |
| 100 // values of 0 will be returned via |initialization_callback| with | 110 // values of 0 will be returned via |initialization_callback| with |
| 101 // |success == true|. | 111 // |success == true|. |
| 102 /// If an error loading the GCM store is encountered, | 112 /// If an error loading the GCM store is encountered, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 120 // Whether to use RMQ depends on whether the protobuf has |ttl| set or not. | 130 // Whether to use RMQ depends on whether the protobuf has |ttl| set or not. |
| 121 // |ttl == 0| denotes the message should only be sent if the connection is | 131 // |ttl == 0| denotes the message should only be sent if the connection is |
| 122 // open. |ttl > 0| will keep the message saved for |ttl| seconds, after which | 132 // open. |ttl > 0| will keep the message saved for |ttl| seconds, after which |
| 123 // it will be dropped if it was unable to be sent. When a message is dropped, | 133 // it will be dropped if it was unable to be sent. When a message is dropped, |
| 124 // |message_sent_callback_| is invoked with a TTL expiration error. | 134 // |message_sent_callback_| is invoked with a TTL expiration error. |
| 125 virtual void SendMessage(const MCSMessage& message); | 135 virtual void SendMessage(const MCSMessage& message); |
| 126 | 136 |
| 127 // Returns the current state of the client. | 137 // Returns the current state of the client. |
| 128 State state() const { return state_; } | 138 State state() const { return state_; } |
| 129 | 139 |
| 140 // Returns the size of the send message queue. |
| 141 int GetSendQueueSize() const; |
| 142 |
| 143 // Returns the size of the un-acked messaage queue. |
| 144 int GetUnackedQueueSize() const; |
| 145 |
| 130 // Returns text representation of the state enum. | 146 // Returns text representation of the state enum. |
| 131 std::string GetStateString() const; | 147 std::string GetStateString() const; |
| 132 | 148 |
| 133 protected: | 149 protected: |
| 134 // Sets a |gcm_store| for testing. Does not take ownership. | 150 // Sets a |gcm_store| for testing. Does not take ownership. |
| 135 // TODO(fgorski): Remove this method. Create GCMEngineFactory that will create | 151 // TODO(fgorski): Remove this method. Create GCMEngineFactory that will create |
| 136 // components of the engine. | 152 // components of the engine. |
| 137 void SetGCMStoreForTesting(GCMStore* gcm_store); | 153 void SetGCMStoreForTesting(GCMStore* gcm_store); |
| 138 | 154 |
| 139 private: | 155 private: |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 // acknowledged. They do not have associated stream ids, and will be | 274 // acknowledged. They do not have associated stream ids, and will be |
| 259 // acknowledged on the next login attempt. | 275 // acknowledged on the next login attempt. |
| 260 PersistentIdList restored_unackeds_server_ids_; | 276 PersistentIdList restored_unackeds_server_ids_; |
| 261 | 277 |
| 262 // The GCM persistent store. Not owned. | 278 // The GCM persistent store. Not owned. |
| 263 GCMStore* gcm_store_; | 279 GCMStore* gcm_store_; |
| 264 | 280 |
| 265 // Manager to handle triggering/detecting heartbeats. | 281 // Manager to handle triggering/detecting heartbeats. |
| 266 HeartbeatManager heartbeat_manager_; | 282 HeartbeatManager heartbeat_manager_; |
| 267 | 283 |
| 284 // Recorder that records GCM activities for debugging purpose. Not owned. |
| 285 GCMStatsRecorder* recorder_; |
| 286 |
| 268 base::WeakPtrFactory<MCSClient> weak_ptr_factory_; | 287 base::WeakPtrFactory<MCSClient> weak_ptr_factory_; |
| 269 | 288 |
| 270 DISALLOW_COPY_AND_ASSIGN(MCSClient); | 289 DISALLOW_COPY_AND_ASSIGN(MCSClient); |
| 271 }; | 290 }; |
| 272 | 291 |
| 273 } // namespace gcm | 292 } // namespace gcm |
| 274 | 293 |
| 275 #endif // GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ | 294 #endif // GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ |
| OLD | NEW |