OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef GOOGLE_APIS_GCM_ENGINE_USER_LIST_H_ | |
6 #define GOOGLE_APIS_GCM_ENGINE_USER_LIST_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/callback.h" | |
12 #include "google_apis/gcm/engine/gcm_store.h" | |
13 #include "google_apis/gcm/gcm_client.h" | |
14 | |
15 namespace gcm { | |
16 | |
17 // UserList stores mappings between usernames, serial numbers and delegates to | |
18 // enable dispatching messages to applications. | |
19 class GCM_EXPORT UserList { | |
20 public: | |
21 // A callback used by SetDelegate method to return a |user_serial_number| | |
22 // assigned to the delegate identified by |username|. | |
23 typedef base::Callback<void(const std::string& username, | |
24 int64 user_serial_number)> SetDelegateCallback; | |
25 | |
26 explicit UserList(GCMStore* gcm_store); | |
27 ~UserList(); | |
28 | |
29 // Initializes the User List with a set of mappings and next serial number. | |
30 void Initialize(const GCMStore::SerialNumberMappings& result); | |
31 | |
32 // Sets a user delegate for |username|. It will create a new entry for the | |
33 // user if one does not exist. | |
34 void SetDelegate(const std::string& username, | |
35 GCMClient::Delegate* delegate, | |
36 const SetDelegateCallback& callback); | |
37 | |
38 // Returns a delegate for the user identified by |serial_number| or NULL, if | |
39 // a matching delegate was not found. | |
40 GCMClient::Delegate* GetDelegateBySerialNumber(int64 serial_number) const; | |
41 | |
42 // Returns a delegate for the user identified by |username| or NULL, if a | |
43 // matching delegate was not found. | |
44 GCMClient::Delegate* GetDelegateByUsername(const std::string& username) const; | |
45 | |
46 // Gets the serial number assigned to a specified |username|, if one is | |
47 // assigned. When return value is true, |serial_number| will be set to that | |
48 // value. If the result is false, |serial_number| does not contain a valid | |
49 // value. | |
50 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.
| |
51 int64* serial_number) const; | |
52 | |
53 private: | |
54 friend class UserListTest; | |
55 | |
56 struct UserInfo { | |
57 UserInfo(); | |
58 explicit UserInfo(int64 serial_number); | |
59 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.
| |
60 ~UserInfo(); | |
61 | |
62 int64 serial_number; | |
63 // Delegate related to the username. Not owned by the UserDelegate. | |
64 GCMClient::Delegate* delegate; | |
65 SetDelegateCallback callback; | |
66 }; | |
67 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.
| |
68 | |
69 // Assigns a serial number to the user identitified by |username|. | |
70 void AssignSerialNumber(const std::string& username); | |
71 | |
72 // A callback invoked once the Backend is done updating the next serial | |
73 // number. | |
74 void IncrementSerialNumberCompleted(const std::string& username, | |
75 int64 user_serial_number, | |
76 bool success); | |
77 | |
78 // Callback for serial number completion. | |
79 void AssignSerialNumberCompleted(const std::string& username, bool success); | |
80 | |
81 // Concludes the process of setting a delegate by running a callback with | |
82 // |username| and |serial_number| assigned to that |username|. It will also | |
83 // reset the callback, so that it is not called again. | |
84 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.
| |
85 | |
86 // Sets the serial number related to the username. It expects the entry to not | |
87 // exist yet and will create it. | |
88 void SetSerialNumber(const std::string& username, int64 serial_number); | |
89 | |
90 bool initialized_; | |
91 int64 next_serial_number_; | |
92 UserInfoMap delegates_; | |
93 GCMStore* gcm_store_; | |
94 | |
95 DISALLOW_COPY_AND_ASSIGN(UserList); | |
96 }; | |
97 | |
98 } // namespace gcm | |
99 | |
100 #endif // GOOGLE_APIS_GCM_ENGINE_USER_LIST_H_ | |
OLD | NEW |