blob: 51a98458a7e1e4dd43138ab20f11369a1db1e5d6 [file] [log] [blame]
[email protected]21b77652014-05-31 01:21:091// 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.
4
5#ifndef COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_
6#define COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
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"
fgorski0d5c00d2014-08-28 16:21:4517#include "base/observer_list.h"
[email protected]cd57f372014-06-09 17:13:0618#include "components/gcm_driver/gcm_client.h"
fgorski0d5c00d2014-08-28 16:21:4519#include "components/gcm_driver/gcm_connection_observer.h"
[email protected]21b77652014-05-31 01:21:0920#include "components/gcm_driver/gcm_driver.h"
[email protected]21b77652014-05-31 01:21:0921
22namespace base {
23class FilePath;
24class SequencedTaskRunner;
25}
26
27namespace extensions {
28class ExtensionGCMAppHandlerTest;
29}
30
31namespace net {
32class URLRequestContextGetter;
33}
34
35namespace gcm {
36
37class GCMAppHandler;
38class GCMClientFactory;
fgorski05d507c2014-09-10 17:28:1039class GCMDelayedTaskController;
[email protected]21b77652014-05-31 01:21:0940
41// GCMDriver implementation for desktop and Chrome OS, using GCMClient.
[email protected]da54623f2014-06-17 17:03:5542class GCMDriverDesktop : public GCMDriver {
[email protected]21b77652014-05-31 01:21:0943 public:
44 GCMDriverDesktop(
45 scoped_ptr<GCMClientFactory> gcm_client_factory,
[email protected]21b77652014-05-31 01:21:0946 const GCMClient::ChromeBuildInfo& chrome_build_info,
47 const base::FilePath& store_path,
48 const scoped_refptr<net::URLRequestContextGetter>& request_context,
49 const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
50 const scoped_refptr<base::SequencedTaskRunner>& io_thread,
51 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner);
52 virtual ~GCMDriverDesktop();
53
54 // GCMDriver overrides:
55 virtual void Shutdown() OVERRIDE;
[email protected]da54623f2014-06-17 17:03:5556 virtual void OnSignedIn() OVERRIDE;
jianli69676522014-09-13 20:45:4157 virtual void OnSignedOut() OVERRIDE;
[email protected]da54623f2014-06-17 17:03:5558 virtual void Purge() OVERRIDE;
[email protected]21b77652014-05-31 01:21:0959 virtual void AddAppHandler(const std::string& app_id,
60 GCMAppHandler* handler) OVERRIDE;
61 virtual void RemoveAppHandler(const std::string& app_id) OVERRIDE;
fgorski0d5c00d2014-08-28 16:21:4562 virtual void AddConnectionObserver(GCMConnectionObserver* observer) OVERRIDE;
63 virtual void RemoveConnectionObserver(
64 GCMConnectionObserver* observer) OVERRIDE;
[email protected]21b77652014-05-31 01:21:0965 virtual void Enable() OVERRIDE;
66 virtual void Disable() OVERRIDE;
[email protected]21b77652014-05-31 01:21:0967 virtual GCMClient* GetGCMClientForTesting() const OVERRIDE;
68 virtual bool IsStarted() const OVERRIDE;
[email protected]fc6078a2014-06-14 08:28:4369 virtual bool IsConnected() const OVERRIDE;
[email protected]21b77652014-05-31 01:21:0970 virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
71 bool clear_logs) OVERRIDE;
72 virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback,
73 bool recording) OVERRIDE;
[email protected]72d4f252014-08-20 22:34:2874 virtual void UpdateAccountMapping(
75 const AccountMapping& account_mapping) OVERRIDE;
76 virtual void RemoveAccountMapping(const std::string& account_id) OVERRIDE;
[email protected]21b77652014-05-31 01:21:0977
[email protected]7df5ef22014-07-17 07:35:5878 // GCMDriverDesktop specific implementation.
79 // Sets a list of accounts with OAuth2 tokens for the next checkin.
80 // |account_tokens| maps email addresses to OAuth2 access tokens.
81 // |account_removed| indicates that an account has been removed since the
82 // last time the callback was called, which triggers an immediate checkin,
83 // to ensure that association between device and account is removed.
84 void SetAccountsForCheckin(
85 const std::map<std::string, std::string>& account_tokens);
86
[email protected]c27c10792014-06-05 15:27:2387 protected:
88 // GCMDriver implementation:
89 virtual GCMClient::Result EnsureStarted() OVERRIDE;
90 virtual void RegisterImpl(
91 const std::string& app_id,
92 const std::vector<std::string>& sender_ids) OVERRIDE;
93 virtual void UnregisterImpl(const std::string& app_id) OVERRIDE;
94 virtual void SendImpl(const std::string& app_id,
95 const std::string& receiver_id,
96 const GCMClient::OutgoingMessage& message) OVERRIDE;
[email protected]21b77652014-05-31 01:21:0997
98 private:
[email protected]21b77652014-05-31 01:21:0999 class IOWorker;
100
[email protected]21b77652014-05-31 01:21:09101 // Stops the GCM service. It can be restarted by calling EnsureStarted again.
102 void Stop();
103
104 // Remove cached data when GCM service is stopped.
105 void RemoveCachedData();
106
[email protected]21b77652014-05-31 01:21:09107 void DoRegister(const std::string& app_id,
108 const std::vector<std::string>& sender_ids);
109 void DoUnregister(const std::string& app_id);
110 void DoSend(const std::string& app_id,
111 const std::string& receiver_id,
112 const GCMClient::OutgoingMessage& message);
113
114 // Callbacks posted from IO thread to UI thread.
[email protected]21b77652014-05-31 01:21:09115 void MessageReceived(const std::string& app_id,
[email protected]25908352014-06-03 10:19:11116 const GCMClient::IncomingMessage& message);
[email protected]21b77652014-05-31 01:21:09117 void MessagesDeleted(const std::string& app_id);
118 void MessageSendError(const std::string& app_id,
119 const GCMClient::SendErrorDetails& send_error_details);
[email protected]292af2b22014-08-06 19:42:45120 void SendAcknowledged(const std::string& app_id,
121 const std::string& message_id);
[email protected]21b77652014-05-31 01:21:09122 void GCMClientReady();
[email protected]fc6078a2014-06-14 08:28:43123 void OnConnected(const net::IPEndPoint& ip_endpoint);
124 void OnDisconnected();
[email protected]21b77652014-05-31 01:21:09125
[email protected]25908352014-06-03 10:19:11126 void GetGCMStatisticsFinished(const GCMClient::GCMStatistics& stats);
[email protected]21b77652014-05-31 01:21:09127
[email protected]da54623f2014-06-17 17:03:55128 // Flag to indicate whether the user is signed in to a GAIA account.
129 // TODO(jianli): To be removed when sign-in enforcement is dropped.
130 bool signed_in_;
131
132 // Flag to indicate if GCM is started.
133 bool gcm_started_;
134
[email protected]21b77652014-05-31 01:21:09135 // Flag to indicate if GCM is enabled.
136 bool gcm_enabled_;
137
[email protected]fc6078a2014-06-14 08:28:43138 // Flag to indicate the last known state of the GCM client. Because this
139 // flag lives on the UI thread, while the GCM client lives on the IO thread,
140 // it may be out of date while connection changes are happening.
141 bool connected_;
142
fgorski0d5c00d2014-08-28 16:21:45143 // List of observers to notify when connection state changes.
144 // Makes sure list is empty on destruction.
145 ObserverList<GCMConnectionObserver, true> connection_observer_list_;
146
[email protected]21b77652014-05-31 01:21:09147 scoped_refptr<base::SequencedTaskRunner> ui_thread_;
148 scoped_refptr<base::SequencedTaskRunner> io_thread_;
149
fgorski05d507c2014-09-10 17:28:10150 scoped_ptr<GCMDelayedTaskController> delayed_task_controller_;
[email protected]21b77652014-05-31 01:21:09151
152 // For all the work occurring on the IO thread. Must be destroyed on the IO
153 // thread.
154 scoped_ptr<IOWorker> io_worker_;
155
[email protected]21b77652014-05-31 01:21:09156 // Callback for GetGCMStatistics.
157 GetGCMStatisticsCallback request_gcm_statistics_callback_;
158
159 // Used to pass a weak pointer to the IO worker.
160 base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_;
161
162 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop);
163};
164
165} // namespace gcm
166
167#endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_