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
18namespace base {
19class Clock;
20}
21
22namespace gcm {
23
24class GCMDriver;
25
26// Class for mapping signed-in GAIA accounts to the GCM Device ID.
27class 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
37  // Called by AccountTracker, when a new list of account tokens is available.
38  // This will cause a refresh of account mappings and sending updates to GCM.
39  void SetAccountTokens(
40      const std::vector<GCMClient::AccountTokenInfo> account_tokens);
41
42  // Implementation of GCMAppHandler:
43  virtual void ShutdownHandler() OVERRIDE;
44  virtual void OnMessage(const std::string& app_id,
45                         const GCMClient::IncomingMessage& message) OVERRIDE;
46  virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE;
47  virtual void OnSendError(
48      const std::string& app_id,
49      const GCMClient::SendErrorDetails& send_error_details) OVERRIDE;
50  virtual void OnSendAcknowledged(const std::string& app_id,
51                                  const std::string& message_id) OVERRIDE;
52  virtual bool CanHandle(const std::string& app_id) const OVERRIDE;
53
54 private:
55  friend class GCMAccountMapperTest;
56
57  typedef std::map<std::string, GCMClient::OutgoingMessage> OutgoingMessages;
58
59  // Checks whether account mapper is ready to process new account tokens.
60  bool IsReady();
61
62  // Informs GCM of an added or refreshed account mapping.
63  void SendAddMappingMessage(AccountMapping& account_mapping);
64
65  // Informs GCM of a removed account mapping.
66  void SendRemoveMappingMessage(AccountMapping& account_mapping);
67
68  void CreateAndSendMessage(const AccountMapping& account_mapping);
69
70  // Callback for sending a message.
71  void OnSendFinished(const std::string& account_id,
72                      const std::string& message_id,
73                      GCMClient::Result result);
74
75  // Gets a registration for account mapper from GCM.
76  void GetRegistration();
77
78  // Callback for registering with GCM.
79  void OnRegisterFinished(const std::string& registration_id,
80                          GCMClient::Result result);
81
82  // Checks whether the update can be triggered now. If the current time is
83  // within reasonable time (6 hours) of when the update is due, we want to
84  // trigger the update immediately to take advantage of a fresh OAuth2 token.
85  bool CanTriggerUpdate(const base::Time& last_update_time) const;
86
87  // Checks whether last status change is older than a TTL of a message.
88  bool IsLastStatusChangeOlderThanTTL(
89      const AccountMapping& account_mapping) const;
90
91  // Finds an account mapping in |accounts_| by |account_id|.
92  AccountMapping* FindMappingByAccountId(const std::string& account_id);
93  // Finds an account mapping in |accounts_| by |message_id|.
94  // Returns iterator that can be used to delete the account.
95  AccountMappings::iterator FindMappingByMessageId(
96      const std::string& message_id);
97
98  // Sets the clock for testing.
99  void SetClockForTesting(scoped_ptr<base::Clock> clock);
100
101  // GCMDriver owns GCMAccountMapper.
102  GCMDriver* gcm_driver_;
103
104  // Clock for timestamping status changes.
105  scoped_ptr<base::Clock> clock_;
106
107  // Currnetly tracked account mappings.
108  AccountMappings accounts_;
109
110  std::vector<GCMClient::AccountTokenInfo> pending_account_tokens_;
111
112  // GCM Registration ID of the account mapper.
113  std::string registration_id_;
114
115  bool initialized_;
116
117  base::WeakPtrFactory<GCMAccountMapper> weak_ptr_factory_;
118
119  DISALLOW_COPY_AND_ASSIGN(GCMAccountMapper);
120};
121
122}  // namespace gcm
123
124#endif  // COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_
125