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_
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_client.h"
19 #include "components/gcm_driver/gcm_connection_observer.h"
20 #include "components/gcm_driver/gcm_driver.h"
24 class SequencedTaskRunner
;
27 namespace extensions
{
28 class ExtensionGCMAppHandlerTest
;
32 class URLRequestContextGetter
;
38 class GCMClientFactory
;
40 // GCMDriver implementation for desktop and Chrome OS, using GCMClient.
41 class GCMDriverDesktop
: public GCMDriver
{
44 scoped_ptr
<GCMClientFactory
> gcm_client_factory
,
45 const GCMClient::ChromeBuildInfo
& chrome_build_info
,
46 const base::FilePath
& store_path
,
47 const scoped_refptr
<net::URLRequestContextGetter
>& request_context
,
48 const scoped_refptr
<base::SequencedTaskRunner
>& ui_thread
,
49 const scoped_refptr
<base::SequencedTaskRunner
>& io_thread
,
50 const scoped_refptr
<base::SequencedTaskRunner
>& blocking_task_runner
);
51 virtual ~GCMDriverDesktop();
53 // GCMDriver overrides:
54 virtual void Shutdown() OVERRIDE
;
55 virtual void OnSignedIn() OVERRIDE
;
56 virtual void Purge() OVERRIDE
;
57 virtual void AddAppHandler(const std::string
& app_id
,
58 GCMAppHandler
* handler
) OVERRIDE
;
59 virtual void RemoveAppHandler(const std::string
& app_id
) OVERRIDE
;
60 virtual void AddConnectionObserver(GCMConnectionObserver
* observer
) OVERRIDE
;
61 virtual void RemoveConnectionObserver(
62 GCMConnectionObserver
* observer
) OVERRIDE
;
63 virtual void Enable() OVERRIDE
;
64 virtual void Disable() OVERRIDE
;
65 virtual GCMClient
* GetGCMClientForTesting() const OVERRIDE
;
66 virtual bool IsStarted() const OVERRIDE
;
67 virtual bool IsConnected() const OVERRIDE
;
68 virtual void GetGCMStatistics(const GetGCMStatisticsCallback
& callback
,
69 bool clear_logs
) OVERRIDE
;
70 virtual void SetGCMRecording(const GetGCMStatisticsCallback
& callback
,
71 bool recording
) OVERRIDE
;
72 virtual void UpdateAccountMapping(
73 const AccountMapping
& account_mapping
) OVERRIDE
;
74 virtual void RemoveAccountMapping(const std::string
& account_id
) OVERRIDE
;
76 // GCMDriverDesktop specific implementation.
77 // Sets a list of accounts with OAuth2 tokens for the next checkin.
78 // |account_tokens| maps email addresses to OAuth2 access tokens.
79 // |account_removed| indicates that an account has been removed since the
80 // last time the callback was called, which triggers an immediate checkin,
81 // to ensure that association between device and account is removed.
82 void SetAccountsForCheckin(
83 const std::map
<std::string
, std::string
>& account_tokens
);
86 // GCMDriver implementation:
87 virtual GCMClient::Result
EnsureStarted() OVERRIDE
;
88 virtual void RegisterImpl(
89 const std::string
& app_id
,
90 const std::vector
<std::string
>& sender_ids
) OVERRIDE
;
91 virtual void UnregisterImpl(const std::string
& app_id
) OVERRIDE
;
92 virtual void SendImpl(const std::string
& app_id
,
93 const std::string
& receiver_id
,
94 const GCMClient::OutgoingMessage
& message
) OVERRIDE
;
97 class DelayedTaskController
;
100 // Stops the GCM service. It can be restarted by calling EnsureStarted again.
103 // Remove cached data when GCM service is stopped.
104 void RemoveCachedData();
106 void DoRegister(const std::string
& app_id
,
107 const std::vector
<std::string
>& sender_ids
);
108 void DoUnregister(const std::string
& app_id
);
109 void DoSend(const std::string
& app_id
,
110 const std::string
& receiver_id
,
111 const GCMClient::OutgoingMessage
& message
);
113 // Callbacks posted from IO thread to UI thread.
114 void MessageReceived(const std::string
& app_id
,
115 const GCMClient::IncomingMessage
& message
);
116 void MessagesDeleted(const std::string
& app_id
);
117 void MessageSendError(const std::string
& app_id
,
118 const GCMClient::SendErrorDetails
& send_error_details
);
119 void SendAcknowledged(const std::string
& app_id
,
120 const std::string
& message_id
);
121 void GCMClientReady();
122 void OnConnected(const net::IPEndPoint
& ip_endpoint
);
123 void OnDisconnected();
125 void GetGCMStatisticsFinished(const GCMClient::GCMStatistics
& stats
);
127 // Flag to indicate whether the user is signed in to a GAIA account.
128 // TODO(jianli): To be removed when sign-in enforcement is dropped.
131 // Flag to indicate if GCM is started.
134 // Flag to indicate if GCM is enabled.
137 // Flag to indicate the last known state of the GCM client. Because this
138 // flag lives on the UI thread, while the GCM client lives on the IO thread,
139 // it may be out of date while connection changes are happening.
142 // List of observers to notify when connection state changes.
143 // Makes sure list is empty on destruction.
144 ObserverList
<GCMConnectionObserver
, true> connection_observer_list_
;
146 scoped_refptr
<base::SequencedTaskRunner
> ui_thread_
;
147 scoped_refptr
<base::SequencedTaskRunner
> io_thread_
;
149 scoped_ptr
<DelayedTaskController
> delayed_task_controller_
;
151 // For all the work occurring on the IO thread. Must be destroyed on the IO
153 scoped_ptr
<IOWorker
> io_worker_
;
155 // Callback for GetGCMStatistics.
156 GetGCMStatisticsCallback request_gcm_statistics_callback_
;
158 // Used to pass a weak pointer to the IO worker.
159 base::WeakPtrFactory
<GCMDriverDesktop
> weak_ptr_factory_
;
161 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop
);
166 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_