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 "components/gcm_driver/gcm_channel_status_syncer.h"
19 #include "components/gcm_driver/gcm_client.h"
20 #include "components/gcm_driver/gcm_connection_observer.h"
21 #include "components/gcm_driver/gcm_driver.h"
27 class SequencedTaskRunner
;
30 namespace extensions
{
31 class ExtensionGCMAppHandlerTest
;
35 class URLRequestContextGetter
;
40 class GCMAccountMapper
;
42 class GCMClientFactory
;
43 class GCMDelayedTaskController
;
45 // GCMDriver implementation for desktop and Chrome OS, using GCMClient.
46 class GCMDriverDesktop
: public GCMDriver
{
49 scoped_ptr
<GCMClientFactory
> gcm_client_factory
,
50 const GCMClient::ChromeBuildInfo
& chrome_build_info
,
51 const std::string
& channel_status_request_url
,
52 const std::string
& user_agent
,
54 const base::FilePath
& store_path
,
55 const scoped_refptr
<net::URLRequestContextGetter
>& request_context
,
56 const scoped_refptr
<base::SequencedTaskRunner
>& ui_thread
,
57 const scoped_refptr
<base::SequencedTaskRunner
>& io_thread
,
58 const scoped_refptr
<base::SequencedTaskRunner
>& blocking_task_runner
);
59 ~GCMDriverDesktop() override
;
61 // GCMDriver overrides:
62 void Shutdown() override
;
63 void OnSignedIn() override
;
64 void OnSignedOut() override
;
65 void AddAppHandler(const std::string
& app_id
,
66 GCMAppHandler
* handler
) override
;
67 void RemoveAppHandler(const std::string
& app_id
) override
;
68 void AddConnectionObserver(GCMConnectionObserver
* observer
) override
;
69 void RemoveConnectionObserver(GCMConnectionObserver
* observer
) override
;
70 void Enable() override
;
71 void Disable() override
;
72 GCMClient
* GetGCMClientForTesting() const override
;
73 bool IsStarted() const override
;
74 bool IsConnected() const override
;
75 void GetGCMStatistics(const GetGCMStatisticsCallback
& callback
,
76 bool clear_logs
) override
;
77 void SetGCMRecording(const GetGCMStatisticsCallback
& callback
,
78 bool recording
) override
;
79 void SetAccountTokens(
80 const std::vector
<GCMClient::AccountTokenInfo
>& account_tokens
) override
;
81 void UpdateAccountMapping(const AccountMapping
& account_mapping
) override
;
82 void RemoveAccountMapping(const std::string
& account_id
) override
;
83 base::Time
GetLastTokenFetchTime() override
;
84 void SetLastTokenFetchTime(const base::Time
& time
) override
;
85 void WakeFromSuspendForHeartbeat(bool wake
) override
;
87 // Exposed for testing purpose.
88 bool gcm_enabled() const { return gcm_enabled_
; }
89 GCMChannelStatusSyncer
* gcm_channel_status_syncer_for_testing() {
90 return gcm_channel_status_syncer_
.get();
94 // GCMDriver implementation:
95 GCMClient::Result
EnsureStarted(GCMClient::StartMode start_mode
) override
;
96 void RegisterImpl(const std::string
& app_id
,
97 const std::vector
<std::string
>& sender_ids
) override
;
98 void UnregisterImpl(const std::string
& app_id
) override
;
99 void SendImpl(const std::string
& app_id
,
100 const std::string
& receiver_id
,
101 const GCMClient::OutgoingMessage
& message
) override
;
106 // Stops the GCM service. It can be restarted by calling EnsureStarted again.
109 // Remove cached data when GCM service is stopped.
110 void RemoveCachedData();
112 void DoRegister(const std::string
& app_id
,
113 const std::vector
<std::string
>& sender_ids
);
114 void DoUnregister(const std::string
& app_id
);
115 void DoSend(const std::string
& app_id
,
116 const std::string
& receiver_id
,
117 const GCMClient::OutgoingMessage
& message
);
119 // Callbacks posted from IO thread to UI thread.
120 void MessageReceived(const std::string
& app_id
,
121 const GCMClient::IncomingMessage
& message
);
122 void MessagesDeleted(const std::string
& app_id
);
123 void MessageSendError(const std::string
& app_id
,
124 const GCMClient::SendErrorDetails
& send_error_details
);
125 void SendAcknowledged(const std::string
& app_id
,
126 const std::string
& message_id
);
127 void GCMClientReady(const std::vector
<AccountMapping
>& account_mappings
,
128 const base::Time
& last_token_fetch_time
);
129 void OnConnected(const net::IPEndPoint
& ip_endpoint
);
130 void OnDisconnected();
132 void GetGCMStatisticsFinished(const GCMClient::GCMStatistics
& stats
);
134 scoped_ptr
<GCMChannelStatusSyncer
> gcm_channel_status_syncer_
;
136 // Flag to indicate whether the user is signed in to a GAIA account.
139 // Flag to indicate if GCM is started.
142 // Flag to indicate if GCM is enabled.
145 // Flag to indicate the last known state of the GCM client. Because this
146 // flag lives on the UI thread, while the GCM client lives on the IO thread,
147 // it may be out of date while connection changes are happening.
150 // List of observers to notify when connection state changes.
151 ObserverList
<GCMConnectionObserver
, false> connection_observer_list_
;
153 // Account mapper. Only works when user is signed in.
154 scoped_ptr
<GCMAccountMapper
> account_mapper_
;
156 // Time of last token fetching.
157 base::Time last_token_fetch_time_
;
159 scoped_refptr
<base::SequencedTaskRunner
> ui_thread_
;
160 scoped_refptr
<base::SequencedTaskRunner
> io_thread_
;
162 scoped_ptr
<GCMDelayedTaskController
> delayed_task_controller_
;
164 // Whether the HeartbeatManager should try to wake the system from suspend for
165 // sending heartbeat messages.
166 bool wake_from_suspend_enabled_
;
168 // For all the work occurring on the IO thread. Must be destroyed on the IO
170 scoped_ptr
<IOWorker
> io_worker_
;
172 // Callback for GetGCMStatistics.
173 GetGCMStatisticsCallback request_gcm_statistics_callback_
;
175 // Used to pass a weak pointer to the IO worker.
176 base::WeakPtrFactory
<GCMDriverDesktop
> weak_ptr_factory_
;
178 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop
);
183 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_