[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_ |
| 6 | #define COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 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" |
fgorski | 0d5c00d | 2014-08-28 16:21:45 | [diff] [blame] | 17 | #include "base/observer_list.h" |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 18 | #include "components/gcm_driver/gcm_client.h" |
fgorski | 0d5c00d | 2014-08-28 16:21:45 | [diff] [blame] | 19 | #include "components/gcm_driver/gcm_connection_observer.h" |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 20 | #include "components/gcm_driver/gcm_driver.h" |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 21 | |
| 22 | namespace base { |
| 23 | class FilePath; |
| 24 | class SequencedTaskRunner; |
| 25 | } |
| 26 | |
| 27 | namespace extensions { |
| 28 | class ExtensionGCMAppHandlerTest; |
| 29 | } |
| 30 | |
| 31 | namespace net { |
| 32 | class URLRequestContextGetter; |
| 33 | } |
| 34 | |
| 35 | namespace gcm { |
| 36 | |
| 37 | class GCMAppHandler; |
| 38 | class GCMClientFactory; |
fgorski | 05d507c | 2014-09-10 17:28:10 | [diff] [blame] | 39 | class GCMDelayedTaskController; |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 40 | |
| 41 | // GCMDriver implementation for desktop and Chrome OS, using GCMClient. |
[email protected] | da54623f | 2014-06-17 17:03:55 | [diff] [blame] | 42 | class GCMDriverDesktop : public GCMDriver { |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 43 | public: |
| 44 | GCMDriverDesktop( |
| 45 | scoped_ptr<GCMClientFactory> gcm_client_factory, |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 46 | const GCMClient::ChromeBuildInfo& chrome_build_info, |
| 47 | const base::FilePath& store_path, |
| 48 | const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| 49 | const scoped_refptr<base::SequencedTaskRunner>& ui_thread, |
| 50 | const scoped_refptr<base::SequencedTaskRunner>& io_thread, |
| 51 | const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner); |
| 52 | virtual ~GCMDriverDesktop(); |
| 53 | |
| 54 | // GCMDriver overrides: |
| 55 | virtual void Shutdown() OVERRIDE; |
[email protected] | da54623f | 2014-06-17 17:03:55 | [diff] [blame] | 56 | virtual void OnSignedIn() OVERRIDE; |
jianli | 6967652 | 2014-09-13 20:45:41 | [diff] [blame] | 57 | virtual void OnSignedOut() OVERRIDE; |
[email protected] | da54623f | 2014-06-17 17:03:55 | [diff] [blame] | 58 | virtual void Purge() OVERRIDE; |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 59 | virtual void AddAppHandler(const std::string& app_id, |
| 60 | GCMAppHandler* handler) OVERRIDE; |
| 61 | virtual void RemoveAppHandler(const std::string& app_id) OVERRIDE; |
fgorski | 0d5c00d | 2014-08-28 16:21:45 | [diff] [blame] | 62 | virtual void AddConnectionObserver(GCMConnectionObserver* observer) OVERRIDE; |
| 63 | virtual void RemoveConnectionObserver( |
| 64 | GCMConnectionObserver* observer) OVERRIDE; |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 65 | virtual void Enable() OVERRIDE; |
| 66 | virtual void Disable() OVERRIDE; |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 67 | virtual GCMClient* GetGCMClientForTesting() const OVERRIDE; |
| 68 | virtual bool IsStarted() const OVERRIDE; |
[email protected] | fc6078a | 2014-06-14 08:28:43 | [diff] [blame] | 69 | virtual bool IsConnected() const OVERRIDE; |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 70 | virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback, |
| 71 | bool clear_logs) OVERRIDE; |
| 72 | virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback, |
| 73 | bool recording) OVERRIDE; |
[email protected] | 72d4f25 | 2014-08-20 22:34:28 | [diff] [blame] | 74 | virtual void UpdateAccountMapping( |
| 75 | const AccountMapping& account_mapping) OVERRIDE; |
| 76 | virtual void RemoveAccountMapping(const std::string& account_id) OVERRIDE; |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 77 | |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 78 | // GCMDriverDesktop specific implementation. |
| 79 | // Sets a list of accounts with OAuth2 tokens for the next checkin. |
| 80 | // |account_tokens| maps email addresses to OAuth2 access tokens. |
| 81 | // |account_removed| indicates that an account has been removed since the |
| 82 | // last time the callback was called, which triggers an immediate checkin, |
| 83 | // to ensure that association between device and account is removed. |
| 84 | void SetAccountsForCheckin( |
| 85 | const std::map<std::string, std::string>& account_tokens); |
| 86 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 87 | protected: |
| 88 | // GCMDriver implementation: |
| 89 | virtual GCMClient::Result EnsureStarted() OVERRIDE; |
| 90 | virtual void RegisterImpl( |
| 91 | const std::string& app_id, |
| 92 | const std::vector<std::string>& sender_ids) OVERRIDE; |
| 93 | virtual void UnregisterImpl(const std::string& app_id) OVERRIDE; |
| 94 | virtual void SendImpl(const std::string& app_id, |
| 95 | const std::string& receiver_id, |
| 96 | const GCMClient::OutgoingMessage& message) OVERRIDE; |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 97 | |
| 98 | private: |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 99 | class IOWorker; |
| 100 | |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 101 | // Stops the GCM service. It can be restarted by calling EnsureStarted again. |
| 102 | void Stop(); |
| 103 | |
| 104 | // Remove cached data when GCM service is stopped. |
| 105 | void RemoveCachedData(); |
| 106 | |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 107 | void DoRegister(const std::string& app_id, |
| 108 | const std::vector<std::string>& sender_ids); |
| 109 | void DoUnregister(const std::string& app_id); |
| 110 | void DoSend(const std::string& app_id, |
| 111 | const std::string& receiver_id, |
| 112 | const GCMClient::OutgoingMessage& message); |
| 113 | |
| 114 | // Callbacks posted from IO thread to UI thread. |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 115 | void MessageReceived(const std::string& app_id, |
[email protected] | 2590835 | 2014-06-03 10:19:11 | [diff] [blame] | 116 | const GCMClient::IncomingMessage& message); |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 117 | void MessagesDeleted(const std::string& app_id); |
| 118 | void MessageSendError(const std::string& app_id, |
| 119 | const GCMClient::SendErrorDetails& send_error_details); |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 120 | void SendAcknowledged(const std::string& app_id, |
| 121 | const std::string& message_id); |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 122 | void GCMClientReady(); |
[email protected] | fc6078a | 2014-06-14 08:28:43 | [diff] [blame] | 123 | void OnConnected(const net::IPEndPoint& ip_endpoint); |
| 124 | void OnDisconnected(); |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 125 | |
[email protected] | 2590835 | 2014-06-03 10:19:11 | [diff] [blame] | 126 | void GetGCMStatisticsFinished(const GCMClient::GCMStatistics& stats); |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 127 | |
[email protected] | da54623f | 2014-06-17 17:03:55 | [diff] [blame] | 128 | // Flag to indicate whether the user is signed in to a GAIA account. |
| 129 | // TODO(jianli): To be removed when sign-in enforcement is dropped. |
| 130 | bool signed_in_; |
| 131 | |
| 132 | // Flag to indicate if GCM is started. |
| 133 | bool gcm_started_; |
| 134 | |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 135 | // Flag to indicate if GCM is enabled. |
| 136 | bool gcm_enabled_; |
| 137 | |
[email protected] | fc6078a | 2014-06-14 08:28:43 | [diff] [blame] | 138 | // Flag to indicate the last known state of the GCM client. Because this |
| 139 | // flag lives on the UI thread, while the GCM client lives on the IO thread, |
| 140 | // it may be out of date while connection changes are happening. |
| 141 | bool connected_; |
| 142 | |
fgorski | 0d5c00d | 2014-08-28 16:21:45 | [diff] [blame] | 143 | // List of observers to notify when connection state changes. |
| 144 | // Makes sure list is empty on destruction. |
| 145 | ObserverList<GCMConnectionObserver, true> connection_observer_list_; |
| 146 | |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 147 | scoped_refptr<base::SequencedTaskRunner> ui_thread_; |
| 148 | scoped_refptr<base::SequencedTaskRunner> io_thread_; |
| 149 | |
fgorski | 05d507c | 2014-09-10 17:28:10 | [diff] [blame] | 150 | scoped_ptr<GCMDelayedTaskController> delayed_task_controller_; |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 151 | |
| 152 | // For all the work occurring on the IO thread. Must be destroyed on the IO |
| 153 | // thread. |
| 154 | scoped_ptr<IOWorker> io_worker_; |
| 155 | |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 156 | // Callback for GetGCMStatistics. |
| 157 | GetGCMStatisticsCallback request_gcm_statistics_callback_; |
| 158 | |
| 159 | // Used to pass a weak pointer to the IO worker. |
| 160 | base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_; |
| 161 | |
| 162 | DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop); |
| 163 | }; |
| 164 | |
| 165 | } // namespace gcm |
| 166 | |
| 167 | #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_ |