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