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;
41 class GCMClientFactory;
42 class GCMDelayedTaskController;
44 // GCMDriver implementation for desktop and Chrome OS, using GCMClient.
45 class GCMDriverDesktop : public GCMDriver {
48 scoped_ptr<GCMClientFactory> gcm_client_factory,
49 const GCMClient::ChromeBuildInfo& chrome_build_info,
51 const base::FilePath& store_path,
52 const scoped_refptr<net::URLRequestContextGetter>& request_context,
53 const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
54 const scoped_refptr<base::SequencedTaskRunner>& io_thread,
55 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner);
56 virtual ~GCMDriverDesktop();
58 // GCMDriver overrides:
59 virtual void Shutdown() OVERRIDE;
60 virtual void OnSignedIn() OVERRIDE;
61 virtual void Purge() OVERRIDE;
62 virtual void AddAppHandler(const std::string& app_id,
63 GCMAppHandler* handler) OVERRIDE;
64 virtual void RemoveAppHandler(const std::string& app_id) OVERRIDE;
65 virtual void AddConnectionObserver(GCMConnectionObserver* observer) OVERRIDE;
66 virtual void RemoveConnectionObserver(
67 GCMConnectionObserver* observer) OVERRIDE;
68 virtual void Enable() OVERRIDE;
69 virtual void Disable() OVERRIDE;
70 virtual GCMClient* GetGCMClientForTesting() const OVERRIDE;
71 virtual bool IsStarted() const OVERRIDE;
72 virtual bool IsConnected() const OVERRIDE;
73 virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
74 bool clear_logs) OVERRIDE;
75 virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback,
76 bool recording) OVERRIDE;
77 virtual void UpdateAccountMapping(
78 const AccountMapping& account_mapping) OVERRIDE;
79 virtual void RemoveAccountMapping(const std::string& account_id) OVERRIDE;
81 // GCMDriverDesktop specific implementation.
82 // Sets a list of accounts with OAuth2 tokens for the next checkin.
83 // |account_tokens| maps email addresses to OAuth2 access tokens.
84 // |account_removed| indicates that an account has been removed since the
85 // last time the callback was called, which triggers an immediate checkin,
86 // to ensure that association between device and account is removed.
87 void SetAccountsForCheckin(
88 const std::map<std::string, std::string>& account_tokens);
90 // Exposed for testing purpose.
91 bool gcm_enabled() const { return gcm_enabled_; }
92 GCMChannelStatusSyncer* gcm_channel_status_syncer_for_testing() {
93 return gcm_channel_status_syncer_.get();
97 // GCMDriver implementation:
98 virtual GCMClient::Result EnsureStarted() OVERRIDE;
99 virtual void RegisterImpl(
100 const std::string& app_id,
101 const std::vector<std::string>& sender_ids) OVERRIDE;
102 virtual void UnregisterImpl(const std::string& app_id) OVERRIDE;
103 virtual void SendImpl(const std::string& app_id,
104 const std::string& receiver_id,
105 const GCMClient::OutgoingMessage& message) OVERRIDE;
110 // Stops the GCM service. It can be restarted by calling EnsureStarted again.
113 // Remove cached data when GCM service is stopped.
114 void RemoveCachedData();
116 void DoRegister(const std::string& app_id,
117 const std::vector<std::string>& sender_ids);
118 void DoUnregister(const std::string& app_id);
119 void DoSend(const std::string& app_id,
120 const std::string& receiver_id,
121 const GCMClient::OutgoingMessage& message);
123 // Callbacks posted from IO thread to UI thread.
124 void MessageReceived(const std::string& app_id,
125 const GCMClient::IncomingMessage& message);
126 void MessagesDeleted(const std::string& app_id);
127 void MessageSendError(const std::string& app_id,
128 const GCMClient::SendErrorDetails& send_error_details);
129 void SendAcknowledged(const std::string& app_id,
130 const std::string& message_id);
132 const std::vector<AccountMapping>& account_mappings);
133 void OnConnected(const net::IPEndPoint& ip_endpoint);
134 void OnDisconnected();
136 void GetGCMStatisticsFinished(const GCMClient::GCMStatistics& stats);
138 scoped_ptr<GCMChannelStatusSyncer> gcm_channel_status_syncer_;
140 // Flag to indicate whether the user is signed in to a GAIA account.
141 // TODO(jianli): To be removed when sign-in enforcement is dropped.
144 // Flag to indicate if GCM is started.
147 // Flag to indicate if GCM is enabled.
148 // TODO(jianli): Removed when we switch completely to support all users.
151 // Flag to indicate the last known state of the GCM client. Because this
152 // flag lives on the UI thread, while the GCM client lives on the IO thread,
153 // it may be out of date while connection changes are happening.
156 // List of observers to notify when connection state changes.
157 // Makes sure list is empty on destruction.
158 ObserverList<GCMConnectionObserver, true> connection_observer_list_;
160 scoped_refptr<base::SequencedTaskRunner> ui_thread_;
161 scoped_refptr<base::SequencedTaskRunner> io_thread_;
163 scoped_ptr<GCMDelayedTaskController> delayed_task_controller_;
165 // For all the work occurring on the IO thread. Must be destroyed on the IO
167 scoped_ptr<IOWorker> io_worker_;
169 // Callback for GetGCMStatistics.
170 GetGCMStatisticsCallback request_gcm_statistics_callback_;
172 // Used to pass a weak pointer to the IO worker.
173 base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_;
175 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop);
180 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_