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_
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"
28 class SequencedTaskRunner
;
31 namespace extensions
{
32 class ExtensionGCMAppHandlerTest
;
36 class URLRequestContextGetter
;
41 class GCMAccountMapper
;
43 class GCMClientFactory
;
44 class GCMDelayedTaskController
;
46 // GCMDriver implementation for desktop and Chrome OS, using GCMClient.
47 class GCMDriverDesktop
: public GCMDriver
,
48 public InstanceIDHandler
{
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
,
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();
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 OutgoingMessage
& message
) override
;
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.
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 OutgoingMessage
& message
);
145 void DoAddInstanceIDData(const std::string
& app_id
,
146 const std::string
& instance_id
,
147 const std::string
& extra_data
);
148 void DoRemoveInstanceIDData(const std::string
& app_id
);
149 void DoGetInstanceIDData(const std::string
& app_id
);
150 void DoGetToken(const std::string
& app_id
,
151 const std::string
& authorized_entity
,
152 const std::string
& scope
,
153 const std::map
<std::string
, std::string
>& options
);
154 void DoDeleteToken(const std::string
& app_id
,
155 const std::string
& authorized_entity
,
156 const std::string
& scope
);
158 // Callbacks posted from IO thread to UI thread.
159 void MessageReceived(const std::string
& app_id
,
160 const IncomingMessage
& message
);
161 void MessagesDeleted(const std::string
& app_id
);
162 void MessageSendError(const std::string
& app_id
,
163 const GCMClient::SendErrorDetails
& send_error_details
);
164 void SendAcknowledged(const std::string
& app_id
,
165 const std::string
& message_id
);
166 void GCMClientReady(const std::vector
<AccountMapping
>& account_mappings
,
167 const base::Time
& last_token_fetch_time
);
168 void OnConnected(const net::IPEndPoint
& ip_endpoint
);
169 void OnDisconnected();
171 void GetGCMStatisticsFinished(const GCMClient::GCMStatistics
& stats
);
172 void GetInstanceIDDataFinished(const std::string
& app_id
,
173 const std::string
& instance_id
,
174 const std::string
& extra_data
);
175 void GetTokenFinished(const std::string
& app_id
,
176 const std::string
& authorized_entity
,
177 const std::string
& scope
,
178 const std::string
& token
,
179 GCMClient::Result result
);
180 void DeleteTokenFinished(const std::string
& app_id
,
181 const std::string
& authorized_entity
,
182 const std::string
& scope
,
183 GCMClient::Result result
);
185 scoped_ptr
<GCMChannelStatusSyncer
> gcm_channel_status_syncer_
;
187 // Flag to indicate whether the user is signed in to a GAIA account.
190 // Flag to indicate if GCM is started.
193 // Flag to indicate if GCM is enabled.
196 // Flag to indicate the last known state of the GCM client. Because this
197 // flag lives on the UI thread, while the GCM client lives on the IO thread,
198 // it may be out of date while connection changes are happening.
201 // List of observers to notify when connection state changes.
202 base::ObserverList
<GCMConnectionObserver
, false> connection_observer_list_
;
204 // Account mapper. Only works when user is signed in.
205 scoped_ptr
<GCMAccountMapper
> account_mapper_
;
207 // Time of last token fetching.
208 base::Time last_token_fetch_time_
;
210 scoped_refptr
<base::SequencedTaskRunner
> ui_thread_
;
211 scoped_refptr
<base::SequencedTaskRunner
> io_thread_
;
213 scoped_ptr
<GCMDelayedTaskController
> delayed_task_controller_
;
215 // Whether the HeartbeatManager should try to wake the system from suspend for
216 // sending heartbeat messages.
217 bool wake_from_suspend_enabled_
;
219 // For all the work occurring on the IO thread. Must be destroyed on the IO
221 scoped_ptr
<IOWorker
> io_worker_
;
223 // Callback for GetGCMStatistics.
224 GetGCMStatisticsCallback request_gcm_statistics_callback_
;
226 // Callbacks for GetInstanceIDData.
227 std::map
<std::string
, GetInstanceIDDataCallback
>
228 get_instance_id_data_callbacks_
;
230 // Callbacks for GetToken/DeleteToken.
231 std::map
<TokenTuple
, GetTokenCallback
, TokenTupleComparer
>
232 get_token_callbacks_
;
233 std::map
<TokenTuple
, DeleteTokenCallback
, TokenTupleComparer
>
234 delete_token_callbacks_
;
236 // Used to pass a weak pointer to the IO worker.
237 base::WeakPtrFactory
<GCMDriverDesktop
> weak_ptr_factory_
;
239 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop
);
244 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_