Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / components / gcm_driver / gcm_driver_desktop.h
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"
17 #include "components/gcm_driver/gcm_client.h"
18 #include "components/gcm_driver/gcm_driver.h"
19
20 namespace base {
21 class FilePath;
22 class SequencedTaskRunner;
23 }
24
25 namespace extensions {
26 class ExtensionGCMAppHandlerTest;
27 }
28
29 namespace net {
30 class URLRequestContextGetter;
31 }
32
33 namespace gcm {
34
35 class GCMAppHandler;
36 class GCMClientFactory;
37
38 // GCMDriver implementation for desktop and Chrome OS, using GCMClient.
39 class GCMDriverDesktop : public GCMDriver {
40  public:
41   GCMDriverDesktop(
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();
50
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;
58   virtual void Enable() OVERRIDE;
59   virtual void Disable() OVERRIDE;
60   virtual GCMClient* GetGCMClientForTesting() const OVERRIDE;
61   virtual bool IsStarted() const OVERRIDE;
62   virtual bool IsConnected() const OVERRIDE;
63   virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
64                                 bool clear_logs) OVERRIDE;
65   virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback,
66                                bool recording) OVERRIDE;
67
68   // GCMDriverDesktop specific implementation.
69   // Sets a list of accounts with OAuth2 tokens for the next checkin.
70   // |account_tokens| maps email addresses to OAuth2 access tokens.
71   // |account_removed| indicates that an account has been removed since the
72   //     last time the callback was called, which triggers an immediate checkin,
73   //     to ensure that association between device and account is removed.
74   void SetAccountsForCheckin(
75       const std::map<std::string, std::string>& account_tokens);
76
77  protected:
78   // GCMDriver implementation:
79   virtual GCMClient::Result EnsureStarted() OVERRIDE;
80   virtual void RegisterImpl(
81       const std::string& app_id,
82       const std::vector<std::string>& sender_ids) OVERRIDE;
83   virtual void UnregisterImpl(const std::string& app_id) OVERRIDE;
84   virtual void SendImpl(const std::string& app_id,
85                         const std::string& receiver_id,
86                         const GCMClient::OutgoingMessage& message) OVERRIDE;
87
88  private:
89   class DelayedTaskController;
90   class IOWorker;
91
92   //  Stops the GCM service. It can be restarted by calling EnsureStarted again.
93   void Stop();
94
95   // Remove cached data when GCM service is stopped.
96   void RemoveCachedData();
97
98   void DoRegister(const std::string& app_id,
99                   const std::vector<std::string>& sender_ids);
100   void DoUnregister(const std::string& app_id);
101   void DoSend(const std::string& app_id,
102               const std::string& receiver_id,
103               const GCMClient::OutgoingMessage& message);
104
105   // Callbacks posted from IO thread to UI thread.
106   void MessageReceived(const std::string& app_id,
107                        const GCMClient::IncomingMessage& message);
108   void MessagesDeleted(const std::string& app_id);
109   void MessageSendError(const std::string& app_id,
110                         const GCMClient::SendErrorDetails& send_error_details);
111   void SendAcknowledged(const std::string& app_id,
112                         const std::string& message_id);
113   void GCMClientReady();
114   void OnConnected(const net::IPEndPoint& ip_endpoint);
115   void OnDisconnected();
116
117   void GetGCMStatisticsFinished(const GCMClient::GCMStatistics& stats);
118
119   // Flag to indicate whether the user is signed in to a GAIA account.
120   // TODO(jianli): To be removed when sign-in enforcement is dropped.
121   bool signed_in_;
122
123   // Flag to indicate if GCM is started.
124   bool gcm_started_;
125
126   // Flag to indicate if GCM is enabled.
127   bool gcm_enabled_;
128
129   // Flag to indicate the last known state of the GCM client. Because this
130   // flag lives on the UI thread, while the GCM client lives on the IO thread,
131   // it may be out of date while connection changes are happening.
132   bool connected_;
133
134   scoped_refptr<base::SequencedTaskRunner> ui_thread_;
135   scoped_refptr<base::SequencedTaskRunner> io_thread_;
136
137   scoped_ptr<DelayedTaskController> delayed_task_controller_;
138
139   // For all the work occurring on the IO thread. Must be destroyed on the IO
140   // thread.
141   scoped_ptr<IOWorker> io_worker_;
142
143   // Callback for GetGCMStatistics.
144   GetGCMStatisticsCallback request_gcm_statistics_callback_;
145
146   // Used to pass a weak pointer to the IO worker.
147   base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_;
148
149   DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop);
150 };
151
152 }  // namespace gcm
153
154 #endif  // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_