1 // Copyright 2014 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.
5 #ifndef GOOGLE_APIS_GCM_ENGINE_USER_LIST_H_
6 #define GOOGLE_APIS_GCM_ENGINE_USER_LIST_H_
12 #include "base/callback.h"
13 #include "google_apis/gcm/engine/gcm_store.h"
14 #include "google_apis/gcm/gcm_client.h"
18 GCM_EXPORT
extern const int64 kInvalidSerialNumber
;
20 // UserList stores mappings between usernames, serial numbers and delegates to
21 // enable dispatching messages to applications.
22 class GCM_EXPORT UserList
{
24 // A callback used by SetDelegate method to return a |user_serial_number|
25 // assigned to the delegate identified by |username|.
26 typedef base::Callback
<void(const std::string
& username
,
27 int64 user_serial_number
)> SetDelegateCallback
;
29 explicit UserList(GCMStore
* gcm_store
);
32 // Initializes the User List with a set of mappings and next serial number.
33 void Initialize(const GCMStore::SerialNumberMappings
& result
);
35 // Sets a user delegate for |username|. It will create a new entry for the
36 // user if one does not exist.
37 void SetDelegate(const std::string
& username
,
38 GCMClient::Delegate
* delegate
,
39 const SetDelegateCallback
& callback
);
41 // Returns a delegate for the user identified by |serial_number| or NULL, if
42 // a matching delegate was not found.
43 GCMClient::Delegate
* GetDelegateBySerialNumber(int64 serial_number
) const;
45 // Returns a delegate for the user identified by |username| or NULL, if a
46 // matching delegate was not found.
47 GCMClient::Delegate
* GetDelegateByUsername(const std::string
& username
) const;
49 // Returns all delegates.
50 std::vector
<GCMClient::Delegate
*> GetAllDelegates() const;
52 // Gets the serial number assigned to a specified |username|, if one is
53 // assigned, the value will be positive. If there is no matching delegate or
54 // it is not yet assigned a serial number, the result will be equal to
55 // kInvalidSerialNumber.
56 int64
GetSerialNumberForUsername(const std::string
& username
) const;
58 // Returns serial numbers of all active users.
59 std::vector
<int64
> GetAllActiveUserSerialNumbers() const;
62 friend class UserListTest
;
66 explicit UserInfo(int64 serial_number
);
67 UserInfo(GCMClient::Delegate
* delegate
,
68 const SetDelegateCallback
& callback
);
73 // Delegate related to the username. Not owned by the UserDelegate.
74 GCMClient::Delegate
* delegate
;
75 SetDelegateCallback callback
;
77 typedef std::map
<std::string
, UserInfo
> UserInfoMap
;
79 // Assigns a serial number to the user identitified by |username|.
80 void AssignSerialNumber(const std::string
& username
);
82 // A callback invoked once the Backend is done updating the next serial
84 void IncrementSerialNumberCompleted(const std::string
& username
,
85 int64 user_serial_number
,
88 // Callback for serial number completion.
89 void AssignSerialNumberCompleted(const std::string
& username
, bool success
);
91 // Concludes the process of setting a delegate by running a callback with
92 // |username| and |serial_number| assigned to that |username|. It will also
93 // reset the callback, so that it is not called again.
94 void OnSerialNumberReady(const UserInfoMap::iterator
& iter
);
96 // Sets the serial number related to the username. It expects the entry to not
97 // exist yet and will create it.
98 void SetSerialNumber(const std::string
& username
, int64 serial_number
);
101 int64 next_serial_number_
;
102 UserInfoMap delegates_
;
103 GCMStore
* gcm_store_
;
105 DISALLOW_COPY_AND_ASSIGN(UserList
);
110 #endif // GOOGLE_APIS_GCM_ENGINE_USER_LIST_H_