Implement HasPermission() method in PermissionService.
[chromium-blink-merge.git] / components / gcm_driver / gcm_driver_desktop.h
blob6cbc0f932fb1e83f9cb008372afc07331c01432d
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_DESKTOP_H_
6 #define COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_
8 #include <map>
9 #include <string>
10 #include <vector>
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"
17 #include "base/observer_list.h"
18 #include "components/gcm_driver/gcm_channel_status_syncer.h"
19 #include "components/gcm_driver/gcm_client.h"
20 #include "components/gcm_driver/gcm_connection_observer.h"
21 #include "components/gcm_driver/gcm_driver.h"
23 class PrefService;
25 namespace base {
26 class FilePath;
27 class SequencedTaskRunner;
30 namespace extensions {
31 class ExtensionGCMAppHandlerTest;
34 namespace net {
35 class URLRequestContextGetter;
38 namespace gcm {
40 class GCMAccountMapper;
41 class GCMAppHandler;
42 class GCMClientFactory;
43 class GCMDelayedTaskController;
45 // GCMDriver implementation for desktop and Chrome OS, using GCMClient.
46 class GCMDriverDesktop : public GCMDriver {
47 public:
48 GCMDriverDesktop(
49 scoped_ptr<GCMClientFactory> gcm_client_factory,
50 const GCMClient::ChromeBuildInfo& chrome_build_info,
51 const std::string& channel_status_request_url,
52 const std::string& user_agent,
53 PrefService* prefs,
54 const base::FilePath& store_path,
55 const scoped_refptr<net::URLRequestContextGetter>& request_context,
56 const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
57 const scoped_refptr<base::SequencedTaskRunner>& io_thread,
58 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner);
59 ~GCMDriverDesktop() override;
61 // GCMDriver overrides:
62 void Shutdown() override;
63 void OnSignedIn() override;
64 void OnSignedOut() override;
65 void Purge() override;
66 void AddAppHandler(const std::string& app_id,
67 GCMAppHandler* handler) override;
68 void RemoveAppHandler(const std::string& app_id) override;
69 void AddConnectionObserver(GCMConnectionObserver* observer) override;
70 void RemoveConnectionObserver(GCMConnectionObserver* observer) override;
71 void Enable() override;
72 void Disable() override;
73 GCMClient* GetGCMClientForTesting() const override;
74 bool IsStarted() const override;
75 bool IsConnected() const override;
76 void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
77 bool clear_logs) override;
78 void SetGCMRecording(const GetGCMStatisticsCallback& callback,
79 bool recording) override;
80 void SetAccountTokens(
81 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) override;
82 void UpdateAccountMapping(const AccountMapping& account_mapping) override;
83 void RemoveAccountMapping(const std::string& account_id) override;
84 base::Time GetLastTokenFetchTime() override;
85 void SetLastTokenFetchTime(const base::Time& time) override;
86 void WakeFromSuspendForHeartbeat(bool wake) override;
88 // Exposed for testing purpose.
89 bool gcm_enabled() const { return gcm_enabled_; }
90 GCMChannelStatusSyncer* gcm_channel_status_syncer_for_testing() {
91 return gcm_channel_status_syncer_.get();
94 protected:
95 // GCMDriver implementation:
96 GCMClient::Result EnsureStarted() override;
97 void RegisterImpl(const std::string& app_id,
98 const std::vector<std::string>& sender_ids) override;
99 void UnregisterImpl(const std::string& app_id) override;
100 void SendImpl(const std::string& app_id,
101 const std::string& receiver_id,
102 const GCMClient::OutgoingMessage& message) override;
104 private:
105 class IOWorker;
107 // Stops the GCM service. It can be restarted by calling EnsureStarted again.
108 void Stop();
110 // Remove cached data when GCM service is stopped.
111 void RemoveCachedData();
113 void DoRegister(const std::string& app_id,
114 const std::vector<std::string>& sender_ids);
115 void DoUnregister(const std::string& app_id);
116 void DoSend(const std::string& app_id,
117 const std::string& receiver_id,
118 const GCMClient::OutgoingMessage& message);
120 // Callbacks posted from IO thread to UI thread.
121 void MessageReceived(const std::string& app_id,
122 const GCMClient::IncomingMessage& message);
123 void MessagesDeleted(const std::string& app_id);
124 void MessageSendError(const std::string& app_id,
125 const GCMClient::SendErrorDetails& send_error_details);
126 void SendAcknowledged(const std::string& app_id,
127 const std::string& message_id);
128 void GCMClientReady(const std::vector<AccountMapping>& account_mappings,
129 const base::Time& last_token_fetch_time);
130 void OnConnected(const net::IPEndPoint& ip_endpoint);
131 void OnDisconnected();
133 void GetGCMStatisticsFinished(const GCMClient::GCMStatistics& stats);
135 scoped_ptr<GCMChannelStatusSyncer> gcm_channel_status_syncer_;
137 // Flag to indicate whether the user is signed in to a GAIA account.
138 // TODO(jianli): To be removed when sign-in enforcement is dropped.
139 bool signed_in_;
141 // Flag to indicate if GCM is started.
142 bool gcm_started_;
144 // Flag to indicate if GCM is enabled.
145 // TODO(jianli): Removed when we switch completely to support all users.
146 bool gcm_enabled_;
148 // Flag to indicate the last known state of the GCM client. Because this
149 // flag lives on the UI thread, while the GCM client lives on the IO thread,
150 // it may be out of date while connection changes are happening.
151 bool connected_;
153 // List of observers to notify when connection state changes.
154 ObserverList<GCMConnectionObserver, false> connection_observer_list_;
156 // Account mapper. Only works when user is signed in.
157 scoped_ptr<GCMAccountMapper> account_mapper_;
159 // Time of last token fetching.
160 base::Time last_token_fetch_time_;
162 scoped_refptr<base::SequencedTaskRunner> ui_thread_;
163 scoped_refptr<base::SequencedTaskRunner> io_thread_;
165 scoped_ptr<GCMDelayedTaskController> delayed_task_controller_;
167 // Whether the HeartbeatManager should try to wake the system from suspend for
168 // sending heartbeat messages.
169 bool wake_from_suspend_enabled_;
171 // For all the work occurring on the IO thread. Must be destroyed on the IO
172 // thread.
173 scoped_ptr<IOWorker> io_worker_;
175 // Callback for GetGCMStatistics.
176 GetGCMStatisticsCallback request_gcm_statistics_callback_;
178 // Used to pass a weak pointer to the IO worker.
179 base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_;
181 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop);
184 } // namespace gcm
186 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_