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 COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_ | |
6 #define COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "components/gcm_driver/gcm_app_handler.h" | |
15 #include "components/gcm_driver/gcm_client.h" | |
16 #include "google_apis/gcm/engine/account_mapping.h" | |
17 | |
18 namespace base { | |
19 class Clock; | |
20 } | |
21 | |
22 namespace gcm { | |
23 | |
24 class GCMDriver; | |
25 | |
26 // Class for mapping signed-in GAIA accounts to the GCM Device ID. | |
27 class GCMAccountMapper : public GCMAppHandler { | |
28 public: | |
29 // List of account mappings. | |
30 typedef std::vector<AccountMapping> AccountMappings; | |
31 | |
32 explicit GCMAccountMapper(GCMDriver* gcm_driver); | |
33 virtual ~GCMAccountMapper(); | |
34 | |
35 void Initialize(const AccountMappings& account_mappings, | |
36 const std::string& registration_id); | |
37 | |
38 // Called by AccountTracker, when a new list of account tokens is available. | |
39 // This will cause a refresh of account mappings and sending updates to GCM. | |
40 void SetAccountTokens( | |
41 const std::vector<GCMClient::AccountTokenInfo> account_tokens); | |
42 | |
43 // Implementation of GCMAppHandler: | |
44 virtual void ShutdownHandler() OVERRIDE; | |
45 virtual void OnMessage(const std::string& app_id, | |
46 const GCMClient::IncomingMessage& message) OVERRIDE; | |
47 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; | |
48 virtual void OnSendError( | |
49 const std::string& app_id, | |
50 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE; | |
51 virtual void OnSendAcknowledged(const std::string& app_id, | |
52 const std::string& message_id) OVERRIDE; | |
53 virtual bool CanHandle(const std::string& app_id) const OVERRIDE; | |
54 | |
55 private: | |
56 friend class GCMAccountMapperTest; | |
57 | |
58 typedef std::map<std::string, GCMClient::OutgoingMessage> OutgoingMessages; | |
59 | |
60 // Informs GCM of an added or refreshed account mapping. | |
61 void SendAddMappingMessage(AccountMapping& account_mapping); | |
62 | |
63 // Informs GCM of a removed account mapping. | |
64 void SendRemoveMappingMessage(AccountMapping& account_mapping); | |
65 | |
66 // Callback for sending a message. | |
67 void OnSendFinished(const std::string& account_id, | |
68 const std::string& message_id, | |
69 GCMClient::Result result); | |
70 | |
71 // Checks whether the update can be triggered now. If the current time is | |
72 // within reasonable time (6 hours) of when the update is due, we want to | |
73 // trigger the update immediately to take advantage of a fresh OAuth2 token. | |
74 bool CanTriggerUpdate(const base::Time& last_update_time) const; | |
75 | |
76 // Checks whether last status change is older than a TTL of a message. | |
77 bool IsLastStatusChangeOlderThanTTL( | |
78 const AccountMapping& account_mapping) const; | |
79 | |
80 // Finds an account mapping in |accounts_| by |account_id|. | |
81 AccountMapping* FindMappingByAccountId(const std::string& account_id); | |
82 // Finds an account mapping in |accounts_| by |message_id|. | |
83 // Returns iterator that can be used to delete the account. | |
84 AccountMappings::iterator FindMappingByMessageId( | |
85 const std::string& message_id); | |
86 | |
87 // Sets the clock for testing. | |
88 void SetClockForTesting(scoped_ptr<base::Clock> clock); | |
89 | |
90 // GCMDriver owns GCMAccountMapper. | |
91 GCMDriver* gcm_driver_; | |
92 | |
93 // Clock for timestamping status changes. | |
94 scoped_ptr<base::Clock> clock_; | |
95 | |
96 // Currnetly tracked account mappings. | |
97 AccountMappings accounts_; | |
98 | |
99 // GCM Registration ID of the account mapper. | |
100 std::string registration_id_; | |
101 | |
102 // Is the account mapper initialized. | |
jianli
2014/09/02 18:00:10
nit: no need to comment this variable
fgorski
2014/09/03 21:37:02
Done.
| |
103 bool initialized_; | |
104 | |
105 base::WeakPtrFactory<GCMAccountMapper> weak_ptr_factory_; | |
106 | |
107 DISALLOW_COPY_AND_ASSIGN(GCMAccountMapper); | |
108 }; | |
109 | |
110 } // namespace gcm | |
111 | |
112 #endif // COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_ | |
OLD | NEW |