Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / services / gcm / push_messaging_service_impl.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 CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_SERVICE_IMPL_H_
6 #define CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_SERVICE_IMPL_H_
7
8 #include "base/compiler_specific.h"
9 #include "base/memory/weak_ptr.h"
10 #include "components/gcm_driver/gcm_app_handler.h"
11 #include "components/gcm_driver/gcm_client.h"
12 #include "content/public/browser/push_messaging_service.h"
13 #include "content/public/common/push_messaging_status.h"
14 #include "third_party/WebKit/public/platform/WebPushPermissionStatus.h"
15
16 class Profile;
17
18 namespace user_prefs {
19 class PrefRegistrySyncable;
20 }
21
22 namespace gcm {
23
24 class GCMProfileService;
25 struct PushMessagingApplicationId;
26
27 class PushMessagingServiceImpl : public content::PushMessagingService,
28                                  public GCMAppHandler {
29  public:
30   // Register profile-specific prefs for GCM.
31   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
32
33   // If any Service Workers are using push, starts GCM and adds an app handler.
34   static void InitializeForProfile(Profile* profile);
35
36   PushMessagingServiceImpl(GCMProfileService* gcm_profile_service,
37                            Profile* profile);
38   ~PushMessagingServiceImpl() override;
39
40   // GCMAppHandler implementation.
41   void ShutdownHandler() override;
42   void OnMessage(const std::string& app_id,
43                  const GCMClient::IncomingMessage& message) override;
44   void OnMessagesDeleted(const std::string& app_id) override;
45   void OnSendError(
46       const std::string& app_id,
47       const GCMClient::SendErrorDetails& send_error_details) override;
48   void OnSendAcknowledged(const std::string& app_id,
49                           const std::string& message_id) override;
50   bool CanHandle(const std::string& app_id) const override;
51
52   // content::PushMessagingService implementation:
53   void Register(
54       const GURL& origin,
55       int64 service_worker_registration_id,
56       const std::string& sender_id,
57       int renderer_id,
58       int render_frame_id,
59       bool user_gesture,
60       const content::PushMessagingService::RegisterCallback& callback) override;
61   blink::WebPushPermissionStatus GetPermissionStatus(
62       const GURL& requesting_origin,
63       int renderer_id,
64       int render_frame_id) override;
65
66   void SetProfileForTesting(Profile* profile);
67
68  private:
69   void DeliverMessageCallback(const PushMessagingApplicationId& application_id,
70                               const GCMClient::IncomingMessage& message,
71                               content::PushDeliveryStatus status);
72
73   void RegisterEnd(
74       const content::PushMessagingService::RegisterCallback& callback,
75       const std::string& registration_id,
76       content::PushRegistrationStatus status);
77
78   void DidRegister(
79       const content::PushMessagingService::RegisterCallback& callback,
80       const std::string& registration_id,
81       GCMClient::Result result);
82
83   void DidRequestPermission(
84       const PushMessagingApplicationId& application_id,
85       const std::string& sender_id,
86       const content::PushMessagingService::RegisterCallback& callback,
87       bool allow);
88
89   GCMProfileService* gcm_profile_service_;  // It owns us.
90
91   Profile* profile_;  // It owns our owner.
92
93   base::WeakPtrFactory<PushMessagingServiceImpl> weak_factory_;
94
95   DISALLOW_COPY_AND_ASSIGN(PushMessagingServiceImpl);
96 };
97
98 }  // namespace gcm
99
100 #endif  // CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_SERVICE_IMPL_H_