Fix AfterTyping experiment for Android.
[chromium-blink-merge.git] / components / gcm_driver / gcm_driver_desktop.h
blobaa0182e13ec9028c27e83db2b652a48e73de8540
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.
5 #ifndef COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_
6 #define COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/compiler_specific.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/tuple.h"
19 #include "components/gcm_driver/gcm_channel_status_syncer.h"
20 #include "components/gcm_driver/gcm_client.h"
21 #include "components/gcm_driver/gcm_connection_observer.h"
22 #include "components/gcm_driver/gcm_driver.h"
24 class PrefService;
26 namespace base {
27 class FilePath;
28 class SequencedTaskRunner;
31 namespace extensions {
32 class ExtensionGCMAppHandlerTest;
35 namespace net {
36 class URLRequestContextGetter;
39 namespace gcm {
41 class GCMAccountMapper;
42 class GCMAppHandler;
43 class GCMClientFactory;
44 class GCMDelayedTaskController;
46 // GCMDriver implementation for desktop and Chrome OS, using GCMClient.
47 class GCMDriverDesktop : public GCMDriver,
48 public InstanceIDHandler {
49 public:
50 GCMDriverDesktop(
51 scoped_ptr<GCMClientFactory> gcm_client_factory,
52 const GCMClient::ChromeBuildInfo& chrome_build_info,
53 const std::string& channel_status_request_url,
54 const std::string& user_agent,
55 PrefService* prefs,
56 const base::FilePath& store_path,
57 const scoped_refptr<net::URLRequestContextGetter>& request_context,
58 const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
59 const scoped_refptr<base::SequencedTaskRunner>& io_thread,
60 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner);
61 ~GCMDriverDesktop() override;
63 // GCMDriver overrides:
64 void Shutdown() override;
65 void OnSignedIn() override;
66 void OnSignedOut() override;
67 void AddAppHandler(const std::string& app_id,
68 GCMAppHandler* handler) override;
69 void RemoveAppHandler(const std::string& app_id) override;
70 void AddConnectionObserver(GCMConnectionObserver* observer) override;
71 void RemoveConnectionObserver(GCMConnectionObserver* observer) override;
72 void Enable() override;
73 void Disable() override;
74 GCMClient* GetGCMClientForTesting() const override;
75 bool IsStarted() const override;
76 bool IsConnected() const override;
77 void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
78 bool clear_logs) override;
79 void SetGCMRecording(const GetGCMStatisticsCallback& callback,
80 bool recording) override;
81 void SetAccountTokens(
82 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) override;
83 void UpdateAccountMapping(const AccountMapping& account_mapping) override;
84 void RemoveAccountMapping(const std::string& account_id) override;
85 base::Time GetLastTokenFetchTime() override;
86 void SetLastTokenFetchTime(const base::Time& time) override;
87 void WakeFromSuspendForHeartbeat(bool wake) override;
88 InstanceIDHandler* GetInstanceIDHandler() override;
89 void AddHeartbeatInterval(const std::string& scope, int interval_ms) override;
90 void RemoveHeartbeatInterval(const std::string& scope) override;
92 // InstanceIDHandler overrides:
93 void GetToken(const std::string& app_id,
94 const std::string& authorized_entity,
95 const std::string& scope,
96 const std::map<std::string, std::string>& options,
97 const GetTokenCallback& callback) override;
98 void DeleteToken(const std::string& app_id,
99 const std::string& authorized_entity,
100 const std::string& scope,
101 const DeleteTokenCallback& callback) override;
102 void AddInstanceIDData(const std::string& app_id,
103 const std::string& instance_id,
104 const std::string& extra_data) override;
105 void RemoveInstanceIDData(const std::string& app_id) override;
106 void GetInstanceIDData(const std::string& app_id,
107 const GetInstanceIDDataCallback& callback) override;
109 // Exposed for testing purpose.
110 bool gcm_enabled() const { return gcm_enabled_; }
111 GCMChannelStatusSyncer* gcm_channel_status_syncer_for_testing() {
112 return gcm_channel_status_syncer_.get();
115 protected:
116 // GCMDriver implementation:
117 GCMClient::Result EnsureStarted(GCMClient::StartMode start_mode) override;
118 void RegisterImpl(const std::string& app_id,
119 const std::vector<std::string>& sender_ids) override;
120 void UnregisterImpl(const std::string& app_id) override;
121 void SendImpl(const std::string& app_id,
122 const std::string& receiver_id,
123 const GCMClient::OutgoingMessage& message) override;
125 private:
126 class IOWorker;
128 typedef base::Tuple<std::string, std::string, std::string> TokenTuple;
129 struct TokenTupleComparer {
130 bool operator()(const TokenTuple& a, const TokenTuple& b) const;
133 // Stops the GCM service. It can be restarted by calling EnsureStarted again.
134 void Stop();
136 // Remove cached data when GCM service is stopped.
137 void RemoveCachedData();
139 void DoRegister(const std::string& app_id,
140 const std::vector<std::string>& sender_ids);
141 void DoUnregister(const std::string& app_id);
142 void DoSend(const std::string& app_id,
143 const std::string& receiver_id,
144 const GCMClient::OutgoingMessage& message);
145 void DoGetToken(const std::string& app_id,
146 const std::string& authorized_entity,
147 const std::string& scope,
148 const std::map<std::string, std::string>& options);
149 void DoDeleteToken(const std::string& app_id,
150 const std::string& authorized_entity,
151 const std::string& scope);
153 // Callbacks posted from IO thread to UI thread.
154 void MessageReceived(const std::string& app_id,
155 const GCMClient::IncomingMessage& message);
156 void MessagesDeleted(const std::string& app_id);
157 void MessageSendError(const std::string& app_id,
158 const GCMClient::SendErrorDetails& send_error_details);
159 void SendAcknowledged(const std::string& app_id,
160 const std::string& message_id);
161 void GCMClientReady(const std::vector<AccountMapping>& account_mappings,
162 const base::Time& last_token_fetch_time);
163 void OnConnected(const net::IPEndPoint& ip_endpoint);
164 void OnDisconnected();
166 void GetGCMStatisticsFinished(const GCMClient::GCMStatistics& stats);
167 void GetInstanceIDDataFinished(const std::string& app_id,
168 const std::string& instance_id,
169 const std::string& extra_data);
170 void GetTokenFinished(const std::string& app_id,
171 const std::string& authorized_entity,
172 const std::string& scope,
173 const std::string& token,
174 GCMClient::Result result);
175 void DeleteTokenFinished(const std::string& app_id,
176 const std::string& authorized_entity,
177 const std::string& scope,
178 GCMClient::Result result);
180 scoped_ptr<GCMChannelStatusSyncer> gcm_channel_status_syncer_;
182 // Flag to indicate whether the user is signed in to a GAIA account.
183 bool signed_in_;
185 // Flag to indicate if GCM is started.
186 bool gcm_started_;
188 // Flag to indicate if GCM is enabled.
189 bool gcm_enabled_;
191 // Flag to indicate the last known state of the GCM client. Because this
192 // flag lives on the UI thread, while the GCM client lives on the IO thread,
193 // it may be out of date while connection changes are happening.
194 bool connected_;
196 // List of observers to notify when connection state changes.
197 base::ObserverList<GCMConnectionObserver, false> connection_observer_list_;
199 // Account mapper. Only works when user is signed in.
200 scoped_ptr<GCMAccountMapper> account_mapper_;
202 // Time of last token fetching.
203 base::Time last_token_fetch_time_;
205 scoped_refptr<base::SequencedTaskRunner> ui_thread_;
206 scoped_refptr<base::SequencedTaskRunner> io_thread_;
208 scoped_ptr<GCMDelayedTaskController> delayed_task_controller_;
210 // Whether the HeartbeatManager should try to wake the system from suspend for
211 // sending heartbeat messages.
212 bool wake_from_suspend_enabled_;
214 // For all the work occurring on the IO thread. Must be destroyed on the IO
215 // thread.
216 scoped_ptr<IOWorker> io_worker_;
218 // Callback for GetGCMStatistics.
219 GetGCMStatisticsCallback request_gcm_statistics_callback_;
221 // Callbacks for GetInstanceIDData.
222 std::map<std::string, GetInstanceIDDataCallback>
223 get_instance_id_data_callbacks_;
225 // Callbacks for GetToken/DeleteToken.
226 std::map<TokenTuple, GetTokenCallback, TokenTupleComparer>
227 get_token_callbacks_;
228 std::map<TokenTuple, DeleteTokenCallback, TokenTupleComparer>
229 delete_token_callbacks_;
231 // Used to pass a weak pointer to the IO worker.
232 base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_;
234 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop);
237 } // namespace gcm
239 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_