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 "components/gcm_driver/gcm_client.h"
18 #include "components/gcm_driver/gcm_driver.h"
22 class SequencedTaskRunner
;
25 namespace extensions
{
26 class ExtensionGCMAppHandlerTest
;
30 class URLRequestContextGetter
;
36 class GCMClientFactory
;
38 // GCMDriver implementation for desktop and Chrome OS, using GCMClient.
39 class GCMDriverDesktop
: public GCMDriver
{
42 scoped_ptr
<GCMClientFactory
> gcm_client_factory
,
43 const GCMClient::ChromeBuildInfo
& chrome_build_info
,
44 const base::FilePath
& store_path
,
45 const scoped_refptr
<net::URLRequestContextGetter
>& request_context
,
46 const scoped_refptr
<base::SequencedTaskRunner
>& ui_thread
,
47 const scoped_refptr
<base::SequencedTaskRunner
>& io_thread
,
48 const scoped_refptr
<base::SequencedTaskRunner
>& blocking_task_runner
);
49 virtual ~GCMDriverDesktop();
51 // GCMDriver overrides:
52 virtual void Shutdown() OVERRIDE
;
53 virtual void OnSignedIn() OVERRIDE
;
54 virtual void Purge() OVERRIDE
;
55 virtual void AddAppHandler(const std::string
& app_id
,
56 GCMAppHandler
* handler
) OVERRIDE
;
57 virtual void RemoveAppHandler(const std::string
& app_id
) OVERRIDE
;
59 // GCMDriver implementation:
60 virtual void Enable() OVERRIDE
;
61 virtual void Disable() OVERRIDE
;
62 virtual GCMClient
* GetGCMClientForTesting() const OVERRIDE
;
63 virtual bool IsStarted() const OVERRIDE
;
64 virtual bool IsConnected() const OVERRIDE
;
65 virtual void GetGCMStatistics(const GetGCMStatisticsCallback
& callback
,
66 bool clear_logs
) OVERRIDE
;
67 virtual void SetGCMRecording(const GetGCMStatisticsCallback
& callback
,
68 bool recording
) OVERRIDE
;
71 // GCMDriver implementation:
72 virtual GCMClient::Result
EnsureStarted() OVERRIDE
;
73 virtual void RegisterImpl(
74 const std::string
& app_id
,
75 const std::vector
<std::string
>& sender_ids
) OVERRIDE
;
76 virtual void UnregisterImpl(const std::string
& app_id
) OVERRIDE
;
77 virtual void SendImpl(const std::string
& app_id
,
78 const std::string
& receiver_id
,
79 const GCMClient::OutgoingMessage
& message
) OVERRIDE
;
82 class DelayedTaskController
;
85 // Stops the GCM service. It can be restarted by calling EnsureStarted again.
88 // Remove cached data when GCM service is stopped.
89 void RemoveCachedData();
91 void DoRegister(const std::string
& app_id
,
92 const std::vector
<std::string
>& sender_ids
);
93 void DoUnregister(const std::string
& app_id
);
94 void DoSend(const std::string
& app_id
,
95 const std::string
& receiver_id
,
96 const GCMClient::OutgoingMessage
& message
);
98 // Callbacks posted from IO thread to UI thread.
99 void MessageReceived(const std::string
& app_id
,
100 const GCMClient::IncomingMessage
& message
);
101 void MessagesDeleted(const std::string
& app_id
);
102 void MessageSendError(const std::string
& app_id
,
103 const GCMClient::SendErrorDetails
& send_error_details
);
104 void GCMClientReady();
105 void OnConnected(const net::IPEndPoint
& ip_endpoint
);
106 void OnDisconnected();
108 void GetGCMStatisticsFinished(const GCMClient::GCMStatistics
& stats
);
110 // Flag to indicate whether the user is signed in to a GAIA account.
111 // TODO(jianli): To be removed when sign-in enforcement is dropped.
114 // Flag to indicate if GCM is started.
117 // Flag to indicate if GCM is enabled.
120 // Flag to indicate the last known state of the GCM client. Because this
121 // flag lives on the UI thread, while the GCM client lives on the IO thread,
122 // it may be out of date while connection changes are happening.
125 scoped_refptr
<base::SequencedTaskRunner
> ui_thread_
;
126 scoped_refptr
<base::SequencedTaskRunner
> io_thread_
;
128 scoped_ptr
<DelayedTaskController
> delayed_task_controller_
;
130 // For all the work occurring on the IO thread. Must be destroyed on the IO
132 scoped_ptr
<IOWorker
> io_worker_
;
134 // Callback for GetGCMStatistics.
135 GetGCMStatisticsCallback request_gcm_statistics_callback_
;
137 // Used to pass a weak pointer to the IO worker.
138 base::WeakPtrFactory
<GCMDriverDesktop
> weak_ptr_factory_
;
140 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop
);
145 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_