Chromium Code Reviews| 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" |
| 15 #include "chrome/browser/services/gcm/gcm_permission_context.h" | |
| 16 #include "chrome/browser/services/gcm/gcm_permission_context_factory.h" | |
| 14 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 17 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 15 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 18 #include "chrome/browser/services/gcm/gcm_profile_service_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 routing_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, "", GCMClient::UNKNOWN_ERROR); |
| 145 // The page doesn't exist any more. | |
|
jianli
2014/06/20 17:19:30
Not sure I understand this comment.
Miguel Garcia
2014/06/20 20:50:13
No wonder, it's an artifact of a bad merge. Remove
| |
| 138 return; | 146 return; |
| 139 } | 147 } |
| 140 | 148 |
| 141 // If this is registering for the first time then the driver does not have | 149 // If this is registering for the first time then the driver does not have |
| 142 // this as an app handler and registration would fail. | 150 // this as an app handler and registration would fail. |
| 143 if (gcm_profile_service_->driver()->GetAppHandler(kAppIdPrefix) != this) | 151 if (gcm_profile_service_->driver()->GetAppHandler(kAppIdPrefix) != this) |
| 144 gcm_profile_service_->driver()->AddAppHandler(kAppIdPrefix, this); | 152 gcm_profile_service_->driver()->AddAppHandler(kAppIdPrefix, this); |
| 145 | 153 |
| 146 std::vector<std::string> sender_ids(1, sender_id); | 154 content::WebContents* web_contents = |
| 147 gcm_profile_service_->driver()->Register( | 155 tab_util::GetWebContentsByID(renderer_id, routing_id); |
| 148 app_id, | 156 |
| 149 sender_ids, | 157 // The page doesn't exist any more. |
| 150 base::Bind(&PushMessagingServiceImpl::DidRegister, | 158 if (!web_contents) |
| 159 return; | |
| 160 | |
| 161 // TODO(miguelg) need to send this over IPC when bubble support is | |
| 162 // implemented. | |
| 163 int bridge_id = -1; | |
| 164 | |
| 165 const PermissionRequestID id(renderer_id, routing_id, bridge_id, GURL()); | |
| 166 | |
| 167 GURL embedder = web_contents->GetURL(); | |
| 168 gcm::GCMPermissionContext* permission_context = | |
| 169 gcm::GCMPermissionContextFactory::GetForProfile(profile_); | |
|
jianli
2014/06/20 17:19:30
It seems that GCMPermissionContext is only tied wi
Miguel Garcia
2014/06/20 20:50:13
All other permissions seems to be tied to it so th
| |
| 170 | |
| 171 if (permission_context == NULL) { | |
| 172 DidRegister(app_id, callback, "", GCMClient::UNKNOWN_ERROR); | |
|
fgorski
2014/06/20 14:14:15
nit: std::string()
you are using it in earlier fi
Miguel Garcia
2014/06/20 20:50:13
Done.
| |
| 173 return; | |
| 174 } | |
| 175 | |
| 176 permission_context->RequestPermission( | |
| 177 web_contents, | |
| 178 id, | |
| 179 embedder, | |
| 180 false, | |
| 181 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, | |
| 151 weak_factory_.GetWeakPtr(), | 182 weak_factory_.GetWeakPtr(), |
| 183 sender_id, | |
| 152 app_id, | 184 app_id, |
| 153 callback)); | 185 callback)); |
| 154 } | 186 } |
| 155 | 187 |
| 156 void PushMessagingServiceImpl::DidRegister( | 188 void PushMessagingServiceImpl::DidRegister( |
| 157 const std::string& app_id, | 189 const std::string& app_id, |
| 158 const content::PushMessagingService::RegisterCallback& callback, | 190 const content::PushMessagingService::RegisterCallback& callback, |
| 159 const std::string& registration_id, | 191 const std::string& registration_id, |
| 160 GCMClient::Result result) { | 192 GCMClient::Result result) { |
| 161 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); | 193 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); |
| 162 bool success = (result == GCMClient::SUCCESS); | 194 bool success = (result == GCMClient::SUCCESS); |
| 163 callback.Run(endpoint, registration_id, success); | 195 callback.Run(endpoint, registration_id, success); |
| 164 if (success) { | 196 if (success) { |
| 165 // TODO(johnme): Make sure the pref doesn't get out of sync after crashes. | 197 // TODO(johnme): Make sure the pref doesn't get out of sync after crashes. |
| 166 int registration_count = profile_->GetPrefs()->GetInteger( | 198 int registration_count = profile_->GetPrefs()->GetInteger( |
| 167 prefs::kPushMessagingRegistrationCount); | 199 prefs::kPushMessagingRegistrationCount); |
| 168 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, | 200 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, |
| 169 registration_count + 1); | 201 registration_count + 1); |
| 170 } | 202 } |
| 171 } | 203 } |
| 172 | 204 |
| 205 void PushMessagingServiceImpl::DidRequestPermission( | |
| 206 const std::string& sender_id, | |
| 207 const std::string& app_id, | |
| 208 const content::PushMessagingService::RegisterCallback& register_callback, | |
| 209 bool allow) { | |
| 210 if (!allow) { | |
| 211 DidRegister(app_id, register_callback, "", GCMClient::UNKNOWN_ERROR); | |
|
fgorski
2014/06/20 14:14:15
nit: std::string()
fgorski
2014/06/20 14:14:15
in case you think there will be a specific error y
Miguel Garcia
2014/06/20 20:50:12
Done.
Miguel Garcia
2014/06/20 20:50:13
Great, added the TODO and will be extending the en
| |
| 212 return; | |
| 213 } | |
| 214 | |
| 215 // The GCMDriver could be NULL if GCMProfileService has been shut down. | |
| 216 if (!gcm_profile_service_->driver()) | |
| 217 return; | |
| 218 | |
| 219 std::vector<std::string> sender_ids(1, sender_id); | |
| 220 | |
| 221 gcm_profile_service_->driver()->Register( | |
| 222 app_id, | |
| 223 sender_ids, | |
| 224 base::Bind(&PushMessagingServiceImpl::DidRegister, | |
| 225 weak_factory_.GetWeakPtr(), | |
| 226 app_id, | |
| 227 register_callback)); | |
| 228 } | |
| 229 | |
| 173 // TODO(johnme): Unregister should decrement the pref, and call | 230 // TODO(johnme): Unregister should decrement the pref, and call |
| 174 // RemoveAppHandler if the count drops to zero. | 231 // RemoveAppHandler if the count drops to zero. |
| 175 | 232 |
| 176 } // namespace gcm | 233 } // namespace gcm |
| OLD | NEW |