Chromium Code Reviews| Index: google_apis/gcm/engine/user_list.h |
| diff --git a/google_apis/gcm/engine/user_list.h b/google_apis/gcm/engine/user_list.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a6f06f6e540f1a17615b66c9203ea62d62f14d41 |
| --- /dev/null |
| +++ b/google_apis/gcm/engine/user_list.h |
| @@ -0,0 +1,100 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef GOOGLE_APIS_GCM_ENGINE_USER_LIST_H_ |
| +#define GOOGLE_APIS_GCM_ENGINE_USER_LIST_H_ |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/callback.h" |
| +#include "google_apis/gcm/engine/gcm_store.h" |
| +#include "google_apis/gcm/gcm_client.h" |
| + |
| +namespace gcm { |
| + |
| +// UserList stores mappings between usernames, serial numbers and delegates to |
| +// enable dispatching messages to applications. |
| +class GCM_EXPORT UserList { |
| + public: |
| + // A callback used by SetDelegate method to return a |user_serial_number| |
| + // assigned to the delegate identified by |username|. |
| + typedef base::Callback<void(const std::string& username, |
| + int64 user_serial_number)> SetDelegateCallback; |
| + |
| + explicit UserList(GCMStore* gcm_store); |
| + ~UserList(); |
| + |
| + // Initializes the User List with a set of mappings and next serial number. |
| + void Initialize(const GCMStore::SerialNumberMappings& result); |
| + |
| + // Sets a user delegate for |username|. It will create a new entry for the |
| + // user if one does not exist. |
| + void SetDelegate(const std::string& username, |
| + GCMClient::Delegate* delegate, |
| + const SetDelegateCallback& callback); |
| + |
| + // Returns a delegate for the user identified by |serial_number| or NULL, if |
| + // a matching delegate was not found. |
| + GCMClient::Delegate* GetDelegateBySerialNumber(int64 serial_number) const; |
| + |
| + // Returns a delegate for the user identified by |username| or NULL, if a |
| + // matching delegate was not found. |
| + GCMClient::Delegate* GetDelegateByUsername(const std::string& username) const; |
| + |
| + // Gets the serial number assigned to a specified |username|, if one is |
| + // assigned. When return value is true, |serial_number| will be set to that |
| + // value. If the result is false, |serial_number| does not contain a valid |
| + // value. |
| + bool GetSerialNumberForUsername(const std::string& username, |
|
jianli
2014/01/15 01:07:09
nit: probably simpler to expose kSerialNumberMissi
fgorski
2014/01/15 20:42:47
Done.
|
| + int64* serial_number) const; |
| + |
| + private: |
| + friend class UserListTest; |
| + |
| + struct UserInfo { |
| + UserInfo(); |
| + explicit UserInfo(int64 serial_number); |
| + UserInfo(GCMClient::Delegate* delegate, const SetDelegateCallback callback); |
|
Nicolas Zea
2014/01/14 23:56:42
const ref callback
fgorski
2014/01/15 01:29:21
Done.
|
| + ~UserInfo(); |
| + |
| + int64 serial_number; |
| + // Delegate related to the username. Not owned by the UserDelegate. |
| + GCMClient::Delegate* delegate; |
| + SetDelegateCallback callback; |
| + }; |
| + typedef std::map<std::string, UserInfo> UserInfoMap; |
|
jianli
2014/01/15 01:07:09
nit: include map
fgorski
2014/01/15 20:42:47
Done.
|
| + |
| + // Assigns a serial number to the user identitified by |username|. |
| + void AssignSerialNumber(const std::string& username); |
| + |
| + // A callback invoked once the Backend is done updating the next serial |
| + // number. |
| + void IncrementSerialNumberCompleted(const std::string& username, |
| + int64 user_serial_number, |
| + bool success); |
| + |
| + // Callback for serial number completion. |
| + void AssignSerialNumberCompleted(const std::string& username, bool success); |
| + |
| + // Concludes the process of setting a delegate by running a callback with |
| + // |username| and |serial_number| assigned to that |username|. It will also |
| + // reset the callback, so that it is not called again. |
| + void RespondWithSerialNumber(UserInfoMap::iterator& iter); |
|
Nicolas Zea
2014/01/14 23:56:42
const ref
jianli
2014/01/15 01:07:09
nit: name it as OnSerialNumberReady?
fgorski
2014/01/15 01:29:21
Done.
fgorski
2014/01/15 20:42:47
Done.
|
| + |
| + // Sets the serial number related to the username. It expects the entry to not |
| + // exist yet and will create it. |
| + void SetSerialNumber(const std::string& username, int64 serial_number); |
| + |
| + bool initialized_; |
| + int64 next_serial_number_; |
| + UserInfoMap delegates_; |
| + GCMStore* gcm_store_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(UserList); |
| +}; |
| + |
| +} // namespace gcm |
| + |
| +#endif // GOOGLE_APIS_GCM_ENGINE_USER_LIST_H_ |