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 CHROME_BROWSER_SERVICES_GCM_GCM_SERVICE_H_
6 #define CHROME_BROWSER_SERVICES_GCM_GCM_SERVICE_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/files/file_path.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "chrome/browser/services/gcm/default_gcm_app_handler.h"
20 #include "google_apis/gaia/identity_provider.h"
21 #include "google_apis/gcm/gcm_client.h"
23 namespace extensions
{
24 class ExtensionGCMAppHandlerTest
;
28 class URLRequestContextGetter
;
34 class GCMClientFactory
;
36 // A bridge between the GCM users in Chrome and the GCMClient layer.
37 class GCMService
: public IdentityProvider::Observer
{
39 typedef base::Callback
<void(const std::string
& registration_id
,
40 GCMClient::Result result
)> RegisterCallback
;
41 typedef base::Callback
<void(const std::string
& message_id
,
42 GCMClient::Result result
)> SendCallback
;
43 typedef base::Callback
<void(GCMClient::Result result
)> UnregisterCallback
;
44 typedef base::Callback
<void(const GCMClient::GCMStatistics
& stats
)>
45 GetGCMStatisticsCallback
;
47 explicit GCMService(scoped_ptr
<IdentityProvider
> identity_provider
);
48 virtual ~GCMService();
50 void Initialize(scoped_ptr
<GCMClientFactory
> gcm_client_factory
);
56 // This method must be called before destroying the GCMService. Once it has
57 // been called, no other GCMService methods may be used.
58 void ShutdownService();
60 // Adds a handler for a given app.
61 void AddAppHandler(const std::string
& app_id
, GCMAppHandler
* handler
);
63 // Remove the handler for a given app.
64 void RemoveAppHandler(const std::string
& app_id
);
66 // Registers |sender_id| for an app. A registration ID will be returned by
68 // |app_id|: application ID.
69 // |sender_ids|: list of IDs of the servers that are allowed to send the
70 // messages to the application. These IDs are assigned by the
71 // Google API Console.
72 // |callback|: to be called once the asynchronous operation is done.
73 virtual void Register(const std::string
& app_id
,
74 const std::vector
<std::string
>& sender_ids
,
75 RegisterCallback callback
);
77 // Unregisters an app from using GCM.
78 // |app_id|: application ID.
79 // |callback|: to be called once the asynchronous operation is done.
80 virtual void Unregister(const std::string
& app_id
,
81 UnregisterCallback callback
);
83 // Sends a message to a given receiver.
84 // |app_id|: application ID.
85 // |receiver_id|: registration ID of the receiver party.
86 // |message|: message to be sent.
87 // |callback|: to be called once the asynchronous operation is done.
88 virtual void Send(const std::string
& app_id
,
89 const std::string
& receiver_id
,
90 const GCMClient::OutgoingMessage
& message
,
91 SendCallback callback
);
93 // For testing purpose.
94 GCMClient
* GetGCMClientForTesting() const;
96 // Returns true if the service was started.
97 bool IsStarted() const;
99 // Returns true if the gcm client is ready.
100 bool IsGCMClientReady() const;
102 // Get GCM client internal states and statistics.
103 // If clear_logs is true then activity logs will be cleared before the stats
105 void GetGCMStatistics(GetGCMStatisticsCallback callback
, bool clear_logs
);
107 // Enables/disables GCM activity recording, and then returns the stats.
108 void SetGCMRecording(GetGCMStatisticsCallback callback
, bool recording
);
110 // IdentityProvider::Observer:
111 virtual void OnActiveAccountLogin() OVERRIDE
;
112 virtual void OnActiveAccountLogout() OVERRIDE
;
115 virtual bool ShouldStartAutomatically() const = 0;
117 virtual base::FilePath
GetStorePath() const = 0;
119 virtual scoped_refptr
<net::URLRequestContextGetter
>
120 GetURLRequestContextGetter() const = 0;
122 scoped_ptr
<IdentityProvider
> identity_provider_
;
125 friend class TestGCMServiceWrapper
;
126 friend class extensions::ExtensionGCMAppHandlerTest
;
128 class DelayedTaskController
;
131 typedef std::map
<std::string
, GCMAppHandler
*> GCMAppHandlerMap
;
133 // Ensures that the GCMClient is loaded and the GCM check-in is done if the
134 // |identity_provider_| is able to supply an account ID.
137 // Remove cached data when GCM service is stopped.
138 void RemoveCachedData();
140 // Checks out of GCM and erases any cached and persisted data.
143 // Ensures that the app is ready for GCM functions and events.
144 GCMClient::Result
EnsureAppReady(const std::string
& app_id
);
146 // Should be called when an app with |app_id| is trying to un/register.
147 // Checks whether another un/registration is in progress.
148 bool IsAsyncOperationPending(const std::string
& app_id
) const;
150 void DoRegister(const std::string
& app_id
,
151 const std::vector
<std::string
>& sender_ids
);
152 void DoUnregister(const std::string
& app_id
);
153 void DoSend(const std::string
& app_id
,
154 const std::string
& receiver_id
,
155 const GCMClient::OutgoingMessage
& message
);
157 // Callbacks posted from IO thread to UI thread.
158 void RegisterFinished(const std::string
& app_id
,
159 const std::string
& registration_id
,
160 GCMClient::Result result
);
161 void UnregisterFinished(const std::string
& app_id
, GCMClient::Result result
);
162 void SendFinished(const std::string
& app_id
,
163 const std::string
& message_id
,
164 GCMClient::Result result
);
165 void MessageReceived(const std::string
& app_id
,
166 GCMClient::IncomingMessage message
);
167 void MessagesDeleted(const std::string
& app_id
);
168 void MessageSendError(const std::string
& app_id
,
169 const GCMClient::SendErrorDetails
& send_error_details
);
170 void GCMClientReady();
172 // Returns the handler for the given app.
173 GCMAppHandler
* GetAppHandler(const std::string
& app_id
);
175 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats
);
177 // Flag to indicate if GCMClient is ready.
178 bool gcm_client_ready_
;
180 // The account ID that this service is responsible for. Empty when the service
182 std::string account_id_
;
184 scoped_ptr
<DelayedTaskController
> delayed_task_controller_
;
186 // For all the work occurring on the IO thread. Must be destroyed on the IO
188 scoped_ptr
<IOWorker
> io_worker_
;
190 // App handler map (from app_id to handler pointer).
191 // The handler is not owned.
192 GCMAppHandlerMap app_handlers_
;
194 // The default handler when no app handler can be found in the map.
195 DefaultGCMAppHandler default_app_handler_
;
197 // Callback map (from app_id to callback) for Register.
198 std::map
<std::string
, RegisterCallback
> register_callbacks_
;
200 // Callback map (from app_id to callback) for Unregister.
201 std::map
<std::string
, UnregisterCallback
> unregister_callbacks_
;
203 // Callback map (from <app_id, message_id> to callback) for Send.
204 std::map
<std::pair
<std::string
, std::string
>, SendCallback
> send_callbacks_
;
206 // Callback for GetGCMStatistics.
207 GetGCMStatisticsCallback request_gcm_statistics_callback_
;
209 // Used to pass a weak pointer to the IO worker.
210 base::WeakPtrFactory
<GCMService
> weak_ptr_factory_
;
212 DISALLOW_COPY_AND_ASSIGN(GCMService
);
217 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_SERVICE_H_