Allow chrome.systemPrivate to be used by apps.
[chromium-blink-merge.git] / components / gcm_driver / gcm_driver.h
blobf75a8e31f51b14d4de57b36da43fbb7992d5977d
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/callback.h"
13 #include "base/macros.h"
14 #include "base/threading/thread_checker.h"
15 #include "components/gcm_driver/default_gcm_app_handler.h"
16 #include "components/gcm_driver/gcm_client.h"
18 namespace gcm {
20 class GCMAppHandler;
22 // Bridge between GCM users in Chrome and the platform-specific implementation.
23 class GCMDriver {
24 public:
25 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap;
26 typedef base::Callback<void(const std::string& registration_id,
27 GCMClient::Result result)> RegisterCallback;
28 typedef base::Callback<void(const std::string& message_id,
29 GCMClient::Result result)> SendCallback;
30 typedef base::Callback<void(GCMClient::Result result)> UnregisterCallback;
31 typedef base::Callback<void(const GCMClient::GCMStatistics& stats)>
32 GetGCMStatisticsCallback;
34 // Returns true if the GCM is allowed for all users.
35 static bool IsAllowedForAllUsers();
37 GCMDriver();
38 virtual ~GCMDriver();
40 // Registers |sender_id| for an app. A registration ID will be returned by
41 // the GCM server.
42 // |app_id|: application ID.
43 // |sender_ids|: list of IDs of the servers that are allowed to send the
44 // messages to the application. These IDs are assigned by the
45 // Google API Console.
46 // |callback|: to be called once the asynchronous operation is done.
47 void Register(const std::string& app_id,
48 const std::vector<std::string>& sender_ids,
49 const RegisterCallback& callback);
51 // Unregisters an app from using GCM.
52 // |app_id|: application ID.
53 // |callback|: to be called once the asynchronous operation is done.
54 void Unregister(const std::string& app_id,
55 const UnregisterCallback& callback);
57 // Sends a message to a given receiver.
58 // |app_id|: application ID.
59 // |receiver_id|: registration ID of the receiver party.
60 // |message|: message to be sent.
61 // |callback|: to be called once the asynchronous operation is done.
62 void Send(const std::string& app_id,
63 const std::string& receiver_id,
64 const GCMClient::OutgoingMessage& message,
65 const SendCallback& callback);
67 const GCMAppHandlerMap& app_handlers() const { return app_handlers_; }
69 // This method must be called before destroying the GCMDriver. Once it has
70 // been called, no other GCMDriver methods may be used.
71 virtual void Shutdown();
73 // Call this method when the user signs in to a GAIA account.
74 // TODO(jianli): To be removed when sign-in enforcement is dropped.
75 virtual void OnSignedIn() = 0;
77 // Removes all the cached and persisted GCM data. If the GCM service is
78 // restarted after the purge, a new Android ID will be obtained.
79 virtual void Purge() = 0;
81 // Adds a handler for a given app.
82 virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler);
84 // Remove the handler for a given app.
85 virtual void RemoveAppHandler(const std::string& app_id);
87 // Returns the handler for the given app.
88 GCMAppHandler* GetAppHandler(const std::string& app_id);
90 // Enables/disables GCM service.
91 virtual void Enable() = 0;
92 virtual void Disable() = 0;
94 // For testing purpose. Always NULL on Android.
95 virtual GCMClient* GetGCMClientForTesting() const = 0;
97 // Returns true if the service was started.
98 virtual bool IsStarted() const = 0;
100 // Returns true if the gcm client has an open and active connection.
101 virtual bool IsConnected() const = 0;
103 // Get GCM client internal states and statistics.
104 // If clear_logs is true then activity logs will be cleared before the stats
105 // are returned.
106 virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
107 bool clear_logs) = 0;
109 // Enables/disables GCM activity recording, and then returns the stats.
110 virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback,
111 bool recording) = 0;
113 protected:
114 // Ensures that the GCM service starts (if necessary conditions are met).
115 virtual GCMClient::Result EnsureStarted() = 0;
117 // Platform-specific implementation of Register.
118 virtual void RegisterImpl(const std::string& app_id,
119 const std::vector<std::string>& sender_ids) = 0;
121 // Platform-specific implementation of Unregister.
122 virtual void UnregisterImpl(const std::string& app_id) = 0;
124 // Platform-specific implementation of Send.
125 virtual void SendImpl(const std::string& app_id,
126 const std::string& receiver_id,
127 const GCMClient::OutgoingMessage& message) = 0;
129 // Runs the Register callback.
130 void RegisterFinished(const std::string& app_id,
131 const std::string& registration_id,
132 GCMClient::Result result);
134 // Runs the Unregister callback.
135 void UnregisterFinished(const std::string& app_id,
136 GCMClient::Result result);
138 // Runs the Send callback.
139 void SendFinished(const std::string& app_id,
140 const std::string& message_id,
141 GCMClient::Result result);
143 bool HasRegisterCallback(const std::string& app_id);
145 void ClearCallbacks();
147 private:
148 // Should be called when an app with |app_id| is trying to un/register.
149 // Checks whether another un/registration is in progress.
150 bool IsAsyncOperationPending(const std::string& app_id) const;
152 // Callback map (from app_id to callback) for Register.
153 std::map<std::string, RegisterCallback> register_callbacks_;
155 // Callback map (from app_id to callback) for Unregister.
156 std::map<std::string, UnregisterCallback> unregister_callbacks_;
158 // Callback map (from <app_id, message_id> to callback) for Send.
159 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_;
161 // App handler map (from app_id to handler pointer).
162 // The handler is not owned.
163 GCMAppHandlerMap app_handlers_;
165 // The default handler when no app handler can be found in the map.
166 DefaultGCMAppHandler default_app_handler_;
168 DISALLOW_COPY_AND_ASSIGN(GCMDriver);
171 } // namespace gcm
173 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_