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 "components/gcm_driver/gcm_client.h"
18 #include "components/gcm_driver/gcm_driver.h"
22 class SequencedTaskRunner;
25 namespace extensions {
26 class ExtensionGCMAppHandlerTest;
30 class URLRequestContextGetter;
36 class GCMClientFactory;
38 // GCMDriver implementation for desktop and Chrome OS, using GCMClient.
39 class GCMDriverDesktop : public GCMDriver {
42 scoped_ptr<GCMClientFactory> gcm_client_factory,
43 const GCMClient::ChromeBuildInfo& chrome_build_info,
44 const base::FilePath& store_path,
45 const scoped_refptr<net::URLRequestContextGetter>& request_context,
46 const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
47 const scoped_refptr<base::SequencedTaskRunner>& io_thread,
48 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner);
49 virtual ~GCMDriverDesktop();
51 // GCMDriver overrides:
52 virtual void Shutdown() OVERRIDE;
53 virtual void OnSignedIn() OVERRIDE;
54 virtual void Purge() OVERRIDE;
55 virtual void AddAppHandler(const std::string& app_id,
56 GCMAppHandler* handler) OVERRIDE;
57 virtual void RemoveAppHandler(const std::string& app_id) OVERRIDE;
58 virtual void Enable() OVERRIDE;
59 virtual void Disable() OVERRIDE;
60 virtual GCMClient* GetGCMClientForTesting() const OVERRIDE;
61 virtual bool IsStarted() const OVERRIDE;
62 virtual bool IsConnected() const OVERRIDE;
63 virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
64 bool clear_logs) OVERRIDE;
65 virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback,
66 bool recording) OVERRIDE;
68 // GCMDriverDesktop specific implementation.
69 // Sets a list of accounts with OAuth2 tokens for the next checkin.
70 // |account_tokens| maps email addresses to OAuth2 access tokens.
71 // |account_removed| indicates that an account has been removed since the
72 // last time the callback was called, which triggers an immediate checkin,
73 // to ensure that association between device and account is removed.
74 void SetAccountsForCheckin(
75 const std::map<std::string, std::string>& account_tokens);
78 // GCMDriver implementation:
79 virtual GCMClient::Result EnsureStarted() OVERRIDE;
80 virtual void RegisterImpl(
81 const std::string& app_id,
82 const std::vector<std::string>& sender_ids) OVERRIDE;
83 virtual void UnregisterImpl(const std::string& app_id) OVERRIDE;
84 virtual void SendImpl(const std::string& app_id,
85 const std::string& receiver_id,
86 const GCMClient::OutgoingMessage& message) OVERRIDE;
89 class DelayedTaskController;
92 // Stops the GCM service. It can be restarted by calling EnsureStarted again.
95 // Remove cached data when GCM service is stopped.
96 void RemoveCachedData();
98 void DoRegister(const std::string& app_id,
99 const std::vector<std::string>& sender_ids);
100 void DoUnregister(const std::string& app_id);
101 void DoSend(const std::string& app_id,
102 const std::string& receiver_id,
103 const GCMClient::OutgoingMessage& message);
105 // Callbacks posted from IO thread to UI thread.
106 void MessageReceived(const std::string& app_id,
107 const GCMClient::IncomingMessage& message);
108 void MessagesDeleted(const std::string& app_id);
109 void MessageSendError(const std::string& app_id,
110 const GCMClient::SendErrorDetails& send_error_details);
111 void SendAcknowledged(const std::string& app_id,
112 const std::string& message_id);
113 void GCMClientReady();
114 void OnConnected(const net::IPEndPoint& ip_endpoint);
115 void OnDisconnected();
117 void GetGCMStatisticsFinished(const GCMClient::GCMStatistics& stats);
119 // Flag to indicate whether the user is signed in to a GAIA account.
120 // TODO(jianli): To be removed when sign-in enforcement is dropped.
123 // Flag to indicate if GCM is started.
126 // Flag to indicate if GCM is enabled.
129 // Flag to indicate the last known state of the GCM client. Because this
130 // flag lives on the UI thread, while the GCM client lives on the IO thread,
131 // it may be out of date while connection changes are happening.
134 scoped_refptr<base::SequencedTaskRunner> ui_thread_;
135 scoped_refptr<base::SequencedTaskRunner> io_thread_;
137 scoped_ptr<DelayedTaskController> delayed_task_controller_;
139 // For all the work occurring on the IO thread. Must be destroyed on the IO
141 scoped_ptr<IOWorker> io_worker_;
143 // Callback for GetGCMStatistics.
144 GetGCMStatisticsCallback request_gcm_statistics_callback_;
146 // Used to pass a weak pointer to the IO worker.
147 base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_;
149 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop);
154 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_