fake_gcm_client.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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 "base/compiler_specific.h"
9#include "base/memory/weak_ptr.h"
10#include "components/gcm_driver/gcm_client.h"
11
12namespace base {
13class SequencedTaskRunner;
14}
15
16namespace gcm {
17
18class FakeGCMClient : public GCMClient {
19 public:
20  enum Status {
21    UNINITIALIZED,
22    STARTED,
23    STOPPED,
24    CHECKED_OUT
25  };
26
27  enum StartMode {
28    NO_DELAY_START,
29    DELAY_START,
30  };
31
32  FakeGCMClient(StartMode start_mode,
33                const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
34                const scoped_refptr<base::SequencedTaskRunner>& io_thread);
35  virtual ~FakeGCMClient();
36
37  // Overridden from GCMClient:
38  // Called on IO thread.
39  virtual void Initialize(
40      const ChromeBuildInfo& chrome_build_info,
41      const base::FilePath& store_path,
42      const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
43      const scoped_refptr<net::URLRequestContextGetter>&
44          url_request_context_getter,
45      scoped_ptr<Encryptor> encryptor,
46      Delegate* delegate) OVERRIDE;
47  virtual void Start() OVERRIDE;
48  virtual void Stop() OVERRIDE;
49  virtual void CheckOut() OVERRIDE;
50  virtual void Register(const std::string& app_id,
51                        const std::vector<std::string>& sender_ids) OVERRIDE;
52  virtual void Unregister(const std::string& app_id) OVERRIDE;
53  virtual void Send(const std::string& app_id,
54                    const std::string& receiver_id,
55                    const OutgoingMessage& message) OVERRIDE;
56  virtual void SetRecording(bool recording) OVERRIDE;
57  virtual void ClearActivityLogs() OVERRIDE;
58  virtual GCMStatistics GetStatistics() const OVERRIDE;
59  virtual void SetAccountsForCheckin(
60      const std::map<std::string, std::string>& account_tokens) OVERRIDE;
61
62  // Initiate the loading that has been delayed.
63  // Called on UI thread.
64  void PerformDelayedLoading();
65
66  // Simulate receiving something from the server.
67  // Called on UI thread.
68  void ReceiveMessage(const std::string& app_id,
69                      const IncomingMessage& message);
70  void DeleteMessages(const std::string& app_id);
71
72  static std::string GetRegistrationIdFromSenderIds(
73      const std::vector<std::string>& sender_ids);
74
75  Status status() const { return status_; }
76
77 private:
78  // Called on IO thread.
79  void DoLoading();
80  void CheckinFinished();
81  void RegisterFinished(const std::string& app_id,
82                        const std::string& registrion_id);
83  void UnregisterFinished(const std::string& app_id);
84  void SendFinished(const std::string& app_id, const OutgoingMessage& message);
85  void MessageReceived(const std::string& app_id,
86                       const IncomingMessage& message);
87  void MessagesDeleted(const std::string& app_id);
88  void MessageSendError(const std::string& app_id,
89                        const SendErrorDetails& send_error_details);
90  void SendAcknowledgement(const std::string& app_id,
91                           const std::string& message_id);
92
93  Delegate* delegate_;
94  Status status_;
95  StartMode start_mode_;
96  scoped_refptr<base::SequencedTaskRunner> ui_thread_;
97  scoped_refptr<base::SequencedTaskRunner> io_thread_;
98  base::WeakPtrFactory<FakeGCMClient> weak_ptr_factory_;
99
100  DISALLOW_COPY_AND_ASSIGN(FakeGCMClient);
101};
102
103}  // namespace gcm
104
105#endif  // COMPONENTS_GCM_DRIVER_FAKE_GCM_CLIENT_H_
106