OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/services/gcm/push_messaging_service_impl.h" | 5 #include "chrome/browser/services/gcm/push_messaging_service_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "chrome/browser/content_settings/permission_request_id.h" | 13 #include "chrome/browser/content_settings/permission_request_id.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 15 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
16 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 16 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
17 #include "chrome/browser/services/gcm/push_messaging_permission_context.h" | 17 #include "chrome/browser/services/gcm/push_messaging_permission_context.h" |
18 #include "chrome/browser/services/gcm/push_messaging_permission_context_factory. h" | 18 #include "chrome/browser/services/gcm/push_messaging_permission_context_factory. h" |
19 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
20 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
21 #include "components/gcm_driver/gcm_driver.h" | 21 #include "components/gcm_driver/gcm_driver.h" |
22 #include "components/pref_registry/pref_registry_syncable.h" | 22 #include "components/pref_registry/pref_registry_syncable.h" |
23 #include "content/public/browser/push_messaging_application_id.h" | 23 #include "content/public/browser/push_messaging_application_id.h" |
24 #include "content/public/browser/push_messaging_router.h" | |
24 #include "content/public/browser/render_frame_host.h" | 25 #include "content/public/browser/render_frame_host.h" |
25 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
26 | 27 |
27 namespace gcm { | 28 namespace gcm { |
28 | 29 |
29 namespace { | 30 namespace { |
30 const int kMaxRegistrations = 1000000; | 31 const int kMaxRegistrations = 1000000; |
31 } // namespace | 32 } // namespace |
32 | 33 |
33 // static | 34 // static |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
66 gcm_service->driver()->AddAppHandler( | 67 gcm_service->driver()->AddAppHandler( |
67 content::kPushMessagingApplicationIdPrefix, push_service); | 68 content::kPushMessagingApplicationIdPrefix, push_service); |
68 } | 69 } |
69 | 70 |
70 PushMessagingServiceImpl::PushMessagingServiceImpl( | 71 PushMessagingServiceImpl::PushMessagingServiceImpl( |
71 GCMProfileService* gcm_profile_service, | 72 GCMProfileService* gcm_profile_service, |
72 Profile* profile) | 73 Profile* profile) |
73 : gcm_profile_service_(gcm_profile_service), | 74 : gcm_profile_service_(gcm_profile_service), |
74 profile_(profile), | 75 profile_(profile), |
75 weak_factory_(this) { | 76 weak_factory_(this) { |
77 push_messaging_router_ = content::PushMessagingRouter::Create(profile); | |
johnme
2014/07/23 14:25:23
Nit: I guess this could be in the initializer list
Michael van Ouwerkerk
2014/07/23 16:57:51
Done.
| |
76 } | 78 } |
77 | 79 |
78 PushMessagingServiceImpl::~PushMessagingServiceImpl() { | 80 PushMessagingServiceImpl::~PushMessagingServiceImpl() { |
79 // TODO(johnme): If it's possible for this to be destroyed before GCMDriver, | 81 // TODO(johnme): If it's possible for this to be destroyed before GCMDriver, |
80 // then we should call RemoveAppHandler. | 82 // then we should call RemoveAppHandler. |
81 } | 83 } |
82 | 84 |
83 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const { | 85 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const { |
84 return content::PushMessagingApplicationId::Parse(app_id).is_valid(); | 86 return content::PushMessagingApplicationId::Parse(app_id).is_valid(); |
85 } | 87 } |
(...skipping 16 matching lines...) Expand all Loading... | |
102 // "data": "BAZ", | 104 // "data": "BAZ", |
103 // }, | 105 // }, |
104 // "delay_while_idle": true, | 106 // "delay_while_idle": true, |
105 // } | 107 // } |
106 // TODO(johnme): Make sure this is clearly documented for developers. | 108 // TODO(johnme): Make sure this is clearly documented for developers. |
107 content::PushMessagingApplicationId application_id = | 109 content::PushMessagingApplicationId application_id = |
108 content::PushMessagingApplicationId::Parse(app_id); | 110 content::PushMessagingApplicationId::Parse(app_id); |
109 DCHECK(application_id.is_valid()); | 111 DCHECK(application_id.is_valid()); |
110 GCMClient::MessageData::const_iterator it = message.data.find("data"); | 112 GCMClient::MessageData::const_iterator it = message.data.find("data"); |
111 if (application_id.is_valid() && it != message.data.end()) { | 113 if (application_id.is_valid() && it != message.data.end()) { |
112 const std::string& data ALLOW_UNUSED = it->second; | 114 const std::string& data = it->second; |
113 // TODO(mvanouwerkerk): Fire push event with data on the Service Worker | 115 push_messaging_router_->SendMessage( |
114 // corresponding to app_id (and remove ALLOW_UNUSED above). | 116 application_id, |
117 data, | |
118 base::Bind(&PushMessagingServiceImpl::SendMessageCallback, | |
119 weak_factory_.GetWeakPtr(), | |
120 application_id, | |
121 message)); | |
115 } else { | 122 } else { |
116 // Drop the message, as it is invalid. | 123 // Drop the message, as it is invalid. |
117 // TODO(mvanouwerkerk): Show a warning in the developer console of the | 124 // TODO(mvanouwerkerk): Show a warning in the developer console of the |
118 // Service Worker corresponding to app_id. | 125 // Service Worker corresponding to app_id. |
119 // TODO(johnme): Add diagnostic observers (e.g. UMA and an internals page) | 126 // TODO(johnme): Add diagnostic observers (e.g. UMA and an internals page) |
120 // to know when bad things happen. | 127 // to know when bad things happen. |
121 } | 128 } |
122 } | 129 } |
123 | 130 |
131 void PushMessagingServiceImpl::SendMessageCallback( | |
132 const content::PushMessagingApplicationId& application_id, | |
133 const GCMClient::IncomingMessage& message, | |
134 content::PushMessagingStatus status) { | |
135 // TODO(mvanouwerkerk): UMA logging. | |
136 // TODO(mvanouwerkerk): Is there a way to recover from failure? | |
137 } | |
138 | |
124 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) { | 139 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) { |
125 // TODO(mvanouwerkerk): Fire push error event on the Service Worker | 140 // TODO(mvanouwerkerk): Fire push error event on the Service Worker |
126 // corresponding to app_id. | 141 // corresponding to app_id. |
127 } | 142 } |
128 | 143 |
129 void PushMessagingServiceImpl::OnSendError( | 144 void PushMessagingServiceImpl::OnSendError( |
130 const std::string& app_id, | 145 const std::string& app_id, |
131 const GCMClient::SendErrorDetails& send_error_details) { | 146 const GCMClient::SendErrorDetails& send_error_details) { |
132 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; | 147 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; |
133 } | 148 } |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
260 base::Bind(&PushMessagingServiceImpl::DidRegister, | 275 base::Bind(&PushMessagingServiceImpl::DidRegister, |
261 weak_factory_.GetWeakPtr(), | 276 weak_factory_.GetWeakPtr(), |
262 app_id, | 277 app_id, |
263 register_callback)); | 278 register_callback)); |
264 } | 279 } |
265 | 280 |
266 // TODO(johnme): Unregister should decrement the pref, and call | 281 // TODO(johnme): Unregister should decrement the pref, and call |
267 // RemoveAppHandler if the count drops to zero. | 282 // RemoveAppHandler if the count drops to zero. |
268 | 283 |
269 } // namespace gcm | 284 } // namespace gcm |
OLD | NEW |