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,
50 const std::string& channel_status_request_url,
51 const std::string& user_agent,
53 const base::FilePath& store_path,
54 const scoped_refptr<net::URLRequestContextGetter>& request_context,
55 const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
56 const scoped_refptr<base::SequencedTaskRunner>& io_thread,
57 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner);
58 virtual ~GCMDriverDesktop();
60 // GCMDriver overrides:
61 virtual void Shutdown() OVERRIDE;
62 virtual void OnSignedIn() OVERRIDE;
63 virtual void OnSignedOut() OVERRIDE;
64 virtual void Purge() OVERRIDE;
65 virtual void AddAppHandler(const std::string& app_id,
66 GCMAppHandler* handler) OVERRIDE;
67 virtual void RemoveAppHandler(const std::string& app_id) OVERRIDE;
68 virtual void AddConnectionObserver(GCMConnectionObserver* observer) OVERRIDE;
69 virtual void RemoveConnectionObserver(
70 GCMConnectionObserver* observer) OVERRIDE;
71 virtual void Enable() OVERRIDE;
72 virtual void Disable() OVERRIDE;
73 virtual GCMClient* GetGCMClientForTesting() const OVERRIDE;
74 virtual bool IsStarted() const OVERRIDE;
75 virtual bool IsConnected() const OVERRIDE;
76 virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
77 bool clear_logs) OVERRIDE;
78 virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback,
79 bool recording) OVERRIDE;
80 virtual void UpdateAccountMapping(
81 const AccountMapping& account_mapping) OVERRIDE;
82 virtual void RemoveAccountMapping(const std::string& account_id) OVERRIDE;
84 // GCMDriverDesktop specific implementation.
85 // Sets a list of accounts with OAuth2 tokens for the next checkin.
86 // |account_tokens| maps email addresses to OAuth2 access tokens.
87 // |account_removed| indicates that an account has been removed since the
88 // last time the callback was called, which triggers an immediate checkin,
89 // to ensure that association between device and account is removed.
90 void SetAccountsForCheckin(
91 const std::map<std::string, std::string>& account_tokens);
93 // Exposed for testing purpose.
94 bool gcm_enabled() const { return gcm_enabled_; }
95 GCMChannelStatusSyncer* gcm_channel_status_syncer_for_testing() {
96 return gcm_channel_status_syncer_.get();
100 // GCMDriver implementation:
101 virtual GCMClient::Result EnsureStarted() OVERRIDE;
102 virtual void RegisterImpl(
103 const std::string& app_id,
104 const std::vector<std::string>& sender_ids) OVERRIDE;
105 virtual void UnregisterImpl(const std::string& app_id) OVERRIDE;
106 virtual void SendImpl(const std::string& app_id,
107 const std::string& receiver_id,
108 const GCMClient::OutgoingMessage& message) OVERRIDE;
113 // Stops the GCM service. It can be restarted by calling EnsureStarted again.
116 // Remove cached data when GCM service is stopped.
117 void RemoveCachedData();
119 void DoRegister(const std::string& app_id,
120 const std::vector<std::string>& sender_ids);
121 void DoUnregister(const std::string& app_id);
122 void DoSend(const std::string& app_id,
123 const std::string& receiver_id,
124 const GCMClient::OutgoingMessage& message);
126 // Callbacks posted from IO thread to UI thread.
127 void MessageReceived(const std::string& app_id,
128 const GCMClient::IncomingMessage& message);
129 void MessagesDeleted(const std::string& app_id);
130 void MessageSendError(const std::string& app_id,
131 const GCMClient::SendErrorDetails& send_error_details);
132 void SendAcknowledged(const std::string& app_id,
133 const std::string& message_id);
135 const std::vector<AccountMapping>& account_mappings);
136 void OnConnected(const net::IPEndPoint& ip_endpoint);
137 void OnDisconnected();
139 void GetGCMStatisticsFinished(const GCMClient::GCMStatistics& stats);
141 scoped_ptr<GCMChannelStatusSyncer> gcm_channel_status_syncer_;
143 // Flag to indicate whether the user is signed in to a GAIA account.
144 // TODO(jianli): To be removed when sign-in enforcement is dropped.
147 // Flag to indicate if GCM is started.
150 // Flag to indicate if GCM is enabled.
151 // TODO(jianli): Removed when we switch completely to support all users.
154 // Flag to indicate the last known state of the GCM client. Because this
155 // flag lives on the UI thread, while the GCM client lives on the IO thread,
156 // it may be out of date while connection changes are happening.
159 // List of observers to notify when connection state changes.
160 // Makes sure list is empty on destruction.
161 ObserverList<GCMConnectionObserver, true> connection_observer_list_;
163 scoped_refptr<base::SequencedTaskRunner> ui_thread_;
164 scoped_refptr<base::SequencedTaskRunner> io_thread_;
166 scoped_ptr<GCMDelayedTaskController> delayed_task_controller_;
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_