Make sure webrtc::VideoSource is released when WebRtcVideoTrackAdapter is destroyed.
[chromium-blink-merge.git] / components / gcm_driver / gcm_driver.h
blob897dbfa51975ce883b18bdb3440d06e11c20e2c5
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_H_
6 #define COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "components/gcm_driver/default_gcm_app_handler.h"
19 #include "google_apis/gaia/identity_provider.h"
20 #include "google_apis/gcm/gcm_client.h"
22 namespace base {
23 class FilePath;
24 class SequencedTaskRunner;
27 namespace extensions {
28 class ExtensionGCMAppHandlerTest;
31 namespace net {
32 class URLRequestContextGetter;
35 namespace gcm {
37 class GCMAppHandler;
38 class GCMClientFactory;
40 // A bridge between the GCM users in Chrome and the GCMClient layer.
41 class GCMDriver : public IdentityProvider::Observer {
42 public:
43 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap;
44 typedef base::Callback<void(const std::string& registration_id,
45 GCMClient::Result result)> RegisterCallback;
46 typedef base::Callback<void(const std::string& message_id,
47 GCMClient::Result result)> SendCallback;
48 typedef base::Callback<void(GCMClient::Result result)> UnregisterCallback;
49 typedef base::Callback<void(const GCMClient::GCMStatistics& stats)>
50 GetGCMStatisticsCallback;
52 GCMDriver(
53 scoped_ptr<GCMClientFactory> gcm_client_factory,
54 scoped_ptr<IdentityProvider> identity_provider,
55 const GCMClient::ChromeBuildInfo& chrome_build_info,
56 const base::FilePath& store_path,
57 const scoped_refptr<net::URLRequestContextGetter>& request_context,
58 const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
59 const scoped_refptr<base::SequencedTaskRunner>& io_thread,
60 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner);
61 virtual ~GCMDriver();
63 // Enables/disables GCM service.
64 void Enable();
65 void Disable();
67 // This method must be called before destroying the GCMDriver. Once it has
68 // been called, no other GCMDriver methods may be used.
69 virtual void Shutdown();
71 // Adds a handler for a given app.
72 virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler);
74 // Remove the handler for a given app.
75 virtual void RemoveAppHandler(const std::string& app_id);
77 // Registers |sender_id| for an app. A registration ID will be returned by
78 // the GCM server.
79 // |app_id|: application ID.
80 // |sender_ids|: list of IDs of the servers that are allowed to send the
81 // messages to the application. These IDs are assigned by the
82 // Google API Console.
83 // |callback|: to be called once the asynchronous operation is done.
84 virtual void Register(const std::string& app_id,
85 const std::vector<std::string>& sender_ids,
86 const RegisterCallback& callback);
88 // Unregisters an app from using GCM.
89 // |app_id|: application ID.
90 // |callback|: to be called once the asynchronous operation is done.
91 virtual void Unregister(const std::string& app_id,
92 const UnregisterCallback& callback);
94 // Sends a message to a given receiver.
95 // |app_id|: application ID.
96 // |receiver_id|: registration ID of the receiver party.
97 // |message|: message to be sent.
98 // |callback|: to be called once the asynchronous operation is done.
99 virtual void Send(const std::string& app_id,
100 const std::string& receiver_id,
101 const GCMClient::OutgoingMessage& message,
102 const SendCallback& callback);
104 // For testing purpose.
105 GCMClient* GetGCMClientForTesting() const;
107 // Returns true if the service was started.
108 bool IsStarted() const;
110 // Returns true if the gcm client is ready.
111 bool IsGCMClientReady() const;
113 // Get GCM client internal states and statistics.
114 // If clear_logs is true then activity logs will be cleared before the stats
115 // are returned.
116 void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
117 bool clear_logs);
119 // Enables/disables GCM activity recording, and then returns the stats.
120 void SetGCMRecording(const GetGCMStatisticsCallback& callback,
121 bool recording);
123 // Returns the user name if the profile is signed in. Empty string otherwise.
124 std::string SignedInUserName() const;
126 // IdentityProvider::Observer:
127 virtual void OnActiveAccountLogin() OVERRIDE;
128 virtual void OnActiveAccountLogout() OVERRIDE;
130 const GCMAppHandlerMap& app_handlers() const { return app_handlers_; }
132 protected:
133 // Used for constructing fake GCMDriver for testing purpose.
134 GCMDriver();
136 private:
137 class DelayedTaskController;
138 class IOWorker;
140 // Ensures that the GCM service starts when all of the following conditions
141 // satisfy:
142 // 1) GCM is enabled.
143 // 2) The identity provider is able to supply an account ID.
144 GCMClient::Result EnsureStarted();
146 // Stops the GCM service. It can be restarted by calling EnsureStarted again.
147 void Stop();
149 // Remove cached data when GCM service is stopped.
150 void RemoveCachedData();
152 // Checks out of GCM and erases any cached and persisted data.
153 void CheckOut();
155 // Should be called when an app with |app_id| is trying to un/register.
156 // Checks whether another un/registration is in progress.
157 bool IsAsyncOperationPending(const std::string& app_id) const;
159 void DoRegister(const std::string& app_id,
160 const std::vector<std::string>& sender_ids);
161 void DoUnregister(const std::string& app_id);
162 void DoSend(const std::string& app_id,
163 const std::string& receiver_id,
164 const GCMClient::OutgoingMessage& message);
166 // Callbacks posted from IO thread to UI thread.
167 void RegisterFinished(const std::string& app_id,
168 const std::string& registration_id,
169 GCMClient::Result result);
170 void UnregisterFinished(const std::string& app_id, GCMClient::Result result);
171 void SendFinished(const std::string& app_id,
172 const std::string& message_id,
173 GCMClient::Result result);
174 void MessageReceived(const std::string& app_id,
175 GCMClient::IncomingMessage message);
176 void MessagesDeleted(const std::string& app_id);
177 void MessageSendError(const std::string& app_id,
178 const GCMClient::SendErrorDetails& send_error_details);
179 void GCMClientReady();
181 // Returns the handler for the given app.
182 GCMAppHandler* GetAppHandler(const std::string& app_id);
184 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats);
186 // Flag to indicate if GCM is enabled.
187 bool gcm_enabled_;
189 // Flag to indicate if GCMClient is ready.
190 bool gcm_client_ready_;
192 // The account ID that this service is responsible for. Empty when the service
193 // is not running.
194 std::string account_id_;
196 scoped_ptr<IdentityProvider> identity_provider_;
197 scoped_refptr<base::SequencedTaskRunner> ui_thread_;
198 scoped_refptr<base::SequencedTaskRunner> io_thread_;
200 scoped_ptr<DelayedTaskController> delayed_task_controller_;
202 // For all the work occurring on the IO thread. Must be destroyed on the IO
203 // thread.
204 scoped_ptr<IOWorker> io_worker_;
206 // App handler map (from app_id to handler pointer).
207 // The handler is not owned.
208 GCMAppHandlerMap app_handlers_;
210 // The default handler when no app handler can be found in the map.
211 DefaultGCMAppHandler default_app_handler_;
213 // Callback map (from app_id to callback) for Register.
214 std::map<std::string, RegisterCallback> register_callbacks_;
216 // Callback map (from app_id to callback) for Unregister.
217 std::map<std::string, UnregisterCallback> unregister_callbacks_;
219 // Callback map (from <app_id, message_id> to callback) for Send.
220 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_;
222 // Callback for GetGCMStatistics.
223 GetGCMStatisticsCallback request_gcm_statistics_callback_;
225 // Used to pass a weak pointer to the IO worker.
226 base::WeakPtrFactory<GCMDriver> weak_ptr_factory_;
228 DISALLOW_COPY_AND_ASSIGN(GCMDriver);
231 } // namespace gcm
233 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_