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/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 15 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
15 #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" | |
18 #include "chrome/browser/services/gcm/push_messaging_permission_context_factory. h" | |
19 #include "chrome/browser/tab_contents/tab_util.h" | |
16 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
17 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
18 #include "components/gcm_driver/gcm_driver.h" | 22 #include "components/gcm_driver/gcm_driver.h" |
19 #include "components/pref_registry/pref_registry_syncable.h" | 23 #include "components/pref_registry/pref_registry_syncable.h" |
24 #include "content/public/browser/web_contents.h" | |
20 | 25 |
21 namespace gcm { | 26 namespace gcm { |
22 | 27 |
23 namespace { | 28 namespace { |
24 const char kAppIdPrefix[] = "push:"; | 29 const char kAppIdPrefix[] = "push:"; |
25 const int kMaxRegistrations = 1000000; | 30 const int kMaxRegistrations = 1000000; |
26 } // namespace | 31 } // namespace |
27 | 32 |
28 // static | 33 // static |
29 void PushMessagingServiceImpl::RegisterProfilePrefs( | 34 void PushMessagingServiceImpl::RegisterProfilePrefs( |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 | 125 |
121 void PushMessagingServiceImpl::OnSendError( | 126 void PushMessagingServiceImpl::OnSendError( |
122 const std::string& app_id, | 127 const std::string& app_id, |
123 const GCMClient::SendErrorDetails& send_error_details) { | 128 const GCMClient::SendErrorDetails& send_error_details) { |
124 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; | 129 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; |
125 } | 130 } |
126 | 131 |
127 void PushMessagingServiceImpl::Register( | 132 void PushMessagingServiceImpl::Register( |
128 const std::string& app_id, | 133 const std::string& app_id, |
129 const std::string& sender_id, | 134 const std::string& sender_id, |
135 const int renderer_id, | |
136 const int render_view_id, | |
130 const content::PushMessagingService::RegisterCallback& callback) { | 137 const content::PushMessagingService::RegisterCallback& callback) { |
131 if (!gcm_profile_service_->driver()) { | 138 if (!gcm_profile_service_->driver()) { |
132 NOTREACHED() << "There is no GCMDriver. Has GCMProfileService shut down?"; | 139 NOTREACHED() << "There is no GCMDriver. Has GCMProfileService shut down?"; |
133 } | 140 } |
134 | 141 |
135 if (profile_->GetPrefs()->GetInteger( | 142 if (profile_->GetPrefs()->GetInteger( |
136 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { | 143 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { |
137 DidRegister(app_id, callback, "", GCMClient::UNKNOWN_ERROR); | 144 DidRegister(app_id, callback, std::string(), GCMClient::UNKNOWN_ERROR); |
138 return; | 145 return; |
139 } | 146 } |
140 | 147 |
141 // If this is registering for the first time then the driver does not have | 148 // If this is registering for the first time then the driver does not have |
142 // this as an app handler and registration would fail. | 149 // this as an app handler and registration would fail. |
143 if (gcm_profile_service_->driver()->GetAppHandler(kAppIdPrefix) != this) | 150 if (gcm_profile_service_->driver()->GetAppHandler(kAppIdPrefix) != this) |
144 gcm_profile_service_->driver()->AddAppHandler(kAppIdPrefix, this); | 151 gcm_profile_service_->driver()->AddAppHandler(kAppIdPrefix, this); |
145 | 152 |
146 std::vector<std::string> sender_ids(1, sender_id); | 153 content::WebContents* web_contents = |
147 gcm_profile_service_->driver()->Register( | 154 tab_util::GetWebContentsByID(renderer_id, render_view_id); |
148 app_id, | 155 |
149 sender_ids, | 156 // The page doesn't exist any more. |
150 base::Bind(&PushMessagingServiceImpl::DidRegister, | 157 if (!web_contents) |
158 return; | |
159 | |
160 // TODO(miguelg) need to send this over IPC when bubble support is | |
161 // implemented. | |
162 int bridge_id = -1; | |
163 | |
164 const PermissionRequestID id(renderer_id, render_view_id, bridge_id, GURL()); | |
165 | |
166 GURL embedder = web_contents->GetURL(); | |
167 gcm::PushMessagingPermissionContext* permission_context = | |
168 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); | |
169 | |
170 if (permission_context == NULL) { | |
171 DidRegister(app_id, callback, std::string(), GCMClient::UNKNOWN_ERROR); | |
172 return; | |
173 } | |
174 | |
175 permission_context->RequestPermission( | |
176 web_contents, | |
177 id, | |
178 embedder, | |
179 false, | |
Michael van Ouwerkerk
2014/06/23 12:34:10
Nit: What does false stand for? Maybe add a commen
Miguel Garcia
2014/06/23 18:23:14
Done.
| |
180 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, | |
151 weak_factory_.GetWeakPtr(), | 181 weak_factory_.GetWeakPtr(), |
182 sender_id, | |
152 app_id, | 183 app_id, |
153 callback)); | 184 callback)); |
154 } | 185 } |
155 | 186 |
156 void PushMessagingServiceImpl::DidRegister( | 187 void PushMessagingServiceImpl::DidRegister( |
157 const std::string& app_id, | 188 const std::string& app_id, |
158 const content::PushMessagingService::RegisterCallback& callback, | 189 const content::PushMessagingService::RegisterCallback& callback, |
159 const std::string& registration_id, | 190 const std::string& registration_id, |
160 GCMClient::Result result) { | 191 GCMClient::Result result) { |
161 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); | 192 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); |
162 bool success = (result == GCMClient::SUCCESS); | 193 bool success = (result == GCMClient::SUCCESS); |
163 callback.Run(endpoint, registration_id, success); | 194 callback.Run(endpoint, registration_id, success); |
164 if (success) { | 195 if (success) { |
165 // TODO(johnme): Make sure the pref doesn't get out of sync after crashes. | 196 // TODO(johnme): Make sure the pref doesn't get out of sync after crashes. |
166 int registration_count = profile_->GetPrefs()->GetInteger( | 197 int registration_count = profile_->GetPrefs()->GetInteger( |
167 prefs::kPushMessagingRegistrationCount); | 198 prefs::kPushMessagingRegistrationCount); |
168 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, | 199 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, |
169 registration_count + 1); | 200 registration_count + 1); |
170 } | 201 } |
171 } | 202 } |
172 | 203 |
204 void PushMessagingServiceImpl::DidRequestPermission( | |
205 const std::string& sender_id, | |
206 const std::string& app_id, | |
207 const content::PushMessagingService::RegisterCallback& register_callback, | |
208 bool allow) { | |
209 if (!allow) { | |
210 // TODO(miguelg) extend the error enum to allow for pemission failure. | |
211 DidRegister(app_id, register_callback, std::string(), | |
212 GCMClient::UNKNOWN_ERROR); | |
213 return; | |
214 } | |
215 | |
216 // The GCMDriver could be NULL if GCMProfileService has been shut down. | |
217 if (!gcm_profile_service_->driver()) | |
218 return; | |
219 | |
220 std::vector<std::string> sender_ids(1, sender_id); | |
221 | |
222 gcm_profile_service_->driver()->Register( | |
223 app_id, | |
224 sender_ids, | |
225 base::Bind(&PushMessagingServiceImpl::DidRegister, | |
226 weak_factory_.GetWeakPtr(), | |
227 app_id, | |
228 register_callback)); | |
229 } | |
230 | |
173 // TODO(johnme): Unregister should decrement the pref, and call | 231 // TODO(johnme): Unregister should decrement the pref, and call |
174 // RemoveAppHandler if the count drops to zero. | 232 // RemoveAppHandler if the count drops to zero. |
175 | 233 |
176 } // namespace gcm | 234 } // namespace gcm |
OLD | NEW |