Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: components/gcm/gcm_driver_desktop.h

Issue 278493002: Split GCMDriver into platform-specific implementations (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COMPONENTS_GCM_GCM_DRIVER_H_ 5 #ifndef COMPONENTS_GCM_GCM_DRIVER_DESKTOP_H_
6 #define COMPONENTS_GCM_GCM_DRIVER_H_ 6 #define COMPONENTS_GCM_GCM_DRIVER_DESKTOP_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/compiler_specific.h" 14 #include "base/compiler_specific.h"
15 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h" 16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h" 17 #include "base/memory/weak_ptr.h"
18 #include "components/gcm/default_gcm_app_handler.h" 18 #include "components/gcm/default_gcm_app_handler.h"
19 #include "components/gcm/gcm_driver.h"
19 #include "google_apis/gaia/identity_provider.h" 20 #include "google_apis/gaia/identity_provider.h"
20 #include "google_apis/gcm/gcm_client.h" 21 #include "google_apis/gcm/gcm_client.h"
21 22
22 namespace base { 23 namespace base {
23 class FilePath; 24 class FilePath;
24 class MessageLoopProxy; 25 class MessageLoopProxy;
25 } 26 }
26 27
27 namespace net { 28 namespace net {
28 class URLRequestContextGetter; 29 class URLRequestContextGetter;
29 } 30 }
30 31
31 namespace gcm { 32 namespace gcm {
32 33
33 class GCMAppHandler;
34 class GCMClientFactory; 34 class GCMClientFactory;
35 35
36 // A bridge between the GCM users in Chrome and the GCMClient layer. 36 // GCMDriver implementation for Chrome OS, Windows, Linux and Mac.
37 class GCMDriver : public IdentityProvider::Observer { 37 class GCMDriverDesktop : public GCMDriver, public IdentityProvider::Observer {
38 public: 38 public:
39 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap; 39 GCMDriverDesktop(
40 typedef base::Callback<void(const std::string& registration_id, 40 const checkin_proto::ChromeBuildProto& chrome_build_proto,
41 GCMClient::Result result)> RegisterCallback; 41 scoped_ptr<GCMClientFactory> gcm_client_factory,
42 typedef base::Callback<void(const std::string& message_id, 42 scoped_ptr<IdentityProvider> identity_provider,
43 GCMClient::Result result)> SendCallback; 43 const base::FilePath& store_path,
44 typedef base::Callback<void(GCMClient::Result result)> UnregisterCallback; 44 const scoped_refptr<net::URLRequestContextGetter>& request_context,
45 typedef base::Callback<void(const GCMClient::GCMStatistics& stats)> 45 const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner,
46 GetGCMStatisticsCallback; 46 const scoped_refptr<base::MessageLoopProxy>& ui_thread,
47 const scoped_refptr<base::MessageLoopProxy>& io_thread);
48 virtual ~GCMDriverDesktop();
47 49
48 GCMDriver(const checkin_proto::ChromeBuildProto& chrome_build_proto, 50 // GCMDriver implementation.
49 scoped_ptr<GCMClientFactory> gcm_client_factory, 51 virtual void Enable() OVERRIDE;
50 scoped_ptr<IdentityProvider> identity_provider, 52 virtual void Disable() OVERRIDE;
51 const base::FilePath& store_path, 53 virtual void Shutdown() OVERRIDE;
52 const scoped_refptr<net::URLRequestContextGetter>& request_context, 54 virtual void AddAppHandler(const std::string& app_id,
53 const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, 55 GCMAppHandler* handler) OVERRIDE;
54 const scoped_refptr<base::MessageLoopProxy>& ui_thread, 56 virtual void RemoveAppHandler(const std::string& app_id) OVERRIDE;
55 const scoped_refptr<base::MessageLoopProxy>& io_thread);
56 virtual ~GCMDriver();
57
58 // Enables/disables GCM service. When GCM is disabled, the GCM service
59 // will always be stopped. Otherwise, the GCM service will be started
60 // on demand, i.e., some app is asking for the service.
61 void Enable();
62 void Disable();
63
64 // Shuts down the GCM service completely. This will tell all the apps
65 // to shut them down. After that, no GCM service could be consumed.
66 virtual void Shutdown();
67
68 // Adds a handler for a given app. It is OK to call this again to update the
69 // handler.
70 virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler);
71
72 // Removes the handler for a given app.
73 virtual void RemoveAppHandler(const std::string& app_id);
74
75 // Registers |sender_id| for an app. A registration ID will be returned by
76 // the GCM server.
77 // |app_id|: application ID.
78 // |sender_ids|: list of IDs of the servers that are allowed to send the
79 // messages to the application. These IDs are assigned by the
80 // Google API Console.
81 // |callback|: to be called once the asynchronous operation is done.
82 virtual void Register(const std::string& app_id, 57 virtual void Register(const std::string& app_id,
83 const std::vector<std::string>& sender_ids, 58 const std::vector<std::string>& sender_ids,
84 const RegisterCallback& callback); 59 const RegisterCallback& callback) OVERRIDE;
85
86 // Unregisters an app from using GCM.
87 // |app_id|: application ID.
88 // |callback|: to be called once the asynchronous operation is done.
89 virtual void Unregister(const std::string& app_id, 60 virtual void Unregister(const std::string& app_id,
90 const UnregisterCallback& callback); 61 const UnregisterCallback& callback) OVERRIDE;
91
92 // Sends a message to a given receiver.
93 // |app_id|: application ID.
94 // |receiver_id|: registration ID of the receiver party.
95 // |message|: message to be sent.
96 // |callback|: to be called once the asynchronous operation is done.
97 virtual void Send(const std::string& app_id, 62 virtual void Send(const std::string& app_id,
98 const std::string& receiver_id, 63 const std::string& receiver_id,
99 const GCMClient::OutgoingMessage& message, 64 const GCMClient::OutgoingMessage& message,
100 const SendCallback& callback); 65 const SendCallback& callback) OVERRIDE;
66 virtual GCMClient* GetGCMClientForTesting() const OVERRIDE;
67 virtual bool IsStarted() const OVERRIDE;
68 virtual bool IsGCMClientReady() const OVERRIDE;
69 virtual std::string SignedInUserName() const OVERRIDE;
70 virtual void GetGCMStatistics(GetGCMStatisticsCallback callback,
71 bool clear_logs) OVERRIDE;
72 virtual void SetGCMRecording(GetGCMStatisticsCallback callback,
73 bool recording) OVERRIDE;
101 74
102 // For testing purpose. 75 // IdentityProvider::Observer implementation.
103 GCMClient* GetGCMClientForTesting() const;
104
105 // Returns true if the service was started.
106 bool IsStarted() const;
107
108 // Returns true if the gcm client is ready.
109 bool IsGCMClientReady() const;
110
111 std::string SignedInUserName() const;
112
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(GetGCMStatisticsCallback callback, bool clear_logs);
117
118 // Enables/disables GCM activity recording, and then returns the stats.
119 void SetGCMRecording(GetGCMStatisticsCallback callback, bool recording);
120
121 // IdentityProvider::Observer:
122 virtual void OnActiveAccountLogin() OVERRIDE; 76 virtual void OnActiveAccountLogin() OVERRIDE;
123 virtual void OnActiveAccountLogout() OVERRIDE; 77 virtual void OnActiveAccountLogout() OVERRIDE;
124 78
125 const GCMAppHandlerMap& app_handlers() const { return app_handlers_; }
126
127 protected: 79 protected:
128 // Used for constructing fake GCMDriver for testing purpose. 80 // Used for constructing fake GCMDriver for testing purpose.
129 GCMDriver(); 81 GCMDriverDesktop();
130 82
131 private: 83 private:
132 class DelayedTaskController; 84 class DelayedTaskController;
133 class IOWorker; 85 class IOWorker;
134 86
135 // Ensures that the GCM service starts when all of the following conditions 87 // Ensures that the GCM service starts when all of the following conditions
136 // satisfy: 88 // satisfy:
137 // 1) GCM is enabled. 89 // 1) GCM is enabled.
138 // 2) The identity provider is able to supply an account ID. 90 // 2) The identity provider is able to supply an account ID.
139 // 3) An app requests to consume the service by register the app handler. 91 // 3) An app requests to consume the service by register the app handler.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 void MessageSendError(const std::string& app_id, 125 void MessageSendError(const std::string& app_id,
174 const GCMClient::SendErrorDetails& send_error_details); 126 const GCMClient::SendErrorDetails& send_error_details);
175 void GCMClientReady(); 127 void GCMClientReady();
176 128
177 // Returns the handler for the given app. 129 // Returns the handler for the given app.
178 GCMAppHandler* GetAppHandler(const std::string& app_id); 130 GCMAppHandler* GetAppHandler(const std::string& app_id);
179 131
180 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats); 132 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats);
181 133
182 scoped_ptr<IdentityProvider> identity_provider_; 134 scoped_ptr<IdentityProvider> identity_provider_;
183 scoped_refptr<base::MessageLoopProxy> ui_thread_;
184 scoped_refptr<base::MessageLoopProxy> io_thread_; 135 scoped_refptr<base::MessageLoopProxy> io_thread_;
185 136
186 // Flag to indicate if GCM is enabled. 137 // Flag to indicate if GCM is enabled.
187 bool gcm_enabled_; 138 bool gcm_enabled_;
188 139
189 // Flag to indicate if GCMClient is ready. 140 // Flag to indicate if GCMClient is ready.
190 bool gcm_client_ready_; 141 bool gcm_client_ready_;
191 142
192 // The account ID that this service is responsible for. Empty when the service 143 // The account ID that this service is responsible for. Empty when the service
193 // is not running. 144 // is not running.
(...skipping 18 matching lines...) Expand all
212 // Callback map (from app_id to callback) for Unregister. 163 // Callback map (from app_id to callback) for Unregister.
213 std::map<std::string, UnregisterCallback> unregister_callbacks_; 164 std::map<std::string, UnregisterCallback> unregister_callbacks_;
214 165
215 // Callback map (from <app_id, message_id> to callback) for Send. 166 // Callback map (from <app_id, message_id> to callback) for Send.
216 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_; 167 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_;
217 168
218 // Callback for GetGCMStatistics. 169 // Callback for GetGCMStatistics.
219 GetGCMStatisticsCallback request_gcm_statistics_callback_; 170 GetGCMStatisticsCallback request_gcm_statistics_callback_;
220 171
221 // Used to pass a weak pointer to the IO worker. 172 // Used to pass a weak pointer to the IO worker.
222 base::WeakPtrFactory<GCMDriver> weak_ptr_factory_; 173 base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_;
223 174
224 DISALLOW_COPY_AND_ASSIGN(GCMDriver); 175 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop);
225 }; 176 };
226 177
227 } // namespace gcm 178 } // namespace gcm
228 179
229 #endif // COMPONENTS_GCM_GCM_DRIVER_H_ 180 #endif // COMPONENTS_GCM_GCM_DRIVER_DESKTOP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698