summaryrefslogtreecommitdiffstats
path: root/components/gcm_driver/fake_gcm_client.h
blob: badaffd1faddcec09abc98d9bc67680540f2233f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_GCM_DRIVER_FAKE_GCM_CLIENT_H_
#define COMPONENTS_GCM_DRIVER_FAKE_GCM_CLIENT_H_

#include <map>

#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "components/gcm_driver/gcm_client.h"
#include "components/gcm_driver/gcm_stats_recorder_impl.h"

namespace base {
class SequencedTaskRunner;
}

namespace gcm {

class FakeGCMClient : public GCMClient {
 public:
  // For testing purpose.
  enum StartModeOverridding {
    // No change to how delay start is handled.
    RESPECT_START_MODE,
    // Force to delay start GCM until PerformDelayedStart is called.
    FORCE_TO_ALWAYS_DELAY_START_GCM,
  };

  // Generate and return the registration ID/token based on parameters for
  // testing verification.
  static std::string GenerateGCMRegistrationID(
      const std::vector<std::string>& sender_ids);
  static std::string GenerateInstanceIDToken(
      const std::string& authorized_entity, const std::string& scope);

  FakeGCMClient(const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
                const scoped_refptr<base::SequencedTaskRunner>& io_thread);
  ~FakeGCMClient() override;

  // Overridden from GCMClient:
  // Called on IO thread.
  void Initialize(
      const ChromeBuildInfo& chrome_build_info,
      const base::FilePath& store_path,
      const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
      const scoped_refptr<net::URLRequestContextGetter>&
          url_request_context_getter,
      scoped_ptr<Encryptor> encryptor,
      Delegate* delegate) override;
  void Start(StartMode start_mode) override;
  void Stop() override;
  void Register(const linked_ptr<RegistrationInfo>& registration_info) override;
  void Unregister(
      const linked_ptr<RegistrationInfo>& registration_info) override;
  void Send(const std::string& app_id,
            const std::string& receiver_id,
            const OutgoingMessage& message) override;
  void RecordDecryptionFailure(const std::string& app_id,
                               GCMEncryptionProvider::DecryptionResult result)
      override;
  void SetRecording(bool recording) override;
  void ClearActivityLogs() override;
  GCMStatistics GetStatistics() const override;
  void SetAccountTokens(
      const std::vector<AccountTokenInfo>& account_tokens) override;
  void UpdateAccountMapping(const AccountMapping& account_mapping) override;
  void RemoveAccountMapping(const std::string& account_id) override;
  void SetLastTokenFetchTime(const base::Time& time) override;
  void UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer) override;
  void AddInstanceIDData(const std::string& app_id,
                         const std::string& instance_id,
                         const std::string& extra_data) override;
  void RemoveInstanceIDData(const std::string& app_id) override;
  void GetInstanceIDData(const std::string& app_id,
                         std::string* instance_id,
                         std::string* extra_data) override;
  void AddHeartbeatInterval(const std::string& scope, int interval_ms) override;
  void RemoveHeartbeatInterval(const std::string& scope) override;

  // Initiate the start that has been delayed.
  // Called on UI thread.
  void PerformDelayedStart();

  // Simulate receiving something from the server.
  // Called on UI thread.
  void ReceiveMessage(const std::string& app_id,
                      const IncomingMessage& message);
  void DeleteMessages(const std::string& app_id);

  void set_start_mode_overridding(StartModeOverridding overridding) {
    start_mode_overridding_ = overridding;
  }

 private:
  // Called on IO thread.
  void DoStart();
  void Started();
  void RegisterFinished(
      const linked_ptr<RegistrationInfo>& registration_info,
      const std::string& registrion_id);
  void UnregisterFinished(
      const linked_ptr<RegistrationInfo>& registration_info);
  void SendFinished(const std::string& app_id, const OutgoingMessage& message);
  void MessageReceived(const std::string& app_id,
                       const IncomingMessage& message);
  void MessagesDeleted(const std::string& app_id);
  void MessageSendError(const std::string& app_id,
                        const SendErrorDetails& send_error_details);
  void SendAcknowledgement(const std::string& app_id,
                           const std::string& message_id);

  Delegate* delegate_;
  bool started_;
  StartMode start_mode_;
  StartModeOverridding start_mode_overridding_;
  scoped_refptr<base::SequencedTaskRunner> ui_thread_;
  scoped_refptr<base::SequencedTaskRunner> io_thread_;
  std::map<std::string, std::pair<std::string, std::string>> instance_id_data_;
  GCMStatsRecorderImpl recorder_;
  base::WeakPtrFactory<FakeGCMClient> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(FakeGCMClient);
};

}  // namespace gcm

#endif  // COMPONENTS_GCM_DRIVER_FAKE_GCM_CLIENT_H_