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_FAKE_GCM_CLIENT_H_ 6 #define COMPONENTS_GCM_DRIVER_FAKE_GCM_CLIENT_H_ 7 8 #include <map> 9 10 #include "base/compiler_specific.h" 11 #include "base/macros.h" 12 #include "base/memory/weak_ptr.h" 13 #include "base/timer/timer.h" 14 #include "components/gcm_driver/gcm_client.h" 15 #include "components/gcm_driver/gcm_stats_recorder_impl.h" 16 #include "mojo/public/cpp/bindings/pending_receiver.h" 17 18 namespace base { 19 class SequencedTaskRunner; 20 } 21 22 namespace gcm { 23 24 class FakeGCMClient : public GCMClient { 25 public: 26 // For testing purpose. 27 enum StartModeOverridding { 28 // No change to how delay start is handled. 29 RESPECT_START_MODE, 30 // Force to delay start GCM until PerformDelayedStart is called. 31 FORCE_TO_ALWAYS_DELAY_START_GCM, 32 }; 33 34 // Generate and return the registration ID/token based on parameters for 35 // testing verification. 36 static std::string GenerateGCMRegistrationID( 37 const std::vector<std::string>& sender_ids); 38 static std::string GenerateInstanceIDToken( 39 const std::string& authorized_entity, const std::string& scope); 40 41 FakeGCMClient(const scoped_refptr<base::SequencedTaskRunner>& ui_thread, 42 const scoped_refptr<base::SequencedTaskRunner>& io_thread); 43 ~FakeGCMClient() override; 44 45 // Overridden from GCMClient: 46 // Called on IO thread. 47 void Initialize( 48 const ChromeBuildInfo& chrome_build_info, 49 const base::FilePath& store_path, 50 bool remove_account_mappings_with_email_key, 51 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, 52 scoped_refptr<base::SequencedTaskRunner> io_task_runner, 53 base::RepeatingCallback<void( 54 mojo::PendingReceiver<network::mojom::ProxyResolvingSocketFactory>)> 55 get_socket_factory_callback, 56 const scoped_refptr<network::SharedURLLoaderFactory>& url_loader_factory, 57 network::NetworkConnectionTracker* network_connection_tracker, 58 std::unique_ptr<Encryptor> encryptor, 59 Delegate* delegate) override; 60 void Start(StartMode start_mode) override; 61 void Stop() override; 62 void Register(scoped_refptr<RegistrationInfo> registration_info) override; 63 bool ValidateRegistration(scoped_refptr<RegistrationInfo> registration_info, 64 const std::string& registration_id) override; 65 void Unregister(scoped_refptr<RegistrationInfo> registration_info) override; 66 void Send(const std::string& app_id, 67 const std::string& receiver_id, 68 const OutgoingMessage& message) override; 69 void RecordDecryptionFailure(const std::string& app_id, 70 GCMDecryptionResult result) override; 71 void SetRecording(bool recording) override; 72 void ClearActivityLogs() override; 73 GCMStatistics GetStatistics() const override; 74 void SetAccountTokens( 75 const std::vector<AccountTokenInfo>& account_tokens) override; 76 void UpdateAccountMapping(const AccountMapping& account_mapping) override; 77 void RemoveAccountMapping(const CoreAccountId& account_id) override; 78 void SetLastTokenFetchTime(const base::Time& time) override; 79 void UpdateHeartbeatTimer( 80 std::unique_ptr<base::RetainingOneShotTimer> timer) override; 81 void AddInstanceIDData(const std::string& app_id, 82 const std::string& instance_id, 83 const std::string& extra_data) override; 84 void RemoveInstanceIDData(const std::string& app_id) override; 85 void GetInstanceIDData(const std::string& app_id, 86 std::string* instance_id, 87 std::string* extra_data) override; 88 void AddHeartbeatInterval(const std::string& scope, int interval_ms) override; 89 void RemoveHeartbeatInterval(const std::string& scope) override; 90 91 // Initiate the start that has been delayed. 92 // Called on UI thread. 93 void PerformDelayedStart(); 94 95 // Simulate receiving something from the server. 96 // Called on UI thread. 97 void ReceiveMessage(const std::string& app_id, 98 const IncomingMessage& message); 99 void DeleteMessages(const std::string& app_id); 100 set_start_mode_overridding(StartModeOverridding overridding)101 void set_start_mode_overridding(StartModeOverridding overridding) { 102 start_mode_overridding_ = overridding; 103 } 104 105 private: 106 // Called on IO thread. 107 void DoStart(); 108 void Started(); 109 void RegisterFinished(scoped_refptr<RegistrationInfo> registration_info, 110 const std::string& registrion_id); 111 void UnregisterFinished(scoped_refptr<RegistrationInfo> registration_info); 112 void SendFinished(const std::string& app_id, const OutgoingMessage& message); 113 void MessageReceived(const std::string& app_id, 114 const IncomingMessage& message); 115 void MessagesDeleted(const std::string& app_id); 116 void MessageSendError(const std::string& app_id, 117 const SendErrorDetails& send_error_details); 118 void SendAcknowledgement(const std::string& app_id, 119 const std::string& message_id); 120 121 Delegate* delegate_; 122 std::string product_category_for_subtypes_; 123 bool started_; 124 StartMode start_mode_; 125 StartModeOverridding start_mode_overridding_; 126 scoped_refptr<base::SequencedTaskRunner> ui_thread_; 127 scoped_refptr<base::SequencedTaskRunner> io_thread_; 128 std::map<std::string, std::pair<std::string, std::string>> instance_id_data_; 129 GCMStatsRecorderImpl recorder_; 130 base::WeakPtrFactory<FakeGCMClient> weak_ptr_factory_{this}; 131 132 DISALLOW_COPY_AND_ASSIGN(FakeGCMClient); 133 }; 134 135 } // namespace gcm 136 137 #endif // COMPONENTS_GCM_DRIVER_FAKE_GCM_CLIENT_H_ 138