fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame^] | 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 | void CreateAndSendMessage(const AccountMapping& account_mapping); |
| 67 | |
| 68 | // Callback for sending a message. |
| 69 | void OnSendFinished(const std::string& account_id, |
| 70 | const std::string& message_id, |
| 71 | GCMClient::Result result); |
| 72 | |
| 73 | // Checks whether the update can be triggered now. If the current time is |
| 74 | // within reasonable time (6 hours) of when the update is due, we want to |
| 75 | // trigger the update immediately to take advantage of a fresh OAuth2 token. |
| 76 | bool CanTriggerUpdate(const base::Time& last_update_time) const; |
| 77 | |
| 78 | // Checks whether last status change is older than a TTL of a message. |
| 79 | bool IsLastStatusChangeOlderThanTTL( |
| 80 | const AccountMapping& account_mapping) const; |
| 81 | |
| 82 | // Finds an account mapping in |accounts_| by |account_id|. |
| 83 | AccountMapping* FindMappingByAccountId(const std::string& account_id); |
| 84 | // Finds an account mapping in |accounts_| by |message_id|. |
| 85 | // Returns iterator that can be used to delete the account. |
| 86 | AccountMappings::iterator FindMappingByMessageId( |
| 87 | const std::string& message_id); |
| 88 | |
| 89 | // Sets the clock for testing. |
| 90 | void SetClockForTesting(scoped_ptr<base::Clock> clock); |
| 91 | |
| 92 | // GCMDriver owns GCMAccountMapper. |
| 93 | GCMDriver* gcm_driver_; |
| 94 | |
| 95 | // Clock for timestamping status changes. |
| 96 | scoped_ptr<base::Clock> clock_; |
| 97 | |
| 98 | // Currnetly tracked account mappings. |
| 99 | AccountMappings accounts_; |
| 100 | |
| 101 | // GCM Registration ID of the account mapper. |
| 102 | std::string registration_id_; |
| 103 | |
| 104 | bool initialized_; |
| 105 | |
| 106 | base::WeakPtrFactory<GCMAccountMapper> weak_ptr_factory_; |
| 107 | |
| 108 | DISALLOW_COPY_AND_ASSIGN(GCMAccountMapper); |
| 109 | }; |
| 110 | |
| 111 | } // namespace gcm |
| 112 | |
| 113 | #endif // COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_ |