| 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" |
| 14 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 16 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 15 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 17 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 18 #include "chrome/browser/tab_contents/tab_util.h" |
| 16 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 18 #include "components/gcm_driver/gcm_driver.h" | 21 #include "components/gcm_driver/gcm_driver.h" |
| 19 #include "components/pref_registry/pref_registry_syncable.h" | 22 #include "components/pref_registry/pref_registry_syncable.h" |
| 23 #include "content/public/browser/web_contents.h" |
| 20 | 24 |
| 21 namespace gcm { | 25 namespace gcm { |
| 22 | 26 |
| 23 namespace { | 27 namespace { |
| 24 const char kAppIdPrefix[] = "push:"; | 28 const char kAppIdPrefix[] = "push:"; |
| 25 const int kMaxRegistrations = 1000000; | 29 const int kMaxRegistrations = 1000000; |
| 26 } // namespace | 30 } // namespace |
| 27 | 31 |
| 28 // static | 32 // static |
| 29 void PushMessagingServiceImpl::RegisterProfilePrefs( | 33 void PushMessagingServiceImpl::RegisterProfilePrefs( |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 | 124 |
| 121 void PushMessagingServiceImpl::OnSendError( | 125 void PushMessagingServiceImpl::OnSendError( |
| 122 const std::string& app_id, | 126 const std::string& app_id, |
| 123 const GCMClient::SendErrorDetails& send_error_details) { | 127 const GCMClient::SendErrorDetails& send_error_details) { |
| 124 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; | 128 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; |
| 125 } | 129 } |
| 126 | 130 |
| 127 void PushMessagingServiceImpl::Register( | 131 void PushMessagingServiceImpl::Register( |
| 128 const std::string& app_id, | 132 const std::string& app_id, |
| 129 const std::string& sender_id, | 133 const std::string& sender_id, |
| 134 const int renderer_id, |
| 135 const int routing_id, |
| 130 const content::PushMessagingService::RegisterCallback& callback) { | 136 const content::PushMessagingService::RegisterCallback& callback) { |
| 131 if (!gcm_profile_service_->driver()) { | 137 if (!gcm_profile_service_->driver()) { |
| 132 NOTREACHED() << "There is no GCMDriver. Has GCMProfileService shut down?"; | 138 NOTREACHED() << "There is no GCMDriver. Has GCMProfileService shut down?"; |
| 133 } | 139 } |
| 134 | 140 |
| 135 if (profile_->GetPrefs()->GetInteger( | 141 if (profile_->GetPrefs()->GetInteger( |
| 136 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { | 142 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { |
| 137 DidRegister(app_id, callback, "", GCMClient::UNKNOWN_ERROR); | 143 DidRegister(app_id, callback, "", GCMClient::UNKNOWN_ERROR); |
| 144 // The page doesn't exist any more. |
| 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, routing_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, routing_id, bridge_id, GURL()); |
| 165 |
| 166 GURL embedder = web_contents->GetURL(); |
| 167 gcm::GCMPermissionContext* permission_context = |
| 168 profile_->GetPushMessagingPermissionContext(); |
| 169 |
| 170 permission_context->RequestPermission( |
| 171 web_contents, |
| 172 id, |
| 173 embedder, |
| 174 false, |
| 175 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, |
| 151 weak_factory_.GetWeakPtr(), | 176 weak_factory_.GetWeakPtr(), |
| 177 sender_id, |
| 152 app_id, | 178 app_id, |
| 153 callback)); | 179 callback)); |
| 154 } | 180 } |
| 155 | 181 |
| 156 void PushMessagingServiceImpl::DidRegister( | 182 void PushMessagingServiceImpl::DidRegister( |
| 157 const std::string& app_id, | 183 const std::string& app_id, |
| 158 const content::PushMessagingService::RegisterCallback& callback, | 184 const content::PushMessagingService::RegisterCallback& callback, |
| 159 const std::string& registration_id, | 185 const std::string& registration_id, |
| 160 GCMClient::Result result) { | 186 GCMClient::Result result) { |
| 161 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); | 187 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); |
| 162 bool success = (result == GCMClient::SUCCESS); | 188 bool success = (result == GCMClient::SUCCESS); |
| 163 callback.Run(endpoint, registration_id, success); | 189 callback.Run(endpoint, registration_id, success); |
| 164 if (success) { | 190 if (success) { |
| 165 // TODO(johnme): Make sure the pref doesn't get out of sync after crashes. | 191 // TODO(johnme): Make sure the pref doesn't get out of sync after crashes. |
| 166 int registration_count = profile_->GetPrefs()->GetInteger( | 192 int registration_count = profile_->GetPrefs()->GetInteger( |
| 167 prefs::kPushMessagingRegistrationCount); | 193 prefs::kPushMessagingRegistrationCount); |
| 168 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, | 194 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, |
| 169 registration_count + 1); | 195 registration_count + 1); |
| 170 } | 196 } |
| 171 } | 197 } |
| 172 | 198 |
| 199 void PushMessagingServiceImpl::DidRequestPermission( |
| 200 const std::string& sender_id, |
| 201 const std::string& app_id, |
| 202 const content::PushMessagingService::RegisterCallback& register_callback, |
| 203 bool allow) { |
| 204 if (!allow) { |
| 205 DidRegister(app_id, register_callback, "", GCMClient::UNKNOWN_ERROR); |
| 206 return; |
| 207 } |
| 208 |
| 209 // The GCMDriver could be NULL if GCMProfileService has been shut down. |
| 210 if (!gcm_profile_service_->driver()) |
| 211 return; |
| 212 |
| 213 std::vector<std::string> sender_ids(1, sender_id); |
| 214 |
| 215 gcm_profile_service_->driver()->Register( |
| 216 app_id, |
| 217 sender_ids, |
| 218 base::Bind(&PushMessagingServiceImpl::DidRegister, |
| 219 weak_factory_.GetWeakPtr(), |
| 220 app_id, |
| 221 register_callback)); |
| 222 } |
| 223 |
| 173 // TODO(johnme): Unregister should decrement the pref, and call | 224 // TODO(johnme): Unregister should decrement the pref, and call |
| 174 // RemoveAppHandler if the count drops to zero. | 225 // RemoveAppHandler if the count drops to zero. |
| 175 | 226 |
| 176 } // namespace gcm | 227 } // namespace gcm |
| OLD | NEW |